User.php 34 KB

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