User.php 41 KB

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