Index.php 12 KB

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