User.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. use onlogin\onlogin;
  13. use addons\epay\library\Service;
  14. //use addons\epay\library\Wechat;
  15. use app\common\library\Wechat;
  16. /**
  17. * 会员接口,登录,注册,修改资料等
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 会员中心
  29. */
  30. public function index()
  31. {
  32. $this->success('', ['welcome' => $this->auth->nickname]);
  33. }
  34. /**
  35. * 会员登录
  36. *
  37. * @ApiMethod (POST)
  38. * @param string $account 账号
  39. * @param string $password 密码
  40. */
  41. public function login()
  42. {
  43. $account = input('account');
  44. $password = input('password');
  45. if (!$account || !$password) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. $ret = $this->auth->login($account, $password);
  49. if ($ret) {
  50. $data = $this->userInfo('return');
  51. $this->success(__('Logged in successful'), $data);
  52. } else {
  53. $this->error($this->auth->getError());
  54. }
  55. }
  56. //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次。
  57. private function firstopen_send($oneuser){
  58. //找出公会的人
  59. $map = [
  60. 'gh_id' => ['gt',0],
  61. 'gender' => 0,
  62. ];
  63. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(3)->column('id');
  64. //dump($ghuser);
  65. //随机取出一句话
  66. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(3)->column('title');
  67. //dump($oneword);
  68. $tenim = new \app\common\library\Tenim;
  69. for($i = 0;$i < 3;$i++){
  70. $ghuser_one = isset($ghuser[$i]) ? $ghuser[$i] : $ghuser[array_rand($ghuser)];
  71. $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
  72. $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  73. }
  74. }
  75. /**
  76. * 手机验证码登录
  77. *
  78. * @ApiMethod (POST)
  79. * @param string $mobile 手机号
  80. * @param string $captcha 验证码
  81. */
  82. public function mobilelogin()
  83. {
  84. $mobile = input('mobile');
  85. $captcha = input('captcha');
  86. if (!$mobile || !$captcha) {
  87. $this->error(__('Invalid parameters'));
  88. }
  89. if (!Validate::regex($mobile, "^1\d{10}$")) {
  90. $this->error(__('Mobile is incorrect'));
  91. }
  92. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  93. $this->error(__('Captcha is incorrect'));
  94. }
  95. $user = \app\common\model\User::getByMobile($mobile);
  96. if ($user) {
  97. if ($user->status == -1) {
  98. $this->error('账户已注销');
  99. }
  100. if ($user->status != 1) {
  101. $this->error(__('Account is locked'));
  102. }
  103. //如果已经有账号则直接登录
  104. $ret = $this->auth->direct($user->id);
  105. } else {
  106. $extend = [
  107. // 'register_from' => input('register_from',''),
  108. ];
  109. $ret = $this->auth->register('', '', '', $mobile, $extend);
  110. }
  111. if ($ret) {
  112. Sms::flush($mobile, 'mobilelogin');
  113. $data = $this->userInfo('return');
  114. $this->success(__('Logged in successful'), $data);
  115. } else {
  116. $this->error($this->auth->getError());
  117. }
  118. }
  119. /**
  120. * 注册会员
  121. *
  122. * @ApiMethod (POST)
  123. * @param string $username 用户名
  124. * @param string $password 密码
  125. * @param string $email 邮箱
  126. * @param string $mobile 手机号
  127. * @param string $code 验证码
  128. */
  129. /*public function register()
  130. {
  131. $username = $this->request->post('username');
  132. $password = $this->request->post('password');
  133. $email = $this->request->post('email');
  134. $mobile = $this->request->post('mobile');
  135. $code = $this->request->post('code');
  136. if (!$username || !$password) {
  137. $this->error(__('Invalid parameters'));
  138. }
  139. if ($email && !Validate::is($email, "email")) {
  140. $this->error(__('Email is incorrect'));
  141. }
  142. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  143. $this->error(__('Mobile is incorrect'));
  144. }
  145. $ret = Sms::check($mobile, $code, 'register');
  146. if (!$ret) {
  147. $this->error(__('Captcha is incorrect'));
  148. }
  149. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  150. if ($ret) {
  151. $data = $this->userInfo('return');
  152. $this->success(__('Sign up successful'), $data);
  153. } else {
  154. $this->error($this->auth->getError());
  155. }
  156. }*/
  157. //微信登录+注册
  158. public function wechatlogin(){
  159. $nickname = input('nickname','');
  160. $avatar = input('avatar','');
  161. $gender = input('gender',1);
  162. $wechat_openid = input('wechat_openid','');
  163. if (!$wechat_openid) {
  164. $this->error(__('Invalid parameters'));
  165. }
  166. if($gender != 1){
  167. $gender = 0;
  168. }
  169. $user = \app\common\model\User::getByOpenid($wechat_openid);
  170. if ($user) {
  171. if ($user->status != 1) {
  172. $this->error(__('Account is locked'));
  173. }
  174. //如果已经有账号则直接登录
  175. $ret = $this->auth->direct($user->id);
  176. } else {
  177. if (!$nickname || !$avatar) {
  178. $this->error(__('Invalid parameters'));
  179. }
  180. $reg_data = [
  181. 'nickname'=>$nickname,
  182. 'avatar'=>$avatar,
  183. 'gender'=>$gender,
  184. 'register_from' => input('register_from',''),
  185. ];
  186. $ret = $this->auth->openid_register($wechat_openid,$reg_data);
  187. }
  188. if ($ret) {
  189. $data = $this->userInfo('return');
  190. $this->success(__('Logged in successful'), $data);
  191. } else {
  192. $this->error($this->auth->getError());
  193. }
  194. }
  195. /**
  196. * 运营商一键登录
  197. */
  198. public function onLogin()
  199. {
  200. $accessToken = input('accessToken');// 运营商预取号获取到的token
  201. $token = input('tokenT');// 易盾返回的token
  202. if (!$accessToken || !$token) {
  203. $this->error("参数获取失败!");
  204. }
  205. $params = array(
  206. // 运营商预取号获取到的token
  207. "accessToken" => $accessToken,
  208. // 易盾返回的token
  209. "token" => $token
  210. );
  211. // 获取密钥配置
  212. $configInfo = config("onLogin");
  213. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  214. $onret = $onlogin->check($params);
  215. // $ret = [];
  216. // $ret["code"] = 200;
  217. // $ret["msg"] = "ok";
  218. // $ret["data"] = [
  219. // "phone" => "17574504021",
  220. // "resultCode" => 0
  221. // ];
  222. if ($onret["code"] == 200) {
  223. $mobile = $onret["data"]["phone"];
  224. if (empty($mobile)) {
  225. // 取号失败,建议进行二次验证,例如短信验证码
  226. $this->error("取号登录失败,请用验证码方式登录!");
  227. } else {
  228. // 取号成功, 执行登录等流程
  229. // 用户登录逻辑 === 开始
  230. $user = \app\common\model\User::getByMobile($mobile);
  231. if ($user) {
  232. if ($user->status != 1) {
  233. $this->error(__('Account is locked'));
  234. }
  235. //如果已经有账号则直接登录
  236. $ret = $this->auth->direct($user->id);
  237. $is_register = 0;
  238. } else {
  239. $ret = $this->auth->register('', '', '', $mobile, []);
  240. $is_register = 1;
  241. }
  242. if ($ret) {
  243. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  244. } else {
  245. $this->error($this->auth->getError());
  246. }
  247. // 用户登录逻辑 === 结束
  248. }
  249. } else {
  250. $this->error("登录失败,请用验证码方式登录!");
  251. }
  252. }
  253. //用户详细资料
  254. public function userInfo($type = 1){
  255. $info = $this->auth->getUserinfo();
  256. if($type == 'return'){
  257. return $info;
  258. }
  259. $this->success(__('success'),$info);
  260. }
  261. //申请真人认证
  262. public function apply_real_confirm(){
  263. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  264. if(!$avatar){
  265. $this->error('请上传真人头像');
  266. }
  267. Db::startTrans();
  268. $data = [
  269. 'avatar' => $avatar,
  270. 'real_status' => 1,
  271. ];
  272. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  273. if($rs === false){
  274. Db::rollback();
  275. $this->error('认证失败');
  276. }
  277. //tag任务赠送金币
  278. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  279. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  280. if($task_rs === false){
  281. Db::rollback();
  282. $this->error('完成任务赠送奖励失败');
  283. }
  284. //完成真人头像 +5金币
  285. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  286. if($task_rs === false){
  287. Db::rollback();
  288. $this->error('完成任务赠送奖励失败');
  289. }
  290. //邀请人拿奖励,男性3元
  291. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  292. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  293. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  294. if($task_rs['status'] === false){
  295. Db::rollback();
  296. $this->error($task_rs['msg']);
  297. }
  298. }
  299. //系统消息
  300. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  301. Db::commit();
  302. $this->success();
  303. }
  304. //实名认证信息
  305. public function idcard_confirm_info(){
  306. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  307. $this->success('success',$check);
  308. }
  309. //申请实名认证
  310. public function apply_idcard_confirm(){
  311. $truename = input('truename','');
  312. $idcard = input('idcard','');
  313. //$idcard_images = input('idcard_images','');
  314. $alipay_account = input('alipay_account','');
  315. if(empty($truename) || empty($idcard) || empty($alipay_account)){
  316. $this->error('实名认证信息必填');
  317. }
  318. if($this->auth->idcard_status == 1){
  319. $this->error('您已经完成实名认证');
  320. }
  321. if($this->auth->idcard_status == 0){
  322. $this->error('您已经提交实名认证,请等待审核');
  323. }
  324. Db::startTrans();
  325. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  326. if(!empty($check)){
  327. if($check['status'] == 0){
  328. Db::rollback();
  329. $this->error('您已经提交实名认证,请等待审核');
  330. }
  331. if($check['status'] == 1){
  332. Db::rollback();
  333. $this->error('您已经完成实名认证');
  334. }
  335. }
  336. $data = [
  337. 'user_id' => $this->auth->id,
  338. 'truename' => $truename,
  339. 'idcard' => $idcard,
  340. //'idcard_images' => $idcard_images,
  341. 'alipay_account' => $alipay_account,
  342. 'status' => 0,
  343. 'createtime' => time(),
  344. 'updatetime' => time(),
  345. ];
  346. //更新
  347. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  348. if(!empty($check)){
  349. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  350. }else{
  351. $rs = Db::name('user_idconfirm')->insertGetId($data);
  352. }
  353. if(!$rs || !$update_rs){
  354. Db::rollback();
  355. $this->error('提交失败');
  356. }
  357. Db::commit();
  358. $this->success('提交成功,请等待审核');
  359. }
  360. /**
  361. * 退出登录
  362. * @ApiMethod (POST)
  363. */
  364. public function logout()
  365. {
  366. if (!$this->request->isPost()) {
  367. $this->error(__('Invalid parameters'));
  368. }
  369. //退出im
  370. // $tenIm = new Tenim();
  371. // $tenIm->loginoutim($this->auth->id);
  372. //修改用户活跃0
  373. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 0]);
  374. $this->auth->logout();
  375. $this->success(__('Logout successful'));
  376. }
  377. /**
  378. * 修改会员个人信息
  379. *
  380. * @ApiMethod (POST)
  381. * @param string $avatar 头像地址
  382. * @param string $username 用户名
  383. * @param string $nickname 昵称
  384. * @param string $bio 个人简介
  385. */
  386. public function profile()
  387. {
  388. $field_array = ['nickname','introcode','gender','birthday','attribute','shoesize','height','weight','bio','avatar','photo_images','tag_ids'/*,'hide_is_finishinfo','wechat_account'*/];
  389. $data = [];
  390. foreach($field_array as $key => $field){
  391. if(!request()->has($field,'post')){
  392. continue;
  393. }
  394. $newone = input($field);
  395. if($field == 'avatar'){
  396. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  397. }
  398. if($field == 'photo_images'){
  399. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  400. }
  401. $data[$field] = $newone;
  402. }
  403. if(isset($data['birthday'])){
  404. $data['birthday'] = strtotime($data['birthday']);
  405. }
  406. if(isset($data['avatar'])){
  407. //$data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  408. }
  409. if(isset($data['tag_ids'])){
  410. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  411. }
  412. if(isset($data['introcode'])){
  413. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  414. if(!$intro_user){
  415. $this->error('不存在的邀请人');
  416. }
  417. unset($data['introcode']);//别人的邀请码,不能改了自己的
  418. $data['intro_uid'] = $intro_user;
  419. }
  420. //dump($data);
  421. if(empty($data)){
  422. $this->error('没有任何改变');
  423. }
  424. //默认头像
  425. //现在没头像,也没穿头像,但是却传了性别
  426. if($this->auth->avatar == config('site.domain_cdnurl').'/avatar.png' && isset($data['gender']) && $data['avatar'] == config('site.domain_cdnurl').'/avatar.png'){
  427. if($data['gender'] == 1){
  428. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f9277fba97fd76d9ecfc63b506a3674a.png';
  429. }else{
  430. $data['avatar'] = 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/5030460c05c86774f60123ffea7b78a1.png';
  431. }
  432. }
  433. //默认头像
  434. Db::startTrans();
  435. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  436. if($update_rs === false){
  437. Db::rollback();
  438. $this->error('修改资料失败');
  439. }
  440. //tag任务赠送金币
  441. //上传头像加5金币
  442. /*if(isset($data['avatar'])){
  443. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  444. if($task_rs === false){
  445. Db::rollback();
  446. $this->error('完成任务赠送奖励失败');
  447. }
  448. }*/
  449. //上传本人语音介绍 10金币
  450. /*if(isset($data['audio_bio'])){
  451. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  452. if($task_rs === false){
  453. Db::rollback();
  454. $this->error('完成任务赠送奖励失败');
  455. }
  456. }*/
  457. //上传本人五张照片 10金币
  458. /*if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  459. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  460. if($task_rs === false){
  461. Db::rollback();
  462. $this->error('完成任务赠送奖励失败');
  463. }
  464. }*/
  465. //所有资料完成
  466. /*$user_info = Db::name('user')->where('id',$this->auth->id)->find();
  467. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  468. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  469. if($task_rs === false){
  470. Db::rollback();
  471. $this->error('完成任务赠送奖励失败');
  472. }
  473. }*/
  474. Db::commit();
  475. $this->success();
  476. }
  477. /**
  478. * 修改会员权限
  479. */
  480. public function setpower()
  481. {
  482. $is_vip = $this->is_vip($this->auth->id);
  483. if(!$is_vip){
  484. $this->error('VIP才能设置隐私权限');
  485. }
  486. $data = [
  487. 'yinsi' => input('yinsi',0),
  488. 'yinshen' => input('yinshen',0),
  489. 'wuhen' => input('wuhen',0),
  490. ];
  491. $update_rs = Db::name('user_power')->where('id',$this->auth->id)->update($data);
  492. $this->success();
  493. }
  494. public function set_status_switch(){
  495. }
  496. /*
  497. * 修改用户的坐标
  498. * */
  499. public function change_longlat(){
  500. $longitude = input_post('longitude');
  501. $latitude = input_post('latitude');
  502. $cityname = input_post('cityname');
  503. if(empty($longitude) || empty($latitude) || empty($cityname)){
  504. $this->error();
  505. }
  506. $data = [
  507. 'longitude' => $longitude,
  508. 'latitude' => $latitude,
  509. 'cityname' => $cityname,
  510. ];
  511. Db::name('user')->where('id',$this->auth->id)->update($data);
  512. $this->success();
  513. }
  514. /**
  515. * 修改手机号
  516. *
  517. * @ApiMethod (POST)
  518. * @param string $mobile 手机号
  519. * @param string $captcha 验证码
  520. */
  521. public function changemobile()
  522. {
  523. $user = $this->auth->getUser();
  524. $oldcaptcha = $this->request->request('oldcaptcha');
  525. $mobile = $this->request->request('mobile');
  526. $captcha = $this->request->request('captcha');
  527. if (!$oldcaptcha || !$mobile || !$captcha) {
  528. $this->error(__('Invalid parameters'));
  529. }
  530. if (!Validate::regex($mobile, "^1\d{10}$")) {
  531. $this->error(__('Mobile is incorrect'));
  532. }
  533. if($user->mobile == $mobile){
  534. $this->error('新手机号不能与旧手机号相同');
  535. }
  536. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  537. $this->error(__('Mobile already exist'));
  538. }
  539. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  540. if (!$result) {
  541. $this->error(__('Captcha is incorrect'));
  542. }
  543. $result = Sms::check($mobile, $captcha, 'changemobile');
  544. if (!$result) {
  545. $this->error(__('Captcha is incorrect'));
  546. }
  547. $verification = $user->verification;
  548. $verification->mobile = 1;
  549. $user->verification = $verification;
  550. $user->mobile = $mobile;
  551. $user->save();
  552. Sms::flush($user->mobile, 'changemobile');
  553. Sms::flush($mobile, 'changemobile');
  554. $this->success();
  555. }
  556. /**
  557. * 微信注册来的,绑定手机号
  558. *
  559. * @ApiMethod (POST)
  560. * @param string $mobile 手机号
  561. * @param string $captcha 验证码
  562. */
  563. public function bindmobile()
  564. {
  565. $user = $this->auth->getUser();
  566. $mobile = $this->request->request('mobile');
  567. $captcha = $this->request->request('captcha');
  568. if(!empty($this->auth->mobile)){
  569. $this->error('已经绑定了手机号');
  570. }
  571. if (!$mobile || !$captcha) {
  572. $this->error(__('Invalid parameters'));
  573. }
  574. if (!Validate::regex($mobile, "^1\d{10}$")) {
  575. $this->error(__('Mobile is incorrect'));
  576. }
  577. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  578. $this->error('该手机号已被其他用户绑定');
  579. }
  580. $result = Sms::check($mobile, $captcha, 'changemobile');
  581. if (!$result) {
  582. $this->error(__('Captcha is incorrect'));
  583. }
  584. $verification = $user->verification;
  585. $verification->mobile = 1;
  586. $user->verification = $verification;
  587. $user->mobile = $mobile;
  588. $user->save();
  589. Sms::flush($mobile, 'changemobile');
  590. $this->success('success',$this->userInfo('return'));
  591. }
  592. /**
  593. * 微信注册来的,绑定手机号
  594. *
  595. * @ApiMethod (POST)
  596. * @param string $mobile 手机号
  597. * @param string $captcha 验证码
  598. */
  599. /*public function bindopenid()
  600. {
  601. $user = $this->auth->getUser();
  602. $wechat_openid = $this->request->request('wechat_openid');
  603. if(!empty($this->auth->wechat_openid)){
  604. $this->error('已经绑定了微信号');
  605. }
  606. if (!$wechat_openid) {
  607. $this->error(__('Invalid parameters'));
  608. }
  609. if (\app\common\model\User::where('wechat_openid', $wechat_openid)->find()) {
  610. $this->error('该微信号已被其他用户绑定');
  611. }
  612. $user->wechat_openid = $wechat_openid;
  613. $user->save();
  614. $this->success('success',$this->userInfo('return'));
  615. }*/
  616. /**
  617. * 手机注册来的,绑定微信
  618. *
  619. * @ApiMethod (POST)
  620. * @param string $wechat_openid
  621. */
  622. public function bindopenid()
  623. {
  624. Db::startTrans();
  625. try {
  626. $code = $this->request->param('code','');
  627. if(!$code){
  628. throw new Exception(__('Invalid parameters'));
  629. }
  630. //微信
  631. $wechat = new Wechat();
  632. $openid = $wechat->getOpenid($code);
  633. if(!$openid){
  634. throw new Exception('openid获取失败');
  635. }
  636. $user = model('User')->find($this->auth->id);
  637. if(!empty($user['wechat_openid'])/* && $openid != $user['openid']*/){
  638. throw new Exception('已经绑定了微信号');
  639. }
  640. $otherUserWhere['wechat_openid'] = $openid;
  641. $otherUserWhere['id'] = ['neq',$this->auth->id];
  642. $otherUser = model('User')->where($otherUserWhere)->find();
  643. if (!empty($otherUser)) {
  644. throw new Exception('该微信已被其他用户绑定过');
  645. }
  646. $user->wechat_openid = $openid;
  647. $userRes = $user->save();
  648. if (!$userRes) {
  649. throw new Exception('绑定微信失败');
  650. }
  651. Db::commit();
  652. $this->success('success',$this->userInfo('return'));
  653. } catch (Exception $e) {
  654. Db::rollback();
  655. $this->error($e->getMessage());
  656. }
  657. }
  658. /**
  659. * 重置密码
  660. *
  661. * @ApiMethod (POST)
  662. * @param string $mobile 手机号
  663. * @param string $newpassword 新密码
  664. * @param string $captcha 验证码
  665. */
  666. public function resetpwd()
  667. {
  668. //$type = input("type");
  669. $type = 'mobile';
  670. $mobile = input("mobile");
  671. $email = input("email");
  672. $newpassword = input("newpassword");
  673. $captcha = input("captcha");
  674. if (!$newpassword || !$captcha) {
  675. $this->error(__('Invalid parameters'));
  676. }
  677. if ($type == 'mobile') {
  678. if (!Validate::regex($mobile, "^1\d{10}$")) {
  679. $this->error(__('Mobile is incorrect'));
  680. }
  681. $user = \app\common\model\User::getByMobile($mobile);
  682. if (!$user) {
  683. $this->error(__('User not found'));
  684. }
  685. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  686. if (!$ret) {
  687. $this->error(__('Captcha is incorrect'));
  688. }
  689. Sms::flush($mobile, 'resetpwd');
  690. } else {
  691. if (!Validate::is($email, "email")) {
  692. $this->error(__('Email is incorrect'));
  693. }
  694. $user = \app\common\model\User::getByEmail($email);
  695. if (!$user) {
  696. $this->error(__('User not found'));
  697. }
  698. $ret = Ems::check($email, $captcha, 'resetpwd');
  699. if (!$ret) {
  700. $this->error(__('Captcha is incorrect'));
  701. }
  702. Ems::flush($email, 'resetpwd');
  703. }
  704. //模拟一次登录
  705. $this->auth->direct($user->id);
  706. $ret = $this->auth->changepwd($newpassword, '', true);
  707. if ($ret) {
  708. $this->success(__('Reset password successful'));
  709. } else {
  710. $this->error($this->auth->getError());
  711. }
  712. }
  713. /**
  714. * 修改密码
  715. *
  716. * @ApiMethod (POST)
  717. * @param string $newpassword 新密码
  718. * @param string $oldpassword 旧密码
  719. */
  720. public function changepwd(){
  721. $newpassword = input('newpassword');
  722. $oldpassword = input('oldpassword','');
  723. if (!$newpassword) {
  724. $this->error(__('Invalid parameters'));
  725. }
  726. if($this->auth->password && empty($oldpassword)){
  727. $this->error('原密码必填');
  728. }
  729. if(empty($this->auth->password)){
  730. $ret = $this->auth->changepwd($newpassword, '', true);
  731. }else{
  732. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  733. }
  734. if ($ret) {
  735. $this->success('设置密码成功');
  736. } else {
  737. $this->error($this->auth->getError());
  738. }
  739. }
  740. /**
  741. * 记录当前登陆的设备ID,设备信息,IP等
  742. */
  743. public function changeDeviceIp()
  744. {
  745. // 接口防并发
  746. if (!$this->apiLimit(1, 5000)) {
  747. return ;
  748. }
  749. $user = $this->auth->getUser();
  750. $ip = request()->ip();
  751. $deviceId = $this->request->request('device_id','');
  752. $phoneModel = $this->request->request('phone_model','');
  753. $brand = $this->request->request('brand','');
  754. $apiVersion = $this->request->request('api_version','');
  755. $deviceOs = $this->request->request('device_os','');
  756. if ($ip !== $user->loginip){
  757. $update = [];
  758. $update['id'] = $user->id;
  759. $update['loginip'] = $ip;
  760. \app\common\model\User::update($update);
  761. }
  762. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  763. if (empty($userDeviceInfo)){
  764. $userDeviceInfo = new UserDeviceInfo();
  765. $userDeviceInfo->user_id = $user->u_id;
  766. }
  767. $userDeviceInfo->device_os = $deviceOs;
  768. $userDeviceInfo->device_id = $deviceId;
  769. $userDeviceInfo->phone_model = $phoneModel;
  770. $userDeviceInfo->brand = $brand;
  771. $userDeviceInfo->api_version = $apiVersion;
  772. $userDeviceInfo->save();
  773. //首页接口调用,这里不反回信息
  774. // $this->success("更新成功!");
  775. }
  776. //假注销
  777. public function cancleUser(){
  778. if (!$this->request->isPost()) {
  779. $this->error(__('Invalid parameters'));
  780. }
  781. //退出im
  782. // $tenIm = new Tenim();
  783. // $tenIm->loginoutim($this->auth->id);
  784. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  785. $this->auth->logout();
  786. $this->success('注销成功');
  787. }
  788. //修改用户活跃1
  789. public function useractive(){
  790. Db::name('user')->where('id',$this->auth->id)->update(['is_active' => 1,'active_time' => time()]);
  791. $this->success('success');
  792. }
  793. //APP 转化数据统计方案(即:APP 上报对接方案): 广告主上报激活数据,亿米平台搭建服务系统关联点击&下载数据和广告主提供的所有激活数据,将激活数据归因到对应广告。
  794. public function yimi_advert(){
  795. //http://trail.e.mi.com/global/log?appId={appid}&info={data}&conv_type={convType}&customer_id={customerId}
  796. $api_url = 'http://trail.e.mi.com/global/log?';
  797. $api_url_test = 'http://trail.e.mi.com/global/test?';
  798. //应用id 1453045
  799. //秘钥A(encrypt_key):ZxdIaVHvFqSQYzWD
  800. //秘钥B(sign_key):uaeWeunykLRnkyLw
  801. $sign_key = 'uaeWeunykLRnkyLw'; //真的
  802. $encrypt_key = 'ZxdIaVHvFqSQYzWD';//真的
  803. $appid = '1453045';
  804. $conv_type = 'APP_REGISTER';
  805. $customer_id = '292232';
  806. //推荐模式
  807. /*$imei = md5('imei');
  808. $data = [
  809. 'imei' => '91b9185dba1772851dd02b276a6c969e',
  810. 'oaid' => '5fb96f268628810c',
  811. 'conv_time' => '1504687208890',
  812. 'client_ip' => '127.0.0.1',
  813. 'ua' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
  814. ];*/
  815. //采用模式4
  816. /*
  817. $ua = input('ua','','trim');
  818. if(empty($ua)){
  819. return true;
  820. }
  821. $data = [
  822. 'conv_time' => time().substr(microtime(),2,3),
  823. 'client_ip' => request()->ip(),
  824. 'ua' => $ua,
  825. ];
  826. */
  827. //采用模式3
  828. $oaid = input('oaid','','trim');
  829. if(empty($oaid)){
  830. return true;
  831. }
  832. $data = [
  833. 'oaid' => $oaid,
  834. 'conv_time' => time().substr(microtime(),2,3),
  835. 'client_ip' => request()->ip(),
  836. ];
  837. $data_query = http_build_query($data);
  838. //dump($data_query);
  839. $property = $sign_key.'&'.urlencode($data_query);
  840. //dump($property);
  841. $signature = md5($property);
  842. //dump($signature);
  843. $base_data = $data_query .'&sign='.urlencode($signature);
  844. //echo $base_data;
  845. $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
  846. //dump($info);
  847. $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
  848. //echo $request_url;
  849. $result = curl_get($request_url);
  850. //dump($result);
  851. //日志
  852. $log = [
  853. 'param' => $base_data,
  854. 'url' => $request_url,
  855. 'result'=> $result,
  856. 'createtime' => time(),
  857. ];
  858. Db::name('yimi_advert')->insertGetId($log);
  859. return true;
  860. }
  861. //亿米 异或加密,解密
  862. public function xor_enc($str,$key)
  863. {
  864. $crytxt = '';
  865. $keylen = strlen($key);
  866. for($i=0;$i<strlen($str);$i++)
  867. {
  868. $k = $i%$keylen;
  869. $crytxt .= $str[$i] ^ $key[$k];
  870. }
  871. return $crytxt;
  872. }
  873. //公众号获取openid
  874. public function getUserOpenid_gzh(){
  875. $configValue = Service::getConfig('wechat');
  876. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  877. $rs = $wechat->getOpenid();
  878. $this->success('success',$rs);
  879. }
  880. /**
  881. * 微信内H5-JSAPI支付
  882. */
  883. public function jssdkBuildConfig() {
  884. $url = $this->request->request("url");
  885. $configValue = Service::getConfig('wechat');
  886. $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);
  887. $sign = $wechat->getSignPackage(urldecode($url));
  888. $this->success("获取成功!",$sign);
  889. }
  890. }