User.php 32 KB

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