User.php 34 KB

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