123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use app\common\library\Wechat;
- /**
- * 会员接口
- */
- class User extends Api
- {
- protected $noNeedLogin = ['getUserOpenid','wxMiniProgramLogin'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 退出登录
- * @ApiMethod (POST)
- */
- public function logout()
- {
- if (!$this->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','');
- $introcode = $this->request->post('introcode','');
- $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
- if ($mobile) {
- $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find();
- if ($exists) {
- $this->error('手机号已经被他人注册');
- }
- }
- if ($introcode) {
- $exists = \app\common\model\User::where('introcode', $introcode)->find();
- if (!$exists) {
- $this->error('不存在的邀请码');
- }
- }
- $data = [
- 'nickname' => $nickname,
- 'mobile' => $mobile,
- 'introcode' => $introcode,
- 'avatar' => $avatar,
- ];
- Db::name('user')->where('id',$this->auth->id)->update($data);
- $this->success(1);
- }
- //用户详细资料
- 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);
- }
- //手机号
- $wechat = new Wechat();
- $mobile = $wechat->getPhoneNumber($code);
- dump($mobile);
- // 获取的结果存入数据库
- $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);
- }
- }
|