User.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 userintroinfo(){
  141. $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
  142. $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
  143. $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
  144. $rs = [
  145. 'intro_num' => $intro_num,
  146. 'money_sum' => $money_sum,
  147. 'user_list' => $user_list,
  148. ];
  149. $this->success('success',$rs);
  150. }
  151. //申请真人认证
  152. public function apply_real_confirm(){
  153. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  154. if(!$avatar){
  155. $this->error('请上传真人头像');
  156. }
  157. Db::startTrans();
  158. $data = [
  159. 'avatar' => $avatar,
  160. 'real_status' => 1,
  161. ];
  162. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  163. if($rs === false){
  164. Db::rollback();
  165. $this->error('认证失败');
  166. }
  167. //tag任务赠送金币
  168. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  169. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  170. if($task_rs === false){
  171. Db::rollback();
  172. $this->error('完成任务赠送奖励失败');
  173. }
  174. //完成真人头像 +5金币
  175. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  176. if($task_rs === false){
  177. Db::rollback();
  178. $this->error('完成任务赠送奖励失败');
  179. }
  180. //邀请人拿奖励,男性3元
  181. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  182. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  183. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  184. if($task_rs['status'] === false){
  185. Db::rollback();
  186. $this->error($task_rs['msg']);
  187. }
  188. }
  189. //系统消息
  190. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  191. Db::commit();
  192. $this->success();
  193. }
  194. //实名认证信息
  195. public function idcard_confirm_info(){
  196. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  197. $this->success('success',$check);
  198. }
  199. //申请实名认证
  200. public function apply_idcard_confirm(){
  201. $truename = input('truename','');
  202. $idcard = input('idcard','');
  203. $idcard_images = input('idcard_images','');
  204. $alipay_account = input('alipay_account','');
  205. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  206. $this->error('实名认证信息必填');
  207. }
  208. if($this->auth->idcard_status == 1){
  209. $this->error('您已经完成实名认证');
  210. }
  211. if($this->auth->idcard_status == 0){
  212. $this->error('您已经提交实名认证,请等待审核');
  213. }
  214. Db::startTrans();
  215. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  216. if(!empty($check)){
  217. if($check['status'] == 0){
  218. Db::rollback();
  219. $this->error('您已经提交实名认证,请等待审核');
  220. }
  221. if($check['status'] == 1){
  222. Db::rollback();
  223. $this->error('您已经完成实名认证');
  224. }
  225. }
  226. $data = [
  227. 'user_id' => $this->auth->id,
  228. 'truename' => $truename,
  229. 'idcard' => $idcard,
  230. 'idcard_images' => $idcard_images,
  231. 'alipay_account' => $alipay_account,
  232. 'status' => 0,
  233. 'createtime' => time(),
  234. 'updatetime' => time(),
  235. ];
  236. //更新
  237. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  238. if(!empty($check)){
  239. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  240. }else{
  241. $rs = Db::name('user_idconfirm')->insertGetId($data);
  242. }
  243. if(!$rs || !$update_rs){
  244. Db::rollback();
  245. $this->error('提交失败');
  246. }
  247. Db::commit();
  248. $this->success('提交成功,请等待审核');
  249. }
  250. /**
  251. * 退出登录
  252. * @ApiMethod (POST)
  253. */
  254. public function logout()
  255. {
  256. if (!$this->request->isPost()) {
  257. $this->error(__('Invalid parameters'));
  258. }
  259. //退出im
  260. $tenIm = new Tenim();
  261. $tenIm->loginoutim($this->auth->id);
  262. $this->auth->logout();
  263. $this->success(__('Logout successful'));
  264. }
  265. /**
  266. * 修改会员个人信息
  267. *
  268. * @ApiMethod (POST)
  269. * @param string $avatar 头像地址
  270. * @param string $username 用户名
  271. * @param string $nickname 昵称
  272. * @param string $bio 个人简介
  273. */
  274. public function profile()
  275. {
  276. $field_array = ['nickname','introcode','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'];
  277. $data = [];
  278. foreach($field_array as $key => $field){
  279. if(!input('?'.$field)){
  280. continue;
  281. }
  282. $newone = input($field);
  283. if($field == 'avatar'){
  284. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  285. }
  286. if($field == 'photo_images'){
  287. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  288. }
  289. $data[$field] = $newone;
  290. }
  291. if(isset($data['birthday'])){
  292. $data['birthday'] = strtotime($data['birthday']);
  293. }
  294. if(isset($data['avatar'])){
  295. $data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  296. }
  297. if(isset($data['hobby_ids'])){
  298. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  299. }
  300. if(isset($data['tag_ids'])){
  301. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  302. }
  303. if(isset($data['introcode'])){
  304. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  305. if(!$intro_user){
  306. $this->error('不存在的邀请人');
  307. }
  308. $data['intro_uid'] = $intro_user;
  309. }
  310. //dump($data);
  311. if(empty($data)){
  312. $this->error('没有任何改变');
  313. }
  314. Db::startTrans();
  315. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  316. if($update_rs === false){
  317. Db::rollback();
  318. $this->error('修改资料失败');
  319. }
  320. //tag任务赠送金币
  321. //上传头像加5金币
  322. if(isset($data['avatar'])){
  323. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  324. if($task_rs === false){
  325. Db::rollback();
  326. $this->error('完成任务赠送奖励失败');
  327. }
  328. }
  329. //上传本人语音介绍 10金币
  330. if(isset($data['audio_bio'])){
  331. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  332. if($task_rs === false){
  333. Db::rollback();
  334. $this->error('完成任务赠送奖励失败');
  335. }
  336. }
  337. //上传本人五张照片 10金币
  338. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  339. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  340. if($task_rs === false){
  341. Db::rollback();
  342. $this->error('完成任务赠送奖励失败');
  343. }
  344. }
  345. //所有资料完成
  346. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  347. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  348. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  349. if($task_rs === false){
  350. Db::rollback();
  351. $this->error('完成任务赠送奖励失败');
  352. }
  353. }
  354. Db::commit();
  355. $this->success();
  356. }
  357. public function set_status_switch(){
  358. }
  359. /*
  360. * 修改用户的坐标
  361. * */
  362. public function change_longlat(){
  363. $longitude = input_post('longitude');
  364. $latitude = input_post('latitude');
  365. $cityname = input_post('cityname');
  366. if(empty($longitude) || empty($latitude) || empty($cityname)){
  367. $this->error();
  368. }
  369. $data = [
  370. 'longitude' => $longitude,
  371. 'latitude' => $latitude,
  372. 'cityname' => $cityname,
  373. ];
  374. Db::name('user')->where('id',$this->auth->id)->update($data);
  375. $this->success();
  376. }
  377. /**
  378. * 修改邮箱
  379. *
  380. * @ApiMethod (POST)
  381. * @param string $email 邮箱
  382. * @param string $captcha 验证码
  383. */
  384. /*public function changeemail()
  385. {
  386. $user = $this->auth->getUser();
  387. $email = $this->request->post('email');
  388. $captcha = $this->request->post('captcha');
  389. if (!$email || !$captcha) {
  390. $this->error(__('Invalid parameters'));
  391. }
  392. if (!Validate::is($email, "email")) {
  393. $this->error(__('Email is incorrect'));
  394. }
  395. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  396. $this->error(__('Email already exists'));
  397. }
  398. $result = Ems::check($email, $captcha, 'changeemail');
  399. if (!$result) {
  400. $this->error(__('Captcha is incorrect'));
  401. }
  402. $verification = $user->verification;
  403. $verification->email = 1;
  404. $user->verification = $verification;
  405. $user->email = $email;
  406. $user->save();
  407. Ems::flush($email, 'changeemail');
  408. $this->success();
  409. }*/
  410. /**
  411. * 修改手机号
  412. *
  413. * @ApiMethod (POST)
  414. * @param string $mobile 手机号
  415. * @param string $captcha 验证码
  416. */
  417. public function changemobile()
  418. {
  419. $user = $this->auth->getUser();
  420. $oldcaptcha = $this->request->request('oldcaptcha');
  421. $mobile = $this->request->request('mobile');
  422. $captcha = $this->request->request('captcha');
  423. if (!$oldcaptcha || !$mobile || !$captcha) {
  424. $this->error(__('Invalid parameters'));
  425. }
  426. if (!Validate::regex($mobile, "^1\d{10}$")) {
  427. $this->error(__('Mobile is incorrect'));
  428. }
  429. if($user->mobile == $mobile){
  430. $this->error('新手机号不能与旧手机号相同');
  431. }
  432. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  433. $this->error(__('Mobile already exist'));
  434. }
  435. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  436. if (!$result) {
  437. $this->error(__('Captcha is incorrect'));
  438. }
  439. $result = Sms::check($mobile, $captcha, 'changemobile');
  440. if (!$result) {
  441. $this->error(__('Captcha is incorrect'));
  442. }
  443. $verification = $user->verification;
  444. $verification->mobile = 1;
  445. $user->verification = $verification;
  446. $user->mobile = $mobile;
  447. $user->save();
  448. Sms::flush($user->mobile, 'changemobile');
  449. Sms::flush($mobile, 'changemobile');
  450. $this->success();
  451. }
  452. /**
  453. * 第三方登录
  454. *
  455. * @ApiMethod (POST)
  456. * @param string $platform 平台名称
  457. * @param string $code Code码
  458. */
  459. /*public function third()
  460. {
  461. $url = url('user/index');
  462. $platform = $this->request->post("platform");
  463. $code = $this->request->post("code");
  464. $config = get_addon_config('third');
  465. if (!$config || !isset($config[$platform])) {
  466. $this->error(__('Invalid parameters'));
  467. }
  468. $app = new \addons\third\library\Application($config);
  469. //通过code换access_token和绑定会员
  470. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  471. if ($result) {
  472. $loginret = \addons\third\library\Service::connect($platform, $result);
  473. if ($loginret) {
  474. $data = [
  475. 'userinfo' => $this->auth->getUserinfo(),
  476. 'thirdinfo' => $result
  477. ];
  478. $this->success(__('Logged in successful'), $data);
  479. }
  480. }
  481. $this->error(__('Operation failed'), $url);
  482. }*/
  483. /**
  484. * 重置密码
  485. *
  486. * @ApiMethod (POST)
  487. * @param string $mobile 手机号
  488. * @param string $newpassword 新密码
  489. * @param string $captcha 验证码
  490. */
  491. public function resetpwd()
  492. {
  493. //$type = input("type");
  494. $type = 'mobile';
  495. $mobile = input("mobile");
  496. $email = input("email");
  497. $newpassword = input("newpassword");
  498. $captcha = input("captcha");
  499. if (!$newpassword || !$captcha) {
  500. $this->error(__('Invalid parameters'));
  501. }
  502. if ($type == 'mobile') {
  503. if (!Validate::regex($mobile, "^1\d{10}$")) {
  504. $this->error(__('Mobile is incorrect'));
  505. }
  506. $user = \app\common\model\User::getByMobile($mobile);
  507. if (!$user) {
  508. $this->error(__('User not found'));
  509. }
  510. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  511. if (!$ret) {
  512. $this->error(__('Captcha is incorrect'));
  513. }
  514. Sms::flush($mobile, 'resetpwd');
  515. } else {
  516. if (!Validate::is($email, "email")) {
  517. $this->error(__('Email is incorrect'));
  518. }
  519. $user = \app\common\model\User::getByEmail($email);
  520. if (!$user) {
  521. $this->error(__('User not found'));
  522. }
  523. $ret = Ems::check($email, $captcha, 'resetpwd');
  524. if (!$ret) {
  525. $this->error(__('Captcha is incorrect'));
  526. }
  527. Ems::flush($email, 'resetpwd');
  528. }
  529. //模拟一次登录
  530. $this->auth->direct($user->id);
  531. $ret = $this->auth->changepwd($newpassword, '', true);
  532. if ($ret) {
  533. $this->success(__('Reset password successful'));
  534. } else {
  535. $this->error($this->auth->getError());
  536. }
  537. }
  538. /**
  539. * 修改密码
  540. *
  541. * @ApiMethod (POST)
  542. * @param string $newpassword 新密码
  543. * @param string $oldpassword 旧密码
  544. */
  545. public function changepwd(){
  546. $newpassword = input('newpassword');
  547. $oldpassword = input('oldpassword','');
  548. if (!$newpassword) {
  549. $this->error(__('Invalid parameters'));
  550. }
  551. if($this->auth->password && empty($oldpassword)){
  552. $this->error('原密码必填');
  553. }
  554. if(empty($this->auth->password)){
  555. $ret = $this->auth->changepwd($newpassword, '', true);
  556. }else{
  557. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  558. }
  559. if ($ret) {
  560. $this->success(__('Reset password successful'));
  561. } else {
  562. $this->error($this->auth->getError());
  563. }
  564. }
  565. /**
  566. * 记录当前登陆的设备ID,设备信息,IP等
  567. */
  568. public function changeDeviceIp()
  569. {
  570. // 接口防并发
  571. if (!$this->apiLimit(1, 5000)) {
  572. return ;
  573. }
  574. $user = $this->auth->getUser();
  575. $ip = request()->ip();
  576. $deviceId = $this->request->request('device_id','');
  577. $phoneModel = $this->request->request('phone_model','');
  578. $brand = $this->request->request('brand','');
  579. $apiVersion = $this->request->request('api_version','');
  580. $deviceOs = $this->request->request('device_os','');
  581. if ($ip !== $user->loginip){
  582. $update = [];
  583. $update['id'] = $user->id;
  584. $update['loginip'] = $ip;
  585. \app\common\model\User::update($update);
  586. }
  587. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  588. if (empty($userDeviceInfo)){
  589. $userDeviceInfo = new UserDeviceInfo();
  590. $userDeviceInfo->user_id = $user->u_id;
  591. }
  592. $userDeviceInfo->device_os = $deviceOs;
  593. $userDeviceInfo->device_id = $deviceId;
  594. $userDeviceInfo->phone_model = $phoneModel;
  595. $userDeviceInfo->brand = $brand;
  596. $userDeviceInfo->api_version = $apiVersion;
  597. $userDeviceInfo->save();
  598. //首页接口调用,这里不反回信息
  599. // $this->success("更新成功!");
  600. }
  601. }