User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_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. $verification = $user->verification;
  88. $verification->mobile = 1;
  89. $user->verification = $verification;
  90. $user->mobile = $mobile;
  91. $user->save();
  92. Sms::flush($mobile, 'changemobile');
  93. $this->success();
  94. }
  95. /**
  96. * 微信小程序登录+注册
  97. * code得到注册手机号,此手机号登录+注册
  98. */
  99. public function wxmini_regmobile_login(){
  100. $code = input('code');
  101. if (!$code) {
  102. $this->error(__('Invalid parameters'));
  103. }
  104. $config = config('wxMiniProgram');
  105. $wechat = new Wechat($config['appid'],$config['secret']);
  106. $getuserphonenumber = $wechat->getuserphonenumber($code);
  107. if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
  108. $this->error('授权获取手机号失败');
  109. }
  110. $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
  111. $userInfo = Db::name('user')->where('mobile',$mobile)->find();
  112. // 判断用户是否已经存在
  113. if($userInfo) { // 登录
  114. if ($userInfo['status'] != 1) {
  115. $this->error(__('Account is locked'));
  116. }
  117. //如果已经有账号则直接登录
  118. $res = $this->auth->direct($userInfo['id']);
  119. } else {
  120. $res = $this->auth->register('', '', '',$mobile, []);
  121. }
  122. if($res) {
  123. $this->success("登录成功!",$this->auth->getUserinfo());
  124. } else {
  125. $this->error($this->auth->getError());
  126. }
  127. }
  128. //////////////////////上面的没用到/////////////////////
  129. /**
  130. * 退出登录
  131. * @ApiMethod (POST)
  132. */
  133. public function logout()
  134. {
  135. if (!$this->request->isPost()) {
  136. $this->error(__('Invalid parameters'));
  137. }
  138. $this->auth->logout();
  139. $this->success(__('Logout successful'));
  140. }
  141. /**
  142. * 修改会员个人信息
  143. *
  144. * @ApiMethod (POST)
  145. * @param string $avatar 头像地址
  146. * @param string $username 用户名
  147. * @param string $nickname 昵称
  148. * @param string $bio 个人简介
  149. */
  150. public function profile()
  151. {
  152. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  153. $mobile = input('mobile', '');
  154. if(empty($avatar) && empty($mobile)){
  155. $this->success();
  156. }
  157. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find()) {
  158. $this->error(__('Mobile already exists'));
  159. }
  160. //修改用户
  161. $data = [
  162. 'avatar' => $avatar,
  163. 'mobile' => $mobile,
  164. ];
  165. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  166. if($update_rs === false){
  167. $this->error('修改资料失败');
  168. }
  169. $this->success();
  170. }
  171. /**
  172. * 微信小程序登录+注册
  173. * code得到openid
  174. */
  175. public function wxmini_openid_login() {
  176. $code = input('code');
  177. if (!$code) {
  178. $this->error(__('Invalid parameters'));
  179. }
  180. $config = config('wxMiniProgram');
  181. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  182. $openidInfo = $this->getJson($getopenid);
  183. if(!isset($openidInfo['openid'])) {
  184. $this->error('用户openid获取失败',$openidInfo);
  185. }
  186. $openid = $openidInfo['openid'];
  187. if (!$openid) {
  188. $this->error('用户openid获取失败');
  189. }
  190. //用户信息
  191. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  192. if($userInfo) {
  193. if ($userInfo['status'] != 1) {
  194. $this->error(__('Account is locked'));
  195. }
  196. //如果已经有账号则直接登录
  197. $res = $this->auth->direct($userInfo['id']);
  198. } else {
  199. $res = $this->auth->openid_register($openid);
  200. }
  201. if($res) {
  202. $this->success("登录成功!",$this->auth->getUserinfo());
  203. } else {
  204. $this->error($this->auth->getError());
  205. }
  206. }
  207. /**
  208. * json 请求
  209. * @param $url
  210. * @return mixed
  211. */
  212. private function getJson($url){
  213. $ch = curl_init();
  214. curl_setopt($ch, CURLOPT_URL, $url);
  215. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  216. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  217. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  218. $output = curl_exec($ch);
  219. curl_close($ch);
  220. return json_decode($output, true);
  221. }
  222. //用户详细资料
  223. public function getuserinfo(){
  224. $info = $this->auth->getUserinfo();
  225. $this->success(__('success'),$info);
  226. }
  227. }