You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

321 lines
10 KiB

  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. $user->nickname = $nickname;
  152. $user->bio = $bio;
  153. $user->avatar = $avatar;
  154. $user->save();
  155. $this->success();
  156. }
  157. /**
  158. * 修改邮箱
  159. *
  160. * @param string $email 邮箱
  161. * @param string $captcha 验证码
  162. */
  163. public function changeemail()
  164. {
  165. $user = $this->auth->getUser();
  166. $email = $this->request->post('email');
  167. $captcha = $this->request->request('captcha');
  168. if (!$email || !$captcha) {
  169. $this->error(__('Invalid parameters'));
  170. }
  171. if (!Validate::is($email, "email")) {
  172. $this->error(__('Email is incorrect'));
  173. }
  174. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  175. $this->error(__('Email already exists'));
  176. }
  177. $result = Ems::check($email, $captcha, 'changeemail');
  178. if (!$result) {
  179. $this->error(__('Captcha is incorrect'));
  180. }
  181. $verification = $user->verification;
  182. $verification->email = 1;
  183. $user->verification = $verification;
  184. $user->email = $email;
  185. $user->save();
  186. Ems::flush($email, 'changeemail');
  187. $this->success();
  188. }
  189. /**
  190. * 修改手机号
  191. *
  192. * @param string $mobile 手机号
  193. * @param string $captcha 验证码
  194. */
  195. public function changemobile()
  196. {
  197. $user = $this->auth->getUser();
  198. $mobile = $this->request->request('mobile');
  199. $captcha = $this->request->request('captcha');
  200. if (!$mobile || !$captcha) {
  201. $this->error(__('Invalid parameters'));
  202. }
  203. if (!Validate::regex($mobile, "^1\d{10}$")) {
  204. $this->error(__('Mobile is incorrect'));
  205. }
  206. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  207. $this->error(__('Mobile already exists'));
  208. }
  209. $result = Sms::check($mobile, $captcha, 'changemobile');
  210. if (!$result) {
  211. $this->error(__('Captcha is incorrect'));
  212. }
  213. $verification = $user->verification;
  214. $verification->mobile = 1;
  215. $user->verification = $verification;
  216. $user->mobile = $mobile;
  217. $user->save();
  218. Sms::flush($mobile, 'changemobile');
  219. $this->success();
  220. }
  221. /**
  222. * 第三方登录
  223. *
  224. * @param string $platform 平台名称
  225. * @param string $code Code码
  226. */
  227. public function third()
  228. {
  229. $url = url('user/index');
  230. $platform = $this->request->request("platform");
  231. $code = $this->request->request("code");
  232. $config = get_addon_config('third');
  233. if (!$config || !isset($config[$platform])) {
  234. $this->error(__('Invalid parameters'));
  235. }
  236. $app = new \addons\third\library\Application($config);
  237. //通过code换access_token和绑定会员
  238. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  239. if ($result) {
  240. $loginret = \addons\third\library\Service::connect($platform, $result);
  241. if ($loginret) {
  242. $data = [
  243. 'userinfo' => $this->auth->getUserinfo(),
  244. 'thirdinfo' => $result
  245. ];
  246. $this->success(__('Logged in successful'), $data);
  247. }
  248. }
  249. $this->error(__('Operation failed'), $url);
  250. }
  251. /**
  252. * 重置密码
  253. *
  254. * @param string $mobile 手机号
  255. * @param string $newpassword 新密码
  256. * @param string $captcha 验证码
  257. */
  258. public function resetpwd()
  259. {
  260. $type = $this->request->request("type");
  261. $mobile = $this->request->request("mobile");
  262. $email = $this->request->request("email");
  263. $newpassword = $this->request->request("newpassword");
  264. $captcha = $this->request->request("captcha");
  265. if (!$newpassword || !$captcha) {
  266. $this->error(__('Invalid parameters'));
  267. }
  268. if ($type == 'mobile') {
  269. if (!Validate::regex($mobile, "^1\d{10}$")) {
  270. $this->error(__('Mobile is incorrect'));
  271. }
  272. $user = \app\common\model\User::getByMobile($mobile);
  273. if (!$user) {
  274. $this->error(__('User not found'));
  275. }
  276. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  277. if (!$ret) {
  278. $this->error(__('Captcha is incorrect'));
  279. }
  280. Sms::flush($mobile, 'resetpwd');
  281. } else {
  282. if (!Validate::is($email, "email")) {
  283. $this->error(__('Email is incorrect'));
  284. }
  285. $user = \app\common\model\User::getByEmail($email);
  286. if (!$user) {
  287. $this->error(__('User not found'));
  288. }
  289. $ret = Ems::check($email, $captcha, 'resetpwd');
  290. if (!$ret) {
  291. $this->error(__('Captcha is incorrect'));
  292. }
  293. Ems::flush($email, 'resetpwd');
  294. }
  295. //模拟一次登录
  296. $this->auth->direct($user->id);
  297. $ret = $this->auth->changepwd($newpassword, '', true);
  298. if ($ret) {
  299. $this->success(__('Reset password successful'));
  300. } else {
  301. $this->error($this->auth->getError());
  302. }
  303. }
  304. }