request->isPost()) { $this->error(__('Invalid parameters')); } $this->auth->logout(); $this->success(__('Logout successful')); } /** * 修改会员个人信息 * * @ApiMethod (POST) * @param string $avatar 头像地址 * @param string $username 用户名 * @param string $nickname 昵称 * @param string $bio 个人简介 */ public function profile() { $nickname = $this->request->post('nickname',''); $mobile = $this->request->post('mobile',''); $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); $data = [ 'nickname' => $nickname, 'avatar' => $avatar, ]; if ($mobile) { $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error('手机号已经被他人注册'); } $data['mobile'] = $mobile; } Db::name('user')->where('id',$this->auth->id)->update($data); $this->success(); } //用户详细资料 public function getUserinfo($type = 1){ $info = $this->auth->getUserinfo(); if($type == 'return'){ return $info; } $this->success(__('success'),$info); } //获取手机号 public function getPhoneNumber() { // code值 $code = $this->request->param('code'); if (!$code) { $this->error(__('Invalid parameters')); } //手机号 $wechat = new Wechat(); $phoneInfo = $wechat->getPhoneNumber($code); if(isset($phoneInfo['errcode']) && $phoneInfo['errcode'] != 0) { $this->error('获取手机号失败', $phoneInfo['errmsg']); } $mobile = isset($phoneInfo['phone_info']['purePhoneNumber']) ? $phoneInfo['phone_info']['purePhoneNumber'] : ''; $this->success(1,$mobile); } /** * 获取用户openid */ public function getUserOpenid() { // code值 $code = $this->request->param('code'); if (!$code) { $this->error(__('Invalid parameters')); } $config = config('wxMiniProgram'); $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code'; $openidInfo = $this->getJson($getopenid); if(!isset($openidInfo['openid'])) { $this->error('用户openid获取失败',$openidInfo); } // 获取的结果存入数据库 $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find(); if($find) { $update = []; $update['sessionkey'] = $openidInfo['session_key']; $update['createtime'] = time(); $res = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update); } else { $insert = []; $insert['sessionkey'] = $openidInfo['session_key']; $insert['openid'] = $openidInfo['openid']; $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : ''; $insert['createtime'] = time(); $res = Db::name('user_sessionkey')->insertGetId($insert); } if($res !== false) { $this->success('获取成功',$openidInfo); } else { $this->error('获取失败'); } } /** * 微信小程序登录 */ public function wxMiniProgramLogin() { $openid = $this->request->request('openid');// openid值 $introcode = $this->request->request('introcode',''); if (!$openid) { $this->error(__('Invalid parameters')); } // 获取openid和sessionkey $openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find(); $session_key = $openidInfo['sessionkey']; // 微信授权openid登录 $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find(); // 判断用户是否已经存在 if($userInfo) { // 登录 Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]); $res = $this->auth->direct($userInfo['id']); } else { // 用户信息不存在时使用 $extend = [ 'mini_openid' => $openid, 'mini_sessionkey'=> $session_key, 'unionid' => $openidInfo['unionid'], ]; // 注册 if ($introcode) { $intro_uid = \app\common\model\User::where('introcode', $introcode)->value('id'); if (!$intro_uid) { $this->error('不存在的邀请码'); } $extend['intro_uid'] = $intro_uid; } // 默认注册一个会员 $result = $this->auth->register('', '', '','', $extend); if (!$result) { $this->error("注册失败!"); } $res = $this->auth->direct($this->auth->id); } $userInfo = $this->getUserinfo('return'); if($res) { $this->success("登录成功!",$userInfo); } else { $this->error("登录失败!"); } } /** * json 请求 * @param $url * @return mixed */ private function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } }