569 lines
14 KiB
PHP
569 lines
14 KiB
PHP
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app;
|
|
|
|
use app\farm\model\Farmer;
|
|
use app\restaurant\model\Table;
|
|
use app\shop\model\Cap;
|
|
use think\App;
|
|
use think\exception\HttpResponseException;
|
|
use think\Response;
|
|
use think\facade\Db;
|
|
use think\facade\Lang;
|
|
|
|
/**
|
|
* 控制器基础类
|
|
*/
|
|
abstract class ApiRest extends BaseController
|
|
{
|
|
//app名称
|
|
public $_app = null;
|
|
//控制器名称
|
|
public $_controller = null;
|
|
//执行方法名称
|
|
public $_action = null;
|
|
//method
|
|
public $_method = 'GET';
|
|
//query参数
|
|
public $_param = [];
|
|
//body参数
|
|
public $_input = [];
|
|
//头部
|
|
public $_header = [];
|
|
//头部token
|
|
public $_token = null;
|
|
//语言信息
|
|
public $_lang = 'zh-cn';
|
|
//角色
|
|
public $_role = 'guest';
|
|
//host信息
|
|
public $_host = null;
|
|
//访问ip信息
|
|
public $_ip = null;
|
|
//用户信息
|
|
public $_user = null;
|
|
//获取用户id
|
|
public $_user_id = null;
|
|
//唯一app标示
|
|
public $_uniacid = 1;
|
|
//定义检查中间件
|
|
// protected $middleware = [ 'app\middleware\CheckInput' ,'think\middleware\LoadLangPack' ,'app\middleware\GetAuthConfig' ,'app\middleware\AutoStatic','app\middleware\StaticHour'];
|
|
|
|
//获取配置信息
|
|
protected $_config = [];
|
|
|
|
// 小程序登陆每个用户产生的唯一表示
|
|
protected $autograph = '';
|
|
|
|
protected $uniacid = 0;
|
|
|
|
protected $is_app = 0;
|
|
|
|
protected $defaultImage = array(
|
|
// 默认用户头像
|
|
'avatar' => 'https://retail.xiaochengxucms.com/defaultAvatar.png',
|
|
// 默认内容图片
|
|
'image' => 'https://retail.xiaochengxucms.com/lbCardDefaultImage.png',
|
|
);
|
|
|
|
protected $_is_weiqin = false;
|
|
|
|
protected $check_url = "";
|
|
|
|
/**
|
|
* 无需登录的方法,同时也就不需要鉴权了
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
|
|
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
|
|
if (in_array($this->_method, ['options', 'Options', 'OPTIONS'])) {
|
|
echo true;
|
|
exit;
|
|
}
|
|
|
|
//获取param
|
|
$this->_param = $this->request->param();
|
|
//获取body参数
|
|
$this->_input = json_decode($this->request->getInput(), true);
|
|
//获取头部信息
|
|
$this->_header = $this->request->header();
|
|
|
|
|
|
if (is_dev()) {
|
|
// 调试代码
|
|
$debugUid = $this->_param['debug_uid'] ?? ($this->_input['debug_uid'] ?? null);
|
|
if ($debugUid !== null) {
|
|
$this->_user_id = $debugUid;
|
|
$this->_user = [
|
|
'id' => $this->_user_id,
|
|
'uniacid' => $this->_uniacid,
|
|
'nickname' => 'debug',
|
|
];
|
|
$this->noNeedLogin[] = $this->request->action();
|
|
if (isset($this->_param['debug_uniacid'])) {
|
|
$this->_uniacid = $this->_param['debug_uniacid'];
|
|
} elseif (isset($this->_input['debug_uniacid'])) {
|
|
$this->_uniacid = $this->_input['debug_uniacid'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->is_app = !empty($this->_header['isapp']) ? $this->_header['isapp'] : 0;
|
|
|
|
if (defined('IS_WEIQIN')) {
|
|
global $_GPC, $_W;
|
|
$this->_uniacid = $_W['uniacid'];
|
|
} else {
|
|
if (isset($this->_param['i'])) {
|
|
$this->_uniacid = $this->_param['i'];
|
|
}
|
|
|
|
}
|
|
|
|
if (defined('LONGBING_CARD_UNIACID')) {
|
|
|
|
define('LONGBING_CARD_UNIACID', $this->_uniacid);
|
|
}
|
|
|
|
$this->shareChangeData($this->_param);
|
|
//获取autograph 小程序用户唯一标示
|
|
if (isset($this->_header['autograph']) && $this->_header['autograph']) {
|
|
$this->autograph = $this->_header['autograph'];
|
|
} else {
|
|
if (!$this->match($this->noNeedLogin)) {
|
|
|
|
$this->errorMsg('need login', 401);
|
|
|
|
}
|
|
}
|
|
//获取配置信息
|
|
$this->_config = longbingGetAppConfig($this->_uniacid);
|
|
//语言
|
|
if (isset($this->_header['lang'])) $this->_token = $this->_header['lang'];
|
|
|
|
if (!empty($this->autograph) && !$this->match($this->noNeedLogin)) {
|
|
|
|
$this->_user_id = $this->getUserId();
|
|
|
|
$this->_user = $this->getUserInfo();
|
|
}
|
|
|
|
landNotice($this->_uniacid);
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2020-08-21 17:43
|
|
* @功能说明:
|
|
*/
|
|
public function shareChangeData($input)
|
|
{
|
|
|
|
$arr = [
|
|
|
|
'farm/app/Index/getYsToken',
|
|
|
|
'farm/app/Index/ysStartTurn',
|
|
|
|
'farm/app/Index/ysStopTurn',
|
|
|
|
'farm/app/Index/getMonitorInfo',
|
|
|
|
'farm/app/Index/index',
|
|
|
|
'farm/app/Index/couponList',
|
|
|
|
'farm/app/IndexClaim/claimCateList',
|
|
|
|
'farm/app/IndexLand/landList',
|
|
|
|
'farm/app/IndexClaim/claimBanner',
|
|
|
|
'farm/app/IndexClaim/claimCateList',
|
|
|
|
'farm/app/IndexClaim/claimList',
|
|
|
|
'farm/app/IndexGoods/goodsIndex',
|
|
|
|
'farm/app/IndexGoods/carInfo',
|
|
|
|
'farm/app/IndexGoods/goodsList',
|
|
|
|
'farm/app/IndexUser/userInfo',
|
|
|
|
'farm/app/IndexUser/index',
|
|
|
|
'farm/app/IndexUser/farmerInfo',
|
|
|
|
'farm/app/Index/configInfo',
|
|
|
|
'farm/app/Index/indexStoreList',
|
|
];
|
|
|
|
if (!empty($input['s']) && in_array($input['s'], $arr)) {
|
|
|
|
$input['s'] = trim(strrchr($input['s'], '/'), '/');
|
|
|
|
$this->noNeedLogin[] = $input['s'];
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2020-07-09 12:00
|
|
* @功能说明:检测方法传递
|
|
*/
|
|
public function match($arr)
|
|
{
|
|
|
|
|
|
$arr = is_array($arr) ? $arr : explode(',', $arr);
|
|
if (!$arr) {
|
|
return FALSE;
|
|
}
|
|
$arr = array_map('strtolower', $arr);
|
|
// 是否存在
|
|
if (in_array(strtolower($this->request->action()), $arr) || in_array('*', $arr)) {
|
|
return TRUE;
|
|
}
|
|
|
|
// 没找到匹配
|
|
return FALSE;
|
|
}
|
|
|
|
//返回请求成功的数据
|
|
public function success($data, $code = 200)
|
|
{
|
|
$result['data'] = LongbingGetWxApiReturnData($data);
|
|
$result['code'] = $code;
|
|
$result['sign'] = null;
|
|
//复杂的签名
|
|
// if(isset($this->_user['keys'])){
|
|
// $result['sign'] = rsa2CreateSign($this->_user['keys'] ,json_encode($data));
|
|
// }
|
|
//简单的签名
|
|
if (!empty($this->_token)) $result['sign'] = createSimpleSign($this->_token, is_string($data) ? $data : json_encode($data));
|
|
return $this->response($result, 'json', $code);
|
|
}
|
|
|
|
//返回错误数据
|
|
public function error($msg, $code = 400)
|
|
{
|
|
// $result[ 'error' ] = Lang::get($msg);
|
|
// $result[ 'code' ] = $code;
|
|
$result = $this->getErrorData($msg, $code);
|
|
return $this->response($result, 'json', 200);
|
|
}
|
|
|
|
public function getErrorData($msg, $code = 400)
|
|
{
|
|
$result['error'] = Lang::get($msg);
|
|
$result['code'] = $code;
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 输出返回数据
|
|
* @access protected
|
|
* @param mixed $data 要返回的数据
|
|
* @param String $type 返回类型 JSON XML
|
|
* @param integer $code HTTP状态码
|
|
* @return Response
|
|
*/
|
|
protected function response($data, $type = 'json', $code = 200)
|
|
{
|
|
return Response::create($data, $type)->code($code);
|
|
}
|
|
|
|
/**
|
|
* REST 调用
|
|
* @access public
|
|
* @param string $method 方法名
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function _empty($method)
|
|
{
|
|
if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
|
|
// RESTFul方法支持
|
|
$fun = $method . '_' . $this->method . '_' . $this->type;
|
|
} elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->type)) {
|
|
$fun = $method . '_' . $this->type;
|
|
} elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
|
|
$fun = $method . '_' . $this->method;
|
|
}
|
|
if (isset($fun)) {
|
|
return App::invokeMethod([$this, $fun]
|
|
);
|
|
} else {
|
|
// 抛出异常
|
|
throw new \Exception('error action :' . $method);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Purpose: 通过小程序端的用户标示获取用户信息
|
|
*
|
|
* @Author: zzf
|
|
*
|
|
* @Return: mixed 查询返回值(结果集对象)
|
|
*/
|
|
protected function getUserInfo()
|
|
{
|
|
|
|
$value = getCache($this->autograph, $this->_uniacid);
|
|
|
|
if (empty($value)) {
|
|
|
|
$this->errorMsg('need login', 401);
|
|
}
|
|
|
|
if (empty($value['phone'])) {
|
|
|
|
// $this->errorMsg('need phone',403);
|
|
|
|
}
|
|
|
|
$user_model = new \app\farm\model\User();
|
|
|
|
$value['balance'] = $user_model->where(['id' => $value['id']])->value('balance');
|
|
|
|
return $value;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author chenniang
|
|
* @DataTime: 2021-03-19 15:22
|
|
* @功能说明:获取当前的门店信息
|
|
*/
|
|
public function getStoreInfo($err = 1)
|
|
{
|
|
|
|
$user_id = $this->getUserId();
|
|
|
|
$user_model = new \app\farm\model\User();
|
|
|
|
$cap_id = $user_model->where(['id' => $user_id])->value('last_store_id');
|
|
|
|
$cap_info = [];
|
|
|
|
if (!empty($cap_id)) {
|
|
|
|
$cap_model = new Farmer();
|
|
|
|
$dis = [
|
|
|
|
'id' => $cap_id,
|
|
|
|
'status' => 2,
|
|
|
|
'business_status' => 1,
|
|
|
|
'type' => 2,
|
|
];
|
|
|
|
$cap_info = $cap_model->dataInfo($dis);
|
|
}
|
|
|
|
if (empty($cap_info) && $err == 1) {
|
|
|
|
// $this->errorMsg('请选择店铺',-407);
|
|
|
|
}
|
|
|
|
return $cap_info;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @Purpose: 通过小程序端的用户标示获取用户id
|
|
*
|
|
* @Author: zzf
|
|
*
|
|
* @Return: mixed 查询返回值(结果集对象)
|
|
*/
|
|
protected function getUserId()
|
|
{
|
|
|
|
$value = getCache($this->autograph, $this->_uniacid);
|
|
|
|
if ($this->is_app == 1) {
|
|
|
|
|
|
$user_model = new \app\farm\model\User();
|
|
|
|
$id = $user_model->where(['check' => 1])->value('id');
|
|
|
|
return $id;
|
|
}
|
|
|
|
if (($value === false && !$this->match($this->noNeedLogin))) {
|
|
|
|
$this->errorMsg('need login', 401);
|
|
|
|
}
|
|
|
|
|
|
// if($this->match($this->noNeedLogin)&&empty($value)){
|
|
//
|
|
// $user_model = new \app\farm\model\User();
|
|
//
|
|
// $value =$user_model->dataInfo(['uniacid'=>$this->_uniacid,'check'=>1]);
|
|
//
|
|
// // setCache($this->autograph,$value,7200,$this->_uniacid);
|
|
// }
|
|
|
|
// if ( (!empty($value['check']) &&!$this->match($this->noNeedLogin)))
|
|
// {
|
|
//
|
|
// $this->errorMsg('need login',401);
|
|
//
|
|
// }
|
|
|
|
|
|
return !empty($value['id']) ? $value['id'] : 0;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 获取支付信息
|
|
*/
|
|
|
|
|
|
public function payConfig($uniacid = '1', $is_app = 7)
|
|
{
|
|
|
|
if ($is_app == 7) {
|
|
|
|
$is_app = $this->is_app;
|
|
}
|
|
|
|
$uniacid_id = !empty($uniacid) ? $uniacid : $this->_uniacid;
|
|
|
|
$pay = Db::name('lbfarm_pay_config')->where(['uniacid' => $uniacid_id])->find();
|
|
|
|
$config = Db::name('lbfarm_config')->where(['uniacid' => $uniacid_id])->find();
|
|
|
|
if (empty($pay['mch_id']) || empty($pay['pay_key'])) {
|
|
|
|
// $this->errorMsg('未配置支付信息');
|
|
}
|
|
|
|
$setting['payment']['merchant_id'] = $pay['mch_id'];
|
|
|
|
$setting['payment']['key'] = $pay['pay_key'];
|
|
|
|
$setting['payment']['cert_path'] = $pay['cert_path'];
|
|
|
|
$setting['payment']['key_path'] = $pay['key_path'];
|
|
|
|
$setting['payment']['ali_appid'] = $pay['ali_appid'];
|
|
|
|
$setting['payment']['ali_privatekey'] = $pay['ali_privatekey'];
|
|
|
|
$setting['payment']['ali_publickey'] = $pay['ali_publickey'];
|
|
|
|
if ($is_app == 0) {
|
|
|
|
$setting['app_id'] = $config['appid'];
|
|
|
|
$setting['secret'] = $config['appsecret'];
|
|
|
|
} elseif ($is_app == 1) {
|
|
|
|
$setting['app_id'] = $config['app_app_id'];
|
|
|
|
$setting['secret'] = $config['app_app_secret'];
|
|
|
|
} else {
|
|
|
|
$setting['app_id'] = $config['web_app_id'];
|
|
|
|
$setting['secret'] = $config['web_app_secret'];
|
|
|
|
}
|
|
|
|
$setting['is_app'] = $is_app;
|
|
|
|
return $setting;
|
|
}
|
|
|
|
/**
|
|
* @Purpose: 获取formId
|
|
*
|
|
* @Author: zzf
|
|
*
|
|
* @Return: mixed 查询返回值(结果集对象)
|
|
*/
|
|
|
|
public function getFormId($to_uid)
|
|
{
|
|
return [];
|
|
// 七天前开始的的时间戳
|
|
// $beginTime = mktime( 0, 0, 0, date( 'm' ), date( 'd' ) - 6, date( 'Y' ) );
|
|
$beginTime = strtotime(date('Y-m-d', time())) - 86400 * 6;
|
|
$formId = Db::name('longbing_card_formId')
|
|
->where(['user_id' => $to_uid])
|
|
->order('id desc')
|
|
->select();
|
|
if (empty($formId)) {
|
|
return false;
|
|
}
|
|
if ($formId[0]['create_time'] < $beginTime) {
|
|
Db::name('longbing_card_formId')
|
|
->where(['id' => $formId[0]['id']])
|
|
->delete();
|
|
$this->getFormId($to_uid);
|
|
} else {
|
|
Db::name('longbing_card_formId')
|
|
->where(['id' => $formId[0]['id']])
|
|
->delete();
|
|
return $formId[0]['formId'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* User: chenniang
|
|
* Date: 2019-09-12 20:37
|
|
* @param string $msg
|
|
* @return void
|
|
* descption:直接抛出异常
|
|
*/
|
|
protected function errorMsg($msg = '', $code = 400)
|
|
{
|
|
$msg = Lang::get($msg);
|
|
$this->results($msg, $code);
|
|
}
|
|
|
|
/**
|
|
* User: chenniang
|
|
* Date: 2019-09-12 20:42
|
|
* @param $msg
|
|
* @param int $code
|
|
* @param array $header
|
|
* @return void
|
|
* descption:直接抛出状态
|
|
*/
|
|
protected function results($msg, $code, array $header = [])
|
|
{
|
|
$result = [
|
|
'error' => $msg,
|
|
'code' => $code,
|
|
];
|
|
$response = Response::create($result, 'json', 200)->header($header);
|
|
throw new HttpResponseException($response);
|
|
}
|
|
}
|