User.php 35 KB

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