Index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Cache;
  6. /**
  7. * 首页接口
  8. */
  9. class Index extends Api
  10. {
  11. protected $noNeedLogin = ['index'];
  12. protected $noNeedRight = ['*'];
  13. public function index(){
  14. echo 'apisuccess';
  15. exit;
  16. }
  17. //附近
  18. //是vip,且开启了隐身的,不能在内
  19. public function fujin(){
  20. $cityname = input('cityname','');
  21. /*if(empty($cityname)){
  22. $cityname = $this->auth->cityname;
  23. }*/
  24. $where = [
  25. 'user.id' => ['neq',$this->auth->id],
  26. 'user.status' => 1,
  27. //'user.cityname' => $cityname,
  28. 'power.yinshen' => 0,
  29. ];
  30. if(!empty($cityname) && $cityname != $this->auth->cityname){
  31. $where['user.cityname'] = $cityname;
  32. }else{
  33. $where['user.provincename'] = $this->auth->provincename;//仅以此缩小范围
  34. }
  35. //性别
  36. $gender = input('gender','all');
  37. if($gender != 'all'){
  38. $where['user.gender'] = $gender;
  39. }
  40. //属性
  41. $attribute = input('attribute','all');
  42. if($attribute != 'all' && $attribute != 'BOTH'){
  43. $where['user.attribute'] = $attribute;
  44. }
  45. //排除黑名单的
  46. $where_black = [];
  47. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  48. if(!empty($black_ids)){
  49. $where_black['user.id'] = ['NOTIN',$black_ids];
  50. }
  51. //年龄
  52. $agemin = input('agemin',18);
  53. if($agemin > 18){
  54. $where['user.birthday'] = ['lt',time()-$agemin*31536000];
  55. }
  56. $agemax = input('agemax',100);
  57. if($agemax < 100){
  58. $where['user.birthday'] = ['gt',time()-$agemax*31536000];
  59. }
  60. if($agemin > 18 && $agemax < 100){
  61. $where['user.birthday'] = ['between',[time()-$agemax*31536000,time()-$agemin*31536000]];
  62. }
  63. //距离
  64. $having_dis = '';
  65. $distancemin = input('distancemin',0);
  66. if($distancemin > 0){
  67. $having_dis = 'distance > '.$distancemin*1000;
  68. }
  69. $distancemax = input('distancemax',100);
  70. $distancemax = $distancemax > 100 ? 100 : $distancemax;//最大100
  71. if($distancemax > 0){
  72. $having_dis = 'distance < '.$distancemax*1000;
  73. }
  74. if($distancemin > 0 && $distancemax > 0){
  75. $having_dis = 'distance > '.$distancemin*1000 .' and distance < '.$distancemax*1000;
  76. }
  77. //当异地漫游的时候,距离条件作废
  78. if(!empty($cityname) && $cityname != $this->auth->cityname){
  79. $having_dis = '';
  80. }
  81. // dump($where);
  82. // dump($having_dis);
  83. $field = [
  84. 'user.id',
  85. 'user.username',
  86. 'user.nickname',
  87. 'user.avatar',
  88. 'user.photo_images',
  89. 'user.gender',
  90. 'user.birthday',
  91. 'user.cityname',
  92. 'user.longitude',
  93. 'user.latitude',
  94. 'user.attribute',
  95. 'user.is_active',
  96. 'wallet.vip_endtime',
  97. '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
  98. 'active.requesttime',
  99. ];
  100. $list = Db::name('user')->alias('user')->field($field)
  101. ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
  102. ->join('user_power power' ,'user.id = power.user_id','LEFT')
  103. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  104. ->where($where)
  105. ->where($where_black)
  106. ->having($having_dis)
  107. ->order('user.is_active desc,distance asc,user.id desc')
  108. ->autopage()
  109. ->select();
  110. $list = list_domain_image($list,['avatar','photo_images']);
  111. foreach($list as $key => &$val){
  112. $val['age'] = birthtime_to_age($val['birthday']);
  113. $val['photo_images'] = explode(',',$val['photo_images'])[0];
  114. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  115. $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
  116. if($this->auth->longitude == 0 || $val['longitude'] == 0){
  117. $val['distance'] = '未知';
  118. }
  119. $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
  120. //vip如果开了隐私保护,需要隐藏距离
  121. $yinsi = $this->user_power($val['id'],'yinsi');
  122. if($yinsi == 1){
  123. $val['distance'] = '';
  124. }
  125. }
  126. $this->success(1,$list);
  127. }
  128. //推荐
  129. //真人认证的,是推荐用户的,可能也要限制vip的
  130. public function tuijian(){
  131. /*$cityname = input('cityname','');
  132. if(empty($cityname)){
  133. $cityname = $this->auth->cityname;
  134. }*/
  135. //强制条件
  136. $where = [
  137. 'user.id' => ['neq',$this->auth->id],
  138. 'user.status' => 1,
  139. 'power.yinshen' => 0,
  140. ];
  141. //搜索条件 性别
  142. $gender = input('gender','all');
  143. if($gender != 'all'){
  144. $where['user.gender'] = $gender;
  145. }
  146. //搜索条件 属性
  147. $attribute = input('attribute','all');
  148. if($attribute != 'all' && $attribute != 'BOTH'){
  149. $where['user.attribute'] = $attribute;
  150. }
  151. //搜索条件 年龄
  152. $agemin = input('agemin',18);
  153. if($agemin > 18){
  154. $where['user.birthday'] = ['lt',time()-$agemin*31536000];
  155. }
  156. $agemax = input('agemax',100);
  157. if($agemax < 100){
  158. $where['user.birthday'] = ['gt',time()-$agemax*31536000];
  159. }
  160. if($agemin > 18 && $agemax < 100){
  161. $where['user.birthday'] = ['between',[time()-$agemax*31536000,time()-$agemin*31536000]];
  162. }
  163. //排除黑名单的
  164. $where_black = [];
  165. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  166. if(!empty($black_ids)){
  167. $where_black['user.id'] = ['NOTIN',$black_ids];
  168. }
  169. //其他条件 与 推荐条件是 或的关系
  170. $where_other = [
  171. 'user.avatar' => ['neq',''],
  172. 'user.idcard_status' => 1,
  173. ];
  174. if(config('site.index_tuijian_vip_limit') == 1){
  175. $where_other['wallet.vip_endtime'] = ['gt',time()];
  176. }
  177. //选择了推荐,就强制推荐
  178. $where_tuijian = [
  179. 'user.is_tuijian' => 1,
  180. ];
  181. //距离
  182. $having_dis = '';
  183. $distancemin = input('distancemin',0);
  184. if($distancemin > 0){
  185. $having_dis = 'distance > '.$distancemin*1000;
  186. }
  187. $distancemax = input('distancemax',0);
  188. if($distancemax > 0){
  189. $having_dis = 'distance < '.$distancemax*1000;
  190. }
  191. if($distancemin > 0 && $distancemax > 0){
  192. $having_dis = 'distance > '.$distancemin*1000 .' and distance < '.$distancemax*1000;
  193. }
  194. $field = [
  195. 'user.id',
  196. 'user.username',
  197. 'user.nickname',
  198. 'user.avatar',
  199. 'user.photo_images',
  200. 'user.gender',
  201. 'user.birthday',
  202. 'user.cityname',
  203. 'user.longitude',
  204. 'user.latitude',
  205. 'user.attribute',
  206. 'user.is_active',
  207. 'wallet.vip_endtime',
  208. '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
  209. 'active.requesttime',
  210. ];
  211. $list = Db::name('user')->alias('user')->field($field)
  212. ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
  213. ->join('user_power power' ,'user.id = power.user_id','LEFT')
  214. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  215. ->where($where)
  216. ->where($where_black)
  217. ->where(
  218. function($query)use($where_other,$where_tuijian){
  219. $query->where(
  220. function($query)use($where_other){
  221. $query->where($where_other);
  222. }
  223. );
  224. $query->whereOr($where_tuijian);
  225. })
  226. //->having($having_dis)推荐完全不受距离限制
  227. ->order('user.is_active desc,distance asc')
  228. ->autopage()
  229. ->select();
  230. $list = list_domain_image($list,['avatar','photo_images']);
  231. foreach($list as $key => &$val){
  232. $val['age'] = birthtime_to_age($val['birthday']);
  233. $val['photo_images'] = explode(',',$val['photo_images'])[0];
  234. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  235. $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
  236. if($this->auth->longitude == 0 || $val['longitude'] == 0){
  237. $val['distance'] = '未知';
  238. }
  239. $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
  240. //vip如果开了隐私保护,需要隐藏距离
  241. $yinsi = $this->user_power($val['id'],'yinsi');
  242. if($yinsi == 1){
  243. $val['distance'] = '';
  244. }
  245. }
  246. $this->success(1,$list);
  247. }
  248. //匹配配置
  249. public function pipei_config(){
  250. $result = [
  251. 'index_pipei_switch' => config('site.index_pipei_switch'), //匹配开关
  252. ];
  253. //首页匹配每天每人匹配次数
  254. $user_id = $this->auth->id;
  255. $is_vip = $this->is_vip($this->auth->id);
  256. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  257. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  258. $user_times = Cache::get($times_limit_redis) ?: 0;
  259. if($times_limit > -1){
  260. $remain_times = $times_limit - $user_times;
  261. if($remain_times < 0){
  262. $remain_times = 0;
  263. }
  264. }else{
  265. $remain_times = -1;
  266. }
  267. $result['remain_times'] = $remain_times;
  268. $this->success(1,$result);
  269. }
  270. //匹配
  271. //做防止重复处理,参照荔枝
  272. public function pipei(){
  273. //首页匹配功能开关
  274. $index_pipei_switch = config('site.index_pipei_switch');
  275. if($index_pipei_switch != 1){
  276. $this->error('匹配功能维护中,请稍后再试');
  277. }
  278. //缓存,防重复
  279. $user_id = $this->auth->id;
  280. $user_id_redis = 'pipei_repeat_'.$user_id;
  281. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  282. //首页匹配每天每人匹配次数
  283. $is_vip = $this->is_vip($this->auth->id);
  284. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  285. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  286. $user_times = Cache::get($times_limit_redis) ?: 0;
  287. if($times_limit > -1 && $user_times >= $times_limit){
  288. $this->error('今日已超匹配上限'.$times_limit.'次');
  289. }
  290. //where
  291. $where = [
  292. 'user.id' => ['neq',$this->auth->id],
  293. 'user.status' => 1,
  294. 'user.is_active' => 1,
  295. ];
  296. //性别
  297. $gender = input('gender','all');
  298. if($gender != 'all'){
  299. $where['user.gender'] = $gender;
  300. }
  301. //排除黑名单的
  302. $where_black = [];
  303. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  304. if(!empty($black_ids)){
  305. $where_black['user.id'] = ['NOTIN',$black_ids];
  306. }
  307. //匹配一个
  308. $result = $this->pipei_action($redis_ids,$where,$where_black);
  309. //匹配不到,移除防重复
  310. if(!$result) {
  311. Cache::rm($user_id_redis);
  312. $redis_ids = [];
  313. $result = $this->pipei_action($redis_ids,$where,$where_black);
  314. }
  315. // 追加一个防重复
  316. if($result){
  317. if($redis_ids) {
  318. $redis_ids[] = $result;
  319. } else {
  320. $redis_ids = [$result];
  321. }
  322. Cache::set($user_id_redis,json_encode($redis_ids));
  323. //设置次数
  324. $second = strtotime(date('Y-m-d'))+86400 - time();
  325. Cache::set($times_limit_redis,$user_times+1,$second);
  326. }else{
  327. Cache::rm($user_id_redis);
  328. }
  329. $this->success(1,$result);
  330. }
  331. private function pipei_action($redis_ids,$where,$where_black){
  332. $where_op = [];
  333. if(!empty($redis_ids)){
  334. $where_op['user.id'] = ['NOTIN',$redis_ids];
  335. }
  336. $result = Db::name('user')->alias('user')
  337. //->join('user_active active' ,'user.id = active.user_id','LEFT')
  338. ->where($where)
  339. ->where($where_op)
  340. ->where($where_black)
  341. ->orderRaw('rand()')
  342. ->value('user.id');
  343. return $result;
  344. }
  345. ///////////////////////////////////
  346. public function test(){
  347. //缓存,防重复
  348. $user_id = $this->auth->id;
  349. $user_id_redis = 'pipei_repeat_'.$user_id;
  350. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  351. dump($redis_ids);
  352. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  353. $user_times = Cache::get($times_limit_redis) ?: 0;
  354. dump($user_times);
  355. }
  356. public function testrm(){
  357. $user_id = $this->auth->id;
  358. $user_id_redis = 'pipei_repeat_'.$user_id;
  359. Cache::rm($user_id_redis);
  360. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  361. Cache::rm($times_limit_redis);
  362. }
  363. }