User.php 11 KB

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