User.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. use addons\epay\library\Wechat;
  15. /**
  16. * 会员接口,登录,注册,修改资料等
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. if (!Config::get('fastadmin.usercenter')) {
  26. $this->error(__('User center already closed'));
  27. }
  28. }
  29. /**
  30. * 会员中心
  31. */
  32. public function index()
  33. {
  34. $this->success('', ['welcome' => $this->auth->nickname]);
  35. }
  36. /**
  37. * 会员登录
  38. *
  39. * @ApiMethod (POST)
  40. * @param string $account 账号
  41. * @param string $password 密码
  42. */
  43. public function login()
  44. {
  45. $account = input('account');
  46. $password = input('password');
  47. if (!$account || !$password) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $ret = $this->auth->login($account, $password);
  51. if ($ret) {
  52. $data = $this->userInfo('return');
  53. $this->success(__('Logged in successful'), $data);
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次。
  59. private function firstopen_send($oneuser){
  60. //找出公会的人
  61. $map = [
  62. 'gh_id' => ['gt',0],
  63. ];
  64. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(3)->column('id');
  65. //dump($ghuser);
  66. //随机取出一句话
  67. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(3)->column('title');
  68. //dump($oneword);
  69. $tenim = new \app\common\library\Tenim;
  70. for($i = 0;$i < 3;$i++){
  71. $ghuser_one = isset($ghuser[$i]) ? $ghuser[$i] : $ghuser[array_rand($ghuser)];
  72. $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
  73. $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  74. }
  75. }
  76. /**
  77. * 手机验证码登录
  78. *
  79. * @ApiMethod (POST)
  80. * @param string $mobile 手机号
  81. * @param string $captcha 验证码
  82. */
  83. public function mobilelogin()
  84. {
  85. $mobile = input('mobile');
  86. $captcha = input('captcha');
  87. if (!$mobile || !$captcha) {
  88. $this->error(__('Invalid parameters'));
  89. }
  90. if (!Validate::regex($mobile, "^1\d{10}$")) {
  91. $this->error(__('Mobile is incorrect'));
  92. }
  93. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  94. $this->error(__('Captcha is incorrect'));
  95. }
  96. $user = \app\common\model\User::getByMobile($mobile);
  97. if ($user) {
  98. if ($user->status == -1) {
  99. $this->error('账户已注销');
  100. }
  101. if ($user->status != 1) {
  102. $this->error(__('Account is locked'));
  103. }
  104. //如果已经有账号则直接登录
  105. $ret = $this->auth->direct($user->id);
  106. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次
  107. if($user->gender == 1 && $user->gh_id == 0){
  108. $this->firstopen_send($user->id);
  109. }
  110. } else {
  111. $extend = [
  112. 'register_from' => input('register_from',''),
  113. ];
  114. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  115. //亿米
  116. $ua = input('ua','','trim');
  117. $this->yimi_advert($ua);
  118. }
  119. if ($ret) {
  120. Sms::flush($mobile, 'mobilelogin');
  121. $data = $this->userInfo('return');
  122. $this->success(__('Logged in successful'), $data);
  123. } else {
  124. $this->error($this->auth->getError());
  125. }
  126. }
  127. /**
  128. * 注册会员
  129. *
  130. * @ApiMethod (POST)
  131. * @param string $username 用户名
  132. * @param string $password 密码
  133. * @param string $email 邮箱
  134. * @param string $mobile 手机号
  135. * @param string $code 验证码
  136. */
  137. /*public function register()
  138. {
  139. $username = $this->request->post('username');
  140. $password = $this->request->post('password');
  141. $email = $this->request->post('email');
  142. $mobile = $this->request->post('mobile');
  143. $code = $this->request->post('code');
  144. if (!$username || !$password) {
  145. $this->error(__('Invalid parameters'));
  146. }
  147. if ($email && !Validate::is($email, "email")) {
  148. $this->error(__('Email is incorrect'));
  149. }
  150. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  151. $this->error(__('Mobile is incorrect'));
  152. }
  153. $ret = Sms::check($mobile, $code, 'register');
  154. if (!$ret) {
  155. $this->error(__('Captcha is incorrect'));
  156. }
  157. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  158. if ($ret) {
  159. $data = $this->userInfo('return');
  160. $this->success(__('Sign up successful'), $data);
  161. } else {
  162. $this->error($this->auth->getError());
  163. }
  164. }*/
  165. //微信登录+注册
  166. public function wechatlogin(){
  167. $nickname = input('nickname','');
  168. $avatar = input('avatar','');
  169. $gender = input('gender',1);
  170. $wechat_openid = input('wechat_openid','');
  171. if (!$wechat_openid) {
  172. $this->error(__('Invalid parameters'));
  173. }
  174. if($gender != 1){
  175. $gender = 0;
  176. }
  177. $user = \app\common\model\User::getByOpenid($wechat_openid);
  178. if ($user) {
  179. if ($user->status != 1) {
  180. $this->error(__('Account is locked'));
  181. }
  182. //如果已经有账号则直接登录
  183. $ret = $this->auth->direct($user->id);
  184. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次
  185. if($user->gender == 1 && $user->gh_id == 0){
  186. $this->firstopen_send($user->id);
  187. }
  188. } else {
  189. if (!$nickname || !$avatar) {
  190. $this->error(__('Invalid parameters'));
  191. }
  192. $reg_data = [
  193. 'nickname'=>$nickname,
  194. 'avatar'=>$avatar,
  195. 'gender'=>$gender,
  196. 'register_from' => input('register_from',''),
  197. ];
  198. $ret = $this->auth->openid_register($wechat_openid,$reg_data);
  199. //亿米
  200. $ua = input('ua','','trim');
  201. $this->yimi_advert($ua);
  202. }
  203. if ($ret) {
  204. $data = $this->userInfo('return');
  205. $this->success(__('Logged in successful'), $data);
  206. } else {
  207. $this->error($this->auth->getError());
  208. }
  209. }
  210. /**
  211. * 运营商一键登录
  212. */
  213. public function onLogin()
  214. {
  215. $accessToken = input('accessToken');// 运营商预取号获取到的token
  216. $token = input('tokenT');// 易盾返回的token
  217. if (!$accessToken || !$token) {
  218. $this->error("参数获取失败!");
  219. }
  220. $params = array(
  221. // 运营商预取号获取到的token
  222. "accessToken" => $accessToken,
  223. // 易盾返回的token
  224. "token" => $token
  225. );
  226. // 获取密钥配置
  227. $configInfo = config("onLogin");
  228. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  229. $onret = $onlogin->check($params);
  230. // $ret = [];
  231. // $ret["code"] = 200;
  232. // $ret["msg"] = "ok";
  233. // $ret["data"] = [
  234. // "phone" => "17574504021",
  235. // "resultCode" => 0
  236. // ];
  237. if ($onret["code"] == 200) {
  238. $mobile = $onret["data"]["phone"];
  239. if (empty($mobile)) {
  240. // 取号失败,建议进行二次验证,例如短信验证码
  241. $this->error("取号登录失败,请用验证码方式登录!");
  242. } else {
  243. // 取号成功, 执行登录等流程
  244. // 用户登录逻辑 === 开始
  245. $user = \app\common\model\User::getByMobile($mobile);
  246. if ($user) {
  247. if ($user->status != 1) {
  248. $this->error(__('Account is locked'));
  249. }
  250. //如果已经有账号则直接登录
  251. $ret = $this->auth->direct($user->id);
  252. $is_register = 0;
  253. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次
  254. if($user->gender == 1 && $user->gh_id == 0){
  255. $this->firstopen_send($user->id);
  256. }
  257. } else {
  258. $extend = [
  259. 'register_from' => input('register_from',''),
  260. ];
  261. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  262. $is_register = 1;
  263. //亿米
  264. $ua = input('ua','','trim');
  265. $this->yimi_advert($ua);
  266. }
  267. //结果
  268. /*$rs['userinfo'] = $this->auth->getUserinfo();
  269. $rs['is_register'] = $is_register;*/
  270. if ($ret) {
  271. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  272. } else {
  273. $this->error($this->auth->getError());
  274. }
  275. // 用户登录逻辑 === 结束
  276. }
  277. } else {
  278. $this->error("登录失败,请用验证码方式登录!");
  279. }
  280. }
  281. //用户详细资料
  282. public function userInfo($type = 1){
  283. $info = $this->auth->getUserinfo();
  284. if($type == 'return'){
  285. return $info;
  286. }
  287. $this->success(__('success'),$info);
  288. }
  289. //用户邀请信息
  290. public function userintroinfo(){
  291. $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
  292. $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
  293. $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
  294. $rs = [
  295. 'intro_num' => $intro_num,
  296. 'money_sum' => $money_sum,
  297. 'user_list' => $user_list,
  298. ];
  299. $this->success('success',$rs);
  300. }
  301. public function testhaibao(){
  302. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
  303. dump($haibao);
  304. }
  305. //海报
  306. public function haibao($player_id,$data){
  307. //下载页二维码,没必要保留
  308. $params = [
  309. 'text' => config('site.domain_name').'/index/index/appdownload',
  310. 'size' => 90,
  311. 'logo' => false,
  312. 'label' => false,
  313. 'padding' => 0,
  314. ];
  315. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  316. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  317. mk_dir($qrcode_path);
  318. $download_qrcode = $qrcode_path.'/download.png';
  319. $qrCode->writeFile($download_qrcode);
  320. //海报
  321. $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
  322. return $haibao;
  323. }
  324. public function createhaibao($download_qrcode,$player_id,$sub_data){
  325. //背景图
  326. $background = config('site.domain_name').'/assets/img/haibao.png';
  327. //海报图片路径
  328. $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
  329. mk_dir($new_path);
  330. $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
  331. //二维码
  332. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  333. //合成wap图片
  334. $image = new \addons\poster\library\Image2();
  335. $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
  336. return '/'.$wap_file_name;
  337. }
  338. //申请真人认证
  339. public function apply_real_confirm(){
  340. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  341. if(!$avatar){
  342. $this->error('请上传真人头像');
  343. }
  344. Db::startTrans();
  345. $data = [
  346. 'avatar' => $avatar,
  347. 'real_status' => 1,
  348. ];
  349. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  350. if($rs === false){
  351. Db::rollback();
  352. $this->error('认证失败');
  353. }
  354. //tag任务赠送金币
  355. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  356. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  357. if($task_rs === false){
  358. Db::rollback();
  359. $this->error('完成任务赠送奖励失败');
  360. }
  361. //完成真人头像 +5金币
  362. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  363. if($task_rs === false){
  364. Db::rollback();
  365. $this->error('完成任务赠送奖励失败');
  366. }
  367. //邀请人拿奖励,男性3元
  368. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  369. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  370. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  371. if($task_rs['status'] === false){
  372. Db::rollback();
  373. $this->error($task_rs['msg']);
  374. }
  375. }
  376. //系统消息
  377. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  378. Db::commit();
  379. $this->success();
  380. }
  381. //实名认证信息
  382. public function idcard_confirm_info(){
  383. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  384. $this->success('success',$check);
  385. }
  386. //申请实名认证
  387. public function apply_idcard_confirm(){
  388. $truename = input('truename','');
  389. $idcard = input('idcard','');
  390. //$idcard_images = input('idcard_images','');
  391. $alipay_account = input('alipay_account','');
  392. if(empty($truename) || empty($idcard) || empty($alipay_account)){
  393. $this->error('实名认证信息必填');
  394. }
  395. if($this->auth->idcard_status == 1){
  396. $this->error('您已经完成实名认证');
  397. }
  398. if($this->auth->idcard_status == 0){
  399. $this->error('您已经提交实名认证,请等待审核');
  400. }
  401. Db::startTrans();
  402. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  403. if(!empty($check)){
  404. if($check['status'] == 0){
  405. Db::rollback();
  406. $this->error('您已经提交实名认证,请等待审核');
  407. }
  408. if($check['status'] == 1){
  409. Db::rollback();
  410. $this->error('您已经完成实名认证');
  411. }
  412. }
  413. $data = [
  414. 'user_id' => $this->auth->id,
  415. 'truename' => $truename,
  416. 'idcard' => $idcard,
  417. //'idcard_images' => $idcard_images,
  418. 'alipay_account' => $alipay_account,
  419. 'status' => 0,
  420. 'createtime' => time(),
  421. 'updatetime' => time(),
  422. ];
  423. //更新
  424. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  425. if(!empty($check)){
  426. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  427. }else{
  428. $rs = Db::name('user_idconfirm')->insertGetId($data);
  429. }
  430. if(!$rs || !$update_rs){
  431. Db::rollback();
  432. $this->error('提交失败');
  433. }
  434. Db::commit();
  435. $this->success('提交成功,请等待审核');
  436. }
  437. /**
  438. * 退出登录
  439. * @ApiMethod (POST)
  440. */
  441. public function logout()
  442. {
  443. if (!$this->request->isPost()) {
  444. $this->error(__('Invalid parameters'));
  445. }
  446. //退出im
  447. $tenIm = new Tenim();
  448. $tenIm->loginoutim($this->auth->id);
  449. //修改用户活跃0
  450. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  451. $this->auth->logout();
  452. $this->success(__('Logout successful'));
  453. }
  454. /**
  455. * 修改会员个人信息
  456. *
  457. * @ApiMethod (POST)
  458. * @param string $avatar 头像地址
  459. * @param string $username 用户名
  460. * @param string $nickname 昵称
  461. * @param string $bio 个人简介
  462. */
  463. public function profile()
  464. {
  465. $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'];
  466. $data = [];
  467. foreach($field_array as $key => $field){
  468. if(!input('?'.$field)){
  469. continue;
  470. }
  471. $newone = input($field);
  472. if($field == 'avatar'){
  473. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  474. }
  475. if($field == 'photo_images'){
  476. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  477. }
  478. $data[$field] = $newone;
  479. }
  480. if(isset($data['birthday'])){
  481. $data['birthday'] = strtotime($data['birthday']);
  482. }
  483. if(isset($data['avatar'])){
  484. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  485. }
  486. if(isset($data['hobby_ids'])){
  487. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  488. }
  489. if(isset($data['tag_ids'])){
  490. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  491. }
  492. if(isset($data['introcode'])){
  493. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  494. if(!$intro_user){
  495. $this->error('不存在的邀请人');
  496. }
  497. unset($data['introcode']);//别人的邀请码,不能改了自己的
  498. $data['intro_uid'] = $intro_user;
  499. }
  500. //dump($data);
  501. if(empty($data)){
  502. $this->error('没有任何改变');
  503. }
  504. //默认头像
  505. //现在没头像,也没穿头像,但是却传了性别
  506. if($this->auth->avatar == config('site.domain_cdnurl').'/avatar.png' && isset($data['gender']) && $data['avatar'] == config('site.domain_cdnurl').'/avatar.png'){
  507. if($data['gender'] == 1){
  508. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f9277fba97fd76d9ecfc63b506a3674a.png';
  509. }else{
  510. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/5030460c05c86774f60123ffea7b78a1.png';
  511. }
  512. }
  513. //默认头像
  514. Db::startTrans();
  515. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  516. if($update_rs === false){
  517. Db::rollback();
  518. $this->error('修改资料失败');
  519. }
  520. //tag任务赠送金币
  521. //上传头像加5金币
  522. if(isset($data['avatar'])){
  523. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  524. if($task_rs === false){
  525. Db::rollback();
  526. $this->error('完成任务赠送奖励失败');
  527. }
  528. }
  529. //上传本人语音介绍 10金币
  530. if(isset($data['audio_bio'])){
  531. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  532. if($task_rs === false){
  533. Db::rollback();
  534. $this->error('完成任务赠送奖励失败');
  535. }
  536. }
  537. //上传本人五张照片 10金币
  538. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  539. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  540. if($task_rs === false){
  541. Db::rollback();
  542. $this->error('完成任务赠送奖励失败');
  543. }
  544. }
  545. //所有资料完成
  546. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  547. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  548. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  549. if($task_rs === false){
  550. Db::rollback();
  551. $this->error('完成任务赠送奖励失败');
  552. }
  553. }
  554. Db::commit();
  555. $this->success();
  556. }
  557. public function set_status_switch(){
  558. }
  559. /*
  560. * 修改用户的坐标
  561. * */
  562. public function change_longlat(){
  563. $longitude = input_post('longitude');
  564. $latitude = input_post('latitude');
  565. $cityname = input_post('cityname');
  566. if(empty($longitude) || empty($latitude) || empty($cityname)){
  567. $this->error();
  568. }
  569. $data = [
  570. 'longitude' => $longitude,
  571. 'latitude' => $latitude,
  572. 'cityname' => $cityname,
  573. ];
  574. Db::name('user')->where('id',$this->auth->id)->update($data);
  575. $this->success();
  576. }
  577. /**
  578. * 修改邮箱
  579. *
  580. * @ApiMethod (POST)
  581. * @param string $email 邮箱
  582. * @param string $captcha 验证码
  583. */
  584. /*public function changeemail()
  585. {
  586. $user = $this->auth->getUser();
  587. $email = $this->request->post('email');
  588. $captcha = $this->request->post('captcha');
  589. if (!$email || !$captcha) {
  590. $this->error(__('Invalid parameters'));
  591. }
  592. if (!Validate::is($email, "email")) {
  593. $this->error(__('Email is incorrect'));
  594. }
  595. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  596. $this->error(__('Email already exists'));
  597. }
  598. $result = Ems::check($email, $captcha, 'changeemail');
  599. if (!$result) {
  600. $this->error(__('Captcha is incorrect'));
  601. }
  602. $verification = $user->verification;
  603. $verification->email = 1;
  604. $user->verification = $verification;
  605. $user->email = $email;
  606. $user->save();
  607. Ems::flush($email, 'changeemail');
  608. $this->success();
  609. }*/
  610. /**
  611. * 修改手机号
  612. *
  613. * @ApiMethod (POST)
  614. * @param string $mobile 手机号
  615. * @param string $captcha 验证码
  616. */
  617. public function changemobile()
  618. {
  619. $user = $this->auth->getUser();
  620. $oldcaptcha = $this->request->request('oldcaptcha');
  621. $mobile = $this->request->request('mobile');
  622. $captcha = $this->request->request('captcha');
  623. if (!$oldcaptcha || !$mobile || !$captcha) {
  624. $this->error(__('Invalid parameters'));
  625. }
  626. if (!Validate::regex($mobile, "^1\d{10}$")) {
  627. $this->error(__('Mobile is incorrect'));
  628. }
  629. if($user->mobile == $mobile){
  630. $this->error('新手机号不能与旧手机号相同');
  631. }
  632. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  633. $this->error(__('Mobile already exist'));
  634. }
  635. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  636. if (!$result) {
  637. $this->error(__('Captcha is incorrect'));
  638. }
  639. $result = Sms::check($mobile, $captcha, 'changemobile');
  640. if (!$result) {
  641. $this->error(__('Captcha is incorrect'));
  642. }
  643. $verification = $user->verification;
  644. $verification->mobile = 1;
  645. $user->verification = $verification;
  646. $user->mobile = $mobile;
  647. $user->save();
  648. Sms::flush($user->mobile, 'changemobile');
  649. Sms::flush($mobile, 'changemobile');
  650. $this->success();
  651. }
  652. /**
  653. * 微信注册来的,绑定手机号
  654. *
  655. * @ApiMethod (POST)
  656. * @param string $mobile 手机号
  657. * @param string $captcha 验证码
  658. */
  659. public function bindmobile()
  660. {
  661. $user = $this->auth->getUser();
  662. $mobile = $this->request->request('mobile');
  663. $captcha = $this->request->request('captcha');
  664. if(!empty($this->auth->mobile)){
  665. $this->error('已经绑定了手机号');
  666. }
  667. if (!$mobile || !$captcha) {
  668. $this->error(__('Invalid parameters'));
  669. }
  670. if (!Validate::regex($mobile, "^1\d{10}$")) {
  671. $this->error(__('Mobile is incorrect'));
  672. }
  673. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  674. $this->error('该手机号已被其他用户绑定');
  675. }
  676. $result = Sms::check($mobile, $captcha, 'changemobile');
  677. if (!$result) {
  678. $this->error(__('Captcha is incorrect'));
  679. }
  680. $verification = $user->verification;
  681. $verification->mobile = 1;
  682. $user->verification = $verification;
  683. $user->mobile = $mobile;
  684. $user->save();
  685. Sms::flush($mobile, 'changemobile');
  686. $this->success('success',$this->userInfo('return'));
  687. }
  688. /**
  689. * 微信注册来的,绑定手机号
  690. *
  691. * @ApiMethod (POST)
  692. * @param string $mobile 手机号
  693. * @param string $captcha 验证码
  694. */
  695. public function bindopenid()
  696. {
  697. $user = $this->auth->getUser();
  698. $wechat_openid = $this->request->request('wechat_openid');
  699. if(!empty($this->auth->wechat_openid)){
  700. $this->error('已经绑定了微信号');
  701. }
  702. if (!$wechat_openid) {
  703. $this->error(__('Invalid parameters'));
  704. }
  705. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  706. $this->error('该微信号已被其他用户绑定');
  707. }
  708. $user->wechat_openid = $wechat_openid;
  709. $user->save();
  710. $this->success('success',$this->userInfo('return'));
  711. }
  712. /**
  713. * 第三方登录
  714. *
  715. * @ApiMethod (POST)
  716. * @param string $platform 平台名称
  717. * @param string $code Code码
  718. */
  719. /*public function third()
  720. {
  721. $url = url('user/index');
  722. // $platform = $this->request->post("platform");
  723. $platform = 'wechat';
  724. $code = $this->request->post("code");
  725. $config = get_addon_config('third');
  726. if (!$config || !isset($config[$platform])) {
  727. $this->error(__('Invalid parameters'));
  728. }
  729. $app = new \addons\third\library\Application($config);
  730. //通过code换access_token和绑定会员
  731. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  732. if ($result) {
  733. $loginret = \addons\third\library\Service::connect($platform, $result);
  734. if ($loginret) {
  735. $data = [
  736. 'userinfo' => $this->auth->getUserinfo(),
  737. 'thirdinfo' => $result
  738. ];
  739. $this->success(__('Logged in successful'), $data);
  740. }
  741. }
  742. $this->error(__('Operation failed'), $url);
  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(__('Reset password successful'));
  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($ua = ''){
  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. //采用模式
  902. if(empty($ua)){
  903. return true;
  904. }
  905. $data = [
  906. 'conv_time' => time().substr(microtime(),2,3),
  907. 'client_ip' => request()->ip(),
  908. 'ua' => $ua,
  909. ];
  910. $data_query = http_build_query($data);
  911. //dump($data_query);
  912. $property = $sign_key.'&'.urlencode($data_query);
  913. //dump($property);
  914. $signature = md5($property);
  915. //dump($signature);
  916. $base_data = $data_query .'&sign='.urlencode($signature);
  917. //echo $base_data;
  918. $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
  919. //dump($info);
  920. $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
  921. //echo $request_url;
  922. $result = curl_get($request_url);
  923. //dump($result);
  924. //日志
  925. $log = [
  926. 'param' => $base_data,
  927. 'url' => $request_url,
  928. 'result'=> $result,
  929. 'createtime' => time(),
  930. ];
  931. Db::name('yimi_advert')->insertGetId($log);
  932. return true;
  933. }
  934. //亿米 异或加密,解密
  935. public function xor_enc($str,$key)
  936. {
  937. $crytxt = '';
  938. $keylen = strlen($key);
  939. for($i=0;$i<strlen($str);$i++)
  940. {
  941. $k = $i%$keylen;
  942. $crytxt .= $str[$i] ^ $key[$k];
  943. }
  944. return $crytxt;
  945. }
  946. //公众号获取openid
  947. public function getUserOpenid_gzh(){
  948. $configValue = Service::getConfig('wechat');
  949. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  950. $rs = $wechat->getOpenid();
  951. $this->success('success',$rs);
  952. }
  953. /**
  954. * 微信内H5-JSAPI支付
  955. */
  956. public function jssdkBuildConfig() {
  957. $url = $this->request->request("url");
  958. $configValue = Service::getConfig('wechat');
  959. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  960. $sign = $wechat->getSignPackage(urldecode($url));
  961. $this->success("获取成功!",$sign);
  962. }
  963. }