Index.php 13 KB

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