// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller; use app\api\model\User as UserModel; use app\api\model\UserCoupon as UserCouponModel; use app\api\model\Wxapp as WxappModel; use app\api\model\Order as OrderModel; use app\api\model\Coupon as CouponModel; use app\api\service\passport\Login as LoginService; use app\api\service\passport\Captcha as CaptchaService; use app\api\service\passport\SmsCaptcha as SmsCaptchaService; use app\api\service\User as UserService; use app\common\enum\order\PayStatus; use app\common\library\weixin\WXBizDataCrypt; use app\common\model\UserOauth; use think\cache\driver\Redis; use think\facade\Log; use app\common\model\SmsCode; /** * 用户认证模块 * Class Passport * @package app\api\controller */ class Passport extends Controller { /** * 登录接口 (需提交手机号、短信验证码、第三方用户信息) * 该登录接口暂未使用 * * @return array|\think\response\Json * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function login() { // 执行登录 $LoginService = new LoginService; // if (!$LoginService->login($this->postForm())) { if (!$LoginService->login($this->request->post())) { return $this->renderError($LoginService->getError()); } // 用户信息 $userInfo = $LoginService->getUserInfo(); $token = $LoginService->getToken((int)$userInfo['user_id']); /* $user_id = $userInfo['user_id']; $cache_token_key = 'xcx_token_'.$user_id; Cache::set($cache_token_key, $token, 3600);*/ //添加登录后领取新人券的功能 $this->receiveCoupon($userInfo); return $this->renderSuccess([ 'userId' => (int)$userInfo['user_id'], 'token' => $LoginService->getToken((int)$userInfo['user_id']) ], '登录成功'); } //未下过单的用户登录后静默领取新人优惠券 private function receiveCoupon($userInfo){ //有支付成功的订单就不发新人券了 if(OrderModel::where(['user_id'=>$userInfo['user_id'],'pay_status'=>PayStatus::SUCCESS])->count()){ return true; } //获取还能领取的新人券列表 $couponList = CouponModel::where(['send_type'=>20,'audit_status'=>10,'status'=>1])->where('end_time','>',date('Y-m-d H:i:s'))->select(); foreach ($couponList as $coupon){ for ($i=0;$i<$coupon['limit_receive_cnt'];$i++){ $model = new UserCouponModel; $res = $model->receiveLogin($coupon,$userInfo); if (!$res) { continue; } if($res){ //领了新人券,写个缓存值1,下次请求告知前端刚领了, //然后将值改为2,就表示已经领过了 $this->receiveRds($userInfo['user_id']); } } } return true; } //写个缓存 value 1 代表刚领了,2 代表领过了 public function receiveRds($user_id){ $key = (new UserCouponModel)->getUserCouponRdsKey($user_id); $rds = new Redis(config('cache.stores.redis')); $value = $rds->get($key); if($value==null){ $rds->set($key,1); } } /** * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息) * 实现流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token * -> 不存在: 返回false, 调用 解密用户手机号接口 * @return array|\think\response\Json * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function mpWxLogin() { // 微信小程序一键登录 $LoginService = new LoginService; $params = $this->request->post(); if (!$LoginService->mpWxLogin($params)) { return $this->renderError($LoginService->getError()); } // 获取登录成功后的用户信息 $userInfo = $LoginService->getUserInfo(); //添加登录后领取新人券的功能 $this->receiveCoupon($userInfo); return $this->renderSuccess([ 'userId' => (int)$userInfo['user_id'], 'token' => $LoginService->getToken((int)$userInfo['user_id']), 'openid' => $userInfo['open_id'] ], '登录成功'); } //手机号登录注册 public function mobile(){ $params = $this->request->post(); if (empty($params['mobile'])) { return $this->renderError("请输入手机号"); } if(empty($params['sms_code'])){ return $this->renderError("请输入验证码"); } $mobile = $params['mobile']; $sms_code = $params['sms_code']; $openid = ''; if ($sms_code != '000000' || env('APP_DEBUG') == false){ $sms = SmsCode::where("mobile",$mobile)->where('is_use',0)->where("create_time",'>',time()-3000)->order(['id'=>'desc'])->find(); if(empty($sms)){ return $this->renderError("请获取手机验证码"); } if($sms->code!=$sms_code){ return $this->renderError("验证码不正确"); } $sms->is_use = 1; $sms->save(); } $LoginService = new LoginService; $user = UserModel::detail(['mobile' => $mobile]); if($user){ // $user->open_id = '' // $user->save(); // $userOauth = UserOauth::where('user_id', $user->user_id)->find(); // if (!empty($userOauth)) { // $userOauth->oauth_id = $openid; // $userOauth->save(); // } $params = $this->request->post(); $params = array_merge($params,['mobile' => $mobile]); $params['oauth'] = "MOBILE"; // 手机号 $params['isParty'] = false; $params['unionid'] = $info['unionid'] ?? ''; if (!$LoginService->login($params)) { return $this->renderError($LoginService->getError()); } }else{ //注册新用户 // 1.调用名厨之家第三方应用注册接口注册用户并返回用户信息 // $postData = [ // 'mobile' => (string)$mobile, // 'openid' => '', // 'source' => 12 // ]; //$cur_url = config('chef.serv_cookhome').'/api/wx/third_platform_register'; //$res = curl_post($cur_url, $postData); //$res = json_decode($res, true); // 获取名厨用户id //$chef_user_id = $res['id']; // 2.本地数据库写入用户信息 $params = $this->request->post(); $params['mobile'] = (string)$params['mobile']; $params['oauth'] = "MOBILE"; // 手机号 $params['isParty'] = false; $params['openid'] = ''; $params['unionid'] = $info['unionid'] ?? ''; $params = array_merge($params,['userId' => 0, 'mobile' => $mobile]); if (!$LoginService->login($params)) { return $this->renderError($LoginService->getError()); } } // 获取登录成功后的用户信息 $user = $LoginService->getUserInfo(); //添加登录后领取新人券的功能 $this->receiveCoupon($user); return $this->renderSuccess([ 'userId' => (int)$user['user_id'], 'token' => $LoginService->getToken((int)$user['user_id']), 'result' => [] ], '登录成功'); } /** * Notes:解密用户手机号 * * Author: zhangs * DateTime: 2021/9/27 11:42 * @return mixed */ public function mpWxMobile() { $params = $this->request->post(); if (empty($params['openid']) || empty($params['encryptedData']) || empty($params['iv'])) { return $this->renderError("请绑定手机号"); } $openid = $params['openid']; $iv = $params['iv']; $encryptedData = $params['encryptedData']; $rds = new Redis(config('cache.stores.redis')); $key = LoginService::OPEN_ID_KEY.$openid; $info = $rds->get($key); if (empty($info)) { return $this->renderError('请重新授权获取手机号!'); } // 获取当前小程序信息 $wxConfig = WxappModel::getWxappCache(); $sessionKey = $info['session_key']; $pc = new WXBizDataCrypt($wxConfig['app_id'], $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data); if($errCode == 0){ $LoginService = new LoginService; $result = json_decode($data); $mobile = $result->phoneNumber; $user = UserModel::detail(['mobile' => $mobile]); if($user){ $user->open_id = $openid; $user->save(); $userOauth = UserOauth::where('user_id', $user->user_id)->find(); if (!empty($userOauth)) { $userOauth->oauth_id = $openid; $userOauth->save(); } $params = $this->request->post(); $params = array_merge($params,['mobile' => $mobile]); $params['oauth'] = "MP-WEIXIN"; // 微信小程序 $params['isParty'] = true; $params['unionid'] = $info['unionid'] ?? ''; if (!$LoginService->login($params)) { return $this->renderError($LoginService->getError()); } }else{ //注册新用户 // 1.调用名厨之家第三方应用注册接口注册用户并返回用户信息 // $postData = [ // 'mobile' => $mobile, // 'openid' => $openid, // 'source' => 12 // ]; // $res = curl_post(config('chef.serv_cookhome').'/api/wx/third_platform_register', $postData); // $res = json_decode($res, true); // // 获取名厨用户id // $chef_user_id = $res['id']; // 2.本地数据库写入用户信息 $params = $this->request->post(); $params['oauth'] = 'MP-WEIXIN'; $params['isParty'] = true; $params['unionid'] = $info['unionid'] ?? ''; $params = array_merge($params,['userId' => 0, 'mobile' => $mobile]); if (!$LoginService->login($params)) { return $this->renderError($LoginService->getError()); } /* $sharerId = intval($params['sharerId']??0); Log::error('分享者000'.$sharerId); if ($sharerId > 0){ Log::error('分享者'.$sharerId); $flag = UserModel::updateNormalUserRelation($sharerId); if ($flag !== true){ Log::error('分享者'.$sharerId.'变更用户'. json_encode($flag) .'上级关系失败'); } }*/ } // 获取登录成功后的用户信息 $user = $LoginService->getUserInfo(); //添加登录后领取新人券的功能 $this->receiveCoupon($user); return $this->renderSuccess([ 'userId' => (int)$user['user_id'], 'token' => $LoginService->getToken((int)$user['user_id']), 'result' => $result ], '登录成功'); }else{ return $this->renderError('请重新授权获取手机号'); } } /** * 小程序自动领取新人优惠券 * @author: zjwhust * @Time: 2022/1/11 13:58 */ public function autoCoupon(){ $user = UserService::getCurrentLoginUser(true); //添加登录后领取新人券的功能 $this->receiveCoupon($user); return $this->renderSuccess(); } /** * Notes:更新微信用户信息 * Author: zhangs * DateTime: 2021/10/8 11:52 * @return mixed */ public function updateWxUserInfo() { $params = $this->request->post(); $userInfo = UserService::getCurrentLoginUser(true); $LoginService = new LoginService; $ret = $LoginService->updateWxUserInfo($params, $userInfo); if ($ret === false) { return $this->renderError('更新失败'); } return $this->renderSuccess([], '更新成功'); } /** * 图形验证码 * @return array|\think\response\Json */ public function captcha() { $CaptchaService = new CaptchaService; return $this->renderSuccess($CaptchaService->create()); } /** * 发送短信验证码 * @return array|\think\response\Json */ public function sendSmsCaptcha() { $SmsCaptchaService = new SmsCaptchaService; if (!$SmsCaptchaService->sendSmsCaptcha($this->postForm())) { return $this->renderError($SmsCaptchaService->getError()); } return $this->renderSuccess('发送成功,请注意查收'); } /** * 修改普通用户绑定门店信息 * @return array|\think\response\Json * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function changeUpperUser(){ $params = $this->request->post(); $sharerId = intval($params['sharerId']??0); $isScan = intval($params['isScanCode']??0); Log::debug('分享者A--'.$sharerId); if ($sharerId <= 0){ Log::error('分享者'.$sharerId); return $this->renderError('参数有误'); } $flag = UserModel::updateNormalUserRelation($sharerId,$isScan); if ($flag !== true){ return $this->renderError('更新失败'); } return $this->renderSuccess([], '更新成功'); } }