User.php 32 KB

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