User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. * 退出登录
  263. * @ApiMethod (POST)
  264. */
  265. public function logout()
  266. {
  267. if (!$this->request->isPost()) {
  268. $this->error(__('Invalid parameters'));
  269. }
  270. //退出im
  271. // $tenIm = new Tenim();
  272. // $tenIm->loginoutim($this->auth->id);
  273. $this->auth->logout();
  274. $this->success(__('Logout successful'));
  275. }
  276. /**
  277. * 修改会员个人信息
  278. *
  279. * @ApiMethod (POST)
  280. * @param string $avatar 头像地址
  281. * @param string $username 用户名
  282. * @param string $nickname 昵称
  283. * @param string $bio 个人简介
  284. */
  285. public function profile()
  286. {
  287. $field_array = ['nickname','introcode','gender','birthday','attribute','shoesize','height','weight','bio','avatar','photo_images','tag_ids','hide_is_finishinfo'];
  288. $data = [];
  289. foreach($field_array as $key => $field){
  290. //前端传不了post,改了
  291. /*if(!request()->has($field,'post')){
  292. continue;
  293. }*/
  294. if(!input('?'.$field)){
  295. continue;
  296. }
  297. $newone = input($field);
  298. if($field == 'avatar'){
  299. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  300. }
  301. if($field == 'photo_images'){
  302. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  303. }
  304. $data[$field] = $newone;
  305. }
  306. if(isset($data['birthday'])){
  307. $data['birthday'] = strtotime($data['birthday']);
  308. }
  309. if(isset($data['tag_ids'])){
  310. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  311. }
  312. if(isset($data['introcode'])){
  313. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  314. if(!$intro_user){
  315. $this->error('不存在的邀请人');
  316. }
  317. unset($data['introcode']);//别人的邀请码,不能改了自己的
  318. $data['intro_uid'] = $intro_user;
  319. }
  320. //dump($data);
  321. if(empty($data)){
  322. $this->error('没有任何改变');
  323. }
  324. //默认头像
  325. //现在没头像,也没穿头像,但是却传了性别
  326. if($this->auth->avatar == config('site.domain_cdnurl').'/avatar.png' && isset($data['gender']) && $data['avatar'] == config('site.domain_cdnurl').'/avatar.png'){
  327. if($data['gender'] == 1){
  328. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f9277fba97fd76d9ecfc63b506a3674a.png';
  329. }else{
  330. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/5030460c05c86774f60123ffea7b78a1.png';
  331. }
  332. }
  333. //默认头像
  334. Db::startTrans();
  335. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  336. if($update_rs === false){
  337. Db::rollback();
  338. $this->error('修改资料失败');
  339. }
  340. //tag任务赠送金币
  341. //上传头像加5金币
  342. /*if(isset($data['avatar'])){
  343. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  344. if($task_rs === false){
  345. Db::rollback();
  346. $this->error('完成任务赠送奖励失败');
  347. }
  348. }*/
  349. //上传本人语音介绍 10金币
  350. /*if(isset($data['audio_bio'])){
  351. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  352. if($task_rs === false){
  353. Db::rollback();
  354. $this->error('完成任务赠送奖励失败');
  355. }
  356. }*/
  357. //上传本人五张照片 10金币
  358. /*if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  359. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  360. if($task_rs === false){
  361. Db::rollback();
  362. $this->error('完成任务赠送奖励失败');
  363. }
  364. }*/
  365. //所有资料完成
  366. /*$user_info = Db::name('user')->where('id',$this->auth->id)->find();
  367. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  368. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  369. if($task_rs === false){
  370. Db::rollback();
  371. $this->error('完成任务赠送奖励失败');
  372. }
  373. }*/
  374. Db::commit();
  375. $this->success();
  376. }
  377. /**
  378. * 修改会员权限
  379. */
  380. public function setpower()
  381. {
  382. $is_vip = $this->is_vip($this->auth->id);
  383. if(!$is_vip){
  384. $this->error('VIP才能设置隐私权限');
  385. }
  386. $data = [
  387. 'yinsi' => input('yinsi',0),
  388. 'yinshen' => input('yinshen',0),
  389. 'wuhen' => input('wuhen',0),
  390. ];
  391. $update_rs = Db::name('user_power')->where('id',$this->auth->id)->update($data);
  392. $this->success();
  393. }
  394. public function set_status_switch(){
  395. }
  396. /*
  397. * 修改用户的坐标
  398. * */
  399. public function change_longlat(){
  400. $longitude = input_post('longitude',0);
  401. $latitude = input_post('latitude',0);
  402. $cityname = input_post('cityname','');
  403. if(empty($longitude) || empty($latitude) || empty($cityname)){
  404. $this->error();
  405. }
  406. $data = [
  407. 'longitude' => $longitude,
  408. 'latitude' => $latitude,
  409. 'cityname' => $cityname,
  410. ];
  411. Db::name('user')->where('id',$this->auth->id)->update($data);
  412. $this->success();
  413. }
  414. /**
  415. * 修改手机号
  416. *
  417. * @ApiMethod (POST)
  418. * @param string $mobile 手机号
  419. * @param string $captcha 验证码
  420. */
  421. public function changemobile()
  422. {
  423. $user = $this->auth->getUser();
  424. $oldcaptcha = $this->request->request('oldcaptcha');
  425. $mobile = $this->request->request('mobile');
  426. $captcha = $this->request->request('captcha');
  427. if (!$oldcaptcha || !$mobile || !$captcha) {
  428. $this->error(__('Invalid parameters'));
  429. }
  430. if (!Validate::regex($mobile, "^1\d{10}$")) {
  431. $this->error(__('Mobile is incorrect'));
  432. }
  433. if($user->mobile == $mobile){
  434. $this->error('新手机号不能与旧手机号相同');
  435. }
  436. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  437. $this->error(__('Mobile already exist'));
  438. }
  439. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  440. if (!$result) {
  441. $this->error(__('Captcha is incorrect'));
  442. }
  443. $result = Sms::check($mobile, $captcha, 'changemobile');
  444. if (!$result) {
  445. $this->error(__('Captcha is incorrect'));
  446. }
  447. $verification = $user->verification;
  448. $verification->mobile = 1;
  449. $user->verification = $verification;
  450. $user->mobile = $mobile;
  451. $user->save();
  452. Sms::flush($user->mobile, 'changemobile');
  453. Sms::flush($mobile, 'changemobile');
  454. $this->success();
  455. }
  456. /**
  457. * 微信注册来的,绑定手机号
  458. *
  459. * @ApiMethod (POST)
  460. * @param string $mobile 手机号
  461. * @param string $captcha 验证码
  462. */
  463. public function bindmobile()
  464. {
  465. $user = $this->auth->getUser();
  466. $mobile = $this->request->request('mobile');
  467. $captcha = $this->request->request('captcha');
  468. if(!empty($this->auth->mobile)){
  469. $this->error('已经绑定了手机号');
  470. }
  471. if (!$mobile || !$captcha) {
  472. $this->error(__('Invalid parameters'));
  473. }
  474. if (!Validate::regex($mobile, "^1\d{10}$")) {
  475. $this->error(__('Mobile is incorrect'));
  476. }
  477. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  478. $this->error('该手机号已被其他用户绑定');
  479. }
  480. $result = Sms::check($mobile, $captcha, 'changemobile');
  481. if (!$result) {
  482. $this->error(__('Captcha is incorrect'));
  483. }
  484. $verification = $user->verification;
  485. $verification->mobile = 1;
  486. $user->verification = $verification;
  487. $user->mobile = $mobile;
  488. $user->save();
  489. Sms::flush($mobile, 'changemobile');
  490. $this->success('success',$this->userInfo('return'));
  491. }
  492. /**
  493. * 微信注册来的,绑定手机号
  494. *
  495. * @ApiMethod (POST)
  496. * @param string $mobile 手机号
  497. * @param string $captcha 验证码
  498. */
  499. /*public function bindopenid()
  500. {
  501. $user = $this->auth->getUser();
  502. $wechat_openid = $this->request->request('wechat_openid');
  503. if(!empty($this->auth->wechat_openid)){
  504. $this->error('已经绑定了微信号');
  505. }
  506. if (!$wechat_openid) {
  507. $this->error(__('Invalid parameters'));
  508. }
  509. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  510. $this->error('该微信号已被其他用户绑定');
  511. }
  512. $user->wechat_openid = $wechat_openid;
  513. $user->save();
  514. $this->success('success',$this->userInfo('return'));
  515. }*/
  516. /**
  517. * 手机注册来的,绑定微信
  518. *
  519. * @ApiMethod (POST)
  520. * @param string $wechat_openid
  521. */
  522. public function bindopenid()
  523. {
  524. Db::startTrans();
  525. try {
  526. $code = $this->request->param('code','');
  527. if(!$code){
  528. throw new Exception(__('Invalid parameters'));
  529. }
  530. //微信
  531. $wechat = new Wechat();
  532. $openid = $wechat->getOpenid($code);
  533. if(!$openid){
  534. throw new Exception('openid获取失败');
  535. }
  536. $user = model('User')->find($this->auth->id);
  537. if(!empty($user['wechat_openid'])/* && $openid != $user['openid']*/){
  538. throw new Exception('已经绑定了微信号');
  539. }
  540. $otherUserWhere['wechat_openid'] = $openid;
  541. $otherUserWhere['id'] = ['neq',$this->auth->id];
  542. $otherUser = model('User')->where($otherUserWhere)->find();
  543. if (!empty($otherUser)) {
  544. throw new Exception('该微信已被其他用户绑定过');
  545. }
  546. $user->wechat_openid = $openid;
  547. $userRes = $user->save();
  548. if (!$userRes) {
  549. throw new Exception('绑定微信失败');
  550. }
  551. Db::commit();
  552. $this->success('success',$this->userInfo('return'));
  553. } catch (Exception $e) {
  554. Db::rollback();
  555. $this->error($e->getMessage());
  556. }
  557. }
  558. /**
  559. * 重置密码
  560. *
  561. * @ApiMethod (POST)
  562. * @param string $mobile 手机号
  563. * @param string $newpassword 新密码
  564. * @param string $captcha 验证码
  565. */
  566. public function resetpwd()
  567. {
  568. //$type = input("type");
  569. $type = 'mobile';
  570. $mobile = input("mobile");
  571. $email = input("email");
  572. $newpassword = input("newpassword");
  573. $captcha = input("captcha");
  574. if (!$newpassword || !$captcha) {
  575. $this->error(__('Invalid parameters'));
  576. }
  577. if ($type == 'mobile') {
  578. if (!Validate::regex($mobile, "^1\d{10}$")) {
  579. $this->error(__('Mobile is incorrect'));
  580. }
  581. $user = \app\common\model\User::getByMobile($mobile);
  582. if (!$user) {
  583. $this->error(__('User not found'));
  584. }
  585. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  586. if (!$ret) {
  587. $this->error(__('Captcha is incorrect'));
  588. }
  589. Sms::flush($mobile, 'resetpwd');
  590. } else {
  591. if (!Validate::is($email, "email")) {
  592. $this->error(__('Email is incorrect'));
  593. }
  594. $user = \app\common\model\User::getByEmail($email);
  595. if (!$user) {
  596. $this->error(__('User not found'));
  597. }
  598. $ret = Ems::check($email, $captcha, 'resetpwd');
  599. if (!$ret) {
  600. $this->error(__('Captcha is incorrect'));
  601. }
  602. Ems::flush($email, 'resetpwd');
  603. }
  604. //模拟一次登录
  605. $this->auth->direct($user->id);
  606. $ret = $this->auth->changepwd($newpassword, '', true);
  607. if ($ret) {
  608. $this->success(__('Reset password successful'));
  609. } else {
  610. $this->error($this->auth->getError());
  611. }
  612. }
  613. /**
  614. * 修改密码
  615. *
  616. * @ApiMethod (POST)
  617. * @param string $newpassword 新密码
  618. * @param string $oldpassword 旧密码
  619. */
  620. public function changepwd(){
  621. $newpassword = input('newpassword');
  622. $oldpassword = input('oldpassword','');
  623. if (!$newpassword) {
  624. $this->error(__('Invalid parameters'));
  625. }
  626. if($this->auth->password && empty($oldpassword)){
  627. $this->error('原密码必填');
  628. }
  629. if(empty($this->auth->password)){
  630. $ret = $this->auth->changepwd($newpassword, '', true);
  631. }else{
  632. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  633. }
  634. if ($ret) {
  635. $this->success('设置密码成功');
  636. } else {
  637. $this->error($this->auth->getError());
  638. }
  639. }
  640. /**
  641. * 记录当前登陆的设备ID,设备信息,IP等
  642. */
  643. public function changeDeviceIp()
  644. {
  645. // 接口防并发
  646. if (!$this->apiLimit(1, 5000)) {
  647. return ;
  648. }
  649. $user = $this->auth->getUser();
  650. $ip = request()->ip();
  651. $deviceId = $this->request->request('device_id','');
  652. $phoneModel = $this->request->request('phone_model','');
  653. $brand = $this->request->request('brand','');
  654. $apiVersion = $this->request->request('api_version','');
  655. $deviceOs = $this->request->request('device_os','');
  656. if ($ip !== $user->loginip){
  657. $update = [];
  658. $update['id'] = $user->id;
  659. $update['loginip'] = $ip;
  660. \app\common\model\User::update($update);
  661. }
  662. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  663. if (empty($userDeviceInfo)){
  664. $userDeviceInfo = new UserDeviceInfo();
  665. $userDeviceInfo->user_id = $user->u_id;
  666. }
  667. $userDeviceInfo->device_os = $deviceOs;
  668. $userDeviceInfo->device_id = $deviceId;
  669. $userDeviceInfo->phone_model = $phoneModel;
  670. $userDeviceInfo->brand = $brand;
  671. $userDeviceInfo->api_version = $apiVersion;
  672. $userDeviceInfo->save();
  673. //首页接口调用,这里不反回信息
  674. // $this->success("更新成功!");
  675. }
  676. //假注销
  677. public function cancleUser(){
  678. if (!$this->request->isPost()) {
  679. $this->error(__('Invalid parameters'));
  680. }
  681. //退出im
  682. // $tenIm = new Tenim();
  683. // $tenIm->loginoutim($this->auth->id);
  684. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  685. $this->auth->logout();
  686. $this->success('注销成功');
  687. }
  688. //APP 转化数据统计方案(即:APP 上报对接方案): 广告主上报激活数据,亿米平台搭建服务系统关联点击&下载数据和广告主提供的所有激活数据,将激活数据归因到对应广告。
  689. public function yimi_advert(){
  690. //http://trail.e.mi.com/global/log?appId={appid}&info={data}&conv_type={convType}&customer_id={customerId}
  691. $api_url = 'http://trail.e.mi.com/global/log?';
  692. $api_url_test = 'http://trail.e.mi.com/global/test?';
  693. //应用id 1453045
  694. //秘钥A(encrypt_key):ZxdIaVHvFqSQYzWD
  695. //秘钥B(sign_key):uaeWeunykLRnkyLw
  696. $sign_key = 'uaeWeunykLRnkyLw'; //真的
  697. $encrypt_key = 'ZxdIaVHvFqSQYzWD';//真的
  698. $appid = '1453045';
  699. $conv_type = 'APP_REGISTER';
  700. $customer_id = '292232';
  701. //推荐模式
  702. /*$imei = md5('imei');
  703. $data = [
  704. 'imei' => '91b9185dba1772851dd02b276a6c969e',
  705. 'oaid' => '5fb96f268628810c',
  706. 'conv_time' => '1504687208890',
  707. 'client_ip' => '127.0.0.1',
  708. 'ua' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
  709. ];*/
  710. //采用模式4
  711. /*
  712. $ua = input('ua','','trim');
  713. if(empty($ua)){
  714. return true;
  715. }
  716. $data = [
  717. 'conv_time' => time().substr(microtime(),2,3),
  718. 'client_ip' => request()->ip(),
  719. 'ua' => $ua,
  720. ];
  721. */
  722. //采用模式3
  723. $oaid = input('oaid','','trim');
  724. if(empty($oaid)){
  725. return true;
  726. }
  727. $data = [
  728. 'oaid' => $oaid,
  729. 'conv_time' => time().substr(microtime(),2,3),
  730. 'client_ip' => request()->ip(),
  731. ];
  732. $data_query = http_build_query($data);
  733. //dump($data_query);
  734. $property = $sign_key.'&'.urlencode($data_query);
  735. //dump($property);
  736. $signature = md5($property);
  737. //dump($signature);
  738. $base_data = $data_query .'&sign='.urlencode($signature);
  739. //echo $base_data;
  740. $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
  741. //dump($info);
  742. $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
  743. //echo $request_url;
  744. $result = curl_get($request_url);
  745. //dump($result);
  746. //日志
  747. $log = [
  748. 'param' => $base_data,
  749. 'url' => $request_url,
  750. 'result'=> $result,
  751. 'createtime' => time(),
  752. ];
  753. Db::name('yimi_advert')->insertGetId($log);
  754. return true;
  755. }
  756. //亿米 异或加密,解密
  757. public function xor_enc($str,$key)
  758. {
  759. $crytxt = '';
  760. $keylen = strlen($key);
  761. for($i=0;$i<strlen($str);$i++)
  762. {
  763. $k = $i%$keylen;
  764. $crytxt .= $str[$i] ^ $key[$k];
  765. }
  766. return $crytxt;
  767. }
  768. //公众号获取openid
  769. public function getUserOpenid_gzh(){
  770. $configValue = Service::getConfig('wechat');
  771. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  772. $rs = $wechat->getOpenid();
  773. $this->success('success',$rs);
  774. }
  775. /**
  776. * 微信内H5-JSAPI支付
  777. *
  778. */
  779. public function jssdkBuildConfig() {
  780. $url = $this->request->request("url");
  781. $configValue = Service::getConfig('wechat');
  782. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  783. $sign = $wechat->getSignPackage(urldecode($url));
  784. $this->success("获取成功!",$sign);
  785. }
  786. }