Index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. $distancemin = input('distancemin',0);
  156. if($distancemin > 0){
  157. $where['distance'] = ['gt',$distancemin];
  158. }
  159. $distancemax = input('distancemax',0);
  160. if($distancemax > 0){
  161. $where['distance'] = ['lt',$distancemax];
  162. }
  163. if($distancemin > 0 && $distancemax > 0){
  164. $where['distance'] = ['between',[$distancemin,$distancemax]];
  165. }
  166. $field = [
  167. 'user.id',
  168. 'user.username',
  169. 'user.nickname',
  170. 'user.avatar',
  171. 'user.photo_images',
  172. 'user.gender',
  173. 'user.birthday',
  174. 'user.cityname',
  175. 'user.longitude',
  176. 'user.latitude',
  177. 'user.attribute',
  178. 'wallet.vip_endtime',
  179. '(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(user.longitude,user.latitude))*111195) as distance',
  180. 'active.requesttime',
  181. ];
  182. $list = Db::name('user')->alias('user')->field($field)
  183. ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
  184. ->join('user_power power' ,'user.id = power.user_id','LEFT')
  185. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  186. ->where($where)
  187. ->where($where_black)
  188. ->order('distance asc')
  189. ->autopage()
  190. ->select();
  191. $list = list_domain_image($list,['avatar','photo_images']);
  192. foreach($list as $key => &$val){
  193. $val['age'] = birthtime_to_age($val['birthday']);
  194. $val['photo_images'] = explode(',',$val['photo_images'])[0];
  195. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  196. $val['distance'] = bcdiv(intval($val['distance']),1000,2).'km';
  197. $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
  198. }
  199. $this->success(1,$list);
  200. }
  201. //匹配配置
  202. public function pipei_config(){
  203. $result = [
  204. 'index_pipei_switch' => config('site.index_pipei_switch'), //匹配开关
  205. ];
  206. //首页匹配每天每人匹配次数
  207. $user_id = $this->auth->id;
  208. $is_vip = $this->is_vip($this->auth->id);
  209. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  210. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  211. $user_times = Cache::get($times_limit_redis) ?: 0;
  212. if($times_limit > -1){
  213. $remain_times = $times_limit - $user_times;
  214. if($remain_times < 0){
  215. $remain_times = 0;
  216. }
  217. }else{
  218. $remain_times = -1;
  219. }
  220. $result['remain_times'] = $remain_times;
  221. $this->success(1,$result);
  222. }
  223. //匹配
  224. //做防止重复处理,参照荔枝
  225. public function pipei(){
  226. //首页匹配功能开关
  227. $index_pipei_switch = config('site.index_pipei_switch');
  228. if($index_pipei_switch != 1){
  229. $this->error('匹配功能维护中,请稍后再试');
  230. }
  231. //缓存,防重复
  232. $user_id = $this->auth->id;
  233. $user_id_redis = 'pipei_repeat_'.$user_id;
  234. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  235. //首页匹配每天每人匹配次数
  236. $is_vip = $this->is_vip($this->auth->id);
  237. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  238. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  239. $user_times = Cache::get($times_limit_redis) ?: 0;
  240. if($times_limit > -1 && $user_times >= $times_limit){
  241. $this->error('今日已超匹配上限'.$times_limit.'次');
  242. }
  243. //where
  244. $where = [
  245. 'user.id' => ['neq',$this->auth->id],
  246. 'user.status' => 1,
  247. ];
  248. //性别
  249. $gender = input('gender','all');
  250. if($gender != 'all'){
  251. $where['user.gender'] = $gender;
  252. }
  253. //排除黑名单的
  254. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  255. if(!empty($black_ids)){
  256. $where['user.id'] = ['NOTIN',$black_ids];
  257. }
  258. //匹配一个
  259. $result = $this->pipei_action($redis_ids,$where);
  260. //匹配不到,移除防重复
  261. if(!$result) {
  262. Cache::rm($user_id_redis);
  263. $redis_ids = [];
  264. $result = $this->pipei_action($redis_ids,$where);
  265. }
  266. // 追加一个防重复
  267. if($result){
  268. if($redis_ids) {
  269. $redis_ids[] = $result;
  270. } else {
  271. $redis_ids = [$result];
  272. }
  273. Cache::set($user_id_redis,json_encode($redis_ids));
  274. //设置次数
  275. $second = strtotime(date('Y-m-d'))+86400 - time();
  276. Cache::set($times_limit_redis,$user_times+1,$second);
  277. }else{
  278. Cache::rm($user_id_redis);
  279. }
  280. $this->success(1,$result);
  281. }
  282. private function pipei_action($redis_ids,$where){
  283. $where_op = [];
  284. if(!empty($redis_ids)){
  285. $where_op['user.id'] = ['NOTIN',$redis_ids];
  286. }
  287. $result = Db::name('user')->alias('user')
  288. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  289. ->where($where)
  290. ->where($where_op)
  291. ->orderRaw('rand()')
  292. ->value('user.id');
  293. return $result;
  294. }
  295. ///////////////////////////////////
  296. public function test(){
  297. //缓存,防重复
  298. $user_id = $this->auth->id;
  299. $user_id_redis = 'pipei_repeat_'.$user_id;
  300. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  301. dump($redis_ids);
  302. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  303. $user_times = Cache::get($times_limit_redis) ?: 0;
  304. dump($user_times);
  305. }
  306. public function testrm(){
  307. $user_id = $this->auth->id;
  308. $user_id_redis = 'pipei_repeat_'.$user_id;
  309. Cache::rm($user_id_redis);
  310. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  311. Cache::rm($times_limit_redis);
  312. }
  313. }