User.php 35 KB

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