User.php 10 KB

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