param(); $signature = $param['signature'] ?? '';// 签名 $timestamp = $param['timestamp'] ?? '';// 时间戳 $nonce = $param['nonce'] ?? ''; // 随机数 $echostr = $param['echostr'] ?? '0'; // 随机字符串 $checkRes = (new WechatService())->wechatChekToken($signature, $timestamp, $nonce); $param['res_error'] = $checkRes; if ($checkRes) { echo $echostr; die; } return $this->buildFailed(400,'微信认证失败!'); } public function sendSms(Request $request) { $param = $request->param(); try { validate(WechatUserValidate::class)->scene('smsCode')->check($param); $AuthCodeUtil = new AuthCodeUtil(new Redis()); $AuthCodeUtil->setRate($param['mobile']); $code = $AuthCodeUtil->setCode($param['mobile']); $SmsService = new SmsService(); $SmsService->sendSms($param['mobile'],$SmsService->smsCodeTemplate($code)); return $this->buildSuccess([],'短信已发送'); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::INVALID,$e->getMessage()); } } /** * 用户添加手机号用户编号 * @param Request $request * @return \think\Response * @throws \Exception */ public function savePuCode(Request $request) { $param = $request->param(); // 验证数据 $wechat_user_id = $request->wechat_user_id; //$defaultSnsCode = '439764'; try { validate(WechatUserValidate::class)->scene('savePuCode')->check($param); $res = (new AuthCodeUtil(new Redis()))->verifyCode($param['mobile'],$param['snsCode']); if (!$res) { throw new ValidateException('短信验证码错误'); } } catch (ValidateException $e) { return $this->buildFailed(ReturnCode::INVALID,$e->getMessage()); } Db::startTrans(); try { $mobile = $param['mobile']; $pucode = $param['pucode']; $FeeService = new FeeService($pucode); $feeUserInfo = $FeeService->getUsers(); if ($feeUserInfo['UserCode'] != $pucode) { throw new \Exception('用户不存在'); } if ($feeUserInfo['CellPhone'] != $mobile) { throw new \Exception('手机号或用户编号错误'); } $userData = ['phone' => $mobile]; $WechatUser = WechatUser::update($userData,['id' => $wechat_user_id]); if (!$WechatUser) { throw new \Exception('添加失败'); } $WechatPucode = WechatPucode::createPuCode($wechat_user_id,$pucode); if (!$WechatPucode) { throw new \Exception('添加失败'); } Db::commit(); $data = $WechatUser->getUserInfo($wechat_user_id); $token = $this->signToken($data); return $this->buildSuccess(['token' => $token]); } catch (\Exception $e) { Db::rollback(); return $this->buildFailed(ReturnCode::UPDATE_FAILED,$e->getMessage()); } } public function getJsSdkData() { try { $WechatService = new WechatService(); return $this->buildSuccess($WechatService->getJsSdkSignature()); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::INVALID,$e->getMessage()); } } }