User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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', 'apiRegister'];
  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. public function apiRegister()
  122. {
  123. $username = '';
  124. while (true) {
  125. // 随机生成一个8位数字作为账号
  126. $username = Random::numeric(8);
  127. // 判断数据库是否存在当前账号, 存在就重新生成
  128. if (\app\common\model\User::where('username', $username)->find()) {
  129. continue;
  130. }
  131. break;
  132. }
  133. $password = Random::alnum(6);
  134. $email = "test@qq.com";
  135. $mobile = "18888888888";
  136. if ($ret = $this->auth->register($username, $password, $email, $mobile, [])) {
  137. var_dump($username);
  138. var_dump($password);
  139. var_dump($ret);
  140. die;
  141. }
  142. print_r("创建失败");
  143. }
  144. /**
  145. * 注销登录
  146. */
  147. public function logout()
  148. {
  149. $this->auth->logout();
  150. $this->success(__('Logout successful'));
  151. }
  152. /**
  153. * 修改会员个人信息
  154. *
  155. * @param string $avatar 头像地址
  156. * @param string $username 用户名
  157. * @param string $nickname 昵称
  158. * @param string $bio 个人简介
  159. */
  160. public function profile()
  161. {
  162. $user = $this->auth->getUser();
  163. $username = $this->request->request('username');
  164. $nickname = $this->request->request('nickname');
  165. $bio = $this->request->request('bio');
  166. $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
  167. if ($username) {
  168. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  169. if ($exists) {
  170. $this->error(__('Username already exists'));
  171. }
  172. $user->username = $username;
  173. }
  174. $user->nickname = $nickname;
  175. $user->bio = $bio;
  176. $user->avatar = $avatar;
  177. $user->save();
  178. $this->success();
  179. }
  180. /**
  181. * 修改邮箱
  182. *
  183. * @param string $email 邮箱
  184. * @param string $captcha 验证码
  185. */
  186. public function changeemail()
  187. {
  188. $user = $this->auth->getUser();
  189. $email = $this->request->post('email');
  190. $captcha = $this->request->request('captcha');
  191. if (!$email || !$captcha) {
  192. $this->error(__('Invalid parameters'));
  193. }
  194. if (!Validate::is($email, "email")) {
  195. $this->error(__('Email is incorrect'));
  196. }
  197. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  198. $this->error(__('Email already exists'));
  199. }
  200. $result = Ems::check($email, $captcha, 'changeemail');
  201. if (!$result) {
  202. $this->error(__('Captcha is incorrect'));
  203. }
  204. $verification = $user->verification;
  205. $verification->email = 1;
  206. $user->verification = $verification;
  207. $user->email = $email;
  208. $user->save();
  209. Ems::flush($email, 'changeemail');
  210. $this->success();
  211. }
  212. /**
  213. * 修改手机号
  214. *
  215. * @param string $mobile 手机号
  216. * @param string $captcha 验证码
  217. */
  218. public function changemobile()
  219. {
  220. $user = $this->auth->getUser();
  221. $mobile = $this->request->request('mobile');
  222. $captcha = $this->request->request('captcha');
  223. if (!$mobile || !$captcha) {
  224. $this->error(__('Invalid parameters'));
  225. }
  226. if (!Validate::regex($mobile, "^1\d{10}$")) {
  227. $this->error(__('Mobile is incorrect'));
  228. }
  229. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  230. $this->error(__('Mobile already exists'));
  231. }
  232. $result = Sms::check($mobile, $captcha, 'changemobile');
  233. if (!$result) {
  234. $this->error(__('Captcha is incorrect'));
  235. }
  236. $verification = $user->verification;
  237. $verification->mobile = 1;
  238. $user->verification = $verification;
  239. $user->mobile = $mobile;
  240. $user->save();
  241. Sms::flush($mobile, 'changemobile');
  242. $this->success();
  243. }
  244. /**
  245. * 第三方登录
  246. *
  247. * @param string $platform 平台名称
  248. * @param string $code Code码
  249. */
  250. public function third()
  251. {
  252. $url = url('user/index');
  253. $platform = $this->request->request("platform");
  254. $code = $this->request->request("code");
  255. $config = get_addon_config('third');
  256. if (!$config || !isset($config[$platform])) {
  257. $this->error(__('Invalid parameters'));
  258. }
  259. $app = new \addons\third\library\Application($config);
  260. //通过code换access_token和绑定会员
  261. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  262. if ($result) {
  263. $loginret = \addons\third\library\Service::connect($platform, $result);
  264. if ($loginret) {
  265. $data = [
  266. 'userinfo' => $this->auth->getUserinfo(),
  267. 'thirdinfo' => $result
  268. ];
  269. $this->success(__('Logged in successful'), $data);
  270. }
  271. }
  272. $this->error(__('Operation failed'), $url);
  273. }
  274. /**
  275. * 重置密码
  276. *
  277. * @param string $mobile 手机号
  278. * @param string $newpassword 新密码
  279. * @param string $captcha 验证码
  280. */
  281. public function resetpwd()
  282. {
  283. $type = $this->request->request("type");
  284. $mobile = $this->request->request("mobile");
  285. $email = $this->request->request("email");
  286. $newpassword = $this->request->request("newpassword");
  287. $captcha = $this->request->request("captcha");
  288. if (!$newpassword || !$captcha) {
  289. $this->error(__('Invalid parameters'));
  290. }
  291. if ($type == 'mobile') {
  292. if (!Validate::regex($mobile, "^1\d{10}$")) {
  293. $this->error(__('Mobile is incorrect'));
  294. }
  295. $user = \app\common\model\User::getByMobile($mobile);
  296. if (!$user) {
  297. $this->error(__('User not found'));
  298. }
  299. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  300. if (!$ret) {
  301. $this->error(__('Captcha is incorrect'));
  302. }
  303. Sms::flush($mobile, 'resetpwd');
  304. } else {
  305. if (!Validate::is($email, "email")) {
  306. $this->error(__('Email is incorrect'));
  307. }
  308. $user = \app\common\model\User::getByEmail($email);
  309. if (!$user) {
  310. $this->error(__('User not found'));
  311. }
  312. $ret = Ems::check($email, $captcha, 'resetpwd');
  313. if (!$ret) {
  314. $this->error(__('Captcha is incorrect'));
  315. }
  316. Ems::flush($email, 'resetpwd');
  317. }
  318. //模拟一次登录
  319. $this->auth->direct($user->id);
  320. $ret = $this->auth->changepwd($newpassword, '', true);
  321. if ($ret) {
  322. $this->success(__('Reset password successful'));
  323. } else {
  324. $this->error($this->auth->getError());
  325. }
  326. }
  327. }