User.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 onlogin\onlogin;
  12. use app\common\library\Keyworld;
  13. use addons\epay\library\Service;
  14. //use addons\epay\library\Wechat;
  15. use app\common\library\Wechat;
  16. /**
  17. * 会员接口,登录,注册,修改资料等
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login','h5register', 'mobilelogin','wechatlogin','applelogin','bindmobile','applebindmobile', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 会员中心
  29. */
  30. public function index()
  31. {
  32. $this->success('', ['welcome' => $this->auth->nickname]);
  33. }
  34. public function h5register()
  35. {
  36. $mobile = input('mobile');
  37. $captcha = input('captcha');
  38. $invite_no = input('invite_no','');
  39. $intro_no = input('intro_no','');
  40. if (!$mobile || !$captcha) {
  41. $this->error(__('Invalid parameters'));
  42. }
  43. if (!Validate::regex($mobile, "^1\d{10}$")) {
  44. $this->error(__('Mobile is incorrect'));
  45. }
  46. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  47. $this->error(__('Captcha is incorrect'));
  48. }
  49. $user = \app\common\model\User::getByMobile($mobile);
  50. if ($user) {
  51. $this->error('该手机号已经注册过了');
  52. } else {
  53. $extend = [];
  54. if($invite_no == 'undefined'){$invite_no = '';}
  55. if($intro_no == 'undefined'){$intro_no = '';}
  56. if(!empty($invite_no)){
  57. $inviteUserInfo = \app\common\model\User::where(["introcode" => $invite_no])->find();
  58. if (!$inviteUserInfo) {
  59. //$this->error("查询不到该邀请码用户信息!");
  60. }else{
  61. $extend['invite_uid'] = $inviteUserInfo['id'];
  62. }
  63. }elseif(!empty($intro_no)){
  64. $introUserInfo = \app\common\model\User::where(["introcode" => $intro_no])->find();
  65. if (!$introUserInfo) {
  66. //$this->error("查询不到该邀请码用户信息!");
  67. }else{
  68. $extend['intro_uid'] = $introUserInfo['id'];
  69. }
  70. }
  71. $ret = $this->auth->register('','','', $mobile, $extend);
  72. }
  73. if ($ret) {
  74. $this->success('注册成功');
  75. } else {
  76. $this->error($this->auth->getError());
  77. }
  78. }
  79. /**
  80. * 手机验证码登录
  81. *
  82. * @ApiMethod (POST)
  83. * @param string $mobile 手机号
  84. * @param string $captcha 验证码
  85. */
  86. public function mobilelogin()
  87. {
  88. $mobile = input('mobile');
  89. $captcha = input('captcha');
  90. if (!$mobile || !$captcha) {
  91. $this->error(__('Invalid parameters'));
  92. }
  93. if (!Validate::regex($mobile, "^1\d{10}$")) {
  94. $this->error(__('Mobile is incorrect'));
  95. }
  96. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  97. $this->error(__('Captcha is incorrect'));
  98. }
  99. $user = \app\common\model\User::getByMobile($mobile);
  100. if ($user) {
  101. if ($user->status == -1) {
  102. $this->error('账户已注销');
  103. }
  104. if ($user->status != 1) {
  105. $this->error(__('Account is locked'));
  106. }
  107. //如果已经有账号则直接登录
  108. $ret = $this->auth->direct($user->id);
  109. } else {
  110. $extend = [
  111. 'plat_unique_id' => input('plat_unique_id',''),
  112. 'plat_from' => input('plat_from',''),
  113. ];
  114. $ret = $this->auth->register('', '', '', $mobile, $extend);
  115. }
  116. if ($ret) {
  117. Sms::flush($mobile, 'mobilelogin');
  118. $data = $this->auth->getUserinfo_simple();
  119. $this->success(__('Logged in successful'), $data);
  120. } else {
  121. $this->error($this->auth->getError());
  122. }
  123. }
  124. //微信登录,预先假注册
  125. public function wechatlogin(){
  126. $code = $this->request->param('code','');
  127. if(!$code){
  128. $this->error(__('Invalid parameters'));
  129. }
  130. //微信
  131. $wechat = new Wechat();
  132. $wxuserinfo = $wechat->getAccessToken($code);
  133. if(!$wxuserinfo){
  134. $this->error('openid获取失败');
  135. }
  136. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  137. $this->error('openid获取失败');
  138. }
  139. $openid = $wxuserinfo['openid'];
  140. //检查用户
  141. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  142. if ($user) {
  143. if ($user['status'] == -1) {
  144. $this->error('账户已注销');
  145. }
  146. if ($user['status'] != 1) {
  147. $this->error(__('Account is locked'));
  148. }
  149. //如果已经有账号则直接登录
  150. $ret = $this->auth->direct($user['id']);
  151. if ($ret) {
  152. $userInfo = $this->auth->getUserinfo();
  153. $userInfo['is_register'] = 0;
  154. $userInfo['code'] = $code;
  155. $this->success(__('Logged in successful'), $userInfo);
  156. } else {
  157. $this->error($this->auth->getError());
  158. }
  159. } else {
  160. //记录code和openid,绑定手机号的时候更新openid
  161. $wechatCodeData = [
  162. 'code' => $code,
  163. 'openid' => $openid,
  164. 'createtime' => time(),
  165. ];
  166. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  167. if (empty($wechatCode)) {
  168. Db::name('wechat_code')->insertGetId($wechatCodeData);
  169. } else {
  170. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  171. }
  172. //直接返回
  173. $userInfo = [];
  174. $userInfo['is_register'] = 1;
  175. $userInfo['code'] = $code;
  176. $this->success('获取信息成功', $userInfo);
  177. }
  178. }
  179. /**
  180. * 运营商一键登录
  181. */
  182. public function onLogin()
  183. {
  184. $accessToken = input('accessToken');// 运营商预取号获取到的token
  185. $token = input('tokenT');// 易盾返回的token
  186. if (!$accessToken || !$token) {
  187. $this->error("参数获取失败!");
  188. }
  189. $params = array(
  190. // 运营商预取号获取到的token
  191. "accessToken" => $accessToken,
  192. // 易盾返回的token
  193. "token" => $token
  194. );
  195. // 获取密钥配置
  196. $configInfo = config("onLogin");
  197. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  198. $onret = $onlogin->check($params);
  199. // $ret = [];
  200. // $ret["code"] = 200;
  201. // $ret["msg"] = "ok";
  202. // $ret["data"] = [
  203. // "phone" => "17574504021",
  204. // "resultCode" => 0
  205. // ];
  206. if ($onret["code"] == 200) {
  207. $mobile = $onret["data"]["phone"];
  208. if (empty($mobile)) {
  209. // 取号失败,建议进行二次验证,例如短信验证码
  210. $this->error("取号登录失败,请用验证码方式登录!");
  211. } else {
  212. // 取号成功, 执行登录等流程
  213. // 用户登录逻辑 === 开始
  214. $user = \app\common\model\User::getByMobile($mobile);
  215. if ($user) {
  216. if ($user->status == -1) {
  217. $this->error('账户已注销');
  218. }
  219. if ($user->status != 1) {
  220. $this->error(__('Account is locked'));
  221. }
  222. //如果已经有账号则直接登录
  223. $ret = $this->auth->direct($user->id);
  224. $is_register = 0;
  225. } else {
  226. $extend = [
  227. ];
  228. $ret = $this->auth->register('', '', '', $mobile, $extend);
  229. $is_register = 1;
  230. }
  231. if ($ret) {
  232. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  233. } else {
  234. $this->error($this->auth->getError());
  235. }
  236. // 用户登录逻辑 === 结束
  237. }
  238. } else {
  239. $this->error("登录失败,请用验证码方式登录!");
  240. }
  241. }
  242. //苹果登录+预注册
  243. public function applelogin(){
  244. $iosUserId = $this->request->param('ios_user_id','');
  245. if(!$iosUserId){
  246. $this->error(__('Invalid parameters'));
  247. }
  248. //检查用户
  249. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  250. if ($user) {
  251. if ($user['status'] == -1) {
  252. $this->error('账户已经注销');
  253. }
  254. if ($user['status'] != 1) {
  255. $this->error(__('Account is locked'));
  256. }
  257. //如果已经有账号则直接登录
  258. $ret = $this->auth->direct($user['id']);
  259. if ($ret) {
  260. $userInfo = $this->auth->getUserinfo_simple();
  261. $userInfo['is_register'] = 0;
  262. $userInfo['ios_user_id'] = $iosUserId;
  263. $this->success(__('Logged in successful'), $userInfo);
  264. } else {
  265. $this->error($this->auth->getError());
  266. }
  267. } else {
  268. //直接返回
  269. $userInfo = [];
  270. $userInfo['is_register'] = 1;
  271. $userInfo['ios_user_id'] = $iosUserId;
  272. $this->success('获取信息成功', $userInfo);
  273. }
  274. }
  275. //用户详细资料
  276. public function userInfo($type = 1){
  277. $info = $this->auth->getUserinfo();
  278. if($type == 'return'){
  279. return $info;
  280. }
  281. $this->success(__('success'),$info);
  282. }
  283. /**
  284. * 退出登录
  285. * @ApiMethod (POST)
  286. */
  287. public function logout()
  288. {
  289. if (!$this->request->isPost()) {
  290. $this->error(__('Invalid parameters'));
  291. }
  292. //退出im
  293. // $tenIm = new Tenim();
  294. // $tenIm->loginoutim($this->auth->id);
  295. $this->auth->logout();
  296. $this->success(__('Logout successful'));
  297. }
  298. /**
  299. * 修改会员个人信息
  300. *
  301. * @ApiMethod (POST)
  302. * @param string $avatar 头像地址
  303. * @param string $username 用户名
  304. * @param string $nickname 昵称
  305. * @param string $bio 个人简介
  306. */
  307. public function profile()
  308. {
  309. $field_array = [
  310. 'avatar','nickname','birthday','gender',
  311. 'audio_bio','audio_seconds','photo_images','video_bio',
  312. 'height','weight','bio',
  313. 'marital_id','job_id','wages_id','suqiu_id','tag_ids','hobby_ids',
  314. 'hide_is_finishinfo',
  315. ];
  316. $data = [];
  317. foreach($field_array as $key => $field){
  318. //前端传不了post,改了
  319. /*if(!request()->has($field,'post')){
  320. continue;
  321. }*/
  322. if(!input('?'.$field)){
  323. continue;
  324. }
  325. $newone = input($field);
  326. if($field == 'avatar'){
  327. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  328. }
  329. if($field == 'photo_images'){
  330. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  331. }
  332. if($field == 'nickname'){
  333. $newone = Keyworld::sensitive($newone);
  334. }
  335. if($field == 'bio'){
  336. $newone = Keyworld::sensitive($newone);
  337. }
  338. $data[$field] = $newone;
  339. }
  340. //视频,需要审核
  341. $user_audit_switch = 1;
  342. if(isset($data['video_bio']) && !empty($data['video_bio']) && $user_audit_switch == 1){
  343. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','video_bio')->where('status',0)->find();
  344. if(!empty($check_exist)){
  345. $this->error('信息已提交等待审核,请勿重复提交');
  346. }
  347. if($data['video_bio'] != $this->auth->video_bio){
  348. $bio_data = [
  349. 'user_id' => $this->auth->id,
  350. 'type' => 'video_bio',
  351. 'old_data' => $this->auth->video_bio,
  352. 'new_data' => $data['video_bio'],
  353. 'createtime' => time(),
  354. ];
  355. Db::name('user_audit')->insertGetId($bio_data);
  356. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  357. }
  358. unset($data['video_bio']);
  359. }
  360. //相册,需要审核
  361. if(isset($data['photo_images']) && !empty($data['photo_images']) && $user_audit_switch == 1){
  362. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','photo_images')->where('status',0)->find();
  363. if(!empty($check_exist)){
  364. $this->error('信息已提交等待审核,请勿重复提交');
  365. }
  366. if($data['photo_images'] != $this->auth->photo_images){
  367. $bio_data = [
  368. 'user_id' => $this->auth->id,
  369. 'type' => 'photo_images',
  370. 'old_data' => $this->auth->photo_images,
  371. 'new_data' => $data['photo_images'],
  372. 'createtime' => time(),
  373. ];
  374. Db::name('user_audit')->insertGetId($bio_data);
  375. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  376. }
  377. unset($data['photo_images']);
  378. }
  379. //语音,需要审核
  380. if(isset($data['audio_bio']) && !empty($data['audio_bio']) && $user_audit_switch == 1){
  381. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  382. if(!empty($check_exist)){
  383. $this->error('信息已提交等待审核,请勿重复提交');
  384. }
  385. if($data['audio_bio'] != $this->auth->audio_bio){
  386. $bio_data = [
  387. 'user_id' => $this->auth->id,
  388. 'type' => 'audio_bio',
  389. 'old_data' => $this->auth->audio_bio,
  390. 'new_data' => $data['audio_bio'],
  391. 'createtime' => time(),
  392. ];
  393. Db::name('user_audit')->insertGetId($bio_data);
  394. //语音秒数
  395. $bio_data = [
  396. 'user_id' => $this->auth->id,
  397. 'type' => 'audio_seconds',
  398. 'old_data' => $this->auth->audio_seconds,
  399. 'new_data' => $data['audio_seconds'],
  400. 'createtime' => time(),
  401. ];
  402. Db::name('user_audit')->insertGetId($bio_data);
  403. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  404. }
  405. unset($data['audio_bio']);
  406. unset($data['audio_seconds']);
  407. }
  408. //头像,是否需要审核
  409. if(isset($data['avatar']) && !empty($data['avatar']) && $data['avatar'] != config('avatar_boy') && $data['avatar'] != config('avatar_girl') && $user_audit_switch == 1){
  410. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','avatar')->where('status',0)->find();
  411. if(!empty($check_exist)){
  412. $this->error('信息已提交等待审核,请勿重复提交');
  413. }
  414. if($data['avatar'] != $this->auth->avatar){
  415. $bio_data = [
  416. 'user_id' => $this->auth->id,
  417. 'type' => 'avatar',
  418. 'old_data' => $this->auth->avatar,
  419. 'new_data' => $data['avatar'],
  420. 'createtime' => time(),
  421. ];
  422. Db::name('user_audit')->insertGetId($bio_data);
  423. //失去真人认证
  424. $data['real_status'] = -1;
  425. Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  426. if(!isset($data['gender'])){
  427. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  428. }
  429. }
  430. unset($data['avatar']);
  431. }
  432. //第一次传入性别,头像只能用默认,因为自传头像需要审核
  433. if(isset($data['gender']) && $user_audit_switch == 1){
  434. $data['avatar'] = $data['gender'] == 1 ? config('avatar_boy') : config('avatar_girl');
  435. }
  436. //初次设置性别为 男
  437. if(isset($data['gender']) && $data['gender'] == 1 && $this->auth->gender == -1){
  438. //男性赠送
  439. $gift_data = [
  440. 'audio_sec' => config('site.man_reg_audio_sec'),
  441. 'video_sec' => config('site.man_reg_video_sec'),
  442. 'typing_times' => config('site.man_reg_typing_times'),
  443. ];
  444. Db::name('user_wallet')->where('id',$this->auth->id)->update($gift_data);
  445. //修改财富值等级为1级
  446. $data['wealth_level'] = 1;
  447. }
  448. //
  449. if(isset($data['birthday'])){
  450. $data['birthday'] = strtotime($data['birthday']);
  451. }
  452. if(isset($data['tag_ids'])){
  453. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  454. }
  455. if(isset($data['hobby_ids'])){
  456. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  457. }
  458. //dump($data);
  459. if(empty($data)){
  460. $this->success();
  461. }
  462. Db::startTrans();
  463. //修改用户
  464. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  465. if($update_rs === false){
  466. Db::rollback();
  467. $this->error('修改资料失败');
  468. }
  469. //task任务
  470. if(isset($data['tag_ids']) || isset($data['bio'])){
  471. $userinfo = Db::name('user')->where('id',$this->auth->id)->find();
  472. if($userinfo['hide_is_finishinfo'] == 0){
  473. if($userinfo['tag_ids'] && $userinfo['bio']){
  474. //task任务
  475. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  476. if($task_rs === false){
  477. Db::rollback();
  478. $this->error('完成任务失败');
  479. }
  480. $finish['hide_is_finishinfo'] = 1;//完善资料了
  481. Db::name('user')->where('id',$this->auth->id)->update($finish);
  482. }
  483. }
  484. }
  485. Db::commit();
  486. $this->success();
  487. }
  488. public function getpower(){
  489. $rs = Db::name('user_power')->where('user_id',$this->auth->id)->find();
  490. $this->success(1,$rs);
  491. }
  492. /**
  493. * 修改会员权限
  494. */
  495. public function setpower()
  496. {
  497. /*$is_vip = $this->is_vip($this->auth->id);
  498. if(!$is_vip){
  499. $this->error('VIP才能设置隐私权限');
  500. }*/
  501. $field_array = ['meili','caifu','weizhi','giftwall'];
  502. $data = [];
  503. foreach($field_array as $key => $field){
  504. if(!input('?'.$field)){
  505. continue;
  506. }
  507. $newone = input($field);
  508. $data[$field] = $newone;
  509. }
  510. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  511. $this->success();
  512. }
  513. //重新绑定
  514. public function set_intro(){
  515. if(isset($data['introcode']) && !empty($data['introcode'])){
  516. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  517. if(!$intro_user){
  518. $this->error('不存在的邀请人');
  519. }
  520. if(!empty($this->auth->intro_uid)){
  521. $this->error('您已经填写过邀请人');
  522. }
  523. unset($data['introcode']);//别人的邀请码,不能改了自己的
  524. $data['intro_uid'] = $intro_user;
  525. }
  526. }
  527. /*
  528. * 修改用户的坐标
  529. * */
  530. public function change_longlat(){
  531. // $longitude = input('longitude',0);
  532. // $latitude = input('latitude',0);
  533. $cityname = input('cityname','');
  534. $provincename = input('provincename','');
  535. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  536. $this->error();
  537. }*/
  538. $data = [];
  539. // $longitude && $data['longitude'] = $longitude;
  540. // $latitude && $data['latitude'] = $latitude;
  541. $cityname && $data['cityname'] = $cityname;
  542. $provincename && $data['provincename'] = $provincename;
  543. //传入了城市,但是没传入省,直接省名改空
  544. if(isset($data['cityname']) && !isset($data['provincename'])){
  545. $data['provincename'] = '';
  546. }
  547. //没传入城市,但是传入省,直接城市名改空
  548. if(!isset($data['cityname']) && isset($data['provincename'])){
  549. $data['cityname'] = '';
  550. }
  551. if(!empty($data)){
  552. Db::name('user')->where('id',$this->auth->id)->update($data);
  553. }
  554. $this->success();
  555. }
  556. //修改用户设备id
  557. public function change_plat_unique_id(){
  558. $plat_unique_id = input('plat_unique_id','');
  559. $plat_from = input('plat_from','');
  560. $data = [
  561. 'plat_unique_id' => $plat_unique_id,
  562. 'plat_from' => $plat_from,
  563. ];
  564. Db::name('user')->where('id',$this->auth->id)->update($data);
  565. $this->success();
  566. }
  567. /**
  568. * 修改手机号
  569. *
  570. * @ApiMethod (POST)
  571. * @param string $mobile 手机号
  572. * @param string $captcha 验证码
  573. */
  574. public function changemobile()
  575. {
  576. $user = $this->auth->getUser();
  577. $oldcaptcha = $this->request->request('oldcaptcha');
  578. $mobile = $this->request->request('mobile');
  579. $captcha = $this->request->request('captcha');
  580. if (!$oldcaptcha || !$mobile || !$captcha) {
  581. $this->error(__('Invalid parameters'));
  582. }
  583. if (!Validate::regex($mobile, "^1\d{10}$")) {
  584. $this->error(__('Mobile is incorrect'));
  585. }
  586. if($user->mobile == $mobile){
  587. $this->error('新手机号不能与旧手机号相同');
  588. }
  589. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  590. $this->error(__('Mobile already exist'));
  591. }
  592. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  593. if (!$result) {
  594. $this->error(__('Captcha is incorrect'));
  595. }
  596. $result = Sms::check($mobile, $captcha, 'changemobile');
  597. if (!$result) {
  598. $this->error(__('Captcha is incorrect'));
  599. }
  600. Sms::flush($user->mobile, 'changemobile');
  601. Sms::flush($mobile, 'changemobile');
  602. $user->mobile = $mobile;
  603. $user->save();
  604. $this->success();
  605. }
  606. /**
  607. * 苹果注册来的,绑定手机号
  608. *
  609. * @ApiMethod (POST)
  610. * @param string $mobile 手机号
  611. * @param string $captcha 验证码
  612. */
  613. public function applebindmobile()
  614. {
  615. $mobile = $this->request->param('mobile');
  616. $captcha = $this->request->param('captcha');
  617. $iosUserId = $this->request->param('ios_user_id','');
  618. if (!$mobile || !$captcha || !$iosUserId) {
  619. $this->error(__('Invalid parameters'));
  620. }
  621. if (!Validate::regex($mobile, "^1\d{10}$")) {
  622. $this->error(__('Mobile is incorrect'));
  623. }
  624. $result = Sms::check($mobile, $captcha, 'changemobile');
  625. if (!$result) {
  626. $this->error(__('Captcha is incorrect'));
  627. }
  628. //检查ios_user_id绑定的用户
  629. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  630. if ($user) {
  631. if ($user['status'] == -1) {
  632. $this->error('账户已经注销');
  633. }
  634. if ($user['status'] != 1) {
  635. $this->error(__('Account is locked'));
  636. }
  637. //如果已经有账号则直接登录
  638. $ret = $this->auth->direct($user['id']);
  639. $this->success('success',$this->auth->getUserinfo_simple());
  640. }
  641. //新的ios用户
  642. $where = [];
  643. $where['mobile'] = $mobile;
  644. $userData = Db::name('user')->where($where)->find();//老用户
  645. if (!empty($userData)) {
  646. if (empty($userData['ios_user_id'])) {
  647. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  648. } else {
  649. if ($userData['ios_user_id'] != $iosUserId) {
  650. $this->error('该手机号已被其他用户绑定');
  651. }
  652. }
  653. $ret = $this->auth->direct($userData['id']);
  654. } else {
  655. $extend = [
  656. 'ios_user_id' => $iosUserId,
  657. ];
  658. $ret = $this->auth->register('', '','', $mobile, $extend);
  659. }
  660. if (!$ret) {
  661. $this->error($this->auth->getError());
  662. }
  663. $this->success('success',$this->auth->getUserinfo_simple());
  664. }
  665. /**
  666. * 微信注册来的,绑定手机号
  667. *
  668. * @ApiMethod (POST)
  669. * @param string $mobile 手机号
  670. * @param string $captcha 验证码
  671. */
  672. public function bindmobile()
  673. {
  674. $mobile = $this->request->param('mobile');
  675. $captcha = $this->request->param('captcha');
  676. $code = $this->request->param('code');
  677. if (!$mobile || !$captcha || !$code) {
  678. $this->error(__('Invalid parameters'));
  679. }
  680. if (!Validate::regex($mobile, "^1\d{10}$")) {
  681. $this->error(__('Mobile is incorrect'));
  682. }
  683. $result = Sms::check($mobile, $captcha, 'changemobile');
  684. if (!$result) {
  685. $this->error(__('Captcha is incorrect'));
  686. }
  687. $wechatCodeWhere['code'] = $code;
  688. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  689. if (empty($wechatCode)) {
  690. $this->error('请先微信登录');
  691. }
  692. //检查appid绑定的用户
  693. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  694. if ($user) {
  695. if ($user['status'] == -1) {
  696. $this->error('账户已注销');
  697. }
  698. if ($user['status'] != 1) {
  699. $this->error(__('Account is locked'));
  700. }
  701. //如果已经有账号则直接登录
  702. $ret = $this->auth->direct($user['id']);
  703. $this->success('success',$this->auth->getUserinfo_simple());
  704. }
  705. //新的openid用户
  706. $where = [];
  707. $where['mobile'] = $mobile;
  708. $userData = Db::name('user')->where($where)->find();//老用户
  709. if (!empty($userData)) {
  710. if (empty($userData['wechat_openid'])) {
  711. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  712. } else {
  713. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  714. $this->error('该手机号已被其他用户绑定');
  715. }
  716. }
  717. $ret = $this->auth->direct($userData['id']);
  718. } else {
  719. $extend = [
  720. 'wechat_openid' => $wechatCode['openid'],
  721. 'plat_unique_id' => input('plat_unique_id',''),
  722. 'plat_from' => input('plat_from',''),
  723. ];
  724. $ret = $this->auth->register('', '','', $mobile, $extend);
  725. }
  726. if (!$ret) {
  727. $this->error($this->auth->getError());
  728. }
  729. $this->success('success',$this->auth->getUserinfo_simple());
  730. }
  731. /**
  732. * 手机注册来的,绑定微信
  733. *
  734. * @ApiMethod (POST)
  735. * @param string $wechat_openid
  736. */
  737. public function bindopenid()
  738. {
  739. $wechat_openid = input('wechat_openid');
  740. if (!$wechat_openid) {
  741. $this->error(__('Invalid parameters'));
  742. }
  743. if(!empty($this->auth->wechat_openid)){
  744. //$this->error('已经绑定了微信号');
  745. }
  746. $otherUserWhere['wechat_openid'] = $wechat_openid;
  747. $otherUserWhere['id'] = ['neq',$this->auth->id];
  748. if (\app\common\model\User::where($otherUserWhere)->find()) {
  749. $this->error('该微信号已被其他用户绑定');
  750. }
  751. $user = $this->auth->getUser();
  752. $user->wechat_openid = $wechat_openid;
  753. $user->save();
  754. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  755. }
  756. /**
  757. * 重置密码
  758. *
  759. * @ApiMethod (POST)
  760. * @param string $mobile 手机号
  761. * @param string $newpassword 新密码
  762. * @param string $captcha 验证码
  763. */
  764. public function resetpwd()
  765. {
  766. //$type = input("type");
  767. $type = 'mobile';
  768. $mobile = input("mobile");
  769. // $email = input("email");
  770. $newpassword = input("newpassword");
  771. $captcha = input("captcha");
  772. if (!$mobile || !$newpassword || !$captcha) {
  773. $this->error(__('Invalid parameters'));
  774. }
  775. if ($type == 'mobile') {
  776. if (!Validate::regex($mobile, "^1\d{10}$")) {
  777. $this->error(__('Mobile is incorrect'));
  778. }
  779. $user = \app\common\model\User::getByMobile($mobile);
  780. if (!$user) {
  781. $this->error(__('User not found'));
  782. }
  783. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  784. if (!$ret) {
  785. $this->error(__('Captcha is incorrect'));
  786. }
  787. Sms::flush($mobile, 'resetpwd');
  788. }
  789. //模拟一次登录
  790. $this->auth->direct($user->id);
  791. $ret = $this->auth->changepwd($newpassword, '', true);
  792. if ($ret) {
  793. $this->success(__('Reset password successful'));
  794. } else {
  795. $this->error($this->auth->getError());
  796. }
  797. }
  798. /**
  799. * 修改密码
  800. *
  801. * @ApiMethod (POST)
  802. * @param string $newpassword 新密码
  803. * @param string $oldpassword 旧密码
  804. */
  805. public function changepwd(){
  806. $newpassword = input('newpassword');
  807. $oldpassword = input('oldpassword','');
  808. if (!$newpassword) {
  809. $this->error(__('Invalid parameters'));
  810. }
  811. if($this->auth->password && empty($oldpassword)){
  812. $this->error('原密码必填');
  813. }
  814. if(empty($this->auth->password)){
  815. $ret = $this->auth->changepwd($newpassword, '', true);
  816. }else{
  817. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  818. }
  819. if ($ret) {
  820. $this->success('设置密码成功');
  821. } else {
  822. $this->error($this->auth->getError());
  823. }
  824. }
  825. //假注销
  826. public function cancleUser(){
  827. $captcha = input('captcha','');
  828. if (!$captcha) {
  829. $this->error(__('Invalid parameters'));
  830. }
  831. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  832. $this->error(__('Captcha is incorrect'));
  833. }
  834. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  835. $this->auth->logout();
  836. $this->success('注销成功');
  837. }
  838. //文字语音视频收费设置
  839. public function chargeconfig() {
  840. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
  841. if (!in_array($type, [0, 1, 2])) {
  842. $this->error('您的网络开小差啦~');
  843. }
  844. $where['type'] = $type;
  845. $where['level'] = ['elt',$this->auth->charm_level];
  846. $list = Db::name('charge_config')->field('id, price, level')->where($where)->order('price asc')->select();
  847. $this->success('success', $list);
  848. }
  849. //文字语音视频收费/隐藏所在位置设置
  850. public function chargeset() {
  851. if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
  852. $this->error('请先完成实名认证');
  853. }
  854. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  855. $this->error('请先完成真人认证');
  856. }
  857. $chat_id = input('chat_id', 0, 'intval'); //文字收费id
  858. $voice_id = input('voice_id', 0, 'intval'); //语音收费id
  859. $video_id = input('video_id', 0, 'intval'); //视频收费id
  860. $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
  861. $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
  862. $data = [];
  863. //查询我的魅力等级
  864. $level = $this->auth->charm_level;
  865. if ($chat_id) {
  866. $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
  867. if (!$charge_config) {
  868. $this->error('您的网络开小差啦~');
  869. }
  870. if ($level < $charge_config['level']) {
  871. $this->error('您还未满足条件~');
  872. }
  873. $data['match_typing_price'] = $charge_config['price'];
  874. }
  875. if ($voice_id) {
  876. $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
  877. if (!$charge_config) {
  878. $this->error('您的网络开小差啦~');
  879. }
  880. if ($level < $charge_config['level']) {
  881. $this->error('您还未满足条件~');
  882. }
  883. $data['match_audio_price'] = $charge_config['price'];
  884. }
  885. if ($video_id) {
  886. $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
  887. if (!$charge_config) {
  888. $this->error('您的网络开小差啦~');
  889. }
  890. if ($level < $charge_config['level']) {
  891. $this->error('您还未满足条件~');
  892. }
  893. $data['match_video_price'] = $charge_config['price'];
  894. }
  895. if (in_array($open_match_audio, [1, 0])) {
  896. $data['open_match_audio'] = $open_match_audio;
  897. }
  898. if (in_array($open_match_video, [1, 0])) {
  899. $data['open_match_video'] = $open_match_video;
  900. }
  901. if (!$data) {
  902. $this->error('没有修改信息~');
  903. }
  904. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  905. if ($rs === false) {
  906. $this->error('您的网络开小差啦~');
  907. }
  908. $this->success('设置成功');
  909. }
  910. }