User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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\Config;
  8. use think\Validate;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Api
  13. {
  14. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. if (!Config::get('fastadmin.usercenter')) {
  20. $this->error(__('User center already closed'));
  21. }
  22. }
  23. /**
  24. * 会员中心
  25. */
  26. public function index()
  27. {
  28. $this->success('', ['welcome' => $this->auth->nickname]);
  29. }
  30. /**
  31. * 会员登录
  32. *
  33. * @ApiMethod (POST)
  34. * @param string $account 账号
  35. * @param string $password 密码
  36. */
  37. public function login()
  38. {
  39. $account = $this->request->post('account');
  40. $password = $this->request->post('password');
  41. if (!$account || !$password) {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. $ret = $this->auth->login($account, $password);
  45. if ($ret) {
  46. $data = ['userinfo' => $this->auth->getUserinfo()];
  47. $this->success(__('Logged in successful'), $data);
  48. } else {
  49. $this->error($this->auth->getError());
  50. }
  51. }
  52. /**
  53. * 手机验证码登录
  54. *
  55. * @ApiMethod (POST)
  56. * @param string $mobile 手机号
  57. * @param string $captcha 验证码
  58. */
  59. public function mobilelogin()
  60. {
  61. $mobile = $this->request->post('mobile');
  62. $captcha = $this->request->post('captcha');
  63. if (!$mobile || !$captcha) {
  64. $this->error(__('Invalid parameters'));
  65. }
  66. if (!Validate::regex($mobile, "^1\d{10}$")) {
  67. $this->error(__('Mobile is incorrect'));
  68. }
  69. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  70. $this->error(__('Captcha is incorrect'));
  71. }
  72. $user = \app\common\model\User::getByMobile($mobile);
  73. if ($user) {
  74. if ($user->status != 1) {
  75. $this->error(__('Account is locked'));
  76. }
  77. //如果已经有账号则直接登录
  78. $ret = $this->auth->direct($user->id);
  79. } else {
  80. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  81. }
  82. if ($ret) {
  83. Sms::flush($mobile, 'mobilelogin');
  84. $data = ['userinfo' => $this->auth->getUserinfo()];
  85. $this->success(__('Logged in successful'), $data);
  86. } else {
  87. $this->error($this->auth->getError());
  88. }
  89. }
  90. /**
  91. * 注册会员
  92. *
  93. * @ApiMethod (POST)
  94. * @param string $username 用户名
  95. * @param string $password 密码
  96. * @param string $email 邮箱
  97. * @param string $mobile 手机号
  98. * @param string $code 验证码
  99. */
  100. public function register()
  101. {
  102. $username = $this->request->post('username');
  103. $password = $this->request->post('password');
  104. $email = $this->request->post('email');
  105. $mobile = $this->request->post('mobile');
  106. $code = $this->request->post('code');
  107. if (!$username || !$password) {
  108. $this->error(__('Invalid parameters'));
  109. }
  110. if ($email && !Validate::is($email, "email")) {
  111. $this->error(__('Email is incorrect'));
  112. }
  113. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  114. $this->error(__('Mobile is incorrect'));
  115. }
  116. $ret = Sms::check($mobile, $code, 'register');
  117. if (!$ret) {
  118. $this->error(__('Captcha is incorrect'));
  119. }
  120. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  121. if ($ret) {
  122. $data = ['userinfo' => $this->auth->getUserinfo()];
  123. $this->success(__('Sign up successful'), $data);
  124. } else {
  125. $this->error($this->auth->getError());
  126. }
  127. }
  128. //实名认证信息
  129. public function idcard_confirm_info(){
  130. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->find();
  131. $this->success('success',$check);
  132. }
  133. //申请实名认证
  134. public function idcard_confirm(){
  135. $truename = input('truename','');
  136. $idcard = input('idcard','');
  137. $idcard_images = input('idcard_images','');
  138. if(empty($truename) || empty($idcard) || empty($idcard_images)){
  139. $this->error('实名认证信息必填');
  140. }
  141. if($this->auth->idcardstatus == 1){
  142. $this->error('您已经完成实名认证');
  143. }
  144. if($this->auth->idcardstatus == 0){
  145. $this->error('您已经提交实名认证,请等待审核');
  146. }
  147. Db::startTrans();
  148. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  149. if(!empty($check)){
  150. if($check['status'] == 0){
  151. Db::rollback();
  152. $this->error('您已经提交实名认证,请等待审核');
  153. }
  154. if($check['status'] == 1){
  155. Db::rollback();
  156. $this->error('您已经完成实名认证');
  157. }
  158. }
  159. $data = [
  160. 'user_id' => $this->auth->id,
  161. 'truename' => $truename,
  162. 'idcard' => $idcard,
  163. 'idcard_images' => $idcard_images,
  164. 'status' => 0,
  165. 'createtime' => time(),
  166. 'updatetime' => time(),
  167. ];
  168. //更新
  169. if(!empty($check)){
  170. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcardstatus'=>0]);
  171. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  172. }else{
  173. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcardstatus'=>0]);
  174. $rs = Db::name('user_idconfirm')->insertGetId($data);
  175. }
  176. if(!$rs || !$update_rs){
  177. Db::rollback();
  178. $this->error('提交失败');
  179. }
  180. Db::commit();
  181. $this->success('提交成功,请等待审核');
  182. }
  183. /**
  184. * 退出登录
  185. * @ApiMethod (POST)
  186. */
  187. public function logout()
  188. {
  189. if (!$this->request->isPost()) {
  190. $this->error(__('Invalid parameters'));
  191. }
  192. $this->auth->logout();
  193. $this->success(__('Logout successful'));
  194. }
  195. /**
  196. * 修改会员个人信息
  197. *
  198. * @ApiMethod (POST)
  199. * @param string $avatar 头像地址
  200. * @param string $username 用户名
  201. * @param string $nickname 昵称
  202. * @param string $bio 个人简介
  203. */
  204. public function profile()
  205. {
  206. $user = $this->auth->getUser();
  207. $username = $this->request->post('username');
  208. $nickname = $this->request->post('nickname');
  209. $bio = $this->request->post('bio');
  210. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  211. if ($username) {
  212. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  213. if ($exists) {
  214. $this->error(__('Username already exists'));
  215. }
  216. $user->username = $username;
  217. }
  218. if ($nickname) {
  219. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  220. if ($exists) {
  221. $this->error(__('Nickname already exists'));
  222. }
  223. $user->nickname = $nickname;
  224. }
  225. $user->bio = $bio;
  226. $user->avatar = $avatar;
  227. $user->save();
  228. $this->success();
  229. }
  230. /**
  231. * 修改邮箱
  232. *
  233. * @ApiMethod (POST)
  234. * @param string $email 邮箱
  235. * @param string $captcha 验证码
  236. */
  237. public function changeemail()
  238. {
  239. $user = $this->auth->getUser();
  240. $email = $this->request->post('email');
  241. $captcha = $this->request->post('captcha');
  242. if (!$email || !$captcha) {
  243. $this->error(__('Invalid parameters'));
  244. }
  245. if (!Validate::is($email, "email")) {
  246. $this->error(__('Email is incorrect'));
  247. }
  248. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  249. $this->error(__('Email already exists'));
  250. }
  251. $result = Ems::check($email, $captcha, 'changeemail');
  252. if (!$result) {
  253. $this->error(__('Captcha is incorrect'));
  254. }
  255. $verification = $user->verification;
  256. $verification->email = 1;
  257. $user->verification = $verification;
  258. $user->email = $email;
  259. $user->save();
  260. Ems::flush($email, 'changeemail');
  261. $this->success();
  262. }
  263. /**
  264. * 修改手机号
  265. *
  266. * @ApiMethod (POST)
  267. * @param string $mobile 手机号
  268. * @param string $captcha 验证码
  269. */
  270. public function changemobile()
  271. {
  272. $user = $this->auth->getUser();
  273. $mobile = $this->request->post('mobile');
  274. $captcha = $this->request->post('captcha');
  275. if (!$mobile || !$captcha) {
  276. $this->error(__('Invalid parameters'));
  277. }
  278. if (!Validate::regex($mobile, "^1\d{10}$")) {
  279. $this->error(__('Mobile is incorrect'));
  280. }
  281. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  282. $this->error(__('Mobile already exists'));
  283. }
  284. $result = Sms::check($mobile, $captcha, 'changemobile');
  285. if (!$result) {
  286. $this->error(__('Captcha is incorrect'));
  287. }
  288. $verification = $user->verification;
  289. $verification->mobile = 1;
  290. $user->verification = $verification;
  291. $user->mobile = $mobile;
  292. $user->save();
  293. Sms::flush($mobile, 'changemobile');
  294. $this->success();
  295. }
  296. /**
  297. * 第三方登录
  298. *
  299. * @ApiMethod (POST)
  300. * @param string $platform 平台名称
  301. * @param string $code Code码
  302. */
  303. public function third()
  304. {
  305. $url = url('user/index');
  306. $platform = $this->request->post("platform");
  307. $code = $this->request->post("code");
  308. $config = get_addon_config('third');
  309. if (!$config || !isset($config[$platform])) {
  310. $this->error(__('Invalid parameters'));
  311. }
  312. $app = new \addons\third\library\Application($config);
  313. //通过code换access_token和绑定会员
  314. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  315. if ($result) {
  316. $loginret = \addons\third\library\Service::connect($platform, $result);
  317. if ($loginret) {
  318. $data = [
  319. 'userinfo' => $this->auth->getUserinfo(),
  320. 'thirdinfo' => $result
  321. ];
  322. $this->success(__('Logged in successful'), $data);
  323. }
  324. }
  325. $this->error(__('Operation failed'), $url);
  326. }
  327. /**
  328. * 重置密码
  329. *
  330. * @ApiMethod (POST)
  331. * @param string $mobile 手机号
  332. * @param string $newpassword 新密码
  333. * @param string $captcha 验证码
  334. */
  335. public function resetpwd()
  336. {
  337. $type = $this->request->post("type");
  338. $mobile = $this->request->post("mobile");
  339. $email = $this->request->post("email");
  340. $newpassword = $this->request->post("newpassword");
  341. $captcha = $this->request->post("captcha");
  342. if (!$newpassword || !$captcha) {
  343. $this->error(__('Invalid parameters'));
  344. }
  345. if ($type == 'mobile') {
  346. if (!Validate::regex($mobile, "^1\d{10}$")) {
  347. $this->error(__('Mobile is incorrect'));
  348. }
  349. $user = \app\common\model\User::getByMobile($mobile);
  350. if (!$user) {
  351. $this->error(__('User not found'));
  352. }
  353. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  354. if (!$ret) {
  355. $this->error(__('Captcha is incorrect'));
  356. }
  357. Sms::flush($mobile, 'resetpwd');
  358. } else {
  359. if (!Validate::is($email, "email")) {
  360. $this->error(__('Email is incorrect'));
  361. }
  362. $user = \app\common\model\User::getByEmail($email);
  363. if (!$user) {
  364. $this->error(__('User not found'));
  365. }
  366. $ret = Ems::check($email, $captcha, 'resetpwd');
  367. if (!$ret) {
  368. $this->error(__('Captcha is incorrect'));
  369. }
  370. Ems::flush($email, 'resetpwd');
  371. }
  372. //模拟一次登录
  373. $this->auth->direct($user->id);
  374. $ret = $this->auth->changepwd($newpassword, '', true);
  375. if ($ret) {
  376. $this->success(__('Reset password successful'));
  377. } else {
  378. $this->error($this->auth->getError());
  379. }
  380. }
  381. }