Index.php 12 KB

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