User.php 40 KB

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