优化代码

This commit is contained in:
2026-01-04 15:01:16 +08:00
parent e0ff5ccdc7
commit b764c708ce
10 changed files with 2885 additions and 465 deletions

View File

@@ -1,23 +1,17 @@
<?php
declare ( strict_types = 1 );
declare (strict_types=1);
namespace app;
use app\BaseController;
use app\card\model\User;
use app\card\model\UserPhone;
use app\farm\model\Farmer;
use app\restaurant\model\Table;
use app\shop\model\Cap;
use longbingcore\tools\LongbingArr;
use Qiniu\Auth;
use think\App;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Request;
use think\Validate;
use think\Response;
use think\facade\Db;
use think\facade\Lang;
/**
* 控制器基础类
*/
@@ -54,7 +48,7 @@ abstract class ApiRest extends BaseController
//唯一app标示
public $_uniacid = 1;
//定义检查中间件
// protected $middleware = [ 'app\middleware\CheckInput' ,'think\middleware\LoadLangPack' ,'app\middleware\GetAuthConfig' ,'app\middleware\AutoStatic','app\middleware\StaticHour'];
// protected $middleware = [ 'app\middleware\CheckInput' ,'think\middleware\LoadLangPack' ,'app\middleware\GetAuthConfig' ,'app\middleware\AutoStatic','app\middleware\StaticHour'];
//获取配置信息
protected $_config = [];
@@ -70,12 +64,12 @@ abstract class ApiRest extends BaseController
// 默认用户头像
'avatar' => 'https://retail.xiaochengxucms.com/defaultAvatar.png',
// 默认内容图片
'image' => 'https://retail.xiaochengxucms.com/lbCardDefaultImage.png',
'image' => 'https://retail.xiaochengxucms.com/lbCardDefaultImage.png',
);
protected $_is_weiqin = false ;
protected $_is_weiqin = false;
protected $check_url = "";
protected $check_url = "";
/**
* 无需登录的方法,同时也就不需要鉴权了
@@ -84,75 +78,83 @@ abstract class ApiRest extends BaseController
protected $noNeedLogin = [];
public function __construct ( App $app )
public function __construct(App $app)
{
parent::__construct($app);
parent::__construct( $app );
if(in_array($this->_method,['options','Options','OPTIONS'])){
echo true;exit;
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->_input = json_decode($this->request->getInput(), true);
//获取头部信息
$this->_header = $this->request->header();
$this->is_app = !empty($this->_header['isapp'])?$this->_header['isapp']:0;
if ( defined( 'IS_WEIQIN' ) )
{
global $_GPC, $_W;
$this->_uniacid = $_W[ 'uniacid' ];
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'];
}
}
}
else
{
if(isset($this->_param[ 'i' ]))
{
$this->_uniacid = $this->_param[ 'i' ];
$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' ) )
{
if (defined('LONGBING_CARD_UNIACID')) {
define( 'LONGBING_CARD_UNIACID', $this->_uniacid );
define('LONGBING_CARD_UNIACID', $this->_uniacid);
}
$this->shareChangeData($this->_param);
$this->shareChangeData($this->_param);
//获取autograph 小程序用户唯一标示
if ( isset( $this->_header[ 'autograph' ] ) && $this->_header[ 'autograph' ])
{
if (isset($this->_header['autograph']) && $this->_header['autograph']) {
$this->autograph = $this->_header['autograph'];
}
else
{
if(!$this->match($this->noNeedLogin)){
} else {
if (!$this->match($this->noNeedLogin)) {
$this->errorMsg('need login',401);
$this->errorMsg('need login', 401);
}
}
//获取配置信息
$this->_config = longbingGetAppConfig($this->_uniacid);
//语言
if ( isset( $this->_header[ 'lang' ] ) ) $this->_token = $this->_header[ 'lang' ];
if (isset($this->_header['lang'])) $this->_token = $this->_header['lang'];
if(!empty($this->autograph)&&!$this->match($this->noNeedLogin)){
if (!empty($this->autograph) && !$this->match($this->noNeedLogin)) {
$this->_user_id = $this->getUserId();
$this->_user = $this->getUserInfo();
$this->_user = $this->getUserInfo();
}
landNotice($this->_uniacid);
}
@@ -161,7 +163,8 @@ abstract class ApiRest extends BaseController
* @DataTime: 2020-08-21 17:43
* @功能说明:
*/
public function shareChangeData($input){
public function shareChangeData($input)
{
$arr = [
@@ -200,11 +203,13 @@ abstract class ApiRest extends BaseController
'farm/app/IndexUser/farmerInfo',
'farm/app/Index/configInfo',
'farm/app/Index/indexStoreList',
];
if(!empty($input['s'])&&in_array($input['s'],$arr)){
if (!empty($input['s']) && in_array($input['s'], $arr)) {
$input['s'] = trim(strrchr($input['s'], '/'),'/');
$input['s'] = trim(strrchr($input['s'], '/'), '/');
$this->noNeedLogin[] = $input['s'];
}
@@ -218,18 +223,17 @@ abstract class ApiRest extends BaseController
* @DataTime: 2020-07-09 12:00
* @功能说明:检测方法传递
*/
public function match($arr){
public function match($arr)
{
$arr = is_array($arr) ? $arr : explode(',', $arr);
if (!$arr)
{
if (!$arr) {
return FALSE;
}
$arr = array_map('strtolower', $arr);
// 是否存在
if (in_array(strtolower($this->request->action()), $arr) || in_array('*', $arr))
{
if (in_array(strtolower($this->request->action()), $arr) || in_array('*', $arr)) {
return TRUE;
}
@@ -238,35 +242,36 @@ abstract class ApiRest extends BaseController
}
//返回请求成功的数据
public function success ( $data, $code = 200 )
public function success($data, $code = 200)
{
$result[ 'data' ] = LongbingGetWxApiReturnData($data);
$result[ 'code' ] = $code;
$result[ 'sign' ] = null;
$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 );
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 )
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 );
return $this->response($result, 'json', 200);
}
public function getErrorData($msg, $code = 400)
{
$result[ 'error' ] = Lang::get($msg);
$result[ 'code' ] = $code;
$result['error'] = Lang::get($msg);
$result['code'] = $code;
return $result;
}
/**
* 输出返回数据
* @access protected
@@ -275,9 +280,9 @@ abstract class ApiRest extends BaseController
* @param integer $code HTTP状态码
* @return Response
*/
protected function response ( $data, $type = 'json', $code = 200 )
protected function response($data, $type = 'json', $code = 200)
{
return Response::create( $data, $type )->code( $code );
return Response::create($data, $type)->code($code);
}
/**
@@ -287,30 +292,22 @@ abstract class ApiRest extends BaseController
* @return mixed
* @throws \Exception
*/
public function _empty ( $method )
public function _empty($method)
{
if ( method_exists( $this, $method . '_' . $this->method . '_' . $this->type ) )
{
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 ) )
{
} 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 ) )
{
} elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
$fun = $method . '_' . $this->method;
}
if ( isset( $fun ) )
{
return App::invokeMethod( [ $this, $fun ]
if (isset($fun)) {
return App::invokeMethod([$this, $fun]
);
}
else
{
} else {
// 抛出异常
throw new \Exception( 'error action :' . $method );
throw new \Exception('error action :' . $method);
}
}
@@ -321,25 +318,25 @@ abstract class ApiRest extends BaseController
*
* @Return: mixed 查询返回值(结果集对象)
*/
protected function getUserInfo ()
protected function getUserInfo()
{
$value = getCache($this->autograph, $this->_uniacid);
if(empty($value)){
if (empty($value)) {
$this->errorMsg('need login',401);
$this->errorMsg('need login', 401);
}
if(empty($value['phone'])){
if (empty($value['phone'])) {
// $this->errorMsg('need phone',403);
// $this->errorMsg('need phone',403);
}
$user_model = new \app\farm\model\User();
$value['balance'] = $user_model->where(['id'=>$value['id']])->value('balance');
$value['balance'] = $user_model->where(['id' => $value['id']])->value('balance');
return $value;
}
@@ -350,37 +347,38 @@ abstract class ApiRest extends BaseController
* @DataTime: 2021-03-19 15:22
* @功能说明:获取当前的门店信息
*/
public function getStoreInfo($err=1){
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_id = $user_model->where(['id' => $user_id])->value('last_store_id');
$cap_info = [];
if(!empty($cap_id)){
if (!empty($cap_id)) {
$cap_model = new Farmer();
$dis = [
'id' => $cap_id,
'id' => $cap_id,
'status' => 2,
'status' => 2,
'business_status' => 1,
'type' => 2,
'type' => 2,
];
$cap_info = $cap_model->dataInfo($dis);
}
if(empty($cap_info)&&$err==1){
if (empty($cap_info) && $err == 1) {
// $this->errorMsg('请选择店铺',-407);
// $this->errorMsg('请选择店铺',-407);
}
@@ -389,7 +387,6 @@ abstract class ApiRest extends BaseController
}
/**
* @Purpose: 通过小程序端的用户标示获取用户id
*
@@ -397,25 +394,24 @@ abstract class ApiRest extends BaseController
*
* @Return: mixed 查询返回值(结果集对象)
*/
protected function getUserId ()
protected function getUserId()
{
$value = getCache( $this->autograph, $this->_uniacid );
$value = getCache($this->autograph, $this->_uniacid);
if($this->is_app==1){
if ($this->is_app == 1) {
$user_model = new \app\farm\model\User();
$id = $user_model->where(['check'=>1])->value('id');
$id = $user_model->where(['check' => 1])->value('id');
return $id;
}
if ( ($value === false &&!$this->match($this->noNeedLogin)))
{
if (($value === false && !$this->match($this->noNeedLogin))) {
$this->errorMsg('need login',401);
$this->errorMsg('need login', 401);
}
@@ -437,68 +433,69 @@ abstract class ApiRest extends BaseController
// }
return !empty($value[ 'id' ])?$value[ 'id' ]:0;
return !empty($value['id']) ? $value['id'] : 0;
}
/**
*
* 获取支付信息
*/
public function payConfig($uniacid = '1', $is_app = 7)
{
public function payConfig ($uniacid = '1',$is_app=7){
if($is_app==7){
if ($is_app == 7) {
$is_app = $this->is_app;
}
$uniacid_id = !empty($uniacid)?$uniacid:$this->_uniacid;
$uniacid_id = !empty($uniacid) ? $uniacid : $this->_uniacid;
$pay = Db::name('lbfarm_pay_config')->where(['uniacid'=>$uniacid_id])->find();
$pay = Db::name('lbfarm_pay_config')->where(['uniacid' => $uniacid_id])->find();
$config = Db::name( 'lbfarm_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' ])){
if (empty($pay['mch_id']) || empty($pay['pay_key'])) {
// $this->errorMsg('未配置支付信息');
// $this->errorMsg('未配置支付信息');
}
$setting[ 'payment' ][ 'merchant_id' ] = $pay[ 'mch_id' ];
$setting['payment']['merchant_id'] = $pay['mch_id'];
$setting[ 'payment' ][ 'key' ] = $pay[ 'pay_key' ];
$setting['payment']['key'] = $pay['pay_key'];
$setting[ 'payment' ][ 'cert_path' ] = $pay[ 'cert_path' ];
$setting['payment']['cert_path'] = $pay['cert_path'];
$setting[ 'payment' ][ 'key_path' ] = $pay[ 'key_path' ];
$setting['payment']['key_path'] = $pay['key_path'];
$setting[ 'payment' ][ 'ali_appid' ] = $pay[ 'ali_appid' ];
$setting['payment']['ali_appid'] = $pay['ali_appid'];
$setting[ 'payment' ][ 'ali_privatekey' ] = $pay[ 'ali_privatekey' ];
$setting['payment']['ali_privatekey'] = $pay['ali_privatekey'];
$setting[ 'payment' ][ 'ali_publickey' ] = $pay[ 'ali_publickey' ];
$setting['payment']['ali_publickey'] = $pay['ali_publickey'];
if($is_app==0){
if ($is_app == 0) {
$setting[ 'app_id' ] = $config['appid'];
$setting['app_id'] = $config['appid'];
$setting[ 'secret' ] = $config['appsecret'];
$setting['secret'] = $config['appsecret'];
}elseif($is_app==1){
} elseif ($is_app == 1) {
$setting[ 'app_id' ] = $config['app_app_id'];
$setting['app_id'] = $config['app_app_id'];
$setting[ 'secret' ] = $config['app_app_secret'];
$setting['secret'] = $config['app_app_secret'];
}else{
} else {
$setting[ 'app_id' ] = $config['web_app_id'];
$setting['app_id'] = $config['web_app_id'];
$setting[ 'secret' ] = $config['web_app_secret'];
$setting['secret'] = $config['web_app_secret'];
}
$setting[ 'is_app' ]= $is_app;
$setting['is_app'] = $is_app;
return $setting;
}
@@ -511,35 +508,32 @@ abstract class ApiRest extends BaseController
* @Return: mixed 查询返回值(结果集对象)
*/
public function getFormId ( $to_uid )
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' )
$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 ) )
{
if (empty($formId)) {
return false;
}
if ( $formId[ 0 ][ 'create_time' ] < $beginTime )
{
Db::name( 'longbing_card_formId' )
->where( [ 'id' => $formId[ 0 ][ 'id' ] ] )
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' ] ] )
$this->getFormId($to_uid);
} else {
Db::name('longbing_card_formId')
->where(['id' => $formId[0]['id']])
->delete();
return $formId[ 0 ][ 'formId' ];
return $formId[0]['formId'];
}
}
/**
* User: chenniang
* Date: 2019-09-12 20:37
@@ -547,9 +541,10 @@ abstract class ApiRest extends BaseController
* @return void
* descption:直接抛出异常
*/
protected function errorMsg($msg = '',$code = 400){
protected function errorMsg($msg = '', $code = 400)
{
$msg = Lang::get($msg);
$this->results($msg,$code);
$this->results($msg, $code);
}
/**
@@ -563,7 +558,7 @@ abstract class ApiRest extends BaseController
*/
protected function results($msg, $code, array $header = [])
{
$result = [
$result = [
'error' => $msg,
'code' => $code,
];