User.php 37 KB

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