model = new \app\model\User(); } public $noNeedLogin = [ 'wechatJssdk', 'wechatAuth', 'wechatSilenceAuth', 'miniappAuth', 'miniappSilenceAuth', 'miniappUserInfo', 'authBindindPhone', 'wechatSilenceCode', 'notify' ]; // 公众号jssdk public function wechatJssdk(){ $app = MpServiceFacade::option(); $APIs = array('updateAppMessageShareData','updateTimelineShareData','openLocation','getLocation','chooseWXPay'); $uri = $this->request->param('uri'); $app->jssdk->setUrl($uri); $res = $app->jssdk->buildConfig($APIs, $debug = false, $beta = false, $json = false); return $this->success('获取结果', $res); } // 公众号登录 public function wechatAuth(){ global $_GPC; $param = $this->request->param(); $app = MpServiceFacade::option('snsapi_userinfo'); $conf = ConfServiceFacade::groupGet('system.plan', true); $user = $app->oauth->user(); // print_r($app->$oauth->redirect()); if($user['id']){ $userInfo = \app\model\User::where(['openid_wechat'=>$user['id'],'uniacid'=>$_GPC['uniacid']])->find(); if(!empty($userInfo['id'])){ $token = Random::uuid(); $user['token'] = $token; $user['phone'] = $userInfo['phone']; $user['now_money'] = $userInfo['now_money']; Token::set($token, $userInfo['id'], 24 * 60 * 60 * 365); }else{ $param['openid'] = $user['id']; $param['nickname'] = $user['nickname']; $param['avatar'] = $user['avatar']; $param['login_time'] = date('Y-m-d H:i:s'); $param['uniacid'] = $_GPC['uniacid']; $param['openid_wechat'] = $user['id']; $param['coin'] = !empty($conf['register'])?$conf['register']:0; $param['id_number'] = Random::numeric(10); if(!empty($user['original']['unionid'])){ $param['unionid'] = $user['original']['unionid']; } $saveRes = $this->model->save($param); $token = Random::uuid(); $user['token'] = $token; Token::set($token, $this->model->id, 24 * 60 * 60 * 365); } return $this->success('获取结果', $user); } } // 公众号静默登录 public function wechatSilenceAuth(){ $app = MpServiceFacade::option('snsapi_base'); $oauth = $app->oauth; return $oauth->redirect(); } // 公众号静默登录 public function wechatSilenceCode(){ global $_GPC; $param = $this->request->param(); $app = MpServiceFacade::option('snsapi_base'); $response = $app->oauth->scopes(['snsapi_base'])->redirect(''); $user = $app->oauth->user(); if($user['id']){ $userId = \app\model\User::where(['openid_wechat'=>$user['id']])->value('id'); if($userId){ $token = Random::uuid(); $user['token'] = $token; Token::set($token, $userId, 24 * 60 * 60 * 365); }else{ $param['openid'] = $user['id']; $param['login_time'] = date('Y-m-d H:i:s'); $param['uniacid'] = $_GPC['uniacid']; $param['openid_wechat'] = $user['id']; $saveRes = $this->model->save($param); $token = Random::uuid(); $user['token'] = $token; Token::set($token, $this->model->id,0); } return $this->success('获取结果', $user); } } public function wechatCode(){ global $_GPC; $param = $this->request->param(); $app = MpServiceFacade::option(); // $response = $app->oauth->scopes(['snsapi_base']); $user = $app->oauth->user(); if($user['id']){ $userId = \app\model\User::where(['openid_wechat'=>$res['openid']])->value('id'); if($userId){ $token = Random::uuid(); $res['token'] = $token; Token::set($token, $userId, 24 * 60 * 60 * 365); }else{ $param['openid'] = $res['openid']; $param['login_time'] = date('Y-m-d H:i:s'); $param['uniacid'] = $_GPC['uniacid']; $param['openid_wechat'] = $res['openid']; $saveRes = $this->model->save($param); $token = Random::uuid(); $res['token'] = $token; Token::set($token, $this->model->id, 24 * 60 * 60 * 365); } return $this->success('获取结果', $res); } } // 小程序登录 public function miniappAuth(){ $app = MiniappServiceFacade::option(); $post = $this->request->post(); $decryptedData = $app->encryptor->decryptData($post['session_key'], $post['iv'], $post['encryptedData']); $data = array('avatar' => $decryptedData['avatarUrl'], 'nickname' => $decryptedData['nickName']); // print_r($decryptedData); $user = UserServiceFacade::getUser(); $updateRes = $this->model->where('id', '=', $user['id'])->update($data); if (!$updateRes) throw new \Exception('保存基本信息失败'); return $this->success('获取结果', $decryptedData); } // 小程序静默登录 public function miniappSilenceAuth(){ global $_GPC; // print_r(Random::uuid()); $app = MiniappServiceFacade::option(); $code = $this->request->post('code'); $conf = ConfServiceFacade::groupGet('system.plan', true); if(!$code){ return $this->error('注册失败,未上传code'); } $res = $app->auth->session($code); if(!empty($res['errcode'])){ return $this->error('注册失败,' . $res['errmsg'].'错误码-'.$res['errcode']); } else { $userId = \app\model\User::where(['openid_miniapp'=>$res['openid']])->value('id'); // print_r($userId); if($userId){ $token = Random::uuid(); $res['token'] = $token; Token::set($token, $userId, 0); }else{ $param['openid'] = $res['openid']; $param['login_time'] = date('Y-m-d H:i:s'); $param['uniacid'] = $_GPC['uniacid']; $param['openid_miniapp'] = $res['openid']; $param['id_number'] = Random::numeric(10); $param['coin'] = !empty($conf['register'])?$conf['register']:0; $param['nickname'] = '默认用户'; $saveRes = $this->model->save($param); $token = Random::uuid(); $res['token'] = $token; Token::set($token, $this->model->id, 0); // MessageSystemServiceFacade::Unified($this->model->id,'用户注册成功通知','恭喜你注册成功'); } return $this->success('获取结果', $res); } } // 授权获取小程序用户手机号直接绑定 public function authBindindPhone(){ $post = $this->request->param(); if(!$post['session_key'] || !$post['iv'] || !$post['encryptedData']){ return $this->error('注册失败,未上传session_key,iv或encryptedData'); } $app = MiniappServiceFacade::option(); $decryptedData = $app->encryptor->decryptData($post['session_key'], $post['iv'], $post['encryptedData']); $loginUserInfo = UserServiceFacade::getUserInfo(); if($decryptedData['phoneNumber']){ $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update(array('phone' => $decryptedData['phoneNumber'],'update_time'=>date('Y-m-d H:i:s'))); if (!$updateRes) throw new \Exception('保存基本信息失败'); $loginUserInfo['phone'] = $decryptedData['phoneNumber']; return $this->success('获取结果', $loginUserInfo); } else { return $this->error('获取失败'); } } // 手机号验证码登录授权绑定 public function authBindindPhoneCode(){ $post = $this->request->post(); if(!$post['phone'] || !$post['code']){ return $this->error('绑定失败,请上传手机号或验证码!'); } $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']); if(!$check){ return $this->error('验证失败,'.AliSmsServiceFacade::getError()); } $loginUserInfo = UserServiceFacade::getUserInfo(); $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update(array('phone' => $post['phone'])); if (!$updateRes) throw new \Exception('保存基本信息失败'); $loginUserInfo['phone'] = $post['phone']; return $this->success('获取结果', $loginUserInfo); } public function notify(){ $code = $this->request->param('code',''); $app = MpServiceFacade::option('snsapi_userinfo'); $user = $app->oauth->user(); // return $this->success('获取结果', $user); // header('Location:/addons/yizhi_dev/public/page'); // file_put_contents(__DIR__ . '/dss.json', json_encode($user)); } }