User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Wechat;
  6. /**
  7. * 会员接口
  8. */
  9. class User extends Api
  10. {
  11. protected $noNeedLogin = ['getUserOpenid','wxMiniProgramLogin'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. /**
  18. * 退出登录
  19. * @ApiMethod (POST)
  20. */
  21. public function logout()
  22. {
  23. if (!$this->request->isPost()) {
  24. $this->error(__('Invalid parameters'));
  25. }
  26. $this->auth->logout();
  27. $this->success(__('Logout successful'));
  28. }
  29. /**
  30. * 修改会员个人信息
  31. *
  32. * @ApiMethod (POST)
  33. * @param string $avatar 头像地址
  34. * @param string $username 用户名
  35. * @param string $nickname 昵称
  36. * @param string $bio 个人简介
  37. */
  38. public function profile()
  39. {
  40. $nickname = $this->request->post('nickname','');
  41. $mobile = $this->request->post('mobile','');
  42. $introcode = $this->request->post('introcode','');
  43. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  44. if ($mobile) {
  45. $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find();
  46. if ($exists) {
  47. $this->error('手机号已经被他人注册');
  48. }
  49. }
  50. if ($introcode) {
  51. $exists = \app\common\model\User::where('introcode', $introcode)->find();
  52. if (!$exists) {
  53. $this->error('不存在的邀请码');
  54. }
  55. }
  56. $data = [
  57. 'nickname' => $nickname,
  58. 'mobile' => $mobile,
  59. 'introcode' => $introcode,
  60. 'avatar' => $avatar,
  61. ];
  62. Db::name('user')->where('id',$this->auth->id)->update($data);
  63. $this->success(1);
  64. }
  65. //用户详细资料
  66. public function getUserinfo($type = 1){
  67. $info = $this->auth->getUserinfo();
  68. if($type == 'return'){
  69. return $info;
  70. }
  71. $this->success(__('success'),$info);
  72. }
  73. /**
  74. * 获取用户openid
  75. */
  76. public function getUserOpenid() {
  77. // code值
  78. $code = $this->request->param('code');
  79. if (!$code) {
  80. $this->error(__('Invalid parameters'));
  81. }
  82. $config = config('wxMiniProgram');
  83. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  84. $openidInfo = $this->getJson($getopenid);
  85. if(!isset($openidInfo['openid'])) {
  86. $this->error('用户openid获取失败',$openidInfo);
  87. }
  88. //手机号
  89. $wechat = new Wechat();
  90. $mobile = $wechat->getPhoneNumber($code);
  91. dump($mobile);
  92. // 获取的结果存入数据库
  93. $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
  94. if($find) {
  95. $update = [];
  96. $update['sessionkey'] = $openidInfo['session_key'];
  97. $update['createtime'] = time();
  98. $res = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
  99. } else {
  100. $insert = [];
  101. $insert['sessionkey'] = $openidInfo['session_key'];
  102. $insert['openid'] = $openidInfo['openid'];
  103. $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : '';
  104. $insert['createtime'] = time();
  105. $res = Db::name('user_sessionkey')->insertGetId($insert);
  106. }
  107. if($res !== false) {
  108. $this->success('获取成功',$openidInfo);
  109. } else {
  110. $this->error('获取失败');
  111. }
  112. }
  113. /**
  114. * 微信小程序登录
  115. */
  116. public function wxMiniProgramLogin() {
  117. $openid = $this->request->request('openid');// openid值
  118. if (!$openid) {
  119. $this->error(__('Invalid parameters'));
  120. }
  121. // 获取openid和sessionkey
  122. $openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
  123. $session_key = $openidInfo['sessionkey'];
  124. // 微信授权openid登录
  125. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  126. // 判断用户是否已经存在
  127. if($userInfo) { // 登录
  128. Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]);
  129. $res = $this->auth->direct($userInfo['id']);
  130. } else {
  131. // 注册
  132. // 用户信息不存在时使用
  133. $extend = [
  134. 'mini_openid' => $openid,
  135. 'mini_sessionkey'=> $session_key,
  136. 'unionid' => $openidInfo['unionid'],
  137. ];
  138. // 默认注册一个会员
  139. $result = $this->auth->register('', '', '','', $extend);
  140. if (!$result) {
  141. $this->error("注册失败!");
  142. }
  143. $res = $this->auth->direct($this->auth->id);
  144. }
  145. $userInfo = $this->getUserinfo('return');
  146. if($res) {
  147. $this->success("登录成功!",$userInfo);
  148. } else {
  149. $this->error("登录失败!");
  150. }
  151. }
  152. /**
  153. * json 请求
  154. * @param $url
  155. * @return mixed
  156. */
  157. private function getJson($url){
  158. $ch = curl_init();
  159. curl_setopt($ch, CURLOPT_URL, $url);
  160. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  161. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. $output = curl_exec($ch);
  164. curl_close($ch);
  165. return json_decode($output, true);
  166. }
  167. }