Index.php 14 KB

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