User.php 33 KB

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