User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. /**
  12. * 会员接口,登录,注册,修改资料等
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @ApiMethod (POST)
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->post('account');
  42. $password = $this->request->post('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = $this->userInfo('return');
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @ApiMethod (POST)
  58. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. */
  61. public function mobilelogin()
  62. {
  63. $mobile = $this->request->post('mobile');
  64. $captcha = $this->request->post('captcha');
  65. if (!$mobile || !$captcha) {
  66. $this->error(__('Invalid parameters'));
  67. }
  68. if (!Validate::regex($mobile, "^1\d{10}$")) {
  69. $this->error(__('Mobile is incorrect'));
  70. }
  71. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $user = \app\common\model\User::getByMobile($mobile);
  75. if ($user) {
  76. if ($user->status != 1) {
  77. $this->error(__('Account is locked'));
  78. }
  79. //如果已经有账号则直接登录
  80. $ret = $this->auth->direct($user->id);
  81. } else {
  82. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  83. }
  84. if ($ret) {
  85. Sms::flush($mobile, 'mobilelogin');
  86. $data = $this->userInfo('return');
  87. $this->success(__('Logged in successful'), $data);
  88. } else {
  89. $this->error($this->auth->getError());
  90. }
  91. }
  92. /**
  93. * 注册会员
  94. *
  95. * @ApiMethod (POST)
  96. * @param string $username 用户名
  97. * @param string $password 密码
  98. * @param string $email 邮箱
  99. * @param string $mobile 手机号
  100. * @param string $code 验证码
  101. */
  102. /*public function register()
  103. {
  104. $username = $this->request->post('username');
  105. $password = $this->request->post('password');
  106. $email = $this->request->post('email');
  107. $mobile = $this->request->post('mobile');
  108. $code = $this->request->post('code');
  109. if (!$username || !$password) {
  110. $this->error(__('Invalid parameters'));
  111. }
  112. if ($email && !Validate::is($email, "email")) {
  113. $this->error(__('Email is incorrect'));
  114. }
  115. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  116. $this->error(__('Mobile is incorrect'));
  117. }
  118. $ret = Sms::check($mobile, $code, 'register');
  119. if (!$ret) {
  120. $this->error(__('Captcha is incorrect'));
  121. }
  122. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  123. if ($ret) {
  124. $data = $this->userInfo('return');
  125. $this->success(__('Sign up successful'), $data);
  126. } else {
  127. $this->error($this->auth->getError());
  128. }
  129. }*/
  130. //用户详细资料
  131. public function userInfo($type = 1){
  132. $info = $this->auth->getUserinfo();
  133. if($type == 'return'){
  134. return $info;
  135. }
  136. $this->success(__('success'),$info);
  137. }
  138. //申请真人认证
  139. public function apply_real_confirm(){
  140. Db::name('user')->where('id',$this->auth->id)->update(['real_status'=>1]);
  141. $this->success();
  142. }
  143. //实名认证信息
  144. public function idcard_confirm_info(){
  145. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  146. $this->success('success',$check);
  147. }
  148. //申请实名认证
  149. public function apply_idcard_confirm(){
  150. $truename = input('truename','');
  151. $idcard = input('idcard','');
  152. $idcard_images = input('idcard_images','');
  153. $alipay_account = input('alipay_account','');
  154. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  155. $this->error('实名认证信息必填');
  156. }
  157. if($this->auth->idcard_status == 1){
  158. $this->error('您已经完成实名认证');
  159. }
  160. if($this->auth->idcard_status == 0){
  161. $this->error('您已经提交实名认证,请等待审核');
  162. }
  163. Db::startTrans();
  164. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  165. if(!empty($check)){
  166. if($check['status'] == 0){
  167. Db::rollback();
  168. $this->error('您已经提交实名认证,请等待审核');
  169. }
  170. if($check['status'] == 1){
  171. Db::rollback();
  172. $this->error('您已经完成实名认证');
  173. }
  174. }
  175. $data = [
  176. 'user_id' => $this->auth->id,
  177. 'truename' => $truename,
  178. 'idcard' => $idcard,
  179. 'idcard_images' => $idcard_images,
  180. 'alipay_account' => $alipay_account,
  181. 'status' => 0,
  182. 'createtime' => time(),
  183. 'updatetime' => time(),
  184. ];
  185. //更新
  186. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  187. if(!empty($check)){
  188. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  189. }else{
  190. $rs = Db::name('user_idconfirm')->insertGetId($data);
  191. }
  192. if(!$rs || !$update_rs){
  193. Db::rollback();
  194. $this->error('提交失败');
  195. }
  196. Db::commit();
  197. $this->success('提交成功,请等待审核');
  198. }
  199. /**
  200. * 退出登录
  201. * @ApiMethod (POST)
  202. */
  203. public function logout()
  204. {
  205. if (!$this->request->isPost()) {
  206. $this->error(__('Invalid parameters'));
  207. }
  208. $this->auth->logout();
  209. $this->success(__('Logout successful'));
  210. }
  211. /**
  212. * 修改会员个人信息
  213. *
  214. * @ApiMethod (POST)
  215. * @param string $avatar 头像地址
  216. * @param string $username 用户名
  217. * @param string $nickname 昵称
  218. * @param string $bio 个人简介
  219. */
  220. public function profile()
  221. {
  222. $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'];
  223. $data = [];
  224. foreach($field_array as $key => $field){
  225. $newone = input_post($field);
  226. if($field == 'avatar'){
  227. $newone = input_post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  228. }
  229. if($field == 'photo_images'){
  230. $newone = input_post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  231. }
  232. if(!empty($newone)){
  233. $data[$field] = $newone;
  234. }
  235. }
  236. if(isset($data['birthday'])){
  237. $data['birthday'] = strtotime($data['birthday']);
  238. }
  239. if(isset($data['avatar'])){
  240. $data['real_status'] = -1; //或许应该改成0
  241. }
  242. if(isset($data['hobby_ids'])){
  243. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids'])); //或许应该改成0
  244. }
  245. if(isset($data['tag_ids'])){
  246. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids'])); //或许应该改成0
  247. }
  248. //dump($data);
  249. if(empty($data)){
  250. $this->error('没有任何改变');
  251. }
  252. Db::name('user')->where('id',$this->auth->id)->update($data);
  253. $this->success();
  254. }
  255. public function set_status_switch(){
  256. }
  257. /*
  258. * 修改用户的坐标
  259. * */
  260. public function change_longlat(){
  261. $longitude = input_post('longitude');
  262. $latitude = input_post('latitude');
  263. $cityname = input_post('cityname');
  264. if(empty($longitude) || empty($latitude) || empty($cityname)){
  265. $this->error();
  266. }
  267. $data = [
  268. 'longitude' => $longitude,
  269. 'latitude' => $latitude,
  270. 'cityname' => $cityname,
  271. ];
  272. Db::name('user')->where('id',$this->auth->id)->update($data);
  273. $this->success();
  274. }
  275. /**
  276. * 修改邮箱
  277. *
  278. * @ApiMethod (POST)
  279. * @param string $email 邮箱
  280. * @param string $captcha 验证码
  281. */
  282. /*public function changeemail()
  283. {
  284. $user = $this->auth->getUser();
  285. $email = $this->request->post('email');
  286. $captcha = $this->request->post('captcha');
  287. if (!$email || !$captcha) {
  288. $this->error(__('Invalid parameters'));
  289. }
  290. if (!Validate::is($email, "email")) {
  291. $this->error(__('Email is incorrect'));
  292. }
  293. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  294. $this->error(__('Email already exists'));
  295. }
  296. $result = Ems::check($email, $captcha, 'changeemail');
  297. if (!$result) {
  298. $this->error(__('Captcha is incorrect'));
  299. }
  300. $verification = $user->verification;
  301. $verification->email = 1;
  302. $user->verification = $verification;
  303. $user->email = $email;
  304. $user->save();
  305. Ems::flush($email, 'changeemail');
  306. $this->success();
  307. }*/
  308. /**
  309. * 修改手机号
  310. *
  311. * @ApiMethod (POST)
  312. * @param string $mobile 手机号
  313. * @param string $captcha 验证码
  314. */
  315. public function changemobile()
  316. {
  317. $user = $this->auth->getUser();
  318. $oldcaptcha = $this->request->request('oldcaptcha');
  319. $mobile = $this->request->request('mobile');
  320. $captcha = $this->request->request('captcha');
  321. if (!$oldcaptcha || !$mobile || !$captcha) {
  322. $this->error(__('Invalid parameters'));
  323. }
  324. if (!Validate::regex($mobile, "^1\d{10}$")) {
  325. $this->error(__('Mobile is incorrect'));
  326. }
  327. if($user->mobile == $mobile){
  328. $this->error('新手机号不能与旧手机号相同');
  329. }
  330. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  331. $this->error(__('Mobile already exist'));
  332. }
  333. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  334. if (!$result) {
  335. $this->error(__('Captcha is incorrect'));
  336. }
  337. $result = Sms::check($mobile, $captcha, 'changemobile');
  338. if (!$result) {
  339. $this->error(__('Captcha is incorrect'));
  340. }
  341. $verification = $user->verification;
  342. $verification->mobile = 1;
  343. $user->verification = $verification;
  344. $user->mobile = $mobile;
  345. $user->save();
  346. Sms::flush($user->mobile, 'changemobile');
  347. Sms::flush($mobile, 'changemobile');
  348. $this->success();
  349. }
  350. /**
  351. * 第三方登录
  352. *
  353. * @ApiMethod (POST)
  354. * @param string $platform 平台名称
  355. * @param string $code Code码
  356. */
  357. /*public function third()
  358. {
  359. $url = url('user/index');
  360. $platform = $this->request->post("platform");
  361. $code = $this->request->post("code");
  362. $config = get_addon_config('third');
  363. if (!$config || !isset($config[$platform])) {
  364. $this->error(__('Invalid parameters'));
  365. }
  366. $app = new \addons\third\library\Application($config);
  367. //通过code换access_token和绑定会员
  368. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  369. if ($result) {
  370. $loginret = \addons\third\library\Service::connect($platform, $result);
  371. if ($loginret) {
  372. $data = [
  373. 'userinfo' => $this->auth->getUserinfo(),
  374. 'thirdinfo' => $result
  375. ];
  376. $this->success(__('Logged in successful'), $data);
  377. }
  378. }
  379. $this->error(__('Operation failed'), $url);
  380. }*/
  381. /**
  382. * 重置密码
  383. *
  384. * @ApiMethod (POST)
  385. * @param string $mobile 手机号
  386. * @param string $newpassword 新密码
  387. * @param string $captcha 验证码
  388. */
  389. public function resetpwd()
  390. {
  391. //$type = $this->request->post("type");
  392. $type = 'mobile';
  393. $mobile = $this->request->post("mobile");
  394. $email = $this->request->post("email");
  395. $newpassword = $this->request->post("newpassword");
  396. $captcha = $this->request->post("captcha");
  397. if (!$newpassword || !$captcha) {
  398. $this->error(__('Invalid parameters'));
  399. }
  400. if ($type == 'mobile') {
  401. if (!Validate::regex($mobile, "^1\d{10}$")) {
  402. $this->error(__('Mobile is incorrect'));
  403. }
  404. $user = \app\common\model\User::getByMobile($mobile);
  405. if (!$user) {
  406. $this->error(__('User not found'));
  407. }
  408. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  409. if (!$ret) {
  410. $this->error(__('Captcha is incorrect'));
  411. }
  412. Sms::flush($mobile, 'resetpwd');
  413. } else {
  414. if (!Validate::is($email, "email")) {
  415. $this->error(__('Email is incorrect'));
  416. }
  417. $user = \app\common\model\User::getByEmail($email);
  418. if (!$user) {
  419. $this->error(__('User not found'));
  420. }
  421. $ret = Ems::check($email, $captcha, 'resetpwd');
  422. if (!$ret) {
  423. $this->error(__('Captcha is incorrect'));
  424. }
  425. Ems::flush($email, 'resetpwd');
  426. }
  427. //模拟一次登录
  428. $this->auth->direct($user->id);
  429. $ret = $this->auth->changepwd($newpassword, '', true);
  430. if ($ret) {
  431. $this->success(__('Reset password successful'));
  432. } else {
  433. $this->error($this->auth->getError());
  434. }
  435. }
  436. /**
  437. * 修改密码
  438. *
  439. * @ApiMethod (POST)
  440. * @param string $newpassword 新密码
  441. * @param string $oldpassword 旧密码
  442. */
  443. public function changepwd(){
  444. $newpassword = input('post.newpassword');
  445. $oldpassword = input('post.oldpassword','');
  446. if (!$newpassword) {
  447. $this->error(__('Invalid parameters'));
  448. }
  449. if($this->auth->password && empty($oldpassword)){
  450. $this->error('原密码必填');
  451. }
  452. if(empty($this->auth->password)){
  453. $ret = $this->auth->changepwd($newpassword, '', true);
  454. }else{
  455. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  456. }
  457. if ($ret) {
  458. $this->success(__('Reset password successful'));
  459. } else {
  460. $this->error($this->auth->getError());
  461. }
  462. }
  463. }