Index.php 14 KB

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