dggjimai/application/api/common.php

183 lines
4.9 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use think\response\Json;
use app\common\model\Slide;
use app\common\model\ShopConfig;
use tools\Crypt;
if(!function_exists('success')){
/**
* 操作成功
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function success($data = '', $msg = 'success', $code = 200)
{
return result($msg, $data, $code);
}
}
if(!function_exists('error')){
/**
* 操作失败
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function error($msg = 'fail', $data = '', $code = 500)
{
return result($msg, $data, $code);
}
}
if(!function_exists('result')){
/**
* 返回json结果
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function result($msg = 'fail', $data = '', $code = 500)
{
$header = [];
//处理跨域请求问题
if (config('api.cross_domain.allow')) {
$header = ['Access-Control-Allow-Origin' => '*'];
if (request()->isOptions()) {
$header = config('api.cross_domain.header');
return json('',200,$header);
}
}
return json([
'code' => $code,
'msg' => $msg,
'data' => $data,
], $code, $header);
}
}
if(!function_exists('unauthorized')){
/**
* 未授权
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function unauthorized($msg = 'unauthorized', $data = '', $code = 401)
{
return result($msg, $data, $code);
}
}
if(!function_exists('client_error')){
/**
* 客户端错误
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function client_error($msg = 'client error', $data = '', $code = 400)
{
return result($msg, $data, $code);
}
}
if(!function_exists('server_error')){
/**
* 服务端错误
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function server_error($msg = 'server error', $data = '', $code = 500)
{
return result($msg, $data, $code);
}
}
if(!function_exists('error_404')){
/**
* 资源或接口不存在
* @param string $msg
* @param string $data
* @param int $code
* @return Json
*/
function error_404($msg = '404 not found', $data = '', $code = 404)
{
return result($msg, $data, $code);
}
}
if (!function_exists('get_ad')) {
/**
* 广告列表
* @param string $alias 广告别名
* @param string $num。显示数量
* @return li
*/
function get_ad($alias, $num = '999')
{
$_malladviceModel = new Slide();
$where = 'alias="'.$alias.'"';
$malladvice_list = $_malladviceModel->where($where)->limit($num)->order('sort_number desc')->select();
$ad = array();
$web_url = ShopConfig::get('website', 'img_url');
foreach ($malladvice_list as $key => $row) {
$ad[$key]['id'] = $row['id'];
if($row['jump_type']==1){
$ad[$key]['unionid'] = Crypt::encrypt($row['jump_target']);
}else{
$ad[$key]['url'] = $row['jump_target'];
}
$ad[$key]['type'] = $row['jump_type'];
$ad[$key]['img'] = $web_url . $row['img'];
}
return $ad;
}
}
if(!function_exists('curl_request')){
//参数1访问的URL参数2post数据(不填则为GET)参数3提交的$cookies,参数4是否返回$cookies
function curl_request($url,$post='',$cookie='', $returnCookie=0)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
if($returnCookie){
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}
}