Usercenter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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_set(1,125);
  44. dump(redis_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. $data = [
  92. 'uid' => $this->auth->id,
  93. 'to_uid' => $uid,
  94. ];
  95. $check = Db::name('user_visit')->where($data)->find();
  96. if($check){
  97. Db::name('user_visit')->where($data)->update(['number'=>$check['number']+1,'updatetime'=>time()]);
  98. }else{
  99. $data['number'] = 1;
  100. $data['updatetime'] = time();
  101. Db::name('user_visit')->insertGetId($data);
  102. }
  103. $this->success('success',$userinfo);
  104. }
  105. //同城
  106. public function samecity(){
  107. $gender = input_post('gender','all');
  108. $agemin = input_post('agemin',0);
  109. $agemax = input_post('agemax',100);
  110. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  111. $this->success('success',[]);
  112. }
  113. $map = [
  114. 'user.status' => 1,
  115. 'user.cityname' => $this->auth->cityname,
  116. 'user.id' => ['neq',$this->auth->id],
  117. 'user.longitude' => ['neq',''],
  118. 'user.latitude' => ['neq',''],
  119. ];
  120. if($gender != 'all'){
  121. $map['user.gender'] = $gender;
  122. }
  123. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  124. //dump($map);
  125. $field = [
  126. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  127. ];
  128. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  129. //dump($list);
  130. $list = list_domain_image($list,['avatar']);
  131. foreach($list as $key => $one){
  132. $one['age'] = birthtime_to_age($one['birthday']);
  133. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  134. $list[$key] = $one;
  135. }
  136. $this->success('success',$list);
  137. }
  138. //附近
  139. public function nearuser(){
  140. $gender = input_post('gender','all');
  141. $agemin = input_post('agemin',0);
  142. $agemax = input_post('agemax',100);
  143. if(empty($this->auth->cityname) || empty($this->auth->longitude) || empty($this->auth->latitude)){
  144. $this->success('success',[]);
  145. }
  146. //经过地图测算和公式推算,经度纬度 0.1即为11公里
  147. $map = [
  148. 'user.status' => 1,
  149. //'user.cityname' => $this->auth->cityname,
  150. 'user.id' => ['neq',$this->auth->id],
  151. 'user.longitude' => ['between',[$this->auth->longitude - 0.1,$this->auth->longitude + 0.1]],
  152. 'user.latitude' => ['between',[$this->auth->latitude - 0.1,$this->auth->latitude + 0.1]],
  153. ];
  154. if($gender != 'all'){
  155. $map['user.gender'] = $gender;
  156. }
  157. $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
  158. //dump($map);
  159. $field = [
  160. 'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
  161. ];
  162. $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
  163. //dump($list);exit;
  164. $list = list_domain_image($list,['avatar']);
  165. foreach($list as $key => $one){
  166. $one['age'] = birthtime_to_age($one['birthday']);
  167. $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
  168. $list[$key] = $one;
  169. }
  170. $this->success('success',$list);
  171. }
  172. //视频通话每分钟调用一次
  173. public function video_onemin(){
  174. $to_user_id = input_post('to_user_id');
  175. //先检查今天免费的一分钟
  176. $start = strtotime(date('Y-m-d'));
  177. $end = $start + 86399;
  178. $map = [
  179. 'user_id' => $this->auth->id,
  180. 'createtime' => ['between',[$start,$end]],
  181. 'price' => 0,
  182. ];
  183. $check = Db::name('user_match_video_log')->where($map)->find();
  184. //设置价格
  185. $price = config('site.video_min_price');
  186. $price = empty($check) ? 0 : $price;
  187. Db::startTrans();
  188. //记录日志
  189. $data = [
  190. 'user_id' => $this->auth->id,
  191. 'price' => $price,
  192. 'createtime' => time(),
  193. 'to_user_id' => $to_user_id,
  194. ];
  195. $log_id = Db::name('user_match_video_log')->insertGetId($data);
  196. if(!$log_id){
  197. Db::rollback();
  198. $this->error('扣费失败');
  199. }
  200. //扣费
  201. if(!empty($check) && $log_id){
  202. $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_match_video_log',$log_id);
  203. if($rs['status'] === false){
  204. Db::rollback();
  205. $this->error($rs['msg']);
  206. }
  207. }
  208. Db::commit();
  209. $this->success('success');
  210. }
  211. //语音匹配
  212. public function getaudiouser(){
  213. //判断资格
  214. $start = strtotime(date('Y-m-d'));
  215. $end = $start + 86399;
  216. $map = [
  217. 'user_id' => $this->auth->id,
  218. 'createtime' => ['between',[$start,$end]],
  219. 'price' => 0,
  220. ];
  221. $check = Db::name('user_match_audio_log')->where($map)->find();
  222. //$check = true;
  223. //已经用掉免费的了,判断金额
  224. if($check){
  225. $price = config('site.audio_min_price');
  226. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  227. if($gold < $price){
  228. $this->error('您的金币已经不足,请充值');
  229. }
  230. }
  231. //找到互关的人,排除
  232. $follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  233. //dump($follow_me);
  234. $my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  235. //dump($my_follow);exit;
  236. //给出备选用户
  237. /*$map = [
  238. 'status' =>1,
  239. 'gender' => $this->auth->gender == 1 ? 0 : 1,
  240. 'real_status' => 1,
  241. 'idcard_status' => 1,
  242. //打开视频开关的
  243. 'open_match_video' => 1,
  244. 'id' => ['NOT IN',$my_follow]
  245. ];*/
  246. $map = [];
  247. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  248. $lists = $this->fliter_user($lists);
  249. $this->success('success',$lists);
  250. }
  251. //视频匹配
  252. public function getvideouser(){
  253. //判断资格
  254. $start = strtotime(date('Y-m-d'));
  255. $end = $start + 86399;
  256. $map = [
  257. 'user_id' => $this->auth->id,
  258. 'createtime' => ['between',[$start,$end]],
  259. 'price' => 0,
  260. ];
  261. $check = Db::name('user_match_video_log')->where($map)->find();
  262. //$check = true;
  263. //已经用掉免费的了,判断金额
  264. if($check){
  265. $price = config('site.video_min_price');
  266. $gold = model('wallet')->getWallet($this->auth->id,'gold');
  267. if($gold < $price){
  268. $this->error('您的金币已经不足,请充值');
  269. }
  270. }
  271. //找到互关的人,排除
  272. $follow_me = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  273. //dump($follow_me);
  274. $my_follow = Db::name('user_follow')->where(['uid'=>$this->auth->id,'follow_uid'=>['IN',$follow_me]])->column('follow_uid');
  275. //dump($my_follow);exit;
  276. //给出备选用户
  277. /*$map = [
  278. 'status' =>1,
  279. 'gender' => $this->auth->gender == 1 ? 0 : 1,
  280. 'real_status' => 1,
  281. 'idcard_status' => 1,
  282. //打开视频开关的
  283. 'open_match_video' => 1,
  284. 'id' => ['NOT IN',$my_follow]
  285. ];*/
  286. $map = [];
  287. $lists = Db::name('user')->field('id,cityname,status,gender,real_status,tag_ids')->where($map)->order('logintime desc')->page($this->page,100)->select();
  288. $lists = $this->fliter_user($lists);
  289. $this->success('success',$lists);
  290. }
  291. //过滤规则
  292. private function fliter_user($lists){
  293. if(empty($lists)){
  294. return $lists;
  295. }
  296. //过滤掉通话中的
  297. foreach($lists as $key => $val){
  298. if(redis_get($val['id']) == 1){
  299. unset($lists[$key]);
  300. }
  301. }
  302. //预留全部
  303. $all_result = array_column($lists,'id');
  304. //提取同城的
  305. $citydata = [];
  306. foreach($lists as $key => $val){
  307. if( !empty($this->auth->cityname) && $this->auth->cityname == $val['cityname'] ){
  308. $citydata[] = $val['id'];
  309. }
  310. }
  311. //有标签交集的
  312. $tagdata = [];
  313. foreach($lists as $key => $val){
  314. if( !empty($this->auth->tag_ids) && !empty($val['tag_ids']) ){
  315. $auth_tag_ids = explode(',',$this->auth->tag_ids);
  316. $val_tag_ids = explode(',',$val['tag_ids']);
  317. if(count(array_intersect($auth_tag_ids,$val_tag_ids)) > 0){
  318. $tagdata[] = $val['id'];
  319. }
  320. }
  321. }
  322. //两个都满足
  323. $double_data = [];
  324. if(!empty($citydata) && !empty($tagdata)){
  325. $double_data = array_intersect($citydata,$tagdata);
  326. }
  327. //两种条件合并,去重。空数组合并没影响
  328. $merge_data = array_merge($citydata,$tagdata);
  329. $merge_data = array_flip(array_flip($merge_data));
  330. //最终结果
  331. //双条件数量足够就return
  332. if(count($double_data) >= 1){
  333. //echo __LINE__;
  334. return $double_data;
  335. }
  336. //不够就合并
  337. $result_data = array_merge($double_data,$merge_data);
  338. $result_data = array_flip(array_flip($result_data));
  339. if(count($result_data) >= 1){
  340. //echo __LINE__;
  341. return $result_data;
  342. }
  343. //仍然不够,全合并
  344. $all_data = array_merge($result_data,$all_result);
  345. $all_data = array_flip(array_flip($all_data));
  346. return $all_data;
  347. }
  348. /**
  349. * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
  350. * @param array $point_1 第1个点的x,y坐标 array( 101 , 202 )
  351. * @param array $point_2 第2个点的x,y坐标 array( 101 , 202 )
  352. * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
  353. * @return float | false | string
  354. */
  355. private function calc_map_distance( $point_1=array( ) , $point_2=array( ) , $calc_as_string=false ) {
  356. if( empty( $point_1 ) || empty( $point_2 ) ){
  357. return false;
  358. }
  359. // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
  360. $p1_x = $point_1[0];
  361. $p1_y = $point_1[1];
  362. $p2_x = $point_2[0];
  363. $p2_y = $point_2[1];
  364. if(
  365. $p1_x < -180 || $p1_x > 180
  366. || $p2_x < -180 || $p2_x > 180
  367. || $p1_y < -90 || $p1_y > 90
  368. || $p2_y < -90 || $p2_y > 90
  369. ){
  370. return '0公里';
  371. }
  372. // 根据2点各自的坐标,计算2点之间直线距离的公式
  373. $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);
  374. // 是否计算为字符串公里距离
  375. if( !$calc_as_string ){
  376. return (string)round( $distance / 1000 , 1 ) . '公里';
  377. }
  378. // 如果计算为字符串公里距离
  379. if( $distance / 1000 > 1 ){
  380. $k = (string)round( $distance / 1000 , 1 );
  381. $m = (string)$distance % 1000 ;
  382. $distance = "{$k}公里{$m}米";
  383. }
  384. else{
  385. $distance = "{$distance}米";
  386. }
  387. return $distance;
  388. }
  389. //地图api,根据两地坐标,获得两地距离,打卡用的
  390. //type=0直线,type=1开车
  391. private function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
  392. $result = 0;
  393. $apiurl = 'https://restapi.amap.com/v3/distance?';
  394. $param = [
  395. 'key' => '398c424811d1a59beac2f915323d334e',
  396. 'origins' => $start_lon.','.$start_lat,
  397. 'destination' => $end_lon.','.$end_lat,
  398. 'type' => $type,
  399. 'output' => 'json',
  400. ];
  401. $apiurl .= http_build_query($param);
  402. $request_rs = json_decode(curl_get($apiurl),true);
  403. if(isset($request_rs['status']) && $request_rs['status'] == 1){
  404. if(isset($request_rs['results'][0]['distance']))
  405. {
  406. $result = $request_rs['results'][0]['distance'];
  407. }
  408. }
  409. //dump($result);
  410. return $result;
  411. }
  412. public function distance()
  413. {
  414. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
  415. dump($a);
  416. $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
  417. dump($a);
  418. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
  419. dump($b);
  420. $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
  421. dump($b);
  422. }
  423. }