User.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. $this->auth->logout();
  420. $this->success(__('Logout successful'));
  421. }
  422. /**
  423. * 修改会员个人信息
  424. *
  425. * @ApiMethod (POST)
  426. * @param string $avatar 头像地址
  427. * @param string $username 用户名
  428. * @param string $nickname 昵称
  429. * @param string $bio 个人简介
  430. */
  431. public function profile()
  432. {
  433. $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'];
  434. $data = [];
  435. foreach($field_array as $key => $field){
  436. if(!input('?'.$field)){
  437. continue;
  438. }
  439. $newone = input($field);
  440. if($field == 'avatar'){
  441. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  442. }
  443. if($field == 'photo_images'){
  444. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  445. }
  446. $data[$field] = $newone;
  447. }
  448. if(isset($data['birthday'])){
  449. $data['birthday'] = strtotime($data['birthday']);
  450. }
  451. if(isset($data['avatar'])){
  452. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  453. }
  454. if(isset($data['hobby_ids'])){
  455. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  456. }
  457. if(isset($data['tag_ids'])){
  458. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  459. }
  460. if(isset($data['introcode'])){
  461. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  462. if(!$intro_user){
  463. $this->error('不存在的邀请人');
  464. }
  465. unset($data['introcode']);//别人的邀请码,不能改了自己的
  466. $data['intro_uid'] = $intro_user;
  467. }
  468. //dump($data);
  469. if(empty($data)){
  470. $this->error('没有任何改变');
  471. }
  472. //默认头像
  473. //现在没头像,也没穿头像,但是却传了性别
  474. if($this->auth->avatar == config('site.domain_cdnurl').'/avatar.png' && isset($data['gender']) && $data['avatar'] == config('site.domain_cdnurl').'/avatar.png'){
  475. if($data['gender'] == 1){
  476. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f9277fba97fd76d9ecfc63b506a3674a.png';
  477. }else{
  478. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/5030460c05c86774f60123ffea7b78a1.png';
  479. }
  480. }
  481. //默认头像
  482. Db::startTrans();
  483. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  484. if($update_rs === false){
  485. Db::rollback();
  486. $this->error('修改资料失败');
  487. }
  488. //tag任务赠送金币
  489. //上传头像加5金币
  490. if(isset($data['avatar'])){
  491. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  492. if($task_rs === false){
  493. Db::rollback();
  494. $this->error('完成任务赠送奖励失败');
  495. }
  496. }
  497. //上传本人语音介绍 10金币
  498. if(isset($data['audio_bio'])){
  499. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  500. if($task_rs === false){
  501. Db::rollback();
  502. $this->error('完成任务赠送奖励失败');
  503. }
  504. }
  505. //上传本人五张照片 10金币
  506. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  507. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  508. if($task_rs === false){
  509. Db::rollback();
  510. $this->error('完成任务赠送奖励失败');
  511. }
  512. }
  513. //所有资料完成
  514. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  515. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  516. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  517. if($task_rs === false){
  518. Db::rollback();
  519. $this->error('完成任务赠送奖励失败');
  520. }
  521. }
  522. Db::commit();
  523. $this->success();
  524. }
  525. public function set_status_switch(){
  526. }
  527. /*
  528. * 修改用户的坐标
  529. * */
  530. public function change_longlat(){
  531. $longitude = input_post('longitude');
  532. $latitude = input_post('latitude');
  533. $cityname = input_post('cityname');
  534. if(empty($longitude) || empty($latitude) || empty($cityname)){
  535. $this->error();
  536. }
  537. $data = [
  538. 'longitude' => $longitude,
  539. 'latitude' => $latitude,
  540. 'cityname' => $cityname,
  541. ];
  542. Db::name('user')->where('id',$this->auth->id)->update($data);
  543. $this->success();
  544. }
  545. /**
  546. * 修改邮箱
  547. *
  548. * @ApiMethod (POST)
  549. * @param string $email 邮箱
  550. * @param string $captcha 验证码
  551. */
  552. /*public function changeemail()
  553. {
  554. $user = $this->auth->getUser();
  555. $email = $this->request->post('email');
  556. $captcha = $this->request->post('captcha');
  557. if (!$email || !$captcha) {
  558. $this->error(__('Invalid parameters'));
  559. }
  560. if (!Validate::is($email, "email")) {
  561. $this->error(__('Email is incorrect'));
  562. }
  563. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  564. $this->error(__('Email already exists'));
  565. }
  566. $result = Ems::check($email, $captcha, 'changeemail');
  567. if (!$result) {
  568. $this->error(__('Captcha is incorrect'));
  569. }
  570. $verification = $user->verification;
  571. $verification->email = 1;
  572. $user->verification = $verification;
  573. $user->email = $email;
  574. $user->save();
  575. Ems::flush($email, 'changeemail');
  576. $this->success();
  577. }*/
  578. /**
  579. * 修改手机号
  580. *
  581. * @ApiMethod (POST)
  582. * @param string $mobile 手机号
  583. * @param string $captcha 验证码
  584. */
  585. public function changemobile()
  586. {
  587. $user = $this->auth->getUser();
  588. $oldcaptcha = $this->request->request('oldcaptcha');
  589. $mobile = $this->request->request('mobile');
  590. $captcha = $this->request->request('captcha');
  591. if (!$oldcaptcha || !$mobile || !$captcha) {
  592. $this->error(__('Invalid parameters'));
  593. }
  594. if (!Validate::regex($mobile, "^1\d{10}$")) {
  595. $this->error(__('Mobile is incorrect'));
  596. }
  597. if($user->mobile == $mobile){
  598. $this->error('新手机号不能与旧手机号相同');
  599. }
  600. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  601. $this->error(__('Mobile already exist'));
  602. }
  603. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  604. if (!$result) {
  605. $this->error(__('Captcha is incorrect'));
  606. }
  607. $result = Sms::check($mobile, $captcha, 'changemobile');
  608. if (!$result) {
  609. $this->error(__('Captcha is incorrect'));
  610. }
  611. $verification = $user->verification;
  612. $verification->mobile = 1;
  613. $user->verification = $verification;
  614. $user->mobile = $mobile;
  615. $user->save();
  616. Sms::flush($user->mobile, 'changemobile');
  617. Sms::flush($mobile, 'changemobile');
  618. $this->success();
  619. }
  620. /**
  621. * 微信注册来的,绑定手机号
  622. *
  623. * @ApiMethod (POST)
  624. * @param string $mobile 手机号
  625. * @param string $captcha 验证码
  626. */
  627. public function bindmobile()
  628. {
  629. $user = $this->auth->getUser();
  630. $mobile = $this->request->request('mobile');
  631. $captcha = $this->request->request('captcha');
  632. if(!empty($this->auth->mobile)){
  633. $this->error('已经绑定了手机号');
  634. }
  635. if (!$mobile || !$captcha) {
  636. $this->error(__('Invalid parameters'));
  637. }
  638. if (!Validate::regex($mobile, "^1\d{10}$")) {
  639. $this->error(__('Mobile is incorrect'));
  640. }
  641. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  642. $this->error('该手机号已被其他用户绑定');
  643. }
  644. $result = Sms::check($mobile, $captcha, 'changemobile');
  645. if (!$result) {
  646. $this->error(__('Captcha is incorrect'));
  647. }
  648. $verification = $user->verification;
  649. $verification->mobile = 1;
  650. $user->verification = $verification;
  651. $user->mobile = $mobile;
  652. $user->save();
  653. Sms::flush($mobile, 'changemobile');
  654. $this->success('success',$this->userInfo('return'));
  655. }
  656. /**
  657. * 微信注册来的,绑定手机号
  658. *
  659. * @ApiMethod (POST)
  660. * @param string $mobile 手机号
  661. * @param string $captcha 验证码
  662. */
  663. public function bindopenid()
  664. {
  665. $user = $this->auth->getUser();
  666. $wechat_openid = $this->request->request('wechat_openid');
  667. if(!empty($this->auth->wechat_openid)){
  668. $this->error('已经绑定了微信号');
  669. }
  670. if (!$wechat_openid) {
  671. $this->error(__('Invalid parameters'));
  672. }
  673. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  674. $this->error('该微信号已被其他用户绑定');
  675. }
  676. $user->wechat_openid = $wechat_openid;
  677. $user->save();
  678. $this->success('success',$this->userInfo('return'));
  679. }
  680. /**
  681. * 第三方登录
  682. *
  683. * @ApiMethod (POST)
  684. * @param string $platform 平台名称
  685. * @param string $code Code码
  686. */
  687. /*public function third()
  688. {
  689. $url = url('user/index');
  690. // $platform = $this->request->post("platform");
  691. $platform = 'wechat';
  692. $code = $this->request->post("code");
  693. $config = get_addon_config('third');
  694. if (!$config || !isset($config[$platform])) {
  695. $this->error(__('Invalid parameters'));
  696. }
  697. $app = new \addons\third\library\Application($config);
  698. //通过code换access_token和绑定会员
  699. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  700. if ($result) {
  701. $loginret = \addons\third\library\Service::connect($platform, $result);
  702. if ($loginret) {
  703. $data = [
  704. 'userinfo' => $this->auth->getUserinfo(),
  705. 'thirdinfo' => $result
  706. ];
  707. $this->success(__('Logged in successful'), $data);
  708. }
  709. }
  710. $this->error(__('Operation failed'), $url);
  711. }*/
  712. /**
  713. * 重置密码
  714. *
  715. * @ApiMethod (POST)
  716. * @param string $mobile 手机号
  717. * @param string $newpassword 新密码
  718. * @param string $captcha 验证码
  719. */
  720. public function resetpwd()
  721. {
  722. //$type = input("type");
  723. $type = 'mobile';
  724. $mobile = input("mobile");
  725. $email = input("email");
  726. $newpassword = input("newpassword");
  727. $captcha = input("captcha");
  728. if (!$newpassword || !$captcha) {
  729. $this->error(__('Invalid parameters'));
  730. }
  731. if ($type == 'mobile') {
  732. if (!Validate::regex($mobile, "^1\d{10}$")) {
  733. $this->error(__('Mobile is incorrect'));
  734. }
  735. $user = \app\common\model\User::getByMobile($mobile);
  736. if (!$user) {
  737. $this->error(__('User not found'));
  738. }
  739. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  740. if (!$ret) {
  741. $this->error(__('Captcha is incorrect'));
  742. }
  743. Sms::flush($mobile, 'resetpwd');
  744. } else {
  745. if (!Validate::is($email, "email")) {
  746. $this->error(__('Email is incorrect'));
  747. }
  748. $user = \app\common\model\User::getByEmail($email);
  749. if (!$user) {
  750. $this->error(__('User not found'));
  751. }
  752. $ret = Ems::check($email, $captcha, 'resetpwd');
  753. if (!$ret) {
  754. $this->error(__('Captcha is incorrect'));
  755. }
  756. Ems::flush($email, 'resetpwd');
  757. }
  758. //模拟一次登录
  759. $this->auth->direct($user->id);
  760. $ret = $this->auth->changepwd($newpassword, '', true);
  761. if ($ret) {
  762. $this->success(__('Reset password successful'));
  763. } else {
  764. $this->error($this->auth->getError());
  765. }
  766. }
  767. /**
  768. * 修改密码
  769. *
  770. * @ApiMethod (POST)
  771. * @param string $newpassword 新密码
  772. * @param string $oldpassword 旧密码
  773. */
  774. public function changepwd(){
  775. $newpassword = input('newpassword');
  776. $oldpassword = input('oldpassword','');
  777. if (!$newpassword) {
  778. $this->error(__('Invalid parameters'));
  779. }
  780. if($this->auth->password && empty($oldpassword)){
  781. $this->error('原密码必填');
  782. }
  783. if(empty($this->auth->password)){
  784. $ret = $this->auth->changepwd($newpassword, '', true);
  785. }else{
  786. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  787. }
  788. if ($ret) {
  789. $this->success(__('Reset password successful'));
  790. } else {
  791. $this->error($this->auth->getError());
  792. }
  793. }
  794. /**
  795. * 记录当前登陆的设备ID,设备信息,IP等
  796. */
  797. public function changeDeviceIp()
  798. {
  799. // 接口防并发
  800. if (!$this->apiLimit(1, 5000)) {
  801. return ;
  802. }
  803. $user = $this->auth->getUser();
  804. $ip = request()->ip();
  805. $deviceId = $this->request->request('device_id','');
  806. $phoneModel = $this->request->request('phone_model','');
  807. $brand = $this->request->request('brand','');
  808. $apiVersion = $this->request->request('api_version','');
  809. $deviceOs = $this->request->request('device_os','');
  810. if ($ip !== $user->loginip){
  811. $update = [];
  812. $update['id'] = $user->id;
  813. $update['loginip'] = $ip;
  814. \app\common\model\User::update($update);
  815. }
  816. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  817. if (empty($userDeviceInfo)){
  818. $userDeviceInfo = new UserDeviceInfo();
  819. $userDeviceInfo->user_id = $user->u_id;
  820. }
  821. $userDeviceInfo->device_os = $deviceOs;
  822. $userDeviceInfo->device_id = $deviceId;
  823. $userDeviceInfo->phone_model = $phoneModel;
  824. $userDeviceInfo->brand = $brand;
  825. $userDeviceInfo->api_version = $apiVersion;
  826. $userDeviceInfo->save();
  827. //首页接口调用,这里不反回信息
  828. // $this->success("更新成功!");
  829. }
  830. //假注销
  831. public function cancleUser(){
  832. if (!$this->request->isPost()) {
  833. $this->error(__('Invalid parameters'));
  834. }
  835. //退出im
  836. $tenIm = new Tenim();
  837. $tenIm->loginoutim($this->auth->id);
  838. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  839. $this->auth->logout();
  840. $this->success('注销成功');
  841. }
  842. //APP 转化数据统计方案(即:APP 上报对接方案): 广告主上报激活数据,亿米平台搭建服务系统关联点击&下载数据和广告主提供的所有激活数据,将激活数据归因到对应广告。
  843. public function yimi_advert($ua = ''){
  844. //http://trail.e.mi.com/global/log?appId={appid}&info={data}&conv_type={convType}&customer_id={customerId}
  845. $api_url = 'http://trail.e.mi.com/global/log?';
  846. $api_url_test = 'http://trail.e.mi.com/global/test?';
  847. //应用id 1453045
  848. //秘钥A(encrypt_key):ZxdIaVHvFqSQYzWD
  849. //秘钥B(sign_key):uaeWeunykLRnkyLw
  850. $sign_key = 'uaeWeunykLRnkyLw'; //真的
  851. $encrypt_key = 'ZxdIaVHvFqSQYzWD';//真的
  852. $appid = '1453045';
  853. $conv_type = 'APP_REGISTER';
  854. $customer_id = '292232';
  855. //推荐模式
  856. /*$imei = md5('imei');
  857. $data = [
  858. 'imei' => '91b9185dba1772851dd02b276a6c969e',
  859. 'oaid' => '5fb96f268628810c',
  860. 'conv_time' => '1504687208890',
  861. 'client_ip' => '127.0.0.1',
  862. 'ua' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
  863. ];*/
  864. //采用模式
  865. if(empty($ua)){
  866. return true;
  867. }
  868. $data = [
  869. 'conv_time' => time().substr(microtime(),2,3),
  870. 'client_ip' => request()->ip(),
  871. 'ua' => $ua,
  872. ];
  873. $data_query = http_build_query($data);
  874. //dump($data_query);
  875. $property = $sign_key.'&'.urlencode($data_query);
  876. //dump($property);
  877. $signature = md5($property);
  878. //dump($signature);
  879. $base_data = $data_query .'&sign='.urlencode($signature);
  880. //echo $base_data;
  881. $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
  882. //dump($info);
  883. $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
  884. //echo $request_url;
  885. $result = curl_get($request_url);
  886. //dump($result);
  887. //日志
  888. $log = [
  889. 'param' => $base_data,
  890. 'url' => $request_url,
  891. 'result'=> $result,
  892. 'createtime' => time(),
  893. ];
  894. Db::name('yimi_advert')->insertGetId($log);
  895. return true;
  896. }
  897. //亿米 异或加密,解密
  898. public function xor_enc($str,$key)
  899. {
  900. $crytxt = '';
  901. $keylen = strlen($key);
  902. for($i=0;$i<strlen($str);$i++)
  903. {
  904. $k = $i%$keylen;
  905. $crytxt .= $str[$i] ^ $key[$k];
  906. }
  907. return $crytxt;
  908. }
  909. //公众号获取openid
  910. public function getUserOpenid_gzh(){
  911. $configValue = Service::getConfig('wechat');
  912. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  913. $rs = $wechat->getOpenid();
  914. $this->success('success',$rs);
  915. }
  916. /**
  917. * 微信内H5-JSAPI支付
  918. */
  919. public function jssdkBuildConfig() {
  920. $url = $this->request->request("url");
  921. $configValue = Service::getConfig('wechat');
  922. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  923. $sign = $wechat->getSignPackage(urldecode($url));
  924. $this->success("获取成功!",$sign);
  925. }
  926. }