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() { $user = $this->auth->getUser(); $nickname = $this->request->post('nickname'); $nickname = $this->request->post('mobile'); $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); if ($username) { $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Username already exists')); } $user->username = $username; } if ($nickname) { $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Nickname already exists')); } $user->nickname = $nickname; } $user->bio = $bio; $user->avatar = $avatar; $user->save(); $this->success(); } //用户详细资料 public function getUserinfo($type = 1){ $info = $this->auth->getUserinfo(); if($type == 'return'){ return $info; } $this->success(__('success'),$info); } /** * 获取用户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值 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'], ]; // 默认注册一个会员 $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); } }