User.php 17 KB

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