Usercenter.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\model\wallet;
  6. /**
  7. * 会员中心,不是个人中心
  8. */
  9. class Usercenter extends Api
  10. {
  11. protected $noNeedLogin = ['test','testredis'];
  12. protected $noNeedRight = '*';
  13. protected $allowFields = [
  14. 'id',
  15. 'username',
  16. 'nickname',
  17. 'truename',//
  18. 'email',
  19. 'mobile',
  20. 'avatar',
  21. 'real_status',
  22. 'gender',
  23. 'height',
  24. 'weight',
  25. 'birthday',
  26. 'bio',
  27. 'audio_bio',
  28. 'alipay_account',//
  29. 'idcard_status',
  30. 'longitude',
  31. 'latitude',
  32. 'cityname',
  33. 'photo_images',
  34. 'education_id',
  35. 'hobby_ids',
  36. 'job_id',
  37. 'marital_id',
  38. 'tag_ids',
  39. 'wages_id',
  40. 'hometown_cityid',
  41. ];
  42. public function testredis(){
  43. //redis_matching_set(1,125);
  44. dump(redis_matching_get(1));
  45. $a = cache('?mt_matching_uid_1','',['type'=>'Redis']);
  46. dump($a);
  47. }
  48. public function test(){
  49. $a = [1,4,10,22,66,36,102,45,23,52,35,76,7];
  50. $b = [1,10,7,102];
  51. $c = [10,102];
  52. //dump(array_intersect($a,$b));
  53. $data = array_merge($c,$b);
  54. dump($data);
  55. dump(array_flip(array_flip($data)));
  56. dump(array_flip([]));
  57. }
  58. //获取他人用户信息,留下足迹
  59. public function getuserinfo(){
  60. $uid = input_post('uid',0);
  61. $userinfo = Db::name('user')->field($this->allowFields)->where('id',$uid)->find();
  62. if(!$userinfo){
  63. $this->error('不存在的用户');
  64. }
  65. //
  66. $userinfo = info_domain_image($userinfo,['avatar','photo_images']);
  67. $new_data = [
  68. 'age' => birthtime_to_age($userinfo['birthday']),
  69. 'truename' => $userinfo['idcard_status'] == 1 ? $userinfo['truename'] : '',
  70. 'alipay_account' => $userinfo['idcard_status'] == 1 ? $userinfo['alipay_account'] : '',
  71. ];
  72. $userinfo = array_merge($userinfo,$new_data);
  73. //枚举
  74. $userinfo['education'] = Db::name('enum_education')->where('id',$userinfo['education_id'])->value('name');
  75. $userinfo['hobby'] = Db::name('enum_hobby')->where('id','IN',$userinfo['hobby_ids'])->field(['id','name'])->select();
  76. $userinfo['job'] = Db::name('enum_job')->where('id',$userinfo['job_id'])->value('name');
  77. $userinfo['marital'] = Db::name('enum_marital')->where('id',$userinfo['marital_id'])->value('name');
  78. $userinfo['tag'] = Db::name('enum_tag')->where('id','IN',$userinfo['tag_ids'])->field(['id','name'])->select();
  79. $userinfo['wages'] = Db::name('enum_wages')->where('id',$userinfo['wages_id'])->value('name');
  80. //家乡
  81. $userinfo['hometown_city'] = Db::name('area')->where('id',$userinfo['hometown_cityid'])->value('name');
  82. //vip
  83. $userinfo['vip_endtime'] = Db::name('user_wallet')->where('user_id',$uid)->value('vip_endtime');
  84. $userinfo['is_vip'] = $userinfo['vip_endtime'] > time() ? 1 : 0;
  85. //是否喜欢和关注
  86. $is_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>$uid])->find();
  87. $userinfo['is_follow'] = $is_follow ? 1 : 0;
  88. $is_like = Db::name('user_like')->where(['uid'=>$this->auth->id,'like_uid'=>$uid])->find();
  89. $userinfo['is_like'] = $is_like ? 1 : 0;
  90. //是否拉黑
  91. $is_black = Db::name('user_black')->where(['uid'=>$this->auth->id,'black_uid'=>$uid])->find();
  92. $userinfo['is_black'] = $is_black ? 1 : 0;
  93. //查看别人信息,就要留下痕迹
  94. $data = [
  95. 'uid' => $this->auth->id,
  96. 'to_uid' => $uid,
  97. ];
  98. $check = Db::name('user_visit')->where($data)->find();
  99. if($check){
  100. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  101. }else{
  102. $data['number'] = 1;
  103. $data['updatetime'] = time();
  104. Db::name('user_visit')->insertGetId($data);
  105. }
  106. $this->success('success',$userinfo);
  107. }
  108. //同城
  109. public function samecity(){
  110. $gender = input_post('gender','all');
  111. $agemin = input_post('agemin',0);
  112. $agemax = input_post('agemax',100);
  113. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  114. $this->success('success',[]);
  115. }
  116. $map = [
  117. 'user.status' => 1,
  118. 'user.cityname' => $this->auth->cityname,
  119. 'user.id' => ['neq',$this->auth->id],
  120. 'user.longitude' => ['neq',''],
  121. 'user.latitude' => ['neq',''],
  122. ];
  123. if($gender != 'all'){
  124. $map['user.gender'] = $gender;
  125. }
  126. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  127. //dump($map);
  128. $field = [
  129. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  130. ];
  131. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  132. //dump($list);
  133. $list = list_domain_image($list,['avatar']);
  134. foreach($list as $key => $one){
  135. $one['age'] = birthtime_to_age($one['birthday']);
  136. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  137. $list[$key] = $one;
  138. }
  139. $this->success('success',$list);
  140. }
  141. //附近
  142. public function nearuser(){
  143. $gender = input_post('gender','all');
  144. $agemin = input_post('agemin',0);
  145. $agemax = input_post('agemax',100);
  146. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  147. $this->success('success',[]);
  148. }
  149. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  150. $map = [
  151. 'user.status' => 1,
  152. //'user.cityname' => $this->auth->cityname,
  153. 'user.id' => ['neq',$this->auth->id],
  154. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  155. 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  156. ];
  157. if($gender != 'all'){
  158. $map['user.gender'] = $gender;
  159. }
  160. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  161. //dump($map);
  162. $field = [
  163. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  164. ];
  165. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  166. //dump($list);exit;
  167. $list = list_domain_image($list,['avatar']);
  168. foreach($list as $key => $one){
  169. $one['age'] = birthtime_to_age($one['birthday']);
  170. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  171. $list[$key] = $one;
  172. }
  173. $this->success('success',$list);
  174. }
  175. //视频通话每分钟调用一次
  176. public function video_onemin(){
  177. $to_user_id = input_post('to_user_id');
  178. //先检查今天免费的一分钟
  179. $start = strtotime(date('Y-m-d'));
  180. $end = $start + 86399;
  181. $map = [
  182. 'user_id' => $this->auth->id,
  183. 'createtime' => ['between',[$start,$end]],
  184. 'price' => 0,
  185. ];
  186. $check = Db::name('user_match_video_log')->where($map)->find();
  187. //设置价格
  188. $price = config('site.video_min_price');
  189. $price = empty($check) ? 0 : $price;
  190. Db::startTrans();
  191. //记录日志
  192. $data = [
  193. 'user_id' => $this->auth->id,
  194. 'price' => $price,
  195. 'createtime' => time(),
  196. 'to_user_id' => $to_user_id,
  197. ];
  198. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  199. if(!$log_id){
  200. Db::rollback();
  201. $this->error('扣费失败');
  202. }
  203. //扣费
  204. if(!empty($check) && $log_id){
  205. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_match_video_log',$log_id);
  206. if($rs['status'] === false){
  207. Db::rollback();
  208. $this->error($rs['msg']);
  209. }
  210. }
  211. Db::commit();
  212. $this->success('success');
  213. }
  214. //语音通话每分钟调用一次
  215. public function audio_onemin(){
  216. $to_user_id = input_post('to_user_id');
  217. //先检查今天免费的一分钟
  218. $start = strtotime(date('Y-m-d'));
  219. $end = $start + 86399;
  220. $map = [
  221. 'user_id' => $this->auth->id,
  222. 'createtime' => ['between',[$start,$end]],
  223. 'price' => 0,
  224. ];
  225. $check = Db::name('user_match_audio_log')->where($map)->find();
  226. //设置价格
  227. $price = config('site.audio_min_price');
  228. $price = empty($check) ? 0 : $price;
  229. Db::startTrans();
  230. //记录日志
  231. $data = [
  232. 'user_id' => $this->auth->id,
  233. 'price' => $price,
  234. 'createtime' => time(),
  235. 'to_user_id' => $to_user_id,
  236. ];
  237. $log_id = Db::name('user_match_audio_log')->insertGetId($data);
  238. if(!$log_id){
  239. Db::rollback();
  240. $this->error('扣费失败');
  241. }
  242. //扣费
  243. if(!empty($check) && $log_id){
  244. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,12,'','user_match_audio_log',$log_id);
  245. if($rs['status'] === false){
  246. Db::rollback();
  247. $this->error($rs['msg']);
  248. }
  249. }
  250. Db::commit();
  251. $this->success('success');
  252. }
  253. //打字聊天每句话调用一次
  254. public function typing_once(){
  255. $to_user_id = input_post('to_user_id');
  256. //设置价格
  257. $price = config('site.typing_min_price');
  258. Db::startTrans();
  259. //记录日志
  260. $data = [
  261. 'user_id' => $this->auth->id,
  262. 'price' => $price,
  263. 'createtime' => time(),
  264. 'to_user_id' => $to_user_id,
  265. ];
  266. $log_id = Db::name('user_match_typing_log')->insertGetId($data);
  267. if(!$log_id){
  268. Db::rollback();
  269. $this->error('扣费失败');
  270. }
  271. //扣费
  272. if(!empty($check) && $log_id){
  273. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,13,'','user_match_typing_log',$log_id);
  274. if($rs['status'] === false){
  275. Db::rollback();
  276. $this->error($rs['msg']);
  277. }
  278. }
  279. Db::commit();
  280. $this->success('success');
  281. }
  282. //语音匹配
  283. public function getaudiouser(){
  284. //判断资格
  285. $start = strtotime(date('Y-m-d'));
  286. $end = $start + 86399;
  287. $map = [
  288. 'user_id' => $this->auth->id,
  289. 'createtime' => ['between',[$start,$end]],
  290. 'price' => 0,
  291. ];
  292. $check = Db::name('user_match_audio_log')->where($map)->find();
  293. //$check = true;
  294. //已经用掉免费的了,判断金额
  295. if($check){
  296. $price = config('site.audio_min_price');
  297. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  298. if($gold < $price){
  299. $this->error('您的金币已经不足,请充值');
  300. }
  301. }
  302. //找到互关的人,排除
  303. $follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  304. //dump($follow_me);
  305. $my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  306. //dump($my_follow);exit;
  307. //给出备选用户
  308. /*$map = [
  309. 'status' =>1,
  310. 'gender' => $this->auth->gender == 1 ? 0 : 1,
  311. 'real_status' => 1,
  312. 'idcard_status' => 1,
  313. //打开语音开关的
  314. 'open_match_audio' => 1,
  315. 'id' => ['NOT IN',$my_follow]
  316. ];*/
  317. $map = [];
  318. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  319. $lists = $this->fliter_user($lists);
  320. $result = [];
  321. if(!empty($lists)){
  322. foreach($lists as $key => $val){
  323. $result[] = ['id'=>$val];
  324. }
  325. }
  326. $this->success('success',$result);
  327. }
  328. //视频匹配
  329. public function getvideouser(){
  330. //判断资格
  331. $start = strtotime(date('Y-m-d'));
  332. $end = $start + 86399;
  333. $map = [
  334. 'user_id' => $this->auth->id,
  335. 'createtime' => ['between',[$start,$end]],
  336. 'price' => 0,
  337. ];
  338. $check = Db::name('user_match_video_log')->where($map)->find();
  339. //$check = true;
  340. //已经用掉免费的了,判断金额
  341. if($check){
  342. $price = config('site.video_min_price');
  343. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  344. if($gold < $price){
  345. $this->error('您的金币已经不足,请充值');
  346. }
  347. }
  348. //找到互关的人,排除
  349. $follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  350. //dump($follow_me);
  351. $my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  352. //dump($my_follow);exit;
  353. //给出备选用户
  354. /*$map = [
  355. 'status' =>1,
  356. 'gender' => $this->auth->gender == 1 ? 0 : 1,
  357. 'real_status' => 1,
  358. 'idcard_status' => 1,
  359. //打开视频开关的
  360. 'open_match_video' => 1,
  361. 'id' => ['NOT IN',$my_follow]
  362. ];*/
  363. $map = [];
  364. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  365. $lists = $this->fliter_user($lists);
  366. $result = [];
  367. if(!empty($lists)){
  368. foreach($lists as $key => $val){
  369. $result[] = ['id'=>$val];
  370. }
  371. }
  372. $this->success('success',$result);
  373. }
  374. //聊天匹配
  375. public function gettypinguser(){
  376. //找到互关的人,排除
  377. $follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  378. //dump($follow_me);
  379. $my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  380. //dump($my_follow);exit;
  381. //给出备选用户
  382. /*$map = [
  383. 'status' =>1,
  384. 'gender' => $this->auth->gender == 1 ? 0 : 1,
  385. 'real_status' => 1,
  386. 'idcard_status' => 1,
  387. //打开聊天开关的
  388. 'open_match_typing' => 1,
  389. 'id' => ['NOT IN',$my_follow]
  390. ];*/
  391. $map = [];
  392. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  393. $lists = $this->fliter_user($lists,10);
  394. $result = [];
  395. if(!empty($lists)){
  396. foreach($lists as $key => $val){
  397. $result[] = ['id'=>$val];
  398. }
  399. }
  400. $this->success('success',$result);
  401. }
  402. //过滤规则
  403. private function fliter_user($lists,$number = 1){
  404. if(empty($lists)){
  405. return $lists;
  406. }
  407. //dump($lists);
  408. //过滤掉通话中的
  409. foreach($lists as $key => $val){
  410. if(redis_matching_get($val['id']) == 1){
  411. unset($lists[$key]);
  412. }
  413. }
  414. //预留全部
  415. $all_result = array_column($lists,'id');
  416. //dump($all_result);
  417. //提取同城的
  418. $citydata = [];
  419. foreach($lists as $key => $val){
  420. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  421. $citydata[] = $val['id'];
  422. }
  423. }
  424. //dump($citydata);
  425. //有标签交集的
  426. $tagdata = [];
  427. foreach($lists as $key => $val){
  428. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  429. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  430. $val_tag_ids = explode(',',$val['tag_ids']);
  431. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  432. $tagdata[] = $val['id'];
  433. }
  434. }
  435. }
  436. //dump($tagdata);
  437. //双条件都满足
  438. $double_data = [];
  439. if(!empty($citydata) && !empty($tagdata)){
  440. $double_data = array_intersect($citydata,$tagdata);
  441. }
  442. //dump($double_data);
  443. if(count($double_data) >= $number){
  444. return $double_data;
  445. }
  446. //两种条件合并,去重。空数组合并没影响
  447. $merge_data = array_merge($citydata,$tagdata);
  448. $merge_data = array_flip(array_flip($merge_data));
  449. //dump($merge_data);
  450. if(count($merge_data) >= $number){
  451. return $merge_data;
  452. }
  453. return $all_result;
  454. }
  455. /**
  456. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  457. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  458. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  459. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  460. * @return float | false | string
  461. */
  462. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  463. if( empty( $point_1 ) || empty( $point_2 ) ){
  464. return false;
  465. }
  466. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  467. $p1_x = $point_1[0];
  468. $p1_y = $point_1[1];
  469. $p2_x = $point_2[0];
  470. $p2_y = $point_2[1];
  471. if(
  472. $p1_x < -180 || $p1_x > 180
  473. || $p2_x < -180 || $p2_x > 180
  474. || $p1_y < -90 || $p1_y > 90
  475. || $p2_y < -90 || $p2_y > 90
  476. ){
  477. return '0公里';
  478. }
  479. // 根据2点各自的坐标,计算2点之间直线距离的公式
  480. $distance = round(6378.138*2*asin(sqrt(pow(sin(( $p1_x *pi()/180-$p2_x*pi()/180)/2),2)+cos( $p1_x *pi()/180)*cos($p2_x*pi()/180)* pow(sin(( $p1_y *pi()/180-$p2_y*pi()/180)/2),2)))*1000);
  481. // 是否计算为字符串公里距离
  482. if( !$calc_as_string ){
  483. return (string)round( $distance / 1000 , 1 ) . '公里';
  484. }
  485. // 如果计算为字符串公里距离
  486. if( $distance / 1000 > 1 ){
  487. $k = (string)round( $distance / 1000 , 1 );
  488. $m = (string)$distance % 1000 ;
  489. $distance = "{$k}公里{$m}米";
  490. }
  491. else{
  492. $distance = "{$distance}米";
  493. }
  494. return $distance;
  495. }
  496. //地图api,根据两地坐标,获得两地距离,打卡用的
  497. //type=0直线,type=1开车
  498. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  499. $result = 0;
  500. $apiurl = 'https://restapi.amap.com/v3/distance?';
  501. $param = [
  502. 'key' => '398c424811d1a59beac2f915323d334e',
  503. 'origins' => $start_lon.','.$start_lat,
  504. 'destination' => $end_lon.','.$end_lat,
  505. 'type' => $type,
  506. 'output' => 'json',
  507. ];
  508. $apiurl .= http_build_query($param);
  509. $request_rs = json_decode(curl_get($apiurl),true);
  510. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  511. if(isset($request_rs['results'][0]['distance']))
  512. {
  513. $result = $request_rs['results'][0]['distance'];
  514. }
  515. }
  516. //dump($result);
  517. return $result;
  518. }
  519. public function distance()
  520. {
  521. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  522. dump($a);
  523. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  524. dump($a);
  525. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  526. dump($b);
  527. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  528. dump($b);
  529. }
  530. }