123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Cache;
- /**
- * 首页接口
- */
- class Index extends Api
- {
- protected $noNeedLogin = ['index'];
- protected $noNeedRight = ['*'];
- public function index(){
- echo 'apisuccess';
- exit;
- }
- //附近
- //是vip,且开启了隐身的,不能在内
- public function fujin(){
- $cityname = input('cityname','');
- //强制条件
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- 'power.yinshen' => 0,
- ];
- if(!empty($cityname) && $cityname != $this->auth->cityname){
- $where['user.cityname'] = $cityname;
- }else{
- }
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
-
- //排除黑名单的
- $where_black = [];
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where_black['user.id'] = ['NOTIN',$black_ids];
- }
- //年龄
- $agemin = input('agemin',18);
- if($agemin > 18){
- $where['user.birthday'] = ['lt',time()-$agemin*31536000];
- }
- $agemax = input('agemax',100);
- if($agemax < 100){
- $where['user.birthday'] = ['gt',time()-$agemax*31536000];
- }
- if($agemin > 18 && $agemax < 100){
- $where['user.birthday'] = ['between',[time()-$agemax*31536000,time()-$agemin*31536000]];
- }
- //距离
- $having_dis = '';
- $distancemin = input('distancemin',0);
- if($distancemin > 0){
- $having_dis = 'distance > '.$distancemin*1000;
- }
- $distancemax = input('distancemax',100);
- $distancemax = $distancemax > 100 ? 100 : $distancemax;//最大100
- if($distancemax > 0){
- $having_dis = 'distance < '.$distancemax*1000;
- }
- if($distancemin > 0 && $distancemax > 0){
- $having_dis = 'distance > '.$distancemin*1000 .' and distance < '.$distancemax*1000;
- }
- //当异地漫游的时候,距离条件作废
- if(!empty($cityname) && $cityname != $this->auth->cityname){
- $having_dis = '';
- }
- // dump($where);
- // dump($having_dis);
- $field = [
- 'user.id',
- 'user.nickname',
- 'user.avatar',
- 'user.gender',
- 'user.birthday',
- 'user.longitude',
- 'user.latitude',
- 'user.height',
- 'user.weight',
- 'user.cityname',
- 'user.bio',
- 'wallet.vip_endtime',
- '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
- ];
- $list = Db::name('user')->alias('user')->field($field)
- ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
- ->join('user_power power' ,'user.id = power.user_id','LEFT')
- ->where($where)
- ->where($where_black)
- ->having($having_dis)
- ->order('distance asc,user.id desc')
- ->autopage()
- ->select();
- $list = list_domain_image($list,['avatar']);
- foreach($list as $key => &$val){
- $val['age'] = birthtime_to_age($val['birthday']);
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
- if($this->auth->longitude == 0 || $val['longitude'] == 0){
- $val['distance'] = '未知';
- }
- //vip如果开了隐私保护,需要隐藏距离
- $yinsi = $this->user_power($val['id'],'yinsi');
- if($yinsi == 1){
- $val['distance'] = '';
- }
- //简单信息
- $simple_info = '';
- if(!empty($val['height'])){
- $simple_info .= $val['height'];
- }
- if(!empty($val['weight'])){
- if(!empty($simple_info)){
- $simple_info .= '|';
- }
- $simple_info .= $val['weight'];
- }
- if(!empty($val['cityname'])){
- if(!empty($simple_info)){
- $simple_info .= '|';
- }
- $simple_info .= $val['cityname'];
- }
- $val['simple_info'] = $simple_info;
- //
- unset($val['longitude']);
- unset($val['latitude']);
- unset($val['height']);
- unset($val['weight']);
- unset($val['cityname']);
- unset($val['vip_endtime']);
- unset($val['birthday']);
- }
- $this->success(1,$list);
- }
- //推荐
- //真人认证的,是推荐用户的,可能也要限制vip的
- public function tuijian(){
- //强制条件
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- // 'power.yinshen' => 0,
- ];
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
- //排除黑名单的
- $where_black = [];
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where_black['user.id'] = ['NOTIN',$black_ids];
- }
- //年龄
- $agemin = input('agemin',18);
- if($agemin > 18){
- $where['user.birthday'] = ['lt',time()-$agemin*31536000];
- }
- $agemax = input('agemax',100);
- if($agemax < 100){
- $where['user.birthday'] = ['gt',time()-$agemax*31536000];
- }
- if($agemin > 18 && $agemax < 100){
- $where['user.birthday'] = ['between',[time()-$agemax*31536000,time()-$agemin*31536000]];
- }
- //其他条件 与 推荐条件是 或的关系
- $where_other = [
- 'user.avatar' => ['neq',''],
- 'user.idcard_status' => 1,
- ];
- if(config('site.index_tuijian_vip_limit') == 1){
- $where_other['wallet.vip_endtime'] = ['gt',time()];
- }
- //选择了推荐,就强制推荐
- $where_tuijian = [
- 'user.is_tuijian' => 1,
- ];
- //距离
- $having_dis = '';
- $distancemin = input('distancemin',0);
- if($distancemin > 0){
- $having_dis = 'distance > '.$distancemin*1000;
- }
- $distancemax = input('distancemax',0);
- if($distancemax > 0){
- $having_dis = 'distance < '.$distancemax*1000;
- }
- if($distancemin > 0 && $distancemax > 0){
- $having_dis = 'distance > '.$distancemin*1000 .' and distance < '.$distancemax*1000;
- }
- $field = [
- 'user.id',
- 'user.nickname',
- 'user.avatar',
- 'user.gender',
- 'user.birthday',
- 'user.longitude',
- 'user.latitude',
- 'user.height',
- 'user.weight',
- 'user.cityname',
- 'user.bio',
- 'wallet.vip_endtime',
- '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
- ];
- $list = Db::name('user')->alias('user')->field($field)
- ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
- ->join('user_power power' ,'user.id = power.user_id','LEFT')
- ->where($where)
- ->where($where_black)
- ->where(
- function($query)use($where_other,$where_tuijian){
- $query->where(
- function($query)use($where_other){
- $query->where($where_other);
- }
- );
- $query->whereOr($where_tuijian);
- })
- //->having($having_dis)推荐完全不受距离限制
- ->order('user.is_tuijian desc,user.is_active desc,distance asc')
- ->autopage()
- ->select();
- $list = list_domain_image($list,['avatar']);
- foreach($list as $key => &$val){
- $val['age'] = birthtime_to_age($val['birthday']);
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
- if($this->auth->longitude == 0 || $val['longitude'] == 0){
- $val['distance'] = '未知';
- }
- //vip如果开了隐私保护,需要隐藏距离,隐藏所在城市
- $yinsi = $this->user_power($val['id'],'yinsi');
- if($yinsi == 1){
- $val['distance'] = '';
- $val['cityname'] = '';
- }
- //简单信息
- $simple_info = '';
- if(!empty($val['height'])){
- $simple_info .= $val['height'];
- }
- if(!empty($val['weight'])){
- if(!empty($simple_info)){
- $simple_info .= '|';
- }
- $simple_info .= $val['weight'];
- }
- if(!empty($val['cityname'])){
- if(!empty($simple_info)){
- $simple_info .= '|';
- }
- $simple_info .= $val['cityname'];
- }
- $val['simple_info'] = $simple_info;
- //
- unset($val['longitude']);
- unset($val['latitude']);
- unset($val['height']);
- unset($val['weight']);
- unset($val['cityname']);
- unset($val['vip_endtime']);
- unset($val['birthday']);
- }
- $this->success(1,$list);
- }
- //匹配配置
- public function pipei_config(){
- $result = [
- 'index_pipei_switch' => config('site.index_pipei_switch'), //匹配开关
- ];
- //首页匹配每天每人匹配次数
- $user_id = $this->auth->id;
- $is_vip = $this->is_vip($this->auth->id);
- $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
- $times_limit_redis = 'pipei_times_limit_'.$user_id;
- $user_times = Cache::get($times_limit_redis) ?: 0;
- if($times_limit > -1){
- $remain_times = $times_limit - $user_times;
- if($remain_times < 0){
- $remain_times = 0;
- }
- }else{
- $remain_times = -1;
- }
- $result['remain_times'] = $remain_times;
- $this->success(1,$result);
- }
- //匹配
- //做防止重复处理,参照荔枝
- public function pipei(){
- //首页匹配功能开关
- $index_pipei_switch = config('site.index_pipei_switch');
- if($index_pipei_switch != 1){
- $this->error('匹配功能维护中,请稍后再试');
- }
- //缓存,防重复
- $user_id = $this->auth->id;
- $user_id_redis = 'pipei_repeat_'.$user_id;
- $redis_ids = json_decode(Cache::get($user_id_redis),true);
- //首页匹配每天每人匹配次数
- $is_vip = $this->is_vip($this->auth->id);
- $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
- $times_limit_redis = 'pipei_times_limit_'.$user_id;
- $user_times = Cache::get($times_limit_redis) ?: 0;
- if($times_limit > -1 && $user_times >= $times_limit){
- $this->error('今日已超匹配上限'.$times_limit.'次');
- }
- //where
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- 'user.is_active' => 1,
- ];
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
- //排除黑名单的
- $where_black = [];
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where_black['user.id'] = ['NOTIN',$black_ids];
- }
- //匹配一个
- $result = $this->pipei_action($redis_ids,$where,$where_black);
- //匹配不到,移除防重复
- if(!$result) {
- Cache::rm($user_id_redis);
- $redis_ids = [];
- $result = $this->pipei_action($redis_ids,$where,$where_black);
- }
- // 追加一个防重复
- if($result){
- if($redis_ids) {
- $redis_ids[] = $result;
- } else {
- $redis_ids = [$result];
- }
- Cache::set($user_id_redis,json_encode($redis_ids));
- //设置次数
- $second = strtotime(date('Y-m-d'))+86400 - time();
- Cache::set($times_limit_redis,$user_times+1,$second);
- }else{
- Cache::rm($user_id_redis);
- }
- //需要返回头像
- $result_data = [];
- if($result){
- $result_data = Db::name('user')->field('id,avatar')->where('id',$result)->find();
- $result_data = info_domain_image($result_data,['avatar']);
- }
- $this->success(1,$result_data);
- }
- private function pipei_action($redis_ids,$where,$where_black){
- $where_op = [];
- if(!empty($redis_ids)){
- $where_op['user.id'] = ['NOTIN',$redis_ids];
- }
- $result = Db::name('user')->alias('user')
- //->join('user_active active' ,'user.id = active.user_id','LEFT')
- ->where($where)
- ->where($where_op)
- ->where($where_black)
- ->orderRaw('rand()')
- ->value('user.id');
- return $result;
- }
- ///////////////////////////////////
- public function test(){
- //缓存,防重复
- $user_id = $this->auth->id;
- $user_id_redis = 'pipei_repeat_'.$user_id;
- $redis_ids = json_decode(Cache::get($user_id_redis),true);
- dump($redis_ids);
- $times_limit_redis = 'pipei_times_limit_'.$user_id;
- $user_times = Cache::get($times_limit_redis) ?: 0;
- dump($user_times);
- }
- public function testrm(){
- $user_id = $this->auth->id;
- $user_id_redis = 'pipei_repeat_'.$user_id;
- Cache::rm($user_id_redis);
- $times_limit_redis = 'pipei_times_limit_'.$user_id;
- Cache::rm($times_limit_redis);
- }
- }
|