User.php 11 KB

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