User.php 37 KB

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