User.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['login', 'mobilelogin', 'wxmini_openid_login', 'resetpwd', 'changemobile'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. if (!Config::get('fastadmin.usercenter')) {
  21. $this->error(__('User center already closed'));
  22. }
  23. }
  24. /**
  25. * 手机验证码登录
  26. *
  27. * @ApiMethod (POST)
  28. * @param string $mobile 手机号
  29. * @param string $captcha 验证码
  30. */
  31. public function mobilelogin()
  32. {
  33. $mobile = $this->request->post('mobile');
  34. $captcha = $this->request->post('captcha');
  35. if (!$mobile || !$captcha) {
  36. $this->error(__('Invalid parameters'));
  37. }
  38. if (!Validate::regex($mobile, "^1\d{10}$")) {
  39. $this->error(__('Mobile is incorrect'));
  40. }
  41. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  42. $this->error(__('Captcha is incorrect'));
  43. }
  44. $user = \app\common\model\User::getByMobile($mobile);
  45. if ($user) {
  46. if ($user->status != 1) {
  47. $this->error(__('Account is locked'));
  48. }
  49. //如果已经有账号则直接登录
  50. $ret = $this->auth->direct($user->id);
  51. } else {
  52. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  53. }
  54. if ($ret) {
  55. Sms::flush($mobile, 'mobilelogin');
  56. $data = ['userinfo' => $this->auth->getUserinfo()];
  57. $this->success(__('Logged in successful'), $data);
  58. } else {
  59. $this->error($this->auth->getError());
  60. }
  61. }
  62. /**
  63. * 修改手机号
  64. *
  65. * @ApiMethod (POST)
  66. * @param string $mobile 手机号
  67. * @param string $captcha 验证码
  68. */
  69. public function changemobile()
  70. {
  71. $user = $this->auth->getUser();
  72. $mobile = $this->request->post('mobile');
  73. $captcha = $this->request->post('captcha');
  74. if (!$mobile || !$captcha) {
  75. $this->error(__('Invalid parameters'));
  76. }
  77. if (!Validate::regex($mobile, "^1\d{10}$")) {
  78. $this->error(__('Mobile is incorrect'));
  79. }
  80. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  81. $this->error(__('Mobile already exists'));
  82. }
  83. $result = Sms::check($mobile, $captcha, 'changemobile');
  84. if (!$result) {
  85. $this->error(__('Captcha is incorrect'));
  86. }
  87. $user->mobile = $mobile;
  88. $user->save();
  89. Sms::flush($mobile, 'changemobile');
  90. $this->success();
  91. }
  92. /**
  93. * 微信小程序登录+注册
  94. * code得到注册手机号,此手机号登录+注册
  95. */
  96. public function wxmini_regmobile_login(){
  97. $code = input('code');
  98. if (!$code) {
  99. $this->error(__('Invalid parameters'));
  100. }
  101. $config = config('wxMiniProgram');
  102. $wechat = new Wechat($config['appid'],$config['secret']);
  103. $getuserphonenumber = $wechat->getuserphonenumber($code);
  104. if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
  105. $this->error('授权获取手机号失败');
  106. }
  107. $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
  108. $userInfo = Db::name('user')->where('mobile',$mobile)->find();
  109. // 判断用户是否已经存在
  110. if($userInfo) { // 登录
  111. if ($userInfo['status'] != 1) {
  112. $this->error(__('Account is locked'));
  113. }
  114. //如果已经有账号则直接登录
  115. $res = $this->auth->direct($userInfo['id']);
  116. } else {
  117. $res = $this->auth->register('', '', '',$mobile, []);
  118. }
  119. if($res) {
  120. $this->success("登录成功!",$this->auth->getUserinfo());
  121. } else {
  122. $this->error($this->auth->getError());
  123. }
  124. }
  125. //////////////////////上面的没用到/////////////////////
  126. /**
  127. * 退出登录
  128. * @ApiMethod (POST)
  129. */
  130. public function logout()
  131. {
  132. if (!$this->request->isPost()) {
  133. $this->error(__('Invalid parameters'));
  134. }
  135. $this->auth->logout();
  136. $this->success(__('Logout successful'));
  137. }
  138. /**
  139. * 修改会员个人信息
  140. *
  141. * @ApiMethod (POST)
  142. * @param string $avatar 头像地址
  143. * @param string $username 用户名
  144. * @param string $nickname 昵称
  145. * @param string $bio 个人简介
  146. */
  147. public function profile()
  148. {
  149. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  150. $mobile = input('mobile', '');
  151. $nickname = input('nickname', '');
  152. //修改用户
  153. $data = [];
  154. if(!empty($avatar))
  155. {
  156. $data['avatar'] = $avatar;
  157. }
  158. if(!empty($mobile))
  159. {
  160. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find()) {
  161. $this->error('手机号已被占用');
  162. }
  163. $data['mobile'] = $mobile;
  164. }
  165. //未通过实名认证的才能改昵称
  166. if(!empty($nickname) && $this->auth->idcard_status != 1)
  167. {
  168. $data['nickname'] = $nickname;
  169. $data['nickname_time'] = time();
  170. }
  171. if(!empty($data)){
  172. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  173. if($update_rs === false){
  174. $this->error('修改资料失败');
  175. }
  176. }
  177. $this->success();
  178. }
  179. /**
  180. * 微信小程序登录+注册
  181. * code得到openid
  182. */
  183. public function wxmini_openid_login() {
  184. $code = input('code');
  185. if (!$code) {
  186. $this->error(__('Invalid parameters'));
  187. }
  188. $config = config('wxMiniProgram');
  189. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  190. $openidInfo = $this->getJson($getopenid);
  191. if(!isset($openidInfo['openid'])) {
  192. $this->error('用户openid获取失败',$openidInfo);
  193. }
  194. $openid = $openidInfo['openid'];
  195. if (!$openid) {
  196. $this->error('用户openid获取失败');
  197. }
  198. //用户信息
  199. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  200. if($userInfo) {
  201. if ($userInfo['status'] == 0) {
  202. $this->error('账号已被禁用');
  203. }
  204. if ($userInfo['status'] == -1) {
  205. $this->error('账号已被注销');
  206. }
  207. //如果已经有账号则直接登录
  208. $res = $this->auth->direct($userInfo['id']);
  209. } else {
  210. $res = $this->auth->openid_register($openid);
  211. }
  212. if($res) {
  213. $this->success("登录成功!",$this->auth->getUserinfo());
  214. } else {
  215. $this->error($this->auth->getError());
  216. }
  217. }
  218. /**
  219. * json 请求
  220. * @param $url
  221. * @return mixed
  222. */
  223. private function getJson($url){
  224. $ch = curl_init();
  225. curl_setopt($ch, CURLOPT_URL, $url);
  226. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  227. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  228. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  229. $output = curl_exec($ch);
  230. curl_close($ch);
  231. return json_decode($output, true);
  232. }
  233. //用户详细资料
  234. public function getuserinfo(){
  235. $info = $this->auth->getUserinfo();
  236. $this->success(__('success'),$info);
  237. }
  238. }