// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\controller; use app\store\model\store\User as StoreUserModel; use app\store\service\store\User as StoreUserService; use app\api\model\Code as CodeModel; use app\store\model\Order as OrderModel; use think\facade\Cache; use think\response\Json; use think\facade\Request; use GatewayWorker\Lib\Gateway; /** * 商家后台认证 * Class Passport * @package app\store\controller */ class Passport extends Controller { /** * 强制验证当前访问的控制器方法method * 例: [ 'login' => 'POST' ] * @var array */ protected $methodRules = [ 'login' => 'POST', 'logout' => 'POST', ]; /** * 商家用户登录 * @return array|bool|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function login() { $model = new StoreUserModel; $postData = $this->postData(); if (($userInfo = $model->login($postData)) === false) { return $this->renderError($model->getError() ?: '登录失败'); } return $this->renderSuccess([ 'userId' => (int)$userInfo['store_user_id'], 'token' => $model->getToken() ], '登录成功'); } /** * 退出登录 * @return array */ public function logout() { // 清空登录状态 StoreUserService::logout(); return $this->renderSuccess('操作成功'); } public function bindUid() { $postData = $this->postData(); $uid = $postData['uid']; $clientId = $postData['client_id']; Gateway::bindUid($clientId, $uid); } /** * 订单匹配支付码 * @param int $orderId * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function detail(int $orderId): Json { $domain = Request::instance()->domain(); $order = OrderModel::detail($orderId); // 加锁 $lock_key = $orderId . '_lock'; while (true) { $lock_success = Cache::store('redis')->setnx($lock_key, 1); if (!$lock_success) { // 锁定失败,说明其他客户端已经在处理该队列了,可以直接返回或者等待一段时间后重试 continue; } // 设置锁定过期时间,防止锁定无法释放 Cache::store('redis')->expire($lock_key, 20); break; } $update_time = 0; if(empty($order['code_id']) && $order['order_status'] == 10) { /**查询支付码**/ $CodeModel = new CodeModel(); $codeInfo = $CodeModel->getCodeDetail($order['total_price'], $order['passageway'])->toArray(); if (empty($codeInfo['code_id'])) { $order->save([ 'ip' => Request::instance()->ip(), 'buyer_remark' => '分配支付码失败', 'update_time' => time(), ]); $result = ['money'=>0,'qrcode'=>'','seconds'=>0,'passageway'=>$order['passageway']]; return $this->renderSuccess($result,'分配支付码失败'); } else { $order->save([ 'ip' => Request::instance()->ip(), 'code_id' => $codeInfo['code_id'], 'order_no_sn' => $codeInfo['order_no_sn'], 'shop_id' => $codeInfo['shop_id'], 'store_user_id' => $codeInfo['store_user_id'], 'search_url' => $codeInfo['search_url'], 'passageway' => $codeInfo['passageway'], 'update_time' => time(), ]); } $update_time = time(); }elseif($order['code_id']){ $codeInfo = CodeModel::detail($order['code_id']); $update_time = is_string($order['update_time'])?(int)strtotime($order['update_time']):$order['update_time']; } // 释放锁定 Cache::store('redis')->del($lock_key); $seconds = ($update_time+300-time())>0?($update_time+300-time()):0; $qrcode = ''; if(isset($codeInfo['code_id']) && $seconds>0 && $order['order_status'] == 10) { if($order['passageway'] == 'WX') { $url = $domain . '/wxScanPng.php'; //$aa = str_replace("&", "%26", $codeInfo['link']); //$bb = str_replace("amp;", "", $aa); $qrcode = $url . '?url=' . urlencode(htmlspecialchars_decode($codeInfo['link'])); }elseif($order['passageway'] == 'SNZFBH5'){ $qrcode = htmlspecialchars_decode($codeInfo['alipay']); }else{ //$qrcode = htmlspecialchars_decode($codeInfo['alipay']); $qrcode = "alipays://platformapi/startapp?appId=20000067&url=".urlencode(htmlspecialchars_decode($codeInfo['alipay'])); } } $result = ['money'=>$order['total_price'],'qrcode'=>$qrcode,'seconds'=>$seconds,'passageway'=>$order['passageway']]; return $this->renderSuccess($result); } /** * 修改订单点击支付次数 * @param int $orderId * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function clickNum(int $orderId): Json { $order = OrderModel::detail($orderId); if(!empty($order['code_id'])) { $order->save(['click'=>(int)$order['click']+1]); } return $this->renderSuccess(); } }