User.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. 'invite_no',
  316. ];
  317. $data = [];
  318. foreach($field_array as $key => $field){
  319. //前端传不了post,改了
  320. /*if(!request()->has($field,'post')){
  321. continue;
  322. }*/
  323. if(!input('?'.$field)){
  324. continue;
  325. }
  326. $newone = input($field);
  327. if($field == 'avatar'){
  328. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  329. }
  330. if($field == 'photo_images'){
  331. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  332. }
  333. if($field == 'nickname'){
  334. $newone = Keyworld::sensitive($newone);
  335. }
  336. if($field == 'bio'){
  337. $newone = Keyworld::sensitive($newone);
  338. }
  339. $data[$field] = $newone;
  340. }
  341. //视频,需要审核
  342. $user_audit_switch = 1;
  343. if(isset($data['video_bio']) && !empty($data['video_bio']) && $user_audit_switch == 1){
  344. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','video_bio')->where('status',0)->find();
  345. if(!empty($check_exist)){
  346. $this->error('信息已提交等待审核,请勿重复提交');
  347. }
  348. if($data['video_bio'] != $this->auth->video_bio){
  349. $bio_data = [
  350. 'user_id' => $this->auth->id,
  351. 'type' => 'video_bio',
  352. 'old_data' => $this->auth->video_bio,
  353. 'new_data' => $data['video_bio'],
  354. 'createtime' => time(),
  355. ];
  356. Db::name('user_audit')->insertGetId($bio_data);
  357. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  358. }
  359. unset($data['video_bio']);
  360. }
  361. //相册,需要审核
  362. if(isset($data['photo_images']) && !empty($data['photo_images']) && $user_audit_switch == 1){
  363. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','photo_images')->where('status',0)->find();
  364. if(!empty($check_exist)){
  365. $this->error('信息已提交等待审核,请勿重复提交');
  366. }
  367. if($data['photo_images'] != $this->auth->photo_images){
  368. $bio_data = [
  369. 'user_id' => $this->auth->id,
  370. 'type' => 'photo_images',
  371. 'old_data' => $this->auth->photo_images,
  372. 'new_data' => $data['photo_images'],
  373. 'createtime' => time(),
  374. ];
  375. Db::name('user_audit')->insertGetId($bio_data);
  376. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  377. }
  378. unset($data['photo_images']);
  379. }
  380. //语音,需要审核
  381. if(isset($data['audio_bio']) && !empty($data['audio_bio']) && $user_audit_switch == 1){
  382. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  383. if(!empty($check_exist)){
  384. $this->error('信息已提交等待审核,请勿重复提交');
  385. }
  386. if($data['audio_bio'] != $this->auth->audio_bio){
  387. $bio_data = [
  388. 'user_id' => $this->auth->id,
  389. 'type' => 'audio_bio',
  390. 'old_data' => $this->auth->audio_bio,
  391. 'new_data' => $data['audio_bio'],
  392. 'createtime' => time(),
  393. ];
  394. Db::name('user_audit')->insertGetId($bio_data);
  395. //语音秒数
  396. $bio_data = [
  397. 'user_id' => $this->auth->id,
  398. 'type' => 'audio_seconds',
  399. 'old_data' => $this->auth->audio_seconds,
  400. 'new_data' => $data['audio_seconds'],
  401. 'createtime' => time(),
  402. ];
  403. Db::name('user_audit')->insertGetId($bio_data);
  404. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  405. }
  406. unset($data['audio_bio']);
  407. unset($data['audio_seconds']);
  408. }
  409. //头像,是否需要审核
  410. if(isset($data['avatar']) && !empty($data['avatar']) && $data['avatar'] != config('avatar_boy') && $data['avatar'] != config('avatar_girl') && $user_audit_switch == 1){
  411. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','avatar')->where('status',0)->find();
  412. if(!empty($check_exist)){
  413. $this->error('信息已提交等待审核,请勿重复提交');
  414. }
  415. if($data['avatar'] != $this->auth->avatar){
  416. $bio_data = [
  417. 'user_id' => $this->auth->id,
  418. 'type' => 'avatar',
  419. 'old_data' => $this->auth->avatar,
  420. 'new_data' => $data['avatar'],
  421. 'createtime' => time(),
  422. ];
  423. Db::name('user_audit')->insertGetId($bio_data);
  424. //失去真人认证
  425. $data['real_status'] = -1;
  426. Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  427. if(!isset($data['gender'])){
  428. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  429. }
  430. }
  431. unset($data['avatar']);
  432. }
  433. //第一次传入性别,头像只能用默认,因为自传头像需要审核
  434. if(isset($data['gender']) && $user_audit_switch == 1){
  435. $data['avatar'] = $data['gender'] == 1 ? config('avatar_boy') : config('avatar_girl');
  436. }
  437. //初次设置性别为 男
  438. if(isset($data['gender']) && $data['gender'] == 1 && $this->auth->gender == -1){
  439. //男性赠送
  440. $gift_data = [
  441. 'audio_sec' => config('site.man_reg_audio_sec'),
  442. 'video_sec' => config('site.man_reg_video_sec'),
  443. 'typing_times' => config('site.man_reg_typing_times'),
  444. ];
  445. Db::name('user_wallet')->where('id',$this->auth->id)->update($gift_data);
  446. //修改财富值等级为1级
  447. $data['wealth_level'] = 1;
  448. }
  449. if(isset($data['invite_no']) && !empty($data['invite_no']) && empty($this->auth->intro_uid) && empty($this->auth->invite_uid)){
  450. $invite_user = Db::name('user')->where('introcode',$data['invite_no'])->value('id');
  451. if($invite_user){
  452. $data['invite_uid'] = $invite_user;
  453. }
  454. unset($data['invite_no']);//别人的邀请码,不能改了自己的
  455. }
  456. //
  457. if(isset($data['birthday'])){
  458. $data['birthday'] = strtotime($data['birthday']);
  459. }
  460. if(isset($data['tag_ids'])){
  461. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  462. }
  463. if(isset($data['hobby_ids'])){
  464. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  465. }
  466. //dump($data);
  467. if(empty($data)){
  468. $this->success();
  469. }
  470. Db::startTrans();
  471. //修改用户
  472. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  473. if($update_rs === false){
  474. Db::rollback();
  475. $this->error('修改资料失败');
  476. }
  477. //task任务
  478. if(isset($data['tag_ids']) || isset($data['bio'])){
  479. $userinfo = Db::name('user')->where('id',$this->auth->id)->find();
  480. if($userinfo['hide_is_finishinfo'] == 0){
  481. if($userinfo['tag_ids'] && $userinfo['bio']){
  482. //task任务
  483. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  484. if($task_rs === false){
  485. Db::rollback();
  486. $this->error('完成任务失败');
  487. }
  488. $finish['hide_is_finishinfo'] = 1;//完善资料了
  489. Db::name('user')->where('id',$this->auth->id)->update($finish);
  490. }
  491. }
  492. }
  493. Db::commit();
  494. $this->success();
  495. }
  496. public function getpower(){
  497. $rs = Db::name('user_power')->where('user_id',$this->auth->id)->find();
  498. $this->success(1,$rs);
  499. }
  500. /**
  501. * 修改会员权限
  502. */
  503. public function setpower()
  504. {
  505. /*$is_vip = $this->is_vip($this->auth->id);
  506. if(!$is_vip){
  507. $this->error('VIP才能设置隐私权限');
  508. }*/
  509. $field_array = ['meili','caifu','weizhi','giftwall'];
  510. $data = [];
  511. foreach($field_array as $key => $field){
  512. if(!input('?'.$field)){
  513. continue;
  514. }
  515. $newone = input($field);
  516. $data[$field] = $newone;
  517. }
  518. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  519. $this->success();
  520. }
  521. //重新绑定
  522. public function set_intro(){
  523. if(isset($data['introcode']) && !empty($data['introcode'])){
  524. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  525. if(!$intro_user){
  526. $this->error('不存在的邀请人');
  527. }
  528. if(!empty($this->auth->intro_uid)){
  529. $this->error('您已经填写过邀请人');
  530. }
  531. unset($data['introcode']);//别人的邀请码,不能改了自己的
  532. $data['intro_uid'] = $intro_user;
  533. }
  534. }
  535. /*
  536. * 修改用户的坐标
  537. * */
  538. public function change_longlat(){
  539. $this->success();
  540. // $longitude = input('longitude',0);
  541. // $latitude = input('latitude',0);
  542. $cityname = input('cityname','');
  543. $provincename = input('provincename','');
  544. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  545. $this->error();
  546. }*/
  547. $data = [];
  548. // $longitude && $data['longitude'] = $longitude;
  549. // $latitude && $data['latitude'] = $latitude;
  550. $cityname && $data['cityname'] = $cityname;
  551. $provincename && $data['provincename'] = $provincename;
  552. //传入了城市,但是没传入省,直接省名改空
  553. if(isset($data['cityname']) && !isset($data['provincename'])){
  554. $data['provincename'] = '';
  555. }
  556. //没传入城市,但是传入省,直接城市名改空
  557. if(!isset($data['cityname']) && isset($data['provincename'])){
  558. $data['cityname'] = '';
  559. }
  560. if(!empty($data)){
  561. Db::name('user')->where('id',$this->auth->id)->update($data);
  562. }
  563. $this->success();
  564. }
  565. //修改用户设备id
  566. public function change_plat_unique_id(){
  567. $plat_unique_id = input('plat_unique_id','');
  568. $plat_from = input('plat_from','');
  569. $data = [
  570. 'plat_unique_id' => $plat_unique_id,
  571. 'plat_from' => $plat_from,
  572. ];
  573. Db::name('user')->where('id',$this->auth->id)->update($data);
  574. $this->success();
  575. }
  576. /**
  577. * 修改手机号
  578. *
  579. * @ApiMethod (POST)
  580. * @param string $mobile 手机号
  581. * @param string $captcha 验证码
  582. */
  583. public function changemobile()
  584. {
  585. $user = $this->auth->getUser();
  586. $oldcaptcha = $this->request->request('oldcaptcha');
  587. $mobile = $this->request->request('mobile');
  588. $captcha = $this->request->request('captcha');
  589. if (!$oldcaptcha || !$mobile || !$captcha) {
  590. $this->error(__('Invalid parameters'));
  591. }
  592. if (!Validate::regex($mobile, "^1\d{10}$")) {
  593. $this->error(__('Mobile is incorrect'));
  594. }
  595. if($user->mobile == $mobile){
  596. $this->error('新手机号不能与旧手机号相同');
  597. }
  598. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  599. $this->error(__('Mobile already exist'));
  600. }
  601. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  602. if (!$result) {
  603. $this->error(__('Captcha is incorrect'));
  604. }
  605. $result = Sms::check($mobile, $captcha, 'changemobile');
  606. if (!$result) {
  607. $this->error(__('Captcha is incorrect'));
  608. }
  609. Sms::flush($user->mobile, 'changemobile');
  610. Sms::flush($mobile, 'changemobile');
  611. $user->mobile = $mobile;
  612. $user->save();
  613. $this->success();
  614. }
  615. /**
  616. * 苹果注册来的,绑定手机号
  617. *
  618. * @ApiMethod (POST)
  619. * @param string $mobile 手机号
  620. * @param string $captcha 验证码
  621. */
  622. public function applebindmobile()
  623. {
  624. $mobile = $this->request->param('mobile');
  625. $captcha = $this->request->param('captcha');
  626. $iosUserId = $this->request->param('ios_user_id','');
  627. if (!$mobile || !$captcha || !$iosUserId) {
  628. $this->error(__('Invalid parameters'));
  629. }
  630. if (!Validate::regex($mobile, "^1\d{10}$")) {
  631. $this->error(__('Mobile is incorrect'));
  632. }
  633. $result = Sms::check($mobile, $captcha, 'changemobile');
  634. if (!$result) {
  635. $this->error(__('Captcha is incorrect'));
  636. }
  637. //检查ios_user_id绑定的用户
  638. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  639. if ($user) {
  640. if ($user['status'] == -1) {
  641. $this->error('账户已经注销');
  642. }
  643. if ($user['status'] != 1) {
  644. $this->error(__('Account is locked'));
  645. }
  646. //如果已经有账号则直接登录
  647. $ret = $this->auth->direct($user['id']);
  648. $this->success('success',$this->auth->getUserinfo_simple());
  649. }
  650. //新的ios用户
  651. $where = [];
  652. $where['mobile'] = $mobile;
  653. $userData = Db::name('user')->where($where)->find();//老用户
  654. if (!empty($userData)) {
  655. if (empty($userData['ios_user_id'])) {
  656. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  657. } else {
  658. if ($userData['ios_user_id'] != $iosUserId) {
  659. $this->error('该手机号已被其他用户绑定');
  660. }
  661. }
  662. $ret = $this->auth->direct($userData['id']);
  663. } else {
  664. $extend = [
  665. 'ios_user_id' => $iosUserId,
  666. ];
  667. $ret = $this->auth->register('', '','', $mobile, $extend);
  668. }
  669. if (!$ret) {
  670. $this->error($this->auth->getError());
  671. }
  672. $this->success('success',$this->auth->getUserinfo_simple());
  673. }
  674. /**
  675. * 微信注册来的,绑定手机号
  676. *
  677. * @ApiMethod (POST)
  678. * @param string $mobile 手机号
  679. * @param string $captcha 验证码
  680. */
  681. public function bindmobile()
  682. {
  683. $mobile = $this->request->param('mobile');
  684. $captcha = $this->request->param('captcha');
  685. $code = $this->request->param('code');
  686. if (!$mobile || !$captcha || !$code) {
  687. $this->error(__('Invalid parameters'));
  688. }
  689. if (!Validate::regex($mobile, "^1\d{10}$")) {
  690. $this->error(__('Mobile is incorrect'));
  691. }
  692. $result = Sms::check($mobile, $captcha, 'changemobile');
  693. if (!$result) {
  694. $this->error(__('Captcha is incorrect'));
  695. }
  696. $wechatCodeWhere['code'] = $code;
  697. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  698. if (empty($wechatCode)) {
  699. $this->error('请先微信登录');
  700. }
  701. //检查appid绑定的用户
  702. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  703. if ($user) {
  704. if ($user['status'] == -1) {
  705. $this->error('账户已注销');
  706. }
  707. if ($user['status'] != 1) {
  708. $this->error(__('Account is locked'));
  709. }
  710. //如果已经有账号则直接登录
  711. $ret = $this->auth->direct($user['id']);
  712. $this->success('success',$this->auth->getUserinfo_simple());
  713. }
  714. //新的openid用户
  715. $where = [];
  716. $where['mobile'] = $mobile;
  717. $userData = Db::name('user')->where($where)->find();//老用户
  718. if (!empty($userData)) {
  719. if (empty($userData['wechat_openid'])) {
  720. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  721. } else {
  722. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  723. $this->error('该手机号已被其他用户绑定');
  724. }
  725. }
  726. $ret = $this->auth->direct($userData['id']);
  727. } else {
  728. $extend = [
  729. 'wechat_openid' => $wechatCode['openid'],
  730. 'plat_unique_id' => input('plat_unique_id',''),
  731. 'plat_from' => input('plat_from',''),
  732. ];
  733. $ret = $this->auth->register('', '','', $mobile, $extend);
  734. }
  735. if (!$ret) {
  736. $this->error($this->auth->getError());
  737. }
  738. $this->success('success',$this->auth->getUserinfo_simple());
  739. }
  740. /**
  741. * 手机注册来的,绑定微信
  742. *
  743. * @ApiMethod (POST)
  744. * @param string $wechat_openid
  745. */
  746. public function bindopenid()
  747. {
  748. $wechat_openid = input('wechat_openid');
  749. if (!$wechat_openid) {
  750. $this->error(__('Invalid parameters'));
  751. }
  752. if(!empty($this->auth->wechat_openid)){
  753. //$this->error('已经绑定了微信号');
  754. }
  755. $otherUserWhere['wechat_openid'] = $wechat_openid;
  756. $otherUserWhere['id'] = ['neq',$this->auth->id];
  757. if (\app\common\model\User::where($otherUserWhere)->find()) {
  758. $this->error('该微信号已被其他用户绑定');
  759. }
  760. $user = $this->auth->getUser();
  761. $user->wechat_openid = $wechat_openid;
  762. $user->save();
  763. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  764. }
  765. /**
  766. * 重置密码
  767. *
  768. * @ApiMethod (POST)
  769. * @param string $mobile 手机号
  770. * @param string $newpassword 新密码
  771. * @param string $captcha 验证码
  772. */
  773. public function resetpwd()
  774. {
  775. //$type = input("type");
  776. $type = 'mobile';
  777. $mobile = input("mobile");
  778. // $email = input("email");
  779. $newpassword = input("newpassword");
  780. $captcha = input("captcha");
  781. if (!$mobile || !$newpassword || !$captcha) {
  782. $this->error(__('Invalid parameters'));
  783. }
  784. if ($type == 'mobile') {
  785. if (!Validate::regex($mobile, "^1\d{10}$")) {
  786. $this->error(__('Mobile is incorrect'));
  787. }
  788. $user = \app\common\model\User::getByMobile($mobile);
  789. if (!$user) {
  790. $this->error(__('User not found'));
  791. }
  792. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  793. if (!$ret) {
  794. $this->error(__('Captcha is incorrect'));
  795. }
  796. Sms::flush($mobile, 'resetpwd');
  797. }
  798. //模拟一次登录
  799. $this->auth->direct($user->id);
  800. $ret = $this->auth->changepwd($newpassword, '', true);
  801. if ($ret) {
  802. $this->success(__('Reset password successful'));
  803. } else {
  804. $this->error($this->auth->getError());
  805. }
  806. }
  807. /**
  808. * 修改密码
  809. *
  810. * @ApiMethod (POST)
  811. * @param string $newpassword 新密码
  812. * @param string $oldpassword 旧密码
  813. */
  814. public function changepwd(){
  815. $newpassword = input('newpassword');
  816. $oldpassword = input('oldpassword','');
  817. if (!$newpassword) {
  818. $this->error(__('Invalid parameters'));
  819. }
  820. if($this->auth->password && empty($oldpassword)){
  821. $this->error('原密码必填');
  822. }
  823. if(empty($this->auth->password)){
  824. $ret = $this->auth->changepwd($newpassword, '', true);
  825. }else{
  826. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  827. }
  828. if ($ret) {
  829. $this->success('设置密码成功');
  830. } else {
  831. $this->error($this->auth->getError());
  832. }
  833. }
  834. //假注销
  835. public function cancleUser(){
  836. $captcha = input('captcha','');
  837. if (!$captcha) {
  838. $this->error(__('Invalid parameters'));
  839. }
  840. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  841. $this->error(__('Captcha is incorrect'));
  842. }
  843. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  844. $this->auth->logout();
  845. $this->success('注销成功');
  846. }
  847. //文字语音视频收费设置
  848. public function chargeconfig() {
  849. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
  850. if (!in_array($type, [0, 1, 2])) {
  851. $this->error('您的网络开小差啦~');
  852. }
  853. $where['type'] = $type;
  854. $where['level'] = ['elt',$this->auth->charm_level];
  855. $list = Db::name('charge_config')->field('id, price, level')->where($where)->order('price asc')->select();
  856. $this->success('success', $list);
  857. }
  858. //文字语音视频收费/隐藏所在位置设置
  859. public function chargeset() {
  860. if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
  861. $this->error('请先完成实名认证');
  862. }
  863. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  864. $this->error('请先完成真人认证');
  865. }
  866. $chat_id = input('chat_id', 0, 'intval'); //文字收费id
  867. $voice_id = input('voice_id', 0, 'intval'); //语音收费id
  868. $video_id = input('video_id', 0, 'intval'); //视频收费id
  869. $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
  870. $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
  871. $data = [];
  872. //查询我的魅力等级
  873. $level = $this->auth->charm_level;
  874. if ($chat_id) {
  875. $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
  876. if (!$charge_config) {
  877. $this->error('您的网络开小差啦~');
  878. }
  879. if ($level < $charge_config['level']) {
  880. $this->error('您还未满足条件~');
  881. }
  882. $data['match_typing_price'] = $charge_config['price'];
  883. }
  884. if ($voice_id) {
  885. $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
  886. if (!$charge_config) {
  887. $this->error('您的网络开小差啦~');
  888. }
  889. if ($level < $charge_config['level']) {
  890. $this->error('您还未满足条件~');
  891. }
  892. $data['match_audio_price'] = $charge_config['price'];
  893. }
  894. if ($video_id) {
  895. $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
  896. if (!$charge_config) {
  897. $this->error('您的网络开小差啦~');
  898. }
  899. if ($level < $charge_config['level']) {
  900. $this->error('您还未满足条件~');
  901. }
  902. $data['match_video_price'] = $charge_config['price'];
  903. }
  904. if (in_array($open_match_audio, [1, 0])) {
  905. $data['open_match_audio'] = $open_match_audio;
  906. }
  907. if (in_array($open_match_video, [1, 0])) {
  908. $data['open_match_video'] = $open_match_video;
  909. }
  910. if (!$data) {
  911. $this->error('没有修改信息~');
  912. }
  913. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  914. if ($rs === false) {
  915. $this->error('您的网络开小差啦~');
  916. }
  917. $this->success('设置成功');
  918. }
  919. }