User.php 10 KB

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