Index.php 11 KB

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