User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. $this->auth->logout();
  210. $this->success(__('Logout successful'));
  211. }
  212. /**
  213. * 修改会员个人信息
  214. *
  215. * @ApiMethod (POST)
  216. * @param string $avatar 头像地址
  217. * @param string $username 用户名
  218. * @param string $nickname 昵称
  219. * @param string $bio 个人简介
  220. */
  221. public function profile()
  222. {
  223. $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'];
  224. $data = [];
  225. foreach($field_array as $key => $field){
  226. if(!input('?'.$field)){
  227. continue;
  228. }
  229. $newone = input($field);
  230. if($field == 'avatar'){
  231. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  232. }
  233. if($field == 'photo_images'){
  234. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  235. }
  236. $data[$field] = $newone;
  237. }
  238. if(isset($data['birthday'])){
  239. $data['birthday'] = strtotime($data['birthday']);
  240. }
  241. if(isset($data['avatar'])){
  242. $data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  243. }
  244. if(isset($data['hobby_ids'])){
  245. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  246. }
  247. if(isset($data['tag_ids'])){
  248. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  249. }
  250. //dump($data);
  251. if(empty($data)){
  252. $this->error('没有任何改变');
  253. }
  254. Db::name('user')->where('id',$this->auth->id)->update($data);
  255. $this->success();
  256. }
  257. public function set_status_switch(){
  258. }
  259. /*
  260. * 修改用户的坐标
  261. * */
  262. public function change_longlat(){
  263. $longitude = input_post('longitude');
  264. $latitude = input_post('latitude');
  265. $cityname = input_post('cityname');
  266. if(empty($longitude) || empty($latitude) || empty($cityname)){
  267. $this->error();
  268. }
  269. $data = [
  270. 'longitude' => $longitude,
  271. 'latitude' => $latitude,
  272. 'cityname' => $cityname,
  273. ];
  274. Db::name('user')->where('id',$this->auth->id)->update($data);
  275. $this->success();
  276. }
  277. /**
  278. * 修改邮箱
  279. *
  280. * @ApiMethod (POST)
  281. * @param string $email 邮箱
  282. * @param string $captcha 验证码
  283. */
  284. /*public function changeemail()
  285. {
  286. $user = $this->auth->getUser();
  287. $email = $this->request->post('email');
  288. $captcha = $this->request->post('captcha');
  289. if (!$email || !$captcha) {
  290. $this->error(__('Invalid parameters'));
  291. }
  292. if (!Validate::is($email, "email")) {
  293. $this->error(__('Email is incorrect'));
  294. }
  295. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  296. $this->error(__('Email already exists'));
  297. }
  298. $result = Ems::check($email, $captcha, 'changeemail');
  299. if (!$result) {
  300. $this->error(__('Captcha is incorrect'));
  301. }
  302. $verification = $user->verification;
  303. $verification->email = 1;
  304. $user->verification = $verification;
  305. $user->email = $email;
  306. $user->save();
  307. Ems::flush($email, 'changeemail');
  308. $this->success();
  309. }*/
  310. /**
  311. * 修改手机号
  312. *
  313. * @ApiMethod (POST)
  314. * @param string $mobile 手机号
  315. * @param string $captcha 验证码
  316. */
  317. public function changemobile()
  318. {
  319. $user = $this->auth->getUser();
  320. $oldcaptcha = $this->request->request('oldcaptcha');
  321. $mobile = $this->request->request('mobile');
  322. $captcha = $this->request->request('captcha');
  323. if (!$oldcaptcha || !$mobile || !$captcha) {
  324. $this->error(__('Invalid parameters'));
  325. }
  326. if (!Validate::regex($mobile, "^1\d{10}$")) {
  327. $this->error(__('Mobile is incorrect'));
  328. }
  329. if($user->mobile == $mobile){
  330. $this->error('新手机号不能与旧手机号相同');
  331. }
  332. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  333. $this->error(__('Mobile already exist'));
  334. }
  335. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  336. if (!$result) {
  337. $this->error(__('Captcha is incorrect'));
  338. }
  339. $result = Sms::check($mobile, $captcha, 'changemobile');
  340. if (!$result) {
  341. $this->error(__('Captcha is incorrect'));
  342. }
  343. $verification = $user->verification;
  344. $verification->mobile = 1;
  345. $user->verification = $verification;
  346. $user->mobile = $mobile;
  347. $user->save();
  348. Sms::flush($user->mobile, 'changemobile');
  349. Sms::flush($mobile, 'changemobile');
  350. $this->success();
  351. }
  352. /**
  353. * 第三方登录
  354. *
  355. * @ApiMethod (POST)
  356. * @param string $platform 平台名称
  357. * @param string $code Code码
  358. */
  359. /*public function third()
  360. {
  361. $url = url('user/index');
  362. $platform = $this->request->post("platform");
  363. $code = $this->request->post("code");
  364. $config = get_addon_config('third');
  365. if (!$config || !isset($config[$platform])) {
  366. $this->error(__('Invalid parameters'));
  367. }
  368. $app = new \addons\third\library\Application($config);
  369. //通过code换access_token和绑定会员
  370. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  371. if ($result) {
  372. $loginret = \addons\third\library\Service::connect($platform, $result);
  373. if ($loginret) {
  374. $data = [
  375. 'userinfo' => $this->auth->getUserinfo(),
  376. 'thirdinfo' => $result
  377. ];
  378. $this->success(__('Logged in successful'), $data);
  379. }
  380. }
  381. $this->error(__('Operation failed'), $url);
  382. }*/
  383. /**
  384. * 重置密码
  385. *
  386. * @ApiMethod (POST)
  387. * @param string $mobile 手机号
  388. * @param string $newpassword 新密码
  389. * @param string $captcha 验证码
  390. */
  391. public function resetpwd()
  392. {
  393. //$type = input("type");
  394. $type = 'mobile';
  395. $mobile = input("mobile");
  396. $email = input("email");
  397. $newpassword = input("newpassword");
  398. $captcha = input("captcha");
  399. if (!$newpassword || !$captcha) {
  400. $this->error(__('Invalid parameters'));
  401. }
  402. if ($type == 'mobile') {
  403. if (!Validate::regex($mobile, "^1\d{10}$")) {
  404. $this->error(__('Mobile is incorrect'));
  405. }
  406. $user = \app\common\model\User::getByMobile($mobile);
  407. if (!$user) {
  408. $this->error(__('User not found'));
  409. }
  410. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  411. if (!$ret) {
  412. $this->error(__('Captcha is incorrect'));
  413. }
  414. Sms::flush($mobile, 'resetpwd');
  415. } else {
  416. if (!Validate::is($email, "email")) {
  417. $this->error(__('Email is incorrect'));
  418. }
  419. $user = \app\common\model\User::getByEmail($email);
  420. if (!$user) {
  421. $this->error(__('User not found'));
  422. }
  423. $ret = Ems::check($email, $captcha, 'resetpwd');
  424. if (!$ret) {
  425. $this->error(__('Captcha is incorrect'));
  426. }
  427. Ems::flush($email, 'resetpwd');
  428. }
  429. //模拟一次登录
  430. $this->auth->direct($user->id);
  431. $ret = $this->auth->changepwd($newpassword, '', true);
  432. if ($ret) {
  433. $this->success(__('Reset password successful'));
  434. } else {
  435. $this->error($this->auth->getError());
  436. }
  437. }
  438. /**
  439. * 修改密码
  440. *
  441. * @ApiMethod (POST)
  442. * @param string $newpassword 新密码
  443. * @param string $oldpassword 旧密码
  444. */
  445. public function changepwd(){
  446. $newpassword = input('newpassword');
  447. $oldpassword = input('oldpassword','');
  448. if (!$newpassword) {
  449. $this->error(__('Invalid parameters'));
  450. }
  451. if($this->auth->password && empty($oldpassword)){
  452. $this->error('原密码必填');
  453. }
  454. if(empty($this->auth->password)){
  455. $ret = $this->auth->changepwd($newpassword, '', true);
  456. }else{
  457. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  458. }
  459. if ($ret) {
  460. $this->success(__('Reset password successful'));
  461. } else {
  462. $this->error($this->auth->getError());
  463. }
  464. }
  465. /**
  466. * 记录当前登陆的设备ID,设备信息,IP等
  467. */
  468. public function changeDeviceIp()
  469. {
  470. // 接口防并发
  471. if (!$this->apiLimit(1, 5000)) {
  472. return ;
  473. }
  474. $user = $this->auth->getUser();
  475. $ip = request()->ip();
  476. $deviceId = $this->request->request('device_id','');
  477. $phoneModel = $this->request->request('phone_model','');
  478. $brand = $this->request->request('brand','');
  479. $apiVersion = $this->request->request('api_version','');
  480. $deviceOs = $this->request->request('device_os','');
  481. if ($ip !== $user->loginip){
  482. $update = [];
  483. $update['id'] = $user->id;
  484. $update['loginip'] = $ip;
  485. \app\common\model\User::update($update);
  486. }
  487. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  488. if (empty($userDeviceInfo)){
  489. $userDeviceInfo = new UserDeviceInfo();
  490. $userDeviceInfo->user_id = $user->u_id;
  491. }
  492. $userDeviceInfo->device_os = $deviceOs;
  493. $userDeviceInfo->device_id = $deviceId;
  494. $userDeviceInfo->phone_model = $phoneModel;
  495. $userDeviceInfo->brand = $brand;
  496. $userDeviceInfo->api_version = $apiVersion;
  497. $userDeviceInfo->save();
  498. //首页接口调用,这里不反回信息
  499. // $this->success("更新成功!");
  500. }
  501. }