User.php 28 KB

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