User.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Validate;
  8. /**
  9. * 会员接口
  10. */
  11. class User extends Api
  12. {
  13. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  14. protected $noNeedRight = '*';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 会员中心
  21. */
  22. public function index()
  23. {
  24. $this->success('', ['welcome' => $this->auth->nickname]);
  25. }
  26. /**
  27. * 会员登录
  28. *
  29. * @param string $account 账号
  30. * @param string $password 密码
  31. */
  32. public function login()
  33. {
  34. $account = $this->request->request('account');
  35. $password = $this->request->request('password');
  36. if (!$account || !$password) {
  37. $this->error(__('Invalid parameters'));
  38. }
  39. $ret = $this->auth->login($account, $password);
  40. if ($ret) {
  41. $data = ['userinfo' => $this->auth->getUserinfo()];
  42. $this->success(__('Logged in successful'), $data);
  43. } else {
  44. $this->error($this->auth->getError());
  45. }
  46. }
  47. /**
  48. * 手机验证码登录
  49. *
  50. * @param string $mobile 手机号
  51. * @param string $captcha 验证码
  52. */
  53. public function mobilelogin()
  54. {
  55. $mobile = $this->request->request('mobile');
  56. $captcha = $this->request->request('captcha');
  57. if (!$mobile || !$captcha) {
  58. $this->error(__('Invalid parameters'));
  59. }
  60. if (!Validate::regex($mobile, "^1\d{10}$")) {
  61. $this->error(__('Mobile is incorrect'));
  62. }
  63. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  64. $this->error(__('Captcha is incorrect'));
  65. }
  66. $user = \app\common\model\User::getByMobile($mobile);
  67. if ($user) {
  68. if ($user->status != 'normal') {
  69. $this->error(__('Account is locked'));
  70. }
  71. //如果已经有账号则直接登录
  72. $ret = $this->auth->direct($user->id);
  73. } else {
  74. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  75. }
  76. if ($ret) {
  77. Sms::flush($mobile, 'mobilelogin');
  78. $data = ['userinfo' => $this->auth->getUserinfo()];
  79. $this->success(__('Logged in successful'), $data);
  80. } else {
  81. $this->error($this->auth->getError());
  82. }
  83. }
  84. /**
  85. * 注册会员
  86. *
  87. * @param string $username 用户名
  88. * @param string $password 密码
  89. * @param string $email 邮箱
  90. * @param string $mobile 手机号
  91. */
  92. public function register()
  93. {
  94. $username = $this->request->request('username');
  95. $password = $this->request->request('password');
  96. $email = $this->request->request('email');
  97. $mobile = $this->request->request('mobile');
  98. if (!$username || !$password) {
  99. $this->error(__('Invalid parameters'));
  100. }
  101. if ($email && !Validate::is($email, "email")) {
  102. $this->error(__('Email is incorrect'));
  103. }
  104. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  105. $this->error(__('Mobile is incorrect'));
  106. }
  107. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  108. if ($ret) {
  109. $data = ['userinfo' => $this->auth->getUserinfo()];
  110. $this->success(__('Sign up successful'), $data);
  111. } else {
  112. $this->error($this->auth->getError());
  113. }
  114. }
  115. /**
  116. * 注销登录
  117. */
  118. public function logout()
  119. {
  120. $this->auth->logout();
  121. $this->success(__('Logout successful'));
  122. }
  123. /**
  124. * 修改会员个人信息
  125. *
  126. * @param string $avatar 头像地址
  127. * @param string $username 用户名
  128. * @param string $nickname 昵称
  129. * @param string $bio 个人简介
  130. */
  131. public function profile()
  132. {
  133. $user = $this->auth->getUser();
  134. $username = $this->request->request('username');
  135. $nickname = $this->request->request('nickname');
  136. $bio = $this->request->request('bio');
  137. $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
  138. if ($username) {
  139. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  140. if ($exists) {
  141. $this->error(__('Username already exists'));
  142. }
  143. $user->username = $username;
  144. }
  145. $user->nickname = $nickname;
  146. $user->bio = $bio;
  147. $user->avatar = $avatar;
  148. $user->save();
  149. $this->success();
  150. }
  151. /**
  152. * 修改邮箱
  153. *
  154. * @param string $email 邮箱
  155. * @param string $captcha 验证码
  156. */
  157. public function changeemail()
  158. {
  159. $user = $this->auth->getUser();
  160. $email = $this->request->post('email');
  161. $captcha = $this->request->request('captcha');
  162. if (!$email || !$captcha) {
  163. $this->error(__('Invalid parameters'));
  164. }
  165. if (!Validate::is($email, "email")) {
  166. $this->error(__('Email is incorrect'));
  167. }
  168. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  169. $this->error(__('Email already exists'));
  170. }
  171. $result = Ems::check($email, $captcha, 'changeemail');
  172. if (!$result) {
  173. $this->error(__('Captcha is incorrect'));
  174. }
  175. $verification = $user->verification;
  176. $verification->email = 1;
  177. $user->verification = $verification;
  178. $user->email = $email;
  179. $user->save();
  180. Ems::flush($email, 'changeemail');
  181. $this->success();
  182. }
  183. /**
  184. * 修改手机号
  185. *
  186. * @param string $email 手机号
  187. * @param string $captcha 验证码
  188. */
  189. public function changemobile()
  190. {
  191. $user = $this->auth->getUser();
  192. $mobile = $this->request->request('mobile');
  193. $captcha = $this->request->request('captcha');
  194. if (!$mobile || !$captcha) {
  195. $this->error(__('Invalid parameters'));
  196. }
  197. if (!Validate::regex($mobile, "^1\d{10}$")) {
  198. $this->error(__('Mobile is incorrect'));
  199. }
  200. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  201. $this->error(__('Mobile already exists'));
  202. }
  203. $result = Sms::check($mobile, $captcha, 'changemobile');
  204. if (!$result) {
  205. $this->error(__('Captcha is incorrect'));
  206. }
  207. $verification = $user->verification;
  208. $verification->mobile = 1;
  209. $user->verification = $verification;
  210. $user->mobile = $mobile;
  211. $user->save();
  212. Sms::flush($mobile, 'changemobile');
  213. $this->success();
  214. }
  215. /**
  216. * 第三方登录
  217. *
  218. * @param string $platform 平台名称
  219. * @param string $code Code码
  220. */
  221. public function third()
  222. {
  223. $url = url('user/index');
  224. $platform = $this->request->request("platform");
  225. $code = $this->request->request("code");
  226. $config = get_addon_config('third');
  227. if (!$config || !isset($config[$platform])) {
  228. $this->error(__('Invalid parameters'));
  229. }
  230. $app = new \addons\third\library\Application($config);
  231. //通过code换access_token和绑定会员
  232. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  233. if ($result) {
  234. $loginret = \addons\third\library\Service::connect($platform, $result);
  235. if ($loginret) {
  236. $data = [
  237. 'userinfo' => $this->auth->getUserinfo(),
  238. 'thirdinfo' => $result
  239. ];
  240. $this->success(__('Logged in successful'), $data);
  241. }
  242. }
  243. $this->error(__('Operation failed'), $url);
  244. }
  245. /**
  246. * 重置密码
  247. *
  248. * @param string $mobile 手机号
  249. * @param string $newpassword 新密码
  250. * @param string $captcha 验证码
  251. */
  252. public function resetpwd()
  253. {
  254. $type = $this->request->request("type");
  255. $mobile = $this->request->request("mobile");
  256. $email = $this->request->request("email");
  257. $newpassword = $this->request->request("newpassword");
  258. $captcha = $this->request->request("captcha");
  259. if (!$newpassword || !$captcha) {
  260. $this->error(__('Invalid parameters'));
  261. }
  262. if ($type == 'mobile') {
  263. if (!Validate::regex($mobile, "^1\d{10}$")) {
  264. $this->error(__('Mobile is incorrect'));
  265. }
  266. $user = \app\common\model\User::getByMobile($mobile);
  267. if (!$user) {
  268. $this->error(__('User not found'));
  269. }
  270. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  271. if (!$ret) {
  272. $this->error(__('Captcha is incorrect'));
  273. }
  274. Sms::flush($mobile, 'resetpwd');
  275. } else {
  276. if (!Validate::is($email, "email")) {
  277. $this->error(__('Email is incorrect'));
  278. }
  279. $user = \app\common\model\User::getByEmail($email);
  280. if (!$user) {
  281. $this->error(__('User not found'));
  282. }
  283. $ret = Ems::check($email, $captcha, 'resetpwd');
  284. if (!$ret) {
  285. $this->error(__('Captcha is incorrect'));
  286. }
  287. Ems::flush($email, 'resetpwd');
  288. }
  289. //模拟一次登录
  290. $this->auth->direct($user->id);
  291. $ret = $this->auth->changepwd($newpassword, '', true);
  292. if ($ret) {
  293. $this->success(__('Reset password successful'));
  294. } else {
  295. $this->error($this->auth->getError());
  296. }
  297. }
  298. }