123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?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',$this->auth->cityname);
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- 'user.photo_images' => ['neq',''],
- 'user.cityname' => $cityname,
- 'power.yinshen' => 0,
- ];
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
- //属性
- $attribute = input('attribute','all');
- if($attribute != 'all' && $attribute != 'BOTH'){
- $where['user.attribute'] = $attribute;
- }
- //排除黑名单的
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where['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]];
- }
- //距离
- $distancemin = input('distancemin',0);
- if($distancemin > 0){
- $where['distance'] = ['gt',$distancemin];
- }
- $distancemax = input('distancemax',0);
- if($distancemax > 0){
- $where['distance'] = ['lt',$distancemax];
- }
- if($distancemin > 0 && $distancemax > 0){
- $where['distance'] = ['between',[$distancemin,$distancemax]];
- }
- $field = [
- 'user.id',
- 'user.username',
- 'user.nickname',
- 'user.avatar',
- 'user.photo_images',
- 'user.gender',
- 'user.birthday',
- 'user.cityname',
- 'user.longitude',
- 'user.latitude',
- 'user.attribute',
- 'wallet.vip_endtime',
- '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
- 'active.requesttime',
- ];
- $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')
- ->join('user_active active' ,'user.id = active.user_id','LEFT')
- ->where($where)
- ->order('distance asc')
- ->autopage()
- ->select();
- $list = list_domain_image($list,['avatar','photo_images']);
- foreach($list as $key => &$val){
- $val['age'] = birthtime_to_age($val['birthday']);
- $val['photo_images'] = explode(',',$val['photo_images'])[0];
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- $val['distance'] = bcdiv(intval($val['distance']),1000,2).'km';
- $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
- }
- $this->success(1,$list);
- }
- //推荐
- //真人认证的,是推荐用户的,可能也要限制vip的
- public function tuijian(){
- $cityname = input('cityname',$this->auth->cityname);
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- 'user.photo_images' => ['neq',''],
- 'user.cityname' => $cityname,
- 'power.yinshen' => 0,
- ];
- //推荐条件
- $where_tuijian = [
- 'user.idcard_status' => 1,
- 'user.is_tuijian' => 1,
- ];
- if(config('site.index_tuijian_vip_limit') == 1){
- $where_tuijian['wallet.vip_endtime'] = ['gt',time()];
- }
- //推荐条件
- $where = array_merge($where,$where_tuijian);
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
- //属性
- $attribute = input('attribute','all');
- if($attribute != 'all' && $attribute != 'BOTH'){
- $where['user.attribute'] = $attribute;
- }
- //排除黑名单的
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where['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]];
- }
- //距离
- $distancemin = input('distancemin',0);
- if($distancemin > 0){
- $where['distance'] = ['gt',$distancemin];
- }
- $distancemax = input('distancemax',0);
- if($distancemax > 0){
- $where['distance'] = ['lt',$distancemax];
- }
- if($distancemin > 0 && $distancemax > 0){
- $where['distance'] = ['between',[$distancemin,$distancemax]];
- }
- $field = [
- 'user.id',
- 'user.username',
- 'user.nickname',
- 'user.avatar',
- 'user.photo_images',
- 'user.gender',
- 'user.birthday',
- 'user.cityname',
- 'user.longitude',
- 'user.latitude',
- 'user.attribute',
- 'wallet.vip_endtime',
- '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
- 'active.requesttime',
- ];
- $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')
- ->join('user_active active' ,'user.id = active.user_id','LEFT')
- ->where($where)
- ->order('distance asc')
- ->autopage()
- ->select();
- $list = list_domain_image($list,['avatar','photo_images']);
- foreach($list as $key => &$val){
- $val['age'] = birthtime_to_age($val['birthday']);
- $val['photo_images'] = explode(',',$val['photo_images'])[0];
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- $val['distance'] = bcdiv(intval($val['distance']),1000,2).'km';
- $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
- }
- $this->success(1,$list);
- }
- //匹配
- //做防止重复处理,参照荔枝
- public function pipei(){
- //检查剩余次数
- //缓存,防重复
- $user_id = $this->auth->id;
- $user_id_redis = 'u_'.$user_id;
- $redis_ids = json_decode(Cache::get($user_id_redis),true);
- //where
- $where = [
- 'user.id' => ['neq',$this->auth->id],
- 'user.status' => 1,
- ];
- //性别
- $gender = input('gender','all');
- if($gender != 'all'){
- $where['user.gender'] = $gender;
- }
- //排除黑名单的
- $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
- if(!empty($black_ids)){
- $where['user.id'] = ['NOTIN',$black_ids];
- }
- //匹配一个
- $result = $this->pipei_action($redis_ids,$where);
- //匹配不到,移除防重复
- if(!$result) {
- Cache::rm($user_id_redis);
- $redis_ids = [];
- $result = $this->pipei_action($redis_ids,$where);
- }
- // 追加一个防重复
- if($result){
- if($redis_ids) {
- $redis_ids[] = $result;
- } else {
- $redis_ids = [$result];
- }
- Cache::set($user_id_redis,json_encode($redis_ids));
- }else{
- Cache::rm($user_id_redis);
- }
- $this->success(1,$result);
- }
- private function pipei_action($redis_ids,$where){
- $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)
- ->orderRaw('rand()')
- ->value('user.id');
- return $result;
- }
- ///////////////////////////////////
- public function test(){
- //缓存,防重复
- $user_id = $this->auth->id;
- $user_id_redis = 'u_'.$user_id;
- $redis_ids = json_decode(Cache::get($user_id_redis),true);
- dump($redis_ids);
- }
- public function testrm(){
- $user_id = $this->auth->id;
- $user_id_redis = 'u_'.$user_id;
- Cache::rm($user_id_redis);
- }
- }
|