Index.php 14 KB

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