User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  43. $data = [
  44. 'nickname' => $nickname,
  45. 'avatar' => $avatar,
  46. ];
  47. if ($mobile) {
  48. $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find();
  49. if ($exists) {
  50. $this->error('手机号已经被他人注册');
  51. }
  52. $data['mobile'] = $mobile;
  53. }
  54. Db::name('user')->where('id',$this->auth->id)->update($data);
  55. $this->success();
  56. }
  57. //用户详细资料
  58. public function getUserinfo($type = 1){
  59. $info = $this->auth->getUserinfo();
  60. if($type == 'return'){
  61. return $info;
  62. }
  63. $this->success(__('success'),$info);
  64. }
  65. //获取手机号
  66. public function getPhoneNumber() {
  67. // code值
  68. $code = $this->request->param('code');
  69. if (!$code) {
  70. $this->error(__('Invalid parameters'));
  71. }
  72. //手机号
  73. $wechat = new Wechat();
  74. $phoneInfo = $wechat->getPhoneNumber($code);
  75. if(isset($phoneInfo['errcode']) && $phoneInfo['errcode'] != 0) {
  76. $this->error('获取手机号失败', $phoneInfo['errmsg']);
  77. }
  78. $mobile = isset($phoneInfo['phone_info']['purePhoneNumber']) ? $phoneInfo['phone_info']['purePhoneNumber'] : '';
  79. $this->success(1,$mobile);
  80. }
  81. /**
  82. * 获取用户openid
  83. */
  84. public function getUserOpenid() {
  85. // code值
  86. $code = $this->request->param('code');
  87. if (!$code) {
  88. $this->error(__('Invalid parameters'));
  89. }
  90. $config = config('wxMiniProgram');
  91. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  92. $openidInfo = $this->getJson($getopenid);
  93. if(!isset($openidInfo['openid'])) {
  94. $this->error('用户openid获取失败',$openidInfo);
  95. }
  96. // 获取的结果存入数据库
  97. $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
  98. if($find) {
  99. $update = [];
  100. $update['sessionkey'] = $openidInfo['session_key'];
  101. $update['createtime'] = time();
  102. $res = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
  103. } else {
  104. $insert = [];
  105. $insert['sessionkey'] = $openidInfo['session_key'];
  106. $insert['openid'] = $openidInfo['openid'];
  107. $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : '';
  108. $insert['createtime'] = time();
  109. $res = Db::name('user_sessionkey')->insertGetId($insert);
  110. }
  111. if($res !== false) {
  112. $this->success('获取成功',$openidInfo);
  113. } else {
  114. $this->error('获取失败');
  115. }
  116. }
  117. /**
  118. * 微信小程序登录
  119. */
  120. public function wxMiniProgramLogin() {
  121. $openid = $this->request->request('openid');// openid值
  122. $introcode = $this->request->request('introcode','');
  123. if (!$openid) {
  124. $this->error(__('Invalid parameters'));
  125. }
  126. // 获取openid和sessionkey
  127. $openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
  128. $session_key = $openidInfo['sessionkey'];
  129. // 微信授权openid登录
  130. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  131. // 判断用户是否已经存在
  132. if($userInfo) { // 登录
  133. Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]);
  134. $res = $this->auth->direct($userInfo['id']);
  135. } else {
  136. // 用户信息不存在时使用
  137. $extend = [
  138. 'mini_openid' => $openid,
  139. 'mini_sessionkey'=> $session_key,
  140. 'unionid' => $openidInfo['unionid'],
  141. ];
  142. // 注册
  143. if ($introcode) {
  144. $intro_uid = \app\common\model\User::where('introcode', $introcode)->value('id');
  145. if (!$intro_uid) {
  146. $this->error('不存在的邀请码');
  147. }
  148. $extend['intro_uid'] = $intro_uid;
  149. }
  150. // 默认注册一个会员
  151. $result = $this->auth->register('', '', '','', $extend);
  152. if (!$result) {
  153. $this->error("注册失败!");
  154. }
  155. $res = $this->auth->direct($this->auth->id);
  156. }
  157. $userInfo = $this->getUserinfo('return');
  158. if($res) {
  159. $this->success("登录成功!",$userInfo);
  160. } else {
  161. $this->error("登录失败!");
  162. }
  163. }
  164. /**
  165. * json 请求
  166. * @param $url
  167. * @return mixed
  168. */
  169. private function getJson($url){
  170. $ch = curl_init();
  171. curl_setopt($ch, CURLOPT_URL, $url);
  172. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  173. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  174. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  175. $output = curl_exec($ch);
  176. curl_close($ch);
  177. return json_decode($output, true);
  178. }
  179. }