Index.php 12 KB

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