User.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. use app\common\library\Wechat;
  15. use app\common\library\Tenim;
  16. /**
  17. * 会员接口,登录,注册,修改资料等
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['emaillogin','emailregister','mobilelogin','resetpwd'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 邮箱登录
  29. *
  30. * @ApiMethod (POST)
  31. * @param string $account 账号
  32. * @param string $password 密码
  33. */
  34. public function emaillogin()
  35. {
  36. $account = input('account');
  37. $password = input('password');
  38. if (!$account || !$password) {
  39. $this->error(__('Invalid parameters'));
  40. }
  41. if (!Validate::is($account, 'email')) {
  42. $this->error(__('Email is incorrect'));
  43. }
  44. $ret = $this->auth->login($account, $password);
  45. if ($ret) {
  46. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  47. } else {
  48. $this->error($this->auth->getError());
  49. }
  50. }
  51. //邮箱注册
  52. public function emailregister()
  53. {
  54. $account = input('account');
  55. $captcha = input('captcha');
  56. $password = input('password');
  57. if (!$account || !$captcha || !$password) {
  58. $this->error(__('Invalid parameters'));
  59. }
  60. if (!Validate::is($account, 'email')) {
  61. $this->error(__('Email is incorrect'));
  62. }
  63. $ret = Ems::check($account, $captcha, 'register');
  64. if (!$ret) {
  65. $this->error(__('Captcha is incorrect'));
  66. }
  67. $extend = [
  68. 'register_from' => input('register_from',''),
  69. 'gender' => -1
  70. ];
  71. $ret = $this->auth->register('',$password,$account,'', $extend);
  72. if ($ret) {
  73. Ems::flush($account);
  74. $this->success('注册成功', $this->auth->getUserinfo_simple());
  75. } else {
  76. $this->error($this->auth->getError());
  77. }
  78. }
  79. /**
  80. * 手机验证码登录 + 注册
  81. *
  82. * @ApiMethod (POST)
  83. * @param string $mobile 手机号
  84. * @param string $captcha 验证码
  85. */
  86. public function mobilelogin()
  87. {
  88. $mobile = input('mobile');
  89. $captcha = input('captcha');
  90. if (!$mobile || !$captcha) {
  91. $this->error(__('Invalid parameters'));
  92. }
  93. if (!Validate::regex($mobile, "^1\d{10}$")) {
  94. $this->error(__('Mobile is incorrect'));
  95. }
  96. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  97. $this->error(__('Captcha is incorrect'));
  98. }
  99. $user = \app\common\model\User::getByMobile($mobile);
  100. if ($user) {
  101. if ($user->status == -1) {
  102. $this->error('账户已注销');
  103. }
  104. if (!in_array($user->status,[1,2])) {
  105. $this->error(__('Account is locked'));
  106. }
  107. if ($user->frozentime > time()) {
  108. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  109. }
  110. //如果已经有账号则直接登录
  111. $ret = $this->auth->direct($user->id);
  112. } else {
  113. $extend = [
  114. 'register_from' => input('register_from',''),
  115. 'gender' => -1
  116. ];
  117. $ret = $this->auth->register('', '', '', $mobile, $extend);
  118. }
  119. if ($ret) {
  120. Sms::flush($mobile, 'mobilelogin');
  121. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  122. } else {
  123. $this->error($this->auth->getError());
  124. }
  125. }
  126. //注册设置性别
  127. public function setgender() {
  128. $user_id = $this->auth->id;
  129. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  130. if (!in_array($gender, [1, 0])) {
  131. $this->error('性别错误');
  132. }
  133. $edit_data['gender'] = $gender;
  134. $edit_data['avatar'] = $gender == 1 ? config('avatar_boy') : config('avatar_girl'); //头像
  135. $rs = Db::name('user')->where(['id' => $user_id])->update($edit_data);
  136. if ($rs === false) {
  137. $this->error('您的网络开小差啦~');
  138. }
  139. $data['gender'] = $edit_data['gender'];
  140. $data['avatar'] = $edit_data['avatar'];
  141. //设置头像
  142. $tenim = new Tenim();
  143. $tenim->useredit(''.$user_id,'',localpath_to_netpath($data['avatar']));
  144. $this->success('success', $data);
  145. }
  146. //刷新随机昵称
  147. public function get_rand_nick_name(){
  148. $nickname = $this->auth->get_rand_nick_name();
  149. $this->success('success', $nickname);
  150. }
  151. //注册完善资料
  152. public function perfect_info() {
  153. $avatar = input('avatar', '', 'trim'); //头像
  154. $nickname = input('nickname', '', 'trim'); //昵称
  155. $birthday = input('birthday', '', 'strtotime'); //生日
  156. $hometown_cityid = input('hometown_cityid', '', 'trim'); //城市id
  157. $bio = input('bio', '', 'trim'); //个性签名
  158. $hobby = input('hobby', '', 'trim'); //爱好
  159. $marital = input('marital', '', 'trim'); //婚姻
  160. $introcode = input('introcode', '', 'trim'); //邀请码
  161. $data = [];
  162. if ($avatar) {
  163. $data['avatar'] = $avatar;
  164. }
  165. if ($nickname !== '') {
  166. if (iconv_strlen($nickname, 'utf-8') > 10) {
  167. $this->error('昵称最多10个字~');
  168. }
  169. $data['nickname'] = $nickname;
  170. }
  171. if ($birthday) {
  172. $data['birthday'] = $birthday;
  173. }
  174. if ($hometown_cityid) {
  175. $count = Db::name('area')->where('id', $hometown_cityid)->count('id');
  176. if (!$count) {
  177. $this->error('城市不存在');
  178. }
  179. $data['hometown_cityid'] = $hometown_cityid;
  180. }
  181. if ($bio) {
  182. $data['bio'] = $bio;
  183. }
  184. if ($hobby) {
  185. $data['hobby'] = $hobby;
  186. }
  187. if ($marital) {
  188. $data['marital'] = $marital;
  189. }
  190. if ($introcode && !$this->auth->intro_uid) {
  191. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $introcode)->find();
  192. if ($intro_user && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id) {
  193. $data['intro_uid'] = $intro_user['id'];
  194. $data['invite_time'] = time();
  195. }
  196. }
  197. //开启事务
  198. Db::startTrans();
  199. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  200. if($update_rs === false){
  201. Db::rollback();
  202. $this->error('修改失败');
  203. }
  204. //给上级发放钻石
  205. if(isset($data['intro_uid'])){
  206. $intro_gold = config('site.new_user_intro_gold');
  207. if($intro_gold > 0){
  208. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'money',$intro_gold,34,'邀请'.$this->auth->username.'注册奖励');
  209. if($rs_wallet['status'] === false){
  210. Db::rollback();
  211. $this->error('邀请新人奖励赠送失败');
  212. }
  213. }
  214. }
  215. //上传头像加5金币
  216. if(isset($data['avatar'])){
  217. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  218. if($task_rs === false){
  219. Db::rollback();
  220. $this->error('完成任务赠送奖励失败');
  221. }
  222. }
  223. if (isset($data['birthday'])) {
  224. //完成设置生日 +5金币
  225. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  226. if($task_rs === false){
  227. Db::rollback();
  228. $this->error('完成任务赠送奖励失败');
  229. }
  230. }
  231. Db::commit();
  232. $this->success('修改成功');
  233. }
  234. //用户详细资料
  235. public function userInfo($type = 1){
  236. $info = $this->auth->getUserinfo();
  237. $info['is_vip'] = $this->is_vip($info['vip_endtime'],$info['vip_level']);
  238. if($type == 'return'){
  239. return $info;
  240. }
  241. $this->success(__('success'),$info);
  242. }
  243. /**
  244. * 修改会员个人信息
  245. *
  246. * @ApiMethod (POST)
  247. * @param string $avatar 头像地址
  248. * @param string $username 用户名
  249. * @param string $nickname 昵称
  250. * @param string $bio 个人简介
  251. */
  252. public function profile()
  253. {
  254. $field_array = ['nickname','introcode',/*'gender',*/'birthday','height','weight','bio','audio_bio','avatar','photo_images','education','hobby','job','marital','tag','wages','hometown_cityid','hide_is_finishinfo',/*'wechat_account',*/'character','constellation','stature','is_appointment', 'greet_voice', 'greet_chat', 'is_cohabit', 'live', 'is_house', 'car', 'chest', 'waist'];
  255. $data = [];
  256. foreach($field_array as $key => $field){
  257. if(!input('?'.$field)){
  258. continue;
  259. }
  260. $newone = input($field);
  261. if($field == 'avatar'){
  262. // if ($this->auth->real_status == 1) {
  263. //$this->error('您已经真人认证不能修改头像~');
  264. // die;
  265. // }
  266. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  267. }
  268. if($field == 'photo_images'){
  269. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  270. }
  271. $data[$field] = $newone;
  272. }
  273. if(isset($data['birthday'])){
  274. $data['birthday'] = strtotime($data['birthday']);
  275. }
  276. if(isset($data['avatar'])){
  277. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  278. }
  279. if(isset($data['introcode'])){
  280. if ($this->auth->intro_uid != 0) {
  281. $this->error('邀请人不可修改~');
  282. }
  283. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $data['introcode'])->find();
  284. if(!$intro_user){
  285. $this->error('不存在的邀请人');
  286. }
  287. if ($intro_user['id'] == $this->auth->id) {
  288. $this->error('不能填写自己邀请码');
  289. }
  290. if ($intro_user['intro_uid'] == $this->auth->id) {
  291. $this->error('不能填写下级邀请码');
  292. }
  293. unset($data['introcode']);//别人的邀请码,不能改了自己的
  294. $data['intro_uid'] = $intro_user['id'];
  295. $data['invite_time'] = time();
  296. }
  297. //dump($data);
  298. if(empty($data)){
  299. $this->error('没有任何改变');
  300. }
  301. Db::startTrans();
  302. $userData = Db::name('user')->field('avatar,gender,status')->where('id',$this->auth->id)->find();
  303. if (isset($data['avatar']) && !empty($data['avatar']) && $userData['status'] == 2) {//隐藏
  304. $boyAvatar = config('avatar_boy');
  305. $girlAvatar = config('avatar_girl');
  306. if ($userData['gender'] == 1 && $data['avatar'] != $boyAvatar) {
  307. $data['status'] = 1;//更新为正常
  308. } elseif ($userData['gender'] == 0 && $data['avatar'] != $girlAvatar) {
  309. $data['status'] = 1;//更新为正常
  310. }
  311. }
  312. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  313. if($update_rs === false){
  314. Db::rollback();
  315. $this->error('修改资料失败');
  316. }
  317. //给上级发放钻石
  318. if(isset($data['intro_uid'])){
  319. $intro_gold = config('site.new_user_intro_gold');
  320. if($intro_gold > 0){
  321. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'money',$intro_gold,34,'邀请'.$this->auth->username.'注册奖励');
  322. if($rs_wallet['status'] === false){
  323. Db::rollback();
  324. $this->error('邀请新人奖励赠送失败');
  325. }
  326. }
  327. }
  328. //tag任务赠送金币
  329. //上传头像加5金币
  330. if(isset($data['avatar'])){
  331. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  332. if($task_rs === false){
  333. Db::rollback();
  334. $this->error('完成任务赠送奖励失败');
  335. }
  336. }
  337. //上传生日 +5金币
  338. if (isset($data['birthday'])) {
  339. //完成设置生日 +5金币
  340. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  341. if($task_rs === false){
  342. Db::rollback();
  343. $this->error('完成任务赠送奖励失败');
  344. }
  345. }
  346. //上传个性签名 5金币
  347. if(isset($data['bio'])){
  348. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,2);
  349. if($task_rs === false){
  350. Db::rollback();
  351. $this->error('完成任务赠送奖励失败');
  352. }
  353. }
  354. //上传本人语音介绍 10金币
  355. if(isset($data['audio_bio'])){
  356. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,21);
  357. if($task_rs === false){
  358. Db::rollback();
  359. $this->error('完成任务赠送奖励失败');
  360. }
  361. }
  362. //上传本人五张照片 10金币
  363. if(isset($data['photo_images'])){
  364. $photo_images_num = count(explode(',',$data['photo_images'])) + count(explode(',',$this->auth->photo_images));
  365. if ($photo_images_num >= 5) {
  366. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id, 5);
  367. if ($task_rs === false) {
  368. Db::rollback();
  369. $this->error('完成任务赠送奖励失败');
  370. }
  371. }
  372. }
  373. Db::commit();
  374. $this->success();
  375. }
  376. /**
  377. * 退出登录
  378. * @ApiMethod (POST)
  379. */
  380. public function logout()
  381. {
  382. if (!$this->request->isPost()) {
  383. $this->error(__('Invalid parameters'));
  384. }
  385. //退出im
  386. $tenIm = new Tenim();
  387. $tenIm->loginoutim($this->auth->id);
  388. //修改用户活跃0
  389. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  390. $this->auth->logout();
  391. $this->success(__('Logout successful'));
  392. }
  393. /**
  394. * 重置密码
  395. *
  396. * @ApiMethod (POST)
  397. * @param string $mobile 手机号
  398. * @param string $newpassword 新密码
  399. * @param string $captcha 验证码
  400. */
  401. public function resetpwd()
  402. {
  403. //$type = input("type");
  404. $type = 'email';
  405. $mobile = input("mobile");
  406. $email = input("email");
  407. $newpassword = input("newpassword");
  408. $captcha = input("captcha");
  409. if (!$newpassword || !$captcha) {
  410. $this->error(__('Invalid parameters'));
  411. }
  412. if ($type == 'mobile') {
  413. if (!Validate::regex($mobile, "^1\d{10}$")) {
  414. $this->error(__('Mobile is incorrect'));
  415. }
  416. $user = \app\common\model\User::getByMobile($mobile);
  417. if (!$user) {
  418. $this->error(__('User not found'));
  419. }
  420. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  421. if (!$ret) {
  422. $this->error(__('Captcha is incorrect'));
  423. }
  424. Sms::flush($mobile, 'resetpwd');
  425. } else {
  426. if (!Validate::is($email, "email")) {
  427. $this->error(__('Email is incorrect'));
  428. }
  429. $user = \app\common\model\User::getByEmail($email);
  430. if (!$user) {
  431. $this->error(__('User not found'));
  432. }
  433. $ret = Ems::check($email, $captcha, 'resetpwd');
  434. if (!$ret) {
  435. $this->error(__('Captcha is incorrect'));
  436. }
  437. Ems::flush($email, 'resetpwd');
  438. }
  439. //模拟一次登录
  440. $this->auth->direct($user->id);
  441. $ret = $this->auth->changepwd($newpassword, '', true);
  442. if ($ret) {
  443. $this->success(__('Reset password successful'));
  444. } else {
  445. $this->error($this->auth->getError());
  446. }
  447. }
  448. /**
  449. * 微信注册来的,绑定手机号
  450. *
  451. * @ApiMethod (POST)
  452. * @param string $mobile 手机号
  453. * @param string $captcha 验证码
  454. */
  455. public function bindmobile()
  456. {
  457. $user = $this->auth->getUser();
  458. $mobile = $this->request->request('mobile');
  459. $captcha = $this->request->request('captcha');
  460. if(!empty($this->auth->mobile)){
  461. $this->error('已经绑定了手机号');
  462. }
  463. if (!$mobile || !$captcha) {
  464. $this->error(__('Invalid parameters'));
  465. }
  466. if (!Validate::regex($mobile, "^1\d{10}$")) {
  467. $this->error(__('Mobile is incorrect'));
  468. }
  469. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  470. $this->error('该手机号已被其他用户绑定');
  471. }
  472. $result = Sms::check($mobile, $captcha, 'changemobile');
  473. if (!$result) {
  474. $this->error(__('Captcha is incorrect'));
  475. }
  476. $user->mobile = $mobile;
  477. $user->save();
  478. Sms::flush($mobile, 'changemobile');
  479. //手机号奖励
  480. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,18);
  481. if($task_rs === false){
  482. Db::rollback();
  483. return false;
  484. }
  485. $this->success('success',$this->userInfo('return'));
  486. }
  487. //注销配置
  488. public function cancleconfig(){
  489. $rs = [
  490. 'rule' => config('site.user_cancle_rules'),
  491. 'reason' => [
  492. '想换个新账号',
  493. '没有聊得来的',
  494. '不想玩了',
  495. '其他原因',
  496. ],
  497. ];
  498. $this->success(1,$rs);
  499. }
  500. //假注销
  501. public function cancleUser(){
  502. if (!$this->request->isPost()) {
  503. $this->error(__('Invalid parameters'));
  504. }
  505. //退出im
  506. $tenIm = new Tenim();
  507. $tenIm->loginoutim($this->auth->id);
  508. $data = [
  509. 'status' => -1,
  510. 'mobile' => 'close_'.$this->auth->mobile,
  511. 'wechat_openid' => 'close_'.$this->auth->wechat_openid,
  512. ];
  513. Db::name('user')->where('id',$this->auth->id)->update($data);
  514. $this->auth->logout();
  515. $this->success('注销成功');
  516. }
  517. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  518. /**
  519. * 手机验证码验证
  520. *
  521. * @ApiMethod (POST)
  522. * @param string $mobile 手机号
  523. * @param string $captcha 验证码
  524. */
  525. /*public function mobilecheck()
  526. {
  527. $mobile = input('mobile');
  528. $captcha = input('captcha');
  529. if (!$mobile || !$captcha) {
  530. $this->error(__('Invalid parameters'));
  531. }
  532. if (!Validate::regex($mobile, "^1\d{10}$")) {
  533. $this->error(__('Mobile is incorrect'));
  534. }
  535. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  536. $this->error(__('Captcha is incorrect'));
  537. }
  538. $this->success('验证通过');
  539. }*/
  540. /**
  541. * 手机验证码注册
  542. *
  543. * @ApiMethod (POST)
  544. * @param string $mobile 手机号
  545. * @param string $captcha 验证码
  546. * @param string $gender 性别:1=男,0=女
  547. */
  548. /*public function mobileregister()
  549. {
  550. $mobile = input('mobile');
  551. $captcha = input('captcha');
  552. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  553. if (!$mobile || !$captcha) {
  554. $this->error(__('Invalid parameters'));
  555. }
  556. if (!Validate::regex($mobile, "^1\d{10}$")) {
  557. $this->error(__('Mobile is incorrect'));
  558. }
  559. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  560. $this->error(__('Captcha is incorrect'));
  561. }
  562. if (!in_array($gender, [1, 0])) {
  563. $this->error('性别错误');
  564. }
  565. $user = \app\common\model\User::getByMobile($mobile);
  566. if ($user) {
  567. $this->error('账号已经存在,请直接登录');
  568. if ($user->status == -1) {
  569. $this->error('账户已注销');
  570. }
  571. if (!in_array($user->status,[1,2])) {
  572. $this->error(__('Account is locked'));
  573. }
  574. //如果已经有账号则直接登录
  575. $ret = $this->auth->direct($user->id);
  576. } else {
  577. $extend = [
  578. 'register_from' => input('register_from',''),
  579. 'gender' => $gender
  580. ];
  581. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  582. }
  583. if ($ret) {
  584. Sms::flush($mobile, 'mobilelogin');
  585. $data = $this->userInfo('return');
  586. $this->success(__('Logged in successful'), $data);
  587. } else {
  588. $this->error($this->auth->getError());
  589. }
  590. }*/
  591. /**
  592. * 运营商一键登录
  593. */
  594. /*public function onLogin()
  595. {
  596. $accessToken = input('accessToken');// 运营商预取号获取到的token
  597. $token = input('tokenT');// 易盾返回的token
  598. if (!$accessToken || !$token) {
  599. $this->error("参数获取失败!");
  600. }
  601. $params = array(
  602. // 运营商预取号获取到的token
  603. "accessToken" => $accessToken,
  604. // 易盾返回的token
  605. "token" => $token
  606. );
  607. // 获取密钥配置
  608. $configInfo = config("onLogin");
  609. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  610. $onret = $onlogin->check($params);
  611. // $ret = [];
  612. // $ret["code"] = 200;
  613. // $ret["msg"] = "ok";
  614. // $ret["data"] = [
  615. // "phone" => "17574504021",
  616. // "resultCode" => 0
  617. // ];
  618. if ($onret["code"] == 200) {
  619. $mobile = $onret["data"]["phone"];
  620. if (empty($mobile)) {
  621. // 取号失败,建议进行二次验证,例如短信验证码
  622. $this->error("取号登录失败,请用验证码方式登录!");
  623. } else {
  624. // 取号成功, 执行登录等流程
  625. // 用户登录逻辑 === 开始
  626. $user = \app\common\model\User::getByMobile($mobile);
  627. if ($user) {
  628. if (!in_array($user->status,[1,2])) {
  629. $this->error(__('Account is locked'));
  630. }
  631. if ($user->frozentime > time()) {
  632. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  633. }
  634. //如果已经有账号则直接登录
  635. $ret = $this->auth->direct($user->id);
  636. $is_register = 0;
  637. } else {
  638. $extend = [
  639. 'register_from' => input('register_from',''),
  640. 'gender' => -1
  641. ];
  642. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  643. $is_register = 1;
  644. }
  645. if ($ret) {
  646. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  647. } else {
  648. $this->error($this->auth->getError());
  649. }
  650. // 用户登录逻辑 === 结束
  651. }
  652. } else {
  653. $this->error("登录失败,请用验证码方式登录!");
  654. }
  655. }*/
  656. /**
  657. * 运营商一键登录注册
  658. */
  659. /*public function onregister()
  660. {
  661. $accessToken = input('accessToken');// 运营商预取号获取到的token
  662. $token = input('tokenT');// 易盾返回的token
  663. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  664. if (!$accessToken || !$token) {
  665. $this->error("参数获取失败!");
  666. }
  667. if (!in_array($gender, [1, 0])) {
  668. $this->error('性别错误');
  669. }
  670. $params = array(
  671. // 运营商预取号获取到的token
  672. "accessToken" => $accessToken,
  673. // 易盾返回的token
  674. "token" => $token
  675. );
  676. // 获取密钥配置
  677. $configInfo = config("onLogin");
  678. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  679. $onret = $onlogin->check($params);
  680. // $ret = [];
  681. // $ret["code"] = 200;
  682. // $ret["msg"] = "ok";
  683. // $ret["data"] = [
  684. // "phone" => "17574504021",
  685. // "resultCode" => 0
  686. // ];
  687. if ($onret["code"] == 200) {
  688. $mobile = $onret["data"]["phone"];
  689. if (empty($mobile)) {
  690. // 取号失败,建议进行二次验证,例如短信验证码
  691. $this->error("取号登录失败,请用验证码方式登录!");
  692. } else {
  693. // 取号成功, 执行登录等流程
  694. // 用户登录逻辑 === 开始
  695. $user = \app\common\model\User::getByMobile($mobile);
  696. if ($user) {
  697. $this->error('账号已经存在,请直接登录');
  698. if (!in_array($user->status,[1,2])) {
  699. $this->error(__('Account is locked'));
  700. }
  701. //如果已经有账号则直接登录
  702. $ret = $this->auth->direct($user->id);
  703. $is_register = 0;
  704. } else {
  705. $extend = [
  706. 'register_from' => input('register_from',''),
  707. 'gender' => $gender
  708. ];
  709. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  710. $is_register = 1;
  711. }
  712. //结果
  713. if ($ret) {
  714. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  715. } else {
  716. $this->error($this->auth->getError());
  717. }
  718. // 用户登录逻辑 === 结束
  719. }
  720. } else {
  721. $this->error("登录失败,请用验证码方式登录!");
  722. }
  723. }*/
  724. //微信登录,预先假注册
  725. /*public function wechatlogin(){
  726. $code = input('code','');
  727. if(!$code){
  728. $this->error(__('Invalid parameters'));
  729. }
  730. //微信
  731. $wechat = new Wechat();
  732. $wxuserinfo = $wechat->getAccessToken($code);
  733. if(!$wxuserinfo){
  734. $this->error('openid获取失败');
  735. }
  736. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  737. $this->error('openid获取失败');
  738. }
  739. $openid = $wxuserinfo['openid'];
  740. //检查用户
  741. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  742. if ($user) {
  743. if ($user['status'] == -1) {
  744. $this->error('账户已注销');
  745. }
  746. if ($user['status'] != 1) {
  747. $this->error(__('Account is locked'));
  748. }
  749. //如果已经有账号则直接登录
  750. $ret = $this->auth->direct($user['id']);
  751. if ($ret) {
  752. $userInfo = $this->auth->getUserinfo();
  753. $userInfo['is_register'] = 0;
  754. $userInfo['code'] = $code;
  755. $this->success(__('Logged in successful'), $userInfo);
  756. } else {
  757. $this->error($this->auth->getError());
  758. }
  759. } else {
  760. //记录code和openid,绑定手机号的时候更新openid
  761. $wechatCodeData = [
  762. 'code' => $code,
  763. 'openid' => $openid,
  764. 'createtime' => time(),
  765. ];
  766. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  767. if (empty($wechatCode)) {
  768. Db::name('wechat_code')->insertGetId($wechatCodeData);
  769. } else {
  770. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  771. }
  772. //直接返回
  773. $userInfo = [];
  774. $userInfo['is_register'] = 1;
  775. $userInfo['code'] = $code;
  776. $this->success('获取信息成功', $userInfo);
  777. }
  778. }*/
  779. /**
  780. * 微信注册来的,绑定手机号
  781. *
  782. * @ApiMethod (POST)
  783. * @param string $mobile 手机号
  784. * @param string $captcha 验证码
  785. */
  786. /*public function wechatbindmobile()
  787. {
  788. $mobile = $this->request->param('mobile');
  789. $captcha = $this->request->param('captcha');
  790. $code = $this->request->param('code');
  791. if (!$mobile || !$captcha || !$code) {
  792. $this->error(__('Invalid parameters'));
  793. }
  794. if (!Validate::regex($mobile, "^1\d{10}$")) {
  795. $this->error(__('Mobile is incorrect'));
  796. }
  797. $result = Sms::check($mobile, $captcha, 'changemobile');
  798. if (!$result) {
  799. $this->error(__('Captcha is incorrect'));
  800. }
  801. $wechatCodeWhere['code'] = $code;
  802. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  803. if (empty($wechatCode)) {
  804. $this->error('请先微信登录');
  805. }
  806. //检查appid绑定的用户
  807. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  808. if ($user) {
  809. if ($user['status'] == -1) {
  810. $this->error('账户已注销');
  811. }
  812. if ($user['status'] != 1) {
  813. $this->error(__('Account is locked'));
  814. }
  815. //如果已经有账号则直接登录
  816. $ret = $this->auth->direct($user['id']);
  817. $this->success('success',$this->auth->getUserinfo());
  818. }
  819. //新的openid用户
  820. $where = [];
  821. $where['mobile'] = $mobile;
  822. $userData = Db::name('user')->where($where)->find();//老用户
  823. if (!empty($userData)) {
  824. if (empty($userData['wechat_openid'])) {
  825. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  826. } else {
  827. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  828. $this->error('该手机号已被其他用户绑定');
  829. }
  830. }
  831. $ret = $this->auth->direct($userData['id']);
  832. } else {
  833. $extend = [
  834. 'wechat_openid' => $wechatCode['openid'],
  835. ];
  836. $ret = $this->auth->register('', '','', $mobile, $extend);
  837. }
  838. if (!$ret) {
  839. $this->error($this->auth->getError());
  840. }
  841. $this->success('success',$this->auth->getUserinfo());
  842. }*/
  843. /*
  844. * 修改用户的坐标
  845. * */
  846. public function change_longlat(){
  847. $longitude = input_post('longitude');
  848. $latitude = input_post('latitude');
  849. $cityname = input_post('cityname');
  850. if(empty($longitude) || empty($latitude) || empty($cityname)){
  851. $this->error();
  852. }
  853. $data = [
  854. 'longitude' => $longitude,
  855. 'latitude' => $latitude,
  856. 'cityname' => $cityname,
  857. ];
  858. Db::name('user')->where('id',$this->auth->id)->update($data);
  859. $this->success();
  860. }
  861. /**
  862. * 修改手机号
  863. *
  864. * @ApiMethod (POST)
  865. * @param string $mobile 手机号
  866. * @param string $captcha 验证码
  867. */
  868. public function changemobile()
  869. {
  870. $user = $this->auth->getUser();
  871. $oldcaptcha = $this->request->request('oldcaptcha');
  872. $mobile = $this->request->request('mobile');
  873. $captcha = $this->request->request('captcha');
  874. if (!$oldcaptcha || !$mobile || !$captcha) {
  875. $this->error(__('Invalid parameters'));
  876. }
  877. if (!Validate::regex($mobile, "^1\d{10}$")) {
  878. $this->error(__('Mobile is incorrect'));
  879. }
  880. if($user->mobile == $mobile){
  881. $this->error('新手机号不能与旧手机号相同');
  882. }
  883. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  884. $this->error(__('Mobile already exist'));
  885. }
  886. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  887. if (!$result) {
  888. $this->error(__('Captcha is incorrect'));
  889. }
  890. $result = Sms::check($mobile, $captcha, 'changemobile');
  891. if (!$result) {
  892. $this->error(__('Captcha is incorrect'));
  893. }
  894. /*$verification = $user->verification;
  895. $verification->mobile = 1;
  896. $user->verification = $verification;*/
  897. $user->mobile = $mobile;
  898. $user->save();
  899. Sms::flush($user->mobile, 'changemobile');
  900. Sms::flush($mobile, 'changemobile');
  901. $this->success();
  902. }
  903. /**
  904. * 手机号注册来的,绑定微信
  905. *
  906. * @ApiMethod (POST)
  907. * @param string wechat_openid 微信openid
  908. */
  909. /*public function bindopenid()
  910. {
  911. $user = $this->auth->getUser();
  912. $wechat_openid = $this->request->request('wechat_openid');
  913. if(!empty($this->auth->wechat_openid)){
  914. $this->error('已经绑定了微信号');
  915. }
  916. if (!$wechat_openid) {
  917. $this->error(__('Invalid parameters'));
  918. }
  919. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  920. $this->error('该微信号已被其他用户绑定');
  921. }
  922. $user->wechat_openid = $wechat_openid;
  923. $user->save();
  924. $this->success('success',$this->userInfo('return'));
  925. }*/
  926. /**
  927. * 修改密码
  928. *
  929. * @ApiMethod (POST)
  930. * @param string $newpassword 新密码
  931. * @param string $oldpassword 旧密码
  932. */
  933. public function changepwd(){
  934. $newpassword = input('newpassword');
  935. $oldpassword = input('oldpassword','');
  936. if (!$newpassword) {
  937. $this->error(__('Invalid parameters'));
  938. }
  939. if($this->auth->password && empty($oldpassword)){
  940. $this->error('原密码必填');
  941. }
  942. if(empty($this->auth->password)){
  943. $ret = $this->auth->changepwd($newpassword, '', true);
  944. }else{
  945. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  946. }
  947. if ($ret) {
  948. $this->success(__('Reset password successful'));
  949. } else {
  950. $this->error($this->auth->getError());
  951. }
  952. }
  953. /**
  954. * 记录当前登陆的设备ID,设备信息,IP等
  955. */
  956. /* public function changeDeviceIp()
  957. {
  958. // 接口防并发
  959. if (!$this->apiLimit(1, 5)) {
  960. return ;
  961. }
  962. $user = $this->auth->getUser();
  963. $ip = request()->ip();
  964. $deviceId = $this->request->request('device_id','');
  965. $phoneModel = $this->request->request('phone_model','');
  966. $brand = $this->request->request('brand','');
  967. $apiVersion = $this->request->request('api_version','');
  968. $deviceOs = $this->request->request('device_os','');
  969. if ($ip !== $user->loginip){
  970. $update = [];
  971. $update['id'] = $user->id;
  972. $update['loginip'] = $ip;
  973. \app\common\model\User::update($update);
  974. }
  975. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  976. if (empty($userDeviceInfo)){
  977. $userDeviceInfo = new UserDeviceInfo();
  978. $userDeviceInfo->user_id = $user->u_id;
  979. }
  980. $userDeviceInfo->device_os = $deviceOs;
  981. $userDeviceInfo->device_id = $deviceId;
  982. $userDeviceInfo->phone_model = $phoneModel;
  983. $userDeviceInfo->brand = $brand;
  984. $userDeviceInfo->api_version = $apiVersion;
  985. $userDeviceInfo->save();
  986. //首页接口调用,这里不反回信息
  987. // $this->success("更新成功!");
  988. }*/
  989. //修改用户活跃1
  990. /*public function useractive(){
  991. $this->success('success');
  992. }*/
  993. //公众号获取openid
  994. /*public function getUserOpenid_gzh(){
  995. $configValue = Service::getConfig('wechat');
  996. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  997. $rs = $wechat->getOpenid();
  998. $this->success('success',$rs);
  999. }*/
  1000. /**
  1001. * 微信内H5-JSAPI支付
  1002. */
  1003. /*public function jssdkBuildConfig() {
  1004. $url = $this->request->request("url");
  1005. $configValue = Service::getConfig('wechat');
  1006. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  1007. $sign = $wechat->getSignPackage(urldecode($url));
  1008. $this->success("获取成功!",$sign);
  1009. }*/
  1010. //苹果账号登录
  1011. /*public function ioslogin(){
  1012. $ios_openid = input('ios_openid','');
  1013. if (!$ios_openid) {
  1014. $this->error(__('Invalid parameters'));
  1015. }
  1016. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1017. // if (!$user) {
  1018. // $this->success('选择性别', ['code' => 5]);
  1019. // }
  1020. if ($user) {
  1021. if (!in_array($user->status,[1,2])) {
  1022. $this->error(__('Account is locked'));
  1023. }
  1024. if ($user->frozentime > time()) {
  1025. $this->error('您的账号已被封禁至' . date('Y-m-d H:i'));
  1026. }
  1027. //如果已经有账号则直接登录
  1028. $ret = $this->auth->direct($user->id);
  1029. } else {
  1030. $reg_data = [
  1031. 'register_from' => input('register_from',''),
  1032. 'gender' => -1
  1033. ];
  1034. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1035. }
  1036. if ($ret) {
  1037. $data = $this->userInfo('return');
  1038. $this->success(__('Logged in successful'), $data);
  1039. } else {
  1040. $this->error($this->auth->getError());
  1041. }
  1042. }*/
  1043. //苹果账号注册
  1044. /*public function iosregiter(){
  1045. $ios_openid = input('ios_openid', '', 'trim');
  1046. $gender = input('gender', -1, 'intval'); //性别:1=男,0=女
  1047. if (!$ios_openid) {
  1048. $this->error(__('Invalid parameters'));
  1049. }
  1050. if (!in_array($gender, [1, 0])) {
  1051. $this->error('性别错误');
  1052. }
  1053. $user = Db::name('user')->where(['ios_openid' => $ios_openid])->find();
  1054. if ($user) {
  1055. $this->error('账号已经存在,请直接登录');
  1056. }
  1057. $reg_data = [
  1058. 'register_from' => input('register_from',''),
  1059. 'gender' => $gender
  1060. ];
  1061. $ret = $this->auth->iosopenid_register($ios_openid,$reg_data);
  1062. if ($ret) {
  1063. $data = $this->userInfo('return');
  1064. $this->success(__('Logged in successful'), $data);
  1065. } else {
  1066. $this->error($this->auth->getError());
  1067. }
  1068. }*/
  1069. //客服
  1070. public function kefu() {
  1071. $type = input('type', 0, 'intval'); //客服位置: 0客服中心 1充值客服
  1072. if (!in_array($type, [0, 1])) {
  1073. $this->error('您的网络开小差啦~');
  1074. }
  1075. if ($type == 0) {
  1076. $user_id = config('site.customer_service_id') ? : 0; //指定客服id
  1077. } else {
  1078. $user_id = config('site.pay_customer_service_id') ? : 0; //指定客服id
  1079. }
  1080. $list = Db::name('user')->field('id')->where(['status' => 1, 'is_kefu' => 1, 'id' => $user_id])->select();
  1081. if (!$list) {
  1082. $this->success('success', $list);
  1083. }
  1084. foreach ($list as $k => &$v) {
  1085. $v['nickname'] = '客服' . ($k + 1);
  1086. $v['avatar'] = config('avatar_girl');
  1087. }
  1088. $this->success('success', $list);
  1089. }
  1090. //修改城市
  1091. public function editcity() {
  1092. $name = input('name', '', 'trim'); //城市名
  1093. if ($name === '') {
  1094. $this->error('参数缺失');
  1095. }
  1096. $hometown_cityid = Db::name('area')->where(['name' => $name])->value('id');
  1097. if (!$hometown_cityid) {
  1098. $this->success('修改成功');
  1099. }
  1100. Db::name('user')->where(['id' => $this->auth->id])->setField('hometown_cityid', $hometown_cityid);
  1101. $this->success('修改成功');
  1102. }
  1103. //搜索用户
  1104. public function searchuser() {
  1105. $keyword = input('keyword', '', 'trim'); //昵称或ID
  1106. if ($keyword === '') {
  1107. $this->error('请输入关键字');
  1108. }
  1109. $id = Db::name('user')->where(['nickname|username' => $keyword])->value('id');
  1110. $id = $id ? : 0;
  1111. $this->success('用户', $id);
  1112. }
  1113. }