User.php 28 KB

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