User.php 35 KB

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