User.php 41 KB

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