User.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. use addons\epay\library\Wechat;
  15. /**
  16. * 会员接口,登录,注册,修改资料等
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. if (!Config::get('fastadmin.usercenter')) {
  26. $this->error(__('User center already closed'));
  27. }
  28. }
  29. /**
  30. * 会员中心
  31. */
  32. public function index()
  33. {
  34. $this->success('', ['welcome' => $this->auth->nickname]);
  35. }
  36. /**
  37. * 会员登录
  38. *
  39. * @ApiMethod (POST)
  40. * @param string $account 账号
  41. * @param string $password 密码
  42. */
  43. public function login()
  44. {
  45. $account = input('account');
  46. $password = input('password');
  47. if (!$account || !$password) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $ret = $this->auth->login($account, $password);
  51. if ($ret) {
  52. $data = $this->userInfo('return');
  53. $this->success(__('Logged in successful'), $data);
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 手机验证码登录
  60. *
  61. * @ApiMethod (POST)
  62. * @param string $mobile 手机号
  63. * @param string $captcha 验证码
  64. */
  65. public function mobilelogin()
  66. {
  67. $mobile = input('mobile');
  68. $captcha = input('captcha');
  69. if (!$mobile || !$captcha) {
  70. $this->error(__('Invalid parameters'));
  71. }
  72. if (!Validate::regex($mobile, "^1\d{10}$")) {
  73. $this->error(__('Mobile is incorrect'));
  74. }
  75. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  76. $this->error(__('Captcha is incorrect'));
  77. }
  78. $user = \app\common\model\User::getByMobile($mobile);
  79. if ($user) {
  80. if ($user->status == -1) {
  81. $this->error('账户已注销');
  82. }
  83. if ($user->status != 1) {
  84. $this->error(__('Account is locked'));
  85. }
  86. //如果已经有账号则直接登录
  87. $ret = $this->auth->direct($user->id);
  88. } else {
  89. $extend = [
  90. 'register_from' => input('register_from',''),
  91. ];
  92. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  93. //亿米
  94. $ua = input('ua','','trim');
  95. $this->yimi_advert($ua);
  96. }
  97. if ($ret) {
  98. Sms::flush($mobile, 'mobilelogin');
  99. $data = $this->userInfo('return');
  100. $this->success(__('Logged in successful'), $data);
  101. } else {
  102. $this->error($this->auth->getError());
  103. }
  104. }
  105. /**
  106. * 注册会员
  107. *
  108. * @ApiMethod (POST)
  109. * @param string $username 用户名
  110. * @param string $password 密码
  111. * @param string $email 邮箱
  112. * @param string $mobile 手机号
  113. * @param string $code 验证码
  114. */
  115. /*public function register()
  116. {
  117. $username = $this->request->post('username');
  118. $password = $this->request->post('password');
  119. $email = $this->request->post('email');
  120. $mobile = $this->request->post('mobile');
  121. $code = $this->request->post('code');
  122. if (!$username || !$password) {
  123. $this->error(__('Invalid parameters'));
  124. }
  125. if ($email && !Validate::is($email, "email")) {
  126. $this->error(__('Email is incorrect'));
  127. }
  128. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  129. $this->error(__('Mobile is incorrect'));
  130. }
  131. $ret = Sms::check($mobile, $code, 'register');
  132. if (!$ret) {
  133. $this->error(__('Captcha is incorrect'));
  134. }
  135. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  136. if ($ret) {
  137. $data = $this->userInfo('return');
  138. $this->success(__('Sign up successful'), $data);
  139. } else {
  140. $this->error($this->auth->getError());
  141. }
  142. }*/
  143. //微信登录+注册
  144. public function wechatlogin(){
  145. $nickname = input('nickname','');
  146. $avatar = input('avatar','');
  147. $gender = input('gender',1);
  148. $wechat_openid = input('wechat_openid','');
  149. if (!$wechat_openid) {
  150. $this->error(__('Invalid parameters'));
  151. }
  152. if($gender != 1){
  153. $gender = 0;
  154. }
  155. $user = \app\common\model\User::getByOpenid($wechat_openid);
  156. if ($user) {
  157. if ($user->status != 1) {
  158. $this->error(__('Account is locked'));
  159. }
  160. //如果已经有账号则直接登录
  161. $ret = $this->auth->direct($user->id);
  162. } else {
  163. if (!$nickname || !$avatar) {
  164. $this->error(__('Invalid parameters'));
  165. }
  166. $reg_data = [
  167. 'nickname'=>$nickname,
  168. 'avatar'=>$avatar,
  169. 'gender'=>$gender,
  170. 'register_from' => input('register_from',''),
  171. ];
  172. $ret = $this->auth->openid_register($wechat_openid,$reg_data);
  173. //亿米
  174. $ua = input('ua','','trim');
  175. $this->yimi_advert($ua);
  176. }
  177. if ($ret) {
  178. $data = $this->userInfo('return');
  179. $this->success(__('Logged in successful'), $data);
  180. } else {
  181. $this->error($this->auth->getError());
  182. }
  183. }
  184. /**
  185. * 运营商一键登录
  186. */
  187. public function onLogin()
  188. {
  189. $accessToken = input('accessToken');// 运营商预取号获取到的token
  190. $token = input('tokenT');// 易盾返回的token
  191. if (!$accessToken || !$token) {
  192. $this->error("参数获取失败!");
  193. }
  194. $params = array(
  195. // 运营商预取号获取到的token
  196. "accessToken" => $accessToken,
  197. // 易盾返回的token
  198. "token" => $token
  199. );
  200. // 获取密钥配置
  201. $configInfo = config("onLogin");
  202. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  203. $onret = $onlogin->check($params);
  204. // $ret = [];
  205. // $ret["code"] = 200;
  206. // $ret["msg"] = "ok";
  207. // $ret["data"] = [
  208. // "phone" => "17574504021",
  209. // "resultCode" => 0
  210. // ];
  211. if ($onret["code"] == 200) {
  212. $mobile = $onret["data"]["phone"];
  213. if (empty($mobile)) {
  214. // 取号失败,建议进行二次验证,例如短信验证码
  215. $this->error("取号登录失败,请用验证码方式登录!");
  216. } else {
  217. // 取号成功, 执行登录等流程
  218. // 用户登录逻辑 === 开始
  219. $user = \app\common\model\User::getByMobile($mobile);
  220. if ($user) {
  221. if ($user->status != 1) {
  222. $this->error(__('Account is locked'));
  223. }
  224. //如果已经有账号则直接登录
  225. $ret = $this->auth->direct($user->id);
  226. $is_register = 0;
  227. } else {
  228. $extend = [
  229. 'register_from' => input('register_from',''),
  230. ];
  231. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  232. $is_register = 1;
  233. //亿米
  234. $ua = input('ua','','trim');
  235. $this->yimi_advert($ua);
  236. }
  237. //结果
  238. /*$rs['userinfo'] = $this->auth->getUserinfo();
  239. $rs['is_register'] = $is_register;*/
  240. if ($ret) {
  241. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  242. } else {
  243. $this->error($this->auth->getError());
  244. }
  245. // 用户登录逻辑 === 结束
  246. }
  247. } else {
  248. $this->error("登录失败,请用验证码方式登录!");
  249. }
  250. }
  251. //用户详细资料
  252. public function userInfo($type = 1){
  253. $info = $this->auth->getUserinfo();
  254. if($type == 'return'){
  255. return $info;
  256. }
  257. $this->success(__('success'),$info);
  258. }
  259. //用户邀请信息
  260. public function userintroinfo(){
  261. $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
  262. $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
  263. $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
  264. $rs = [
  265. 'intro_num' => $intro_num,
  266. 'money_sum' => $money_sum,
  267. 'user_list' => $user_list,
  268. ];
  269. $this->success('success',$rs);
  270. }
  271. public function testhaibao(){
  272. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
  273. dump($haibao);
  274. }
  275. //海报
  276. public function haibao($player_id,$data){
  277. //下载页二维码,没必要保留
  278. $params = [
  279. 'text' => config('site.domain_name').'/index/index/appdownload',
  280. 'size' => 90,
  281. 'logo' => false,
  282. 'label' => false,
  283. 'padding' => 0,
  284. ];
  285. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  286. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  287. mk_dir($qrcode_path);
  288. $download_qrcode = $qrcode_path.'/download.png';
  289. $qrCode->writeFile($download_qrcode);
  290. //海报
  291. $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
  292. return $haibao;
  293. }
  294. public function createhaibao($download_qrcode,$player_id,$sub_data){
  295. //背景图
  296. $background = config('site.domain_name').'/assets/img/haibao.png';
  297. //海报图片路径
  298. $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
  299. mk_dir($new_path);
  300. $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
  301. //二维码
  302. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  303. //合成wap图片
  304. $image = new \addons\poster\library\Image2();
  305. $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
  306. return '/'.$wap_file_name;
  307. }
  308. //申请真人认证
  309. public function apply_real_confirm(){
  310. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  311. if(!$avatar){
  312. $this->error('请上传真人头像');
  313. }
  314. Db::startTrans();
  315. $data = [
  316. 'avatar' => $avatar,
  317. 'real_status' => 1,
  318. ];
  319. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  320. if($rs === false){
  321. Db::rollback();
  322. $this->error('认证失败');
  323. }
  324. //tag任务赠送金币
  325. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  326. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  327. if($task_rs === false){
  328. Db::rollback();
  329. $this->error('完成任务赠送奖励失败');
  330. }
  331. //完成真人头像 +5金币
  332. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  333. if($task_rs === false){
  334. Db::rollback();
  335. $this->error('完成任务赠送奖励失败');
  336. }
  337. //邀请人拿奖励,男性3元
  338. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  339. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  340. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  341. if($task_rs['status'] === false){
  342. Db::rollback();
  343. $this->error($task_rs['msg']);
  344. }
  345. }
  346. //系统消息
  347. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  348. Db::commit();
  349. $this->success();
  350. }
  351. //实名认证信息
  352. public function idcard_confirm_info(){
  353. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  354. $this->success('success',$check);
  355. }
  356. //申请实名认证
  357. public function apply_idcard_confirm(){
  358. $truename = input('truename','');
  359. $idcard = input('idcard','');
  360. //$idcard_images = input('idcard_images','');
  361. $alipay_account = input('alipay_account','');
  362. if(empty($truename) || empty($idcard) || empty($alipay_account)){
  363. $this->error('实名认证信息必填');
  364. }
  365. if($this->auth->idcard_status == 1){
  366. $this->error('您已经完成实名认证');
  367. }
  368. if($this->auth->idcard_status == 0){
  369. $this->error('您已经提交实名认证,请等待审核');
  370. }
  371. Db::startTrans();
  372. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  373. if(!empty($check)){
  374. if($check['status'] == 0){
  375. Db::rollback();
  376. $this->error('您已经提交实名认证,请等待审核');
  377. }
  378. if($check['status'] == 1){
  379. Db::rollback();
  380. $this->error('您已经完成实名认证');
  381. }
  382. }
  383. $data = [
  384. 'user_id' => $this->auth->id,
  385. 'truename' => $truename,
  386. 'idcard' => $idcard,
  387. //'idcard_images' => $idcard_images,
  388. 'alipay_account' => $alipay_account,
  389. 'status' => 0,
  390. 'createtime' => time(),
  391. 'updatetime' => time(),
  392. ];
  393. //更新
  394. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  395. if(!empty($check)){
  396. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  397. }else{
  398. $rs = Db::name('user_idconfirm')->insertGetId($data);
  399. }
  400. if(!$rs || !$update_rs){
  401. Db::rollback();
  402. $this->error('提交失败');
  403. }
  404. Db::commit();
  405. $this->success('提交成功,请等待审核');
  406. }
  407. /**
  408. * 退出登录
  409. * @ApiMethod (POST)
  410. */
  411. public function logout()
  412. {
  413. if (!$this->request->isPost()) {
  414. $this->error(__('Invalid parameters'));
  415. }
  416. //退出im
  417. $tenIm = new Tenim();
  418. $tenIm->loginoutim($this->auth->id);
  419. //修改用户活跃0
  420. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  421. $this->auth->logout();
  422. $this->success(__('Logout successful'));
  423. }
  424. /**
  425. * 修改会员个人信息
  426. *
  427. * @ApiMethod (POST)
  428. * @param string $avatar 头像地址
  429. * @param string $username 用户名
  430. * @param string $nickname 昵称
  431. * @param string $bio 个人简介
  432. */
  433. public function profile()
  434. {
  435. $field_array = ['nickname','introcode','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo'];
  436. $data = [];
  437. foreach($field_array as $key => $field){
  438. if(!input('?'.$field)){
  439. continue;
  440. }
  441. $newone = input($field);
  442. if($field == 'avatar'){
  443. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  444. }
  445. if($field == 'photo_images'){
  446. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  447. }
  448. $data[$field] = $newone;
  449. }
  450. if(isset($data['birthday'])){
  451. $data['birthday'] = strtotime($data['birthday']);
  452. }
  453. if(isset($data['avatar'])){
  454. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  455. }
  456. if(isset($data['hobby_ids'])){
  457. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  458. }
  459. if(isset($data['tag_ids'])){
  460. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  461. }
  462. if(isset($data['introcode'])){
  463. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  464. if(!$intro_user){
  465. $this->error('不存在的邀请人');
  466. }
  467. unset($data['introcode']);//别人的邀请码,不能改了自己的
  468. $data['intro_uid'] = $intro_user;
  469. }
  470. //dump($data);
  471. if(empty($data)){
  472. $this->error('没有任何改变');
  473. }
  474. //默认头像
  475. //现在没头像,也没穿头像,但是却传了性别
  476. if($this->auth->avatar == config('site.domain_cdnurl').'/avatar.png' && isset($data['gender']) && $data['avatar'] == config('site.domain_cdnurl').'/avatar.png'){
  477. if($data['gender'] == 1){
  478. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f9277fba97fd76d9ecfc63b506a3674a.png';
  479. }else{
  480. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/5030460c05c86774f60123ffea7b78a1.png';
  481. }
  482. }
  483. //默认头像
  484. Db::startTrans();
  485. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  486. if($update_rs === false){
  487. Db::rollback();
  488. $this->error('修改资料失败');
  489. }
  490. //tag任务赠送金币
  491. //上传头像加5金币
  492. if(isset($data['avatar'])){
  493. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  494. if($task_rs === false){
  495. Db::rollback();
  496. $this->error('完成任务赠送奖励失败');
  497. }
  498. }
  499. //上传本人语音介绍 10金币
  500. if(isset($data['audio_bio'])){
  501. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  502. if($task_rs === false){
  503. Db::rollback();
  504. $this->error('完成任务赠送奖励失败');
  505. }
  506. }
  507. //上传本人五张照片 10金币
  508. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  509. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  510. if($task_rs === false){
  511. Db::rollback();
  512. $this->error('完成任务赠送奖励失败');
  513. }
  514. }
  515. //所有资料完成
  516. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  517. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  518. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  519. if($task_rs === false){
  520. Db::rollback();
  521. $this->error('完成任务赠送奖励失败');
  522. }
  523. }
  524. Db::commit();
  525. $this->success();
  526. }
  527. public function set_status_switch(){
  528. }
  529. /*
  530. * 修改用户的坐标
  531. * */
  532. public function change_longlat(){
  533. $longitude = input_post('longitude');
  534. $latitude = input_post('latitude');
  535. $cityname = input_post('cityname');
  536. if(empty($longitude) || empty($latitude) || empty($cityname)){
  537. $this->error();
  538. }
  539. $data = [
  540. 'longitude' => $longitude,
  541. 'latitude' => $latitude,
  542. 'cityname' => $cityname,
  543. ];
  544. Db::name('user')->where('id',$this->auth->id)->update($data);
  545. $this->success();
  546. }
  547. /**
  548. * 修改邮箱
  549. *
  550. * @ApiMethod (POST)
  551. * @param string $email 邮箱
  552. * @param string $captcha 验证码
  553. */
  554. /*public function changeemail()
  555. {
  556. $user = $this->auth->getUser();
  557. $email = $this->request->post('email');
  558. $captcha = $this->request->post('captcha');
  559. if (!$email || !$captcha) {
  560. $this->error(__('Invalid parameters'));
  561. }
  562. if (!Validate::is($email, "email")) {
  563. $this->error(__('Email is incorrect'));
  564. }
  565. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  566. $this->error(__('Email already exists'));
  567. }
  568. $result = Ems::check($email, $captcha, 'changeemail');
  569. if (!$result) {
  570. $this->error(__('Captcha is incorrect'));
  571. }
  572. $verification = $user->verification;
  573. $verification->email = 1;
  574. $user->verification = $verification;
  575. $user->email = $email;
  576. $user->save();
  577. Ems::flush($email, 'changeemail');
  578. $this->success();
  579. }*/
  580. /**
  581. * 修改手机号
  582. *
  583. * @ApiMethod (POST)
  584. * @param string $mobile 手机号
  585. * @param string $captcha 验证码
  586. */
  587. public function changemobile()
  588. {
  589. $user = $this->auth->getUser();
  590. $oldcaptcha = $this->request->request('oldcaptcha');
  591. $mobile = $this->request->request('mobile');
  592. $captcha = $this->request->request('captcha');
  593. if (!$oldcaptcha || !$mobile || !$captcha) {
  594. $this->error(__('Invalid parameters'));
  595. }
  596. if (!Validate::regex($mobile, "^1\d{10}$")) {
  597. $this->error(__('Mobile is incorrect'));
  598. }
  599. if($user->mobile == $mobile){
  600. $this->error('新手机号不能与旧手机号相同');
  601. }
  602. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  603. $this->error(__('Mobile already exist'));
  604. }
  605. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  606. if (!$result) {
  607. $this->error(__('Captcha is incorrect'));
  608. }
  609. $result = Sms::check($mobile, $captcha, 'changemobile');
  610. if (!$result) {
  611. $this->error(__('Captcha is incorrect'));
  612. }
  613. $verification = $user->verification;
  614. $verification->mobile = 1;
  615. $user->verification = $verification;
  616. $user->mobile = $mobile;
  617. $user->save();
  618. Sms::flush($user->mobile, 'changemobile');
  619. Sms::flush($mobile, 'changemobile');
  620. $this->success();
  621. }
  622. /**
  623. * 微信注册来的,绑定手机号
  624. *
  625. * @ApiMethod (POST)
  626. * @param string $mobile 手机号
  627. * @param string $captcha 验证码
  628. */
  629. public function bindmobile()
  630. {
  631. $user = $this->auth->getUser();
  632. $mobile = $this->request->request('mobile');
  633. $captcha = $this->request->request('captcha');
  634. if(!empty($this->auth->mobile)){
  635. $this->error('已经绑定了手机号');
  636. }
  637. if (!$mobile || !$captcha) {
  638. $this->error(__('Invalid parameters'));
  639. }
  640. if (!Validate::regex($mobile, "^1\d{10}$")) {
  641. $this->error(__('Mobile is incorrect'));
  642. }
  643. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  644. $this->error('该手机号已被其他用户绑定');
  645. }
  646. $result = Sms::check($mobile, $captcha, 'changemobile');
  647. if (!$result) {
  648. $this->error(__('Captcha is incorrect'));
  649. }
  650. $verification = $user->verification;
  651. $verification->mobile = 1;
  652. $user->verification = $verification;
  653. $user->mobile = $mobile;
  654. $user->save();
  655. Sms::flush($mobile, 'changemobile');
  656. $this->success('success',$this->userInfo('return'));
  657. }
  658. /**
  659. * 微信注册来的,绑定手机号
  660. *
  661. * @ApiMethod (POST)
  662. * @param string $mobile 手机号
  663. * @param string $captcha 验证码
  664. */
  665. public function bindopenid()
  666. {
  667. $user = $this->auth->getUser();
  668. $wechat_openid = $this->request->request('wechat_openid');
  669. if(!empty($this->auth->wechat_openid)){
  670. $this->error('已经绑定了微信号');
  671. }
  672. if (!$wechat_openid) {
  673. $this->error(__('Invalid parameters'));
  674. }
  675. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  676. $this->error('该微信号已被其他用户绑定');
  677. }
  678. $user->wechat_openid = $wechat_openid;
  679. $user->save();
  680. $this->success('success',$this->userInfo('return'));
  681. }
  682. /**
  683. * 第三方登录
  684. *
  685. * @ApiMethod (POST)
  686. * @param string $platform 平台名称
  687. * @param string $code Code码
  688. */
  689. /*public function third()
  690. {
  691. $url = url('user/index');
  692. // $platform = $this->request->post("platform");
  693. $platform = 'wechat';
  694. $code = $this->request->post("code");
  695. $config = get_addon_config('third');
  696. if (!$config || !isset($config[$platform])) {
  697. $this->error(__('Invalid parameters'));
  698. }
  699. $app = new \addons\third\library\Application($config);
  700. //通过code换access_token和绑定会员
  701. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  702. if ($result) {
  703. $loginret = \addons\third\library\Service::connect($platform, $result);
  704. if ($loginret) {
  705. $data = [
  706. 'userinfo' => $this->auth->getUserinfo(),
  707. 'thirdinfo' => $result
  708. ];
  709. $this->success(__('Logged in successful'), $data);
  710. }
  711. }
  712. $this->error(__('Operation failed'), $url);
  713. }*/
  714. /**
  715. * 重置密码
  716. *
  717. * @ApiMethod (POST)
  718. * @param string $mobile 手机号
  719. * @param string $newpassword 新密码
  720. * @param string $captcha 验证码
  721. */
  722. public function resetpwd()
  723. {
  724. //$type = input("type");
  725. $type = 'mobile';
  726. $mobile = input("mobile");
  727. $email = input("email");
  728. $newpassword = input("newpassword");
  729. $captcha = input("captcha");
  730. if (!$newpassword || !$captcha) {
  731. $this->error(__('Invalid parameters'));
  732. }
  733. if ($type == 'mobile') {
  734. if (!Validate::regex($mobile, "^1\d{10}$")) {
  735. $this->error(__('Mobile is incorrect'));
  736. }
  737. $user = \app\common\model\User::getByMobile($mobile);
  738. if (!$user) {
  739. $this->error(__('User not found'));
  740. }
  741. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  742. if (!$ret) {
  743. $this->error(__('Captcha is incorrect'));
  744. }
  745. Sms::flush($mobile, 'resetpwd');
  746. } else {
  747. if (!Validate::is($email, "email")) {
  748. $this->error(__('Email is incorrect'));
  749. }
  750. $user = \app\common\model\User::getByEmail($email);
  751. if (!$user) {
  752. $this->error(__('User not found'));
  753. }
  754. $ret = Ems::check($email, $captcha, 'resetpwd');
  755. if (!$ret) {
  756. $this->error(__('Captcha is incorrect'));
  757. }
  758. Ems::flush($email, 'resetpwd');
  759. }
  760. //模拟一次登录
  761. $this->auth->direct($user->id);
  762. $ret = $this->auth->changepwd($newpassword, '', true);
  763. if ($ret) {
  764. $this->success(__('Reset password successful'));
  765. } else {
  766. $this->error($this->auth->getError());
  767. }
  768. }
  769. /**
  770. * 修改密码
  771. *
  772. * @ApiMethod (POST)
  773. * @param string $newpassword 新密码
  774. * @param string $oldpassword 旧密码
  775. */
  776. public function changepwd(){
  777. $newpassword = input('newpassword');
  778. $oldpassword = input('oldpassword','');
  779. if (!$newpassword) {
  780. $this->error(__('Invalid parameters'));
  781. }
  782. if($this->auth->password && empty($oldpassword)){
  783. $this->error('原密码必填');
  784. }
  785. if(empty($this->auth->password)){
  786. $ret = $this->auth->changepwd($newpassword, '', true);
  787. }else{
  788. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  789. }
  790. if ($ret) {
  791. $this->success(__('Reset password successful'));
  792. } else {
  793. $this->error($this->auth->getError());
  794. }
  795. }
  796. /**
  797. * 记录当前登陆的设备ID,设备信息,IP等
  798. */
  799. public function changeDeviceIp()
  800. {
  801. // 接口防并发
  802. if (!$this->apiLimit(1, 5000)) {
  803. return ;
  804. }
  805. $user = $this->auth->getUser();
  806. $ip = request()->ip();
  807. $deviceId = $this->request->request('device_id','');
  808. $phoneModel = $this->request->request('phone_model','');
  809. $brand = $this->request->request('brand','');
  810. $apiVersion = $this->request->request('api_version','');
  811. $deviceOs = $this->request->request('device_os','');
  812. if ($ip !== $user->loginip){
  813. $update = [];
  814. $update['id'] = $user->id;
  815. $update['loginip'] = $ip;
  816. \app\common\model\User::update($update);
  817. }
  818. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  819. if (empty($userDeviceInfo)){
  820. $userDeviceInfo = new UserDeviceInfo();
  821. $userDeviceInfo->user_id = $user->u_id;
  822. }
  823. $userDeviceInfo->device_os = $deviceOs;
  824. $userDeviceInfo->device_id = $deviceId;
  825. $userDeviceInfo->phone_model = $phoneModel;
  826. $userDeviceInfo->brand = $brand;
  827. $userDeviceInfo->api_version = $apiVersion;
  828. $userDeviceInfo->save();
  829. //首页接口调用,这里不反回信息
  830. // $this->success("更新成功!");
  831. }
  832. //假注销
  833. public function cancleUser(){
  834. if (!$this->request->isPost()) {
  835. $this->error(__('Invalid parameters'));
  836. }
  837. //退出im
  838. $tenIm = new Tenim();
  839. $tenIm->loginoutim($this->auth->id);
  840. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  841. $this->auth->logout();
  842. $this->success('注销成功');
  843. }
  844. //修改用户活跃1
  845. public function useractive(){
  846. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 1,'active_time' => time()]);
  847. $this->success('success');
  848. }
  849. //APP 转化数据统计方案(即:APP 上报对接方案): 广告主上报激活数据,亿米平台搭建服务系统关联点击&下载数据和广告主提供的所有激活数据,将激活数据归因到对应广告。
  850. public function yimi_advert($ua = ''){
  851. //http://trail.e.mi.com/global/log?appId={appid}&info={data}&conv_type={convType}&customer_id={customerId}
  852. $api_url = 'http://trail.e.mi.com/global/log?';
  853. $api_url_test = 'http://trail.e.mi.com/global/test?';
  854. //应用id 1453045
  855. //秘钥A(encrypt_key):ZxdIaVHvFqSQYzWD
  856. //秘钥B(sign_key):uaeWeunykLRnkyLw
  857. $sign_key = 'uaeWeunykLRnkyLw'; //真的
  858. $encrypt_key = 'ZxdIaVHvFqSQYzWD';//真的
  859. $appid = '1453045';
  860. $conv_type = 'APP_REGISTER';
  861. $customer_id = '292232';
  862. //推荐模式
  863. /*$imei = md5('imei');
  864. $data = [
  865. 'imei' => '91b9185dba1772851dd02b276a6c969e',
  866. 'oaid' => '5fb96f268628810c',
  867. 'conv_time' => '1504687208890',
  868. 'client_ip' => '127.0.0.1',
  869. 'ua' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
  870. ];*/
  871. //采用模式
  872. if(empty($ua)){
  873. return true;
  874. }
  875. $data = [
  876. 'conv_time' => time().substr(microtime(),2,3),
  877. 'client_ip' => request()->ip(),
  878. 'ua' => $ua,
  879. ];
  880. $data_query = http_build_query($data);
  881. //dump($data_query);
  882. $property = $sign_key.'&'.urlencode($data_query);
  883. //dump($property);
  884. $signature = md5($property);
  885. //dump($signature);
  886. $base_data = $data_query .'&sign='.urlencode($signature);
  887. //echo $base_data;
  888. $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
  889. //dump($info);
  890. $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
  891. //echo $request_url;
  892. $result = curl_get($request_url);
  893. //dump($result);
  894. //日志
  895. $log = [
  896. 'param' => $base_data,
  897. 'url' => $request_url,
  898. 'result'=> $result,
  899. 'createtime' => time(),
  900. ];
  901. Db::name('yimi_advert')->insertGetId($log);
  902. return true;
  903. }
  904. //亿米 异或加密,解密
  905. public function xor_enc($str,$key)
  906. {
  907. $crytxt = '';
  908. $keylen = strlen($key);
  909. for($i=0;$i<strlen($str);$i++)
  910. {
  911. $k = $i%$keylen;
  912. $crytxt .= $str[$i] ^ $key[$k];
  913. }
  914. return $crytxt;
  915. }
  916. //公众号获取openid
  917. public function getUserOpenid_gzh(){
  918. $configValue = Service::getConfig('wechat');
  919. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  920. $rs = $wechat->getOpenid();
  921. $this->success('success',$rs);
  922. }
  923. /**
  924. * 微信内H5-JSAPI支付
  925. */
  926. public function jssdkBuildConfig() {
  927. $url = $this->request->request("url");
  928. $configValue = Service::getConfig('wechat');
  929. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  930. $sign = $wechat->getSignPackage(urldecode($url));
  931. $this->success("获取成功!",$sign);
  932. }
  933. }