Index.php 14 KB

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