Index.php 14 KB

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