User.php 32 KB

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