Index.php 12 KB

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