User.php 5.7 KB

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