User.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /**
  10. * 会员接口
  11. */
  12. class User extends Api
  13. {
  14. protected $noNeedLogin = ['login', 'mobilelogin', 'wxmini_login', 'resetpwd', 'changemobile'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. if (!Config::get('fastadmin.usercenter')) {
  20. $this->error(__('User center already closed'));
  21. }
  22. }
  23. /**
  24. * 会员中心
  25. */
  26. public function index()
  27. {
  28. $this->success('', ['welcome' => $this->auth->nickname]);
  29. }
  30. /**
  31. * 会员登录
  32. *
  33. * @ApiMethod (POST)
  34. * @param string $account 账号
  35. * @param string $password 密码
  36. */
  37. public function login()
  38. {
  39. $account = $this->request->post('account');
  40. $password = $this->request->post('password');
  41. if (!$account || !$password) {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. $ret = $this->auth->login($account, $password);
  45. if ($ret) {
  46. $data = ['userinfo' => $this->auth->getUserinfo()];
  47. $this->success(__('Logged in successful'), $data);
  48. } else {
  49. $this->error($this->auth->getError());
  50. }
  51. }
  52. /**
  53. * 手机验证码登录
  54. *
  55. * @ApiMethod (POST)
  56. * @param string $mobile 手机号
  57. * @param string $captcha 验证码
  58. */
  59. public function mobilelogin()
  60. {
  61. $mobile = $this->request->post('mobile');
  62. $captcha = $this->request->post('captcha');
  63. if (!$mobile || !$captcha) {
  64. $this->error(__('Invalid parameters'));
  65. }
  66. if (!Validate::regex($mobile, "^1\d{10}$")) {
  67. $this->error(__('Mobile is incorrect'));
  68. }
  69. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  70. $this->error(__('Captcha is incorrect'));
  71. }
  72. $user = \app\common\model\User::getByMobile($mobile);
  73. if ($user) {
  74. if ($user->status != 1) {
  75. $this->error(__('Account is locked'));
  76. }
  77. //如果已经有账号则直接登录
  78. $ret = $this->auth->direct($user->id);
  79. } else {
  80. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  81. }
  82. if ($ret) {
  83. Sms::flush($mobile, 'mobilelogin');
  84. $data = ['userinfo' => $this->auth->getUserinfo()];
  85. $this->success(__('Logged in successful'), $data);
  86. } else {
  87. $this->error($this->auth->getError());
  88. }
  89. }
  90. /**
  91. * 退出登录
  92. * @ApiMethod (POST)
  93. */
  94. public function logout()
  95. {
  96. if (!$this->request->isPost()) {
  97. $this->error(__('Invalid parameters'));
  98. }
  99. $this->auth->logout();
  100. $this->success(__('Logout successful'));
  101. }
  102. /**
  103. * 修改会员个人信息
  104. *
  105. * @ApiMethod (POST)
  106. * @param string $avatar 头像地址
  107. * @param string $username 用户名
  108. * @param string $nickname 昵称
  109. * @param string $bio 个人简介
  110. */
  111. public function profile()
  112. {
  113. $user = $this->auth->getUser();
  114. $username = $this->request->post('username');
  115. $nickname = $this->request->post('nickname');
  116. $bio = $this->request->post('bio');
  117. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  118. if ($username) {
  119. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  120. if ($exists) {
  121. $this->error(__('Username already exists'));
  122. }
  123. $user->username = $username;
  124. }
  125. if ($nickname) {
  126. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  127. if ($exists) {
  128. $this->error(__('Nickname already exists'));
  129. }
  130. $user->nickname = $nickname;
  131. }
  132. $user->bio = $bio;
  133. $user->avatar = $avatar;
  134. $user->save();
  135. $this->success();
  136. }
  137. /**
  138. * 修改手机号
  139. *
  140. * @ApiMethod (POST)
  141. * @param string $mobile 手机号
  142. * @param string $captcha 验证码
  143. */
  144. public function changemobile()
  145. {
  146. $user = $this->auth->getUser();
  147. $mobile = $this->request->post('mobile');
  148. $captcha = $this->request->post('captcha');
  149. if (!$mobile || !$captcha) {
  150. $this->error(__('Invalid parameters'));
  151. }
  152. if (!Validate::regex($mobile, "^1\d{10}$")) {
  153. $this->error(__('Mobile is incorrect'));
  154. }
  155. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  156. $this->error(__('Mobile already exists'));
  157. }
  158. $result = Sms::check($mobile, $captcha, 'changemobile');
  159. if (!$result) {
  160. $this->error(__('Captcha is incorrect'));
  161. }
  162. $verification = $user->verification;
  163. $verification->mobile = 1;
  164. $user->verification = $verification;
  165. $user->mobile = $mobile;
  166. $user->save();
  167. Sms::flush($mobile, 'changemobile');
  168. $this->success();
  169. }
  170. /**
  171. * 重置密码
  172. *
  173. * @ApiMethod (POST)
  174. * @param string $mobile 手机号
  175. * @param string $newpassword 新密码
  176. * @param string $captcha 验证码
  177. */
  178. public function resetpwd()
  179. {
  180. $type = $this->request->post("type", "mobile");
  181. $mobile = $this->request->post("mobile");
  182. $email = $this->request->post("email");
  183. $newpassword = $this->request->post("newpassword");
  184. $captcha = $this->request->post("captcha");
  185. if (!$newpassword || !$captcha) {
  186. $this->error(__('Invalid parameters'));
  187. }
  188. //验证Token
  189. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  190. $this->error(__('Password must be 6 to 30 characters'));
  191. }
  192. if ($type == 'mobile') {
  193. if (!Validate::regex($mobile, "^1\d{10}$")) {
  194. $this->error(__('Mobile is incorrect'));
  195. }
  196. $user = \app\common\model\User::getByMobile($mobile);
  197. if (!$user) {
  198. $this->error(__('User not found'));
  199. }
  200. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  201. if (!$ret) {
  202. $this->error(__('Captcha is incorrect'));
  203. }
  204. Sms::flush($mobile, 'resetpwd');
  205. } else {
  206. if (!Validate::is($email, "email")) {
  207. $this->error(__('Email is incorrect'));
  208. }
  209. $user = \app\common\model\User::getByEmail($email);
  210. if (!$user) {
  211. $this->error(__('User not found'));
  212. }
  213. $ret = Ems::check($email, $captcha, 'resetpwd');
  214. if (!$ret) {
  215. $this->error(__('Captcha is incorrect'));
  216. }
  217. Ems::flush($email, 'resetpwd');
  218. }
  219. //模拟一次登录
  220. $this->auth->direct($user->id);
  221. $ret = $this->auth->changepwd($newpassword, '', true);
  222. if ($ret) {
  223. $this->success(__('Reset password successful'));
  224. } else {
  225. $this->error($this->auth->getError());
  226. }
  227. }
  228. ///////////////////////////////////////////
  229. /**
  230. * 微信小程序登录+注册
  231. */
  232. public function wxmini_login() {
  233. $code = input('code');
  234. if (!$code) {
  235. $this->error(__('Invalid parameters'));
  236. }
  237. $config = config('wxMiniProgram');
  238. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  239. $openidInfo = $this->getJson($getopenid);
  240. if(!isset($openidInfo['openid'])) {
  241. $this->error('用户openid获取失败',$openidInfo);
  242. }
  243. $openid = $openidInfo['openid'];
  244. if (!$openid) {
  245. $this->error('用户openid获取失败');
  246. }
  247. //用户信息
  248. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  249. if($userInfo) {
  250. if ($userInfo['status'] != 1) {
  251. $this->error(__('Account is locked'));
  252. }
  253. //如果已经有账号则直接登录
  254. $res = $this->auth->direct($userInfo['id']);
  255. } else {
  256. // 注册
  257. /*$extend = [
  258. 'mini_openid' => $openid,
  259. ];*/
  260. $res = $this->auth->openid_register($openid);
  261. if (!$res) {
  262. $this->error($this->auth->getError());
  263. }
  264. }
  265. if($res) {
  266. $this->success("登录成功!",$this->auth->getUserinfo());
  267. } else {
  268. $this->error("登录失败!");
  269. }
  270. }
  271. /**
  272. * json 请求
  273. * @param $url
  274. * @return mixed
  275. */
  276. private function getJson($url){
  277. $ch = curl_init();
  278. curl_setopt($ch, CURLOPT_URL, $url);
  279. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  280. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  281. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  282. $output = curl_exec($ch);
  283. curl_close($ch);
  284. return json_decode($output, true);
  285. }
  286. //用户详细资料
  287. public function getUserinfo(){
  288. $info = $this->auth->getUserinfo();
  289. $this->success(__('success'),$info);
  290. }
  291. }