Index.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. public function jingxuan(){
  19. $where = [
  20. 'user.id' => ['neq',$this->auth->id],
  21. 'user.status' => 1,
  22. 'user.gender' => ['neq',$this->auth->gender],
  23. ];
  24. //排除黑名单的
  25. $where_black = [];
  26. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  27. if(!empty($black_ids)){
  28. $where_black['user.id'] = ['NOTIN',$black_ids];
  29. }
  30. $field = [
  31. 'user.id',
  32. 'user.username',
  33. 'user.nickname',
  34. 'user.avatar',
  35. 'user.idcard_status',
  36. 'user.real_status',
  37. 'user.photo_images',
  38. 'user.gender',
  39. 'user.birthday',
  40. 'user.is_active',
  41. 'wallet.vip_endtime',
  42. 'active.requesttime',
  43. ];
  44. $list = Db::name('user')->alias('user')->field($field)
  45. ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
  46. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  47. ->where($where)
  48. ->where($where_black)
  49. ->order('user.is_active desc,user.id desc')
  50. ->autopage()
  51. ->select();
  52. $list = list_domain_image($list,['avatar','photo_images']);
  53. foreach($list as $key => &$val){
  54. $val['age'] = birthtime_to_age($val['birthday']);
  55. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  56. $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
  57. }
  58. $this->success(1,$list);
  59. }
  60. //新人
  61. public function xinren(){
  62. $where = [
  63. 'user.id' => ['neq',$this->auth->id],
  64. 'user.status' => 1,
  65. 'user.gender' => ['neq',$this->auth->gender],
  66. ];
  67. //排除黑名单的
  68. $where_black = [];
  69. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  70. if(!empty($black_ids)){
  71. $where_black['user.id'] = ['NOTIN',$black_ids];
  72. }
  73. $field = [
  74. 'user.id',
  75. 'user.username',
  76. 'user.nickname',
  77. 'user.avatar',
  78. 'user.idcard_status',
  79. 'user.real_status',
  80. 'user.photo_images',
  81. 'user.gender',
  82. 'user.birthday',
  83. 'user.is_active',
  84. 'wallet.vip_endtime',
  85. 'active.requesttime',
  86. ];
  87. $list = Db::name('user')->alias('user')->field($field)
  88. ->join('user_wallet wallet','user.id = wallet.user_id','LEFT')
  89. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  90. ->where($where)
  91. ->where($where_black)
  92. ->order('user.is_active desc,user.id desc')
  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['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  99. $val['active_info'] = $this->user_activeinfo($val['id'],$val['requesttime']);
  100. }
  101. $this->success(1,$list);
  102. }
  103. //匹配配置
  104. public function pipei_config(){
  105. $result = [
  106. 'index_pipei_switch' => config('site.index_pipei_switch'), //匹配开关
  107. ];
  108. //首页匹配每天每人匹配次数
  109. $user_id = $this->auth->id;
  110. $is_vip = $this->is_vip($this->auth->id);
  111. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  112. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  113. $user_times = Cache::get($times_limit_redis) ?: 0;
  114. if($times_limit > -1){
  115. $remain_times = $times_limit - $user_times;
  116. if($remain_times < 0){
  117. $remain_times = 0;
  118. }
  119. }else{
  120. $remain_times = -1;
  121. }
  122. $result['remain_times'] = $remain_times;
  123. $this->success(1,$result);
  124. }
  125. //匹配
  126. //做防止重复处理,参照荔枝
  127. public function pipei(){
  128. //首页匹配功能开关
  129. $index_pipei_switch = config('site.index_pipei_switch');
  130. if($index_pipei_switch != 1){
  131. $this->error('匹配功能维护中,请稍后再试');
  132. }
  133. //缓存,防重复
  134. $user_id = $this->auth->id;
  135. $user_id_redis = 'pipei_repeat_'.$user_id;
  136. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  137. //首页匹配每天每人匹配次数
  138. $is_vip = $this->is_vip($this->auth->id);
  139. $times_limit = $is_vip == 1 ? config('site.pipei_oneday_vipuser_times') : config('site.index_pipei_oneday_user_times');
  140. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  141. $user_times = Cache::get($times_limit_redis) ?: 0;
  142. if($times_limit > -1 && $user_times >= $times_limit){
  143. $this->error('今日已超匹配上限'.$times_limit.'次');
  144. }
  145. //where
  146. $where = [
  147. 'user.id' => ['neq',$this->auth->id],
  148. 'user.status' => 1,
  149. 'user.is_active' => 1,
  150. ];
  151. //性别
  152. $gender = input('gender','all');
  153. if($gender != 'all'){
  154. $where['user.gender'] = $gender;
  155. }
  156. //排除黑名单的
  157. $where_black = [];
  158. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  159. if(!empty($black_ids)){
  160. $where_black['user.id'] = ['NOTIN',$black_ids];
  161. }
  162. //匹配一个
  163. $result = $this->pipei_action($redis_ids,$where,$where_black);
  164. //匹配不到,移除防重复
  165. if(!$result) {
  166. Cache::rm($user_id_redis);
  167. $redis_ids = [];
  168. $result = $this->pipei_action($redis_ids,$where,$where_black);
  169. }
  170. // 追加一个防重复
  171. if($result){
  172. if($redis_ids) {
  173. $redis_ids[] = $result;
  174. } else {
  175. $redis_ids = [$result];
  176. }
  177. Cache::set($user_id_redis,json_encode($redis_ids));
  178. //设置次数
  179. $second = strtotime(date('Y-m-d'))+86400 - time();
  180. Cache::set($times_limit_redis,$user_times+1,$second);
  181. }else{
  182. Cache::rm($user_id_redis);
  183. }
  184. $this->success(1,$result);
  185. }
  186. private function pipei_action($redis_ids,$where,$where_black){
  187. $where_op = [];
  188. if(!empty($redis_ids)){
  189. $where_op['user.id'] = ['NOTIN',$redis_ids];
  190. }
  191. $result = Db::name('user')->alias('user')
  192. //->join('user_active active' ,'user.id = active.user_id','LEFT')
  193. ->where($where)
  194. ->where($where_op)
  195. ->where($where_black)
  196. ->orderRaw('rand()')
  197. ->value('user.id');
  198. return $result;
  199. }
  200. ///////////////////////////////////
  201. public function test(){
  202. //缓存,防重复
  203. $user_id = $this->auth->id;
  204. $user_id_redis = 'pipei_repeat_'.$user_id;
  205. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  206. dump($redis_ids);
  207. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  208. $user_times = Cache::get($times_limit_redis) ?: 0;
  209. dump($user_times);
  210. }
  211. public function testrm(){
  212. $user_id = $this->auth->id;
  213. $user_id_redis = 'pipei_repeat_'.$user_id;
  214. Cache::rm($user_id_redis);
  215. $times_limit_redis = 'pipei_times_limit_'.$user_id;
  216. Cache::rm($times_limit_redis);
  217. }
  218. }