User.php 17 KB

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