Index.php 12 KB

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