User.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. /**
  13. * 会员接口,登录,注册,修改资料等
  14. */
  15. class User extends Api
  16. {
  17. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if (!Config::get('fastadmin.usercenter')) {
  23. $this->error(__('User center already closed'));
  24. }
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 会员登录
  35. *
  36. * @ApiMethod (POST)
  37. * @param string $account 账号
  38. * @param string $password 密码
  39. */
  40. public function login()
  41. {
  42. $account = input('account');
  43. $password = input('password');
  44. if (!$account || !$password) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. $ret = $this->auth->login($account, $password);
  48. if ($ret) {
  49. $data = $this->userInfo('return');
  50. $this->success(__('Logged in successful'), $data);
  51. } else {
  52. $this->error($this->auth->getError());
  53. }
  54. }
  55. /**
  56. * 手机验证码登录
  57. *
  58. * @ApiMethod (POST)
  59. * @param string $mobile 手机号
  60. * @param string $captcha 验证码
  61. */
  62. public function mobilelogin()
  63. {
  64. $mobile = input('mobile');
  65. $captcha = input('captcha');
  66. if (!$mobile || !$captcha) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. if (!Validate::regex($mobile, "^1\d{10}$")) {
  70. $this->error(__('Mobile is incorrect'));
  71. }
  72. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  73. $this->error(__('Captcha is incorrect'));
  74. }
  75. $user = \app\common\model\User::getByMobile($mobile);
  76. if ($user) {
  77. if ($user->status != 1) {
  78. $this->error(__('Account is locked'));
  79. }
  80. //如果已经有账号则直接登录
  81. $ret = $this->auth->direct($user->id);
  82. } else {
  83. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  84. }
  85. if ($ret) {
  86. Sms::flush($mobile, 'mobilelogin');
  87. $data = $this->userInfo('return');
  88. $this->success(__('Logged in successful'), $data);
  89. } else {
  90. $this->error($this->auth->getError());
  91. }
  92. }
  93. /**
  94. * 注册会员
  95. *
  96. * @ApiMethod (POST)
  97. * @param string $username 用户名
  98. * @param string $password 密码
  99. * @param string $email 邮箱
  100. * @param string $mobile 手机号
  101. * @param string $code 验证码
  102. */
  103. /*public function register()
  104. {
  105. $username = $this->request->post('username');
  106. $password = $this->request->post('password');
  107. $email = $this->request->post('email');
  108. $mobile = $this->request->post('mobile');
  109. $code = $this->request->post('code');
  110. if (!$username || !$password) {
  111. $this->error(__('Invalid parameters'));
  112. }
  113. if ($email && !Validate::is($email, "email")) {
  114. $this->error(__('Email is incorrect'));
  115. }
  116. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  117. $this->error(__('Mobile is incorrect'));
  118. }
  119. $ret = Sms::check($mobile, $code, 'register');
  120. if (!$ret) {
  121. $this->error(__('Captcha is incorrect'));
  122. }
  123. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  124. if ($ret) {
  125. $data = $this->userInfo('return');
  126. $this->success(__('Sign up successful'), $data);
  127. } else {
  128. $this->error($this->auth->getError());
  129. }
  130. }*/
  131. //用户详细资料
  132. public function userInfo($type = 1){
  133. $info = $this->auth->getUserinfo();
  134. if($type == 'return'){
  135. return $info;
  136. }
  137. $this->success(__('success'),$info);
  138. }
  139. //申请真人认证
  140. public function apply_real_confirm(){
  141. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  142. if(!$avatar){
  143. $this->error('请上传真人头像');
  144. }
  145. Db::startTrans();
  146. $data = [
  147. 'avatar' => $avatar,
  148. 'real_status' => 1,
  149. ];
  150. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  151. //tag任务赠送金币
  152. if($rs){
  153. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  154. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  155. if($task_rs === false){
  156. Db::rollback();
  157. $this->error('完成任务赠送奖励失败');
  158. }
  159. //完成真人头像 +5金币
  160. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  161. if($task_rs === false){
  162. Db::rollback();
  163. $this->error('完成任务赠送奖励失败');
  164. }
  165. }
  166. Db::commit();
  167. $this->success();
  168. }
  169. //实名认证信息
  170. public function idcard_confirm_info(){
  171. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  172. $this->success('success',$check);
  173. }
  174. //申请实名认证
  175. public function apply_idcard_confirm(){
  176. $truename = input('truename','');
  177. $idcard = input('idcard','');
  178. $idcard_images = input('idcard_images','');
  179. $alipay_account = input('alipay_account','');
  180. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  181. $this->error('实名认证信息必填');
  182. }
  183. if($this->auth->idcard_status == 1){
  184. $this->error('您已经完成实名认证');
  185. }
  186. if($this->auth->idcard_status == 0){
  187. $this->error('您已经提交实名认证,请等待审核');
  188. }
  189. Db::startTrans();
  190. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  191. if(!empty($check)){
  192. if($check['status'] == 0){
  193. Db::rollback();
  194. $this->error('您已经提交实名认证,请等待审核');
  195. }
  196. if($check['status'] == 1){
  197. Db::rollback();
  198. $this->error('您已经完成实名认证');
  199. }
  200. }
  201. $data = [
  202. 'user_id' => $this->auth->id,
  203. 'truename' => $truename,
  204. 'idcard' => $idcard,
  205. 'idcard_images' => $idcard_images,
  206. 'alipay_account' => $alipay_account,
  207. 'status' => 0,
  208. 'createtime' => time(),
  209. 'updatetime' => time(),
  210. ];
  211. //更新
  212. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  213. if(!empty($check)){
  214. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  215. }else{
  216. $rs = Db::name('user_idconfirm')->insertGetId($data);
  217. }
  218. if(!$rs || !$update_rs){
  219. Db::rollback();
  220. $this->error('提交失败');
  221. }
  222. Db::commit();
  223. $this->success('提交成功,请等待审核');
  224. }
  225. /**
  226. * 退出登录
  227. * @ApiMethod (POST)
  228. */
  229. public function logout()
  230. {
  231. if (!$this->request->isPost()) {
  232. $this->error(__('Invalid parameters'));
  233. }
  234. //退出im
  235. $tenIm = new Tenim();
  236. $tenIm->loginoutim($this->auth->id);
  237. $this->auth->logout();
  238. $this->success(__('Logout successful'));
  239. }
  240. /**
  241. * 修改会员个人信息
  242. *
  243. * @ApiMethod (POST)
  244. * @param string $avatar 头像地址
  245. * @param string $username 用户名
  246. * @param string $nickname 昵称
  247. * @param string $bio 个人简介
  248. */
  249. public function profile()
  250. {
  251. $field_array = ['nickname','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo'];
  252. $data = [];
  253. foreach($field_array as $key => $field){
  254. if(!input('?'.$field)){
  255. continue;
  256. }
  257. $newone = input($field);
  258. if($field == 'avatar'){
  259. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  260. }
  261. if($field == 'photo_images'){
  262. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  263. }
  264. $data[$field] = $newone;
  265. }
  266. if(isset($data['birthday'])){
  267. $data['birthday'] = strtotime($data['birthday']);
  268. }
  269. if(isset($data['avatar'])){
  270. $data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  271. }
  272. if(isset($data['hobby_ids'])){
  273. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  274. }
  275. if(isset($data['tag_ids'])){
  276. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  277. }
  278. //dump($data);
  279. if(empty($data)){
  280. $this->error('没有任何改变');
  281. }
  282. Db::startTrans();
  283. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  284. if($update_rs === false){
  285. Db::rollback();
  286. $this->error('修改资料失败');
  287. }
  288. //tag任务赠送金币
  289. //上传头像加5金币
  290. if(isset($data['avatar'])){
  291. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  292. if($task_rs === false){
  293. Db::rollback();
  294. $this->error('完成任务赠送奖励失败');
  295. }
  296. }
  297. //上传本人语音介绍 10金币
  298. if(isset($data['audio_bio'])){
  299. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  300. if($task_rs === false){
  301. Db::rollback();
  302. $this->error('完成任务赠送奖励失败');
  303. }
  304. }
  305. //上传本人五张照片 10金币
  306. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  307. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  308. if($task_rs === false){
  309. Db::rollback();
  310. $this->error('完成任务赠送奖励失败');
  311. }
  312. }
  313. //所有资料完成
  314. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  315. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  316. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  317. if($task_rs === false){
  318. Db::rollback();
  319. $this->error('完成任务赠送奖励失败');
  320. }
  321. }
  322. Db::commit();
  323. $this->success();
  324. }
  325. public function set_status_switch(){
  326. }
  327. /*
  328. * 修改用户的坐标
  329. * */
  330. public function change_longlat(){
  331. $longitude = input_post('longitude');
  332. $latitude = input_post('latitude');
  333. $cityname = input_post('cityname');
  334. if(empty($longitude) || empty($latitude) || empty($cityname)){
  335. $this->error();
  336. }
  337. $data = [
  338. 'longitude' => $longitude,
  339. 'latitude' => $latitude,
  340. 'cityname' => $cityname,
  341. ];
  342. Db::name('user')->where('id',$this->auth->id)->update($data);
  343. $this->success();
  344. }
  345. /**
  346. * 修改邮箱
  347. *
  348. * @ApiMethod (POST)
  349. * @param string $email 邮箱
  350. * @param string $captcha 验证码
  351. */
  352. /*public function changeemail()
  353. {
  354. $user = $this->auth->getUser();
  355. $email = $this->request->post('email');
  356. $captcha = $this->request->post('captcha');
  357. if (!$email || !$captcha) {
  358. $this->error(__('Invalid parameters'));
  359. }
  360. if (!Validate::is($email, "email")) {
  361. $this->error(__('Email is incorrect'));
  362. }
  363. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  364. $this->error(__('Email already exists'));
  365. }
  366. $result = Ems::check($email, $captcha, 'changeemail');
  367. if (!$result) {
  368. $this->error(__('Captcha is incorrect'));
  369. }
  370. $verification = $user->verification;
  371. $verification->email = 1;
  372. $user->verification = $verification;
  373. $user->email = $email;
  374. $user->save();
  375. Ems::flush($email, 'changeemail');
  376. $this->success();
  377. }*/
  378. /**
  379. * 修改手机号
  380. *
  381. * @ApiMethod (POST)
  382. * @param string $mobile 手机号
  383. * @param string $captcha 验证码
  384. */
  385. public function changemobile()
  386. {
  387. $user = $this->auth->getUser();
  388. $oldcaptcha = $this->request->request('oldcaptcha');
  389. $mobile = $this->request->request('mobile');
  390. $captcha = $this->request->request('captcha');
  391. if (!$oldcaptcha || !$mobile || !$captcha) {
  392. $this->error(__('Invalid parameters'));
  393. }
  394. if (!Validate::regex($mobile, "^1\d{10}$")) {
  395. $this->error(__('Mobile is incorrect'));
  396. }
  397. if($user->mobile == $mobile){
  398. $this->error('新手机号不能与旧手机号相同');
  399. }
  400. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  401. $this->error(__('Mobile already exist'));
  402. }
  403. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  404. if (!$result) {
  405. $this->error(__('Captcha is incorrect'));
  406. }
  407. $result = Sms::check($mobile, $captcha, 'changemobile');
  408. if (!$result) {
  409. $this->error(__('Captcha is incorrect'));
  410. }
  411. $verification = $user->verification;
  412. $verification->mobile = 1;
  413. $user->verification = $verification;
  414. $user->mobile = $mobile;
  415. $user->save();
  416. Sms::flush($user->mobile, 'changemobile');
  417. Sms::flush($mobile, 'changemobile');
  418. $this->success();
  419. }
  420. /**
  421. * 第三方登录
  422. *
  423. * @ApiMethod (POST)
  424. * @param string $platform 平台名称
  425. * @param string $code Code码
  426. */
  427. /*public function third()
  428. {
  429. $url = url('user/index');
  430. $platform = $this->request->post("platform");
  431. $code = $this->request->post("code");
  432. $config = get_addon_config('third');
  433. if (!$config || !isset($config[$platform])) {
  434. $this->error(__('Invalid parameters'));
  435. }
  436. $app = new \addons\third\library\Application($config);
  437. //通过code换access_token和绑定会员
  438. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  439. if ($result) {
  440. $loginret = \addons\third\library\Service::connect($platform, $result);
  441. if ($loginret) {
  442. $data = [
  443. 'userinfo' => $this->auth->getUserinfo(),
  444. 'thirdinfo' => $result
  445. ];
  446. $this->success(__('Logged in successful'), $data);
  447. }
  448. }
  449. $this->error(__('Operation failed'), $url);
  450. }*/
  451. /**
  452. * 重置密码
  453. *
  454. * @ApiMethod (POST)
  455. * @param string $mobile 手机号
  456. * @param string $newpassword 新密码
  457. * @param string $captcha 验证码
  458. */
  459. public function resetpwd()
  460. {
  461. //$type = input("type");
  462. $type = 'mobile';
  463. $mobile = input("mobile");
  464. $email = input("email");
  465. $newpassword = input("newpassword");
  466. $captcha = input("captcha");
  467. if (!$newpassword || !$captcha) {
  468. $this->error(__('Invalid parameters'));
  469. }
  470. if ($type == 'mobile') {
  471. if (!Validate::regex($mobile, "^1\d{10}$")) {
  472. $this->error(__('Mobile is incorrect'));
  473. }
  474. $user = \app\common\model\User::getByMobile($mobile);
  475. if (!$user) {
  476. $this->error(__('User not found'));
  477. }
  478. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  479. if (!$ret) {
  480. $this->error(__('Captcha is incorrect'));
  481. }
  482. Sms::flush($mobile, 'resetpwd');
  483. } else {
  484. if (!Validate::is($email, "email")) {
  485. $this->error(__('Email is incorrect'));
  486. }
  487. $user = \app\common\model\User::getByEmail($email);
  488. if (!$user) {
  489. $this->error(__('User not found'));
  490. }
  491. $ret = Ems::check($email, $captcha, 'resetpwd');
  492. if (!$ret) {
  493. $this->error(__('Captcha is incorrect'));
  494. }
  495. Ems::flush($email, 'resetpwd');
  496. }
  497. //模拟一次登录
  498. $this->auth->direct($user->id);
  499. $ret = $this->auth->changepwd($newpassword, '', true);
  500. if ($ret) {
  501. $this->success(__('Reset password successful'));
  502. } else {
  503. $this->error($this->auth->getError());
  504. }
  505. }
  506. /**
  507. * 修改密码
  508. *
  509. * @ApiMethod (POST)
  510. * @param string $newpassword 新密码
  511. * @param string $oldpassword 旧密码
  512. */
  513. public function changepwd(){
  514. $newpassword = input('newpassword');
  515. $oldpassword = input('oldpassword','');
  516. if (!$newpassword) {
  517. $this->error(__('Invalid parameters'));
  518. }
  519. if($this->auth->password && empty($oldpassword)){
  520. $this->error('原密码必填');
  521. }
  522. if(empty($this->auth->password)){
  523. $ret = $this->auth->changepwd($newpassword, '', true);
  524. }else{
  525. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  526. }
  527. if ($ret) {
  528. $this->success(__('Reset password successful'));
  529. } else {
  530. $this->error($this->auth->getError());
  531. }
  532. }
  533. /**
  534. * 记录当前登陆的设备ID,设备信息,IP等
  535. */
  536. public function changeDeviceIp()
  537. {
  538. // 接口防并发
  539. if (!$this->apiLimit(1, 5000)) {
  540. return ;
  541. }
  542. $user = $this->auth->getUser();
  543. $ip = request()->ip();
  544. $deviceId = $this->request->request('device_id','');
  545. $phoneModel = $this->request->request('phone_model','');
  546. $brand = $this->request->request('brand','');
  547. $apiVersion = $this->request->request('api_version','');
  548. $deviceOs = $this->request->request('device_os','');
  549. if ($ip !== $user->loginip){
  550. $update = [];
  551. $update['id'] = $user->id;
  552. $update['loginip'] = $ip;
  553. \app\common\model\User::update($update);
  554. }
  555. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  556. if (empty($userDeviceInfo)){
  557. $userDeviceInfo = new UserDeviceInfo();
  558. $userDeviceInfo->user_id = $user->u_id;
  559. }
  560. $userDeviceInfo->device_os = $deviceOs;
  561. $userDeviceInfo->device_id = $deviceId;
  562. $userDeviceInfo->phone_model = $phoneModel;
  563. $userDeviceInfo->brand = $brand;
  564. $userDeviceInfo->api_version = $apiVersion;
  565. $userDeviceInfo->save();
  566. //首页接口调用,这里不反回信息
  567. // $this->success("更新成功!");
  568. }
  569. }