Index.php 13 KB

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