User.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. $this->success('success', $data);
  141. }
  142. //刷新随机昵称
  143. public function get_rand_nick_name(){
  144. $nickname = $this->auth->get_rand_nick_name();
  145. $this->success('success', $nickname);
  146. }
  147. //注册完善资料
  148. public function perfect_info() {
  149. $avatar = input('avatar', '', 'trim'); //头像
  150. $nickname = input('nickname', '', 'trim'); //昵称
  151. $birthday = input('birthday', '', 'strtotime'); //生日
  152. $hometown_cityid = input('hometown_cityid', '', 'trim'); //城市id
  153. $bio = input('bio', '', 'trim'); //个性签名
  154. $hobby = input('hobby', '', 'trim'); //爱好
  155. $marital = input('marital', '', 'trim'); //婚姻
  156. $introcode = input('introcode', '', 'trim'); //邀请码
  157. $data = [];
  158. if ($avatar) {
  159. $data['avatar'] = $avatar;
  160. }
  161. if ($nickname !== '') {
  162. if (iconv_strlen($nickname, 'utf-8') > 10) {
  163. $this->error('昵称最多10个字~');
  164. }
  165. $data['nickname'] = $nickname;
  166. }
  167. if ($birthday) {
  168. $data['birthday'] = $birthday;
  169. }
  170. if ($hometown_cityid) {
  171. $count = Db::name('area')->where('id', $hometown_cityid)->count('id');
  172. if (!$count) {
  173. $this->error('城市不存在');
  174. }
  175. $data['hometown_cityid'] = $hometown_cityid;
  176. }
  177. if ($bio) {
  178. $data['bio'] = $bio;
  179. }
  180. if ($hobby) {
  181. $data['hobby'] = $hobby;
  182. }
  183. if ($marital) {
  184. $data['marital'] = $marital;
  185. }
  186. if ($introcode && !$this->auth->intro_uid) {
  187. $intro_user = Db::name('user')->field('id, intro_uid')->where('introcode', $introcode)->find();
  188. if ($intro_user && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id) {
  189. $data['intro_uid'] = $intro_user['id'];
  190. $data['invite_time'] = time();
  191. }
  192. }
  193. //开启事务
  194. Db::startTrans();
  195. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  196. if($update_rs === false){
  197. Db::rollback();
  198. $this->error('修改失败');
  199. }
  200. //给上级发放钻石
  201. if(isset($data['intro_uid'])){
  202. $intro_gold = config('site.new_user_intro_gold');
  203. if($intro_gold > 0){
  204. $rs_wallet = model('wallet')->lockChangeAccountRemain($data['intro_uid'], 0,'money',$intro_gold,34,'邀请'.$this->auth->username.'注册奖励');
  205. if($rs_wallet['status'] === false){
  206. Db::rollback();
  207. $this->error('邀请新人奖励赠送失败');
  208. }
  209. }
  210. }
  211. //上传头像加5金币
  212. if(isset($data['avatar'])){
  213. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,19);
  214. if($task_rs === false){
  215. Db::rollback();
  216. $this->error('完成任务赠送奖励失败');
  217. }
  218. }
  219. if (isset($data['birthday'])) {
  220. //完成设置生日 +5金币
  221. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  222. if($task_rs === false){
  223. Db::rollback();
  224. $this->error('完成任务赠送奖励失败');
  225. }
  226. }
  227. Db::commit();
  228. $this->success('修改成功');
  229. }
  230. //用户详细资料
  231. public function userInfo($type = 1){
  232. $info = $this->auth->getUserinfo();
  233. $info['is_vip'] = $this->is_vip($info['vip_endtime'],$info['vip_level']);
  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,'money',$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. }