User.php 28 KB

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