Index.php 12 KB

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