Index.php 8.1 KB

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