Index.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. //做防止重复处理,参照荔枝
  197. public function pipei(){
  198. //检查剩余次数
  199. //缓存,防重复
  200. $user_id = $this->auth->id;
  201. $user_id_redis = 'u_'.$user_id;
  202. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  203. //where
  204. $where = [
  205. 'user.id' => ['neq',$this->auth->id],
  206. 'user.status' => 1,
  207. ];
  208. //性别
  209. $gender = input('gender','all');
  210. if($gender != 'all'){
  211. $where['user.gender'] = $gender;
  212. }
  213. //排除黑名单的
  214. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  215. if(!empty($black_ids)){
  216. $where['user.id'] = ['NOTIN',$black_ids];
  217. }
  218. //匹配一个
  219. $result = $this->pipei_action($redis_ids,$where);
  220. //匹配不到,移除防重复
  221. if(!$result) {
  222. Cache::rm($user_id_redis);
  223. $redis_ids = [];
  224. $result = $this->pipei_action($redis_ids,$where);
  225. }
  226. // 追加一个防重复
  227. if($result){
  228. if($redis_ids) {
  229. $redis_ids[] = $result;
  230. } else {
  231. $redis_ids = [$result];
  232. }
  233. Cache::set($user_id_redis,json_encode($redis_ids));
  234. }else{
  235. Cache::rm($user_id_redis);
  236. }
  237. $this->success(1,$result);
  238. }
  239. private function pipei_action($redis_ids,$where){
  240. $where_op = [];
  241. if(!empty($redis_ids)){
  242. $where_op['user.id'] = ['NOTIN',$redis_ids];
  243. }
  244. $result = Db::name('user')->alias('user')
  245. ->join('user_active active' ,'user.id = active.user_id','LEFT')
  246. ->where($where)
  247. ->where($where_op)
  248. ->orderRaw('rand()')
  249. ->value('user.id');
  250. return $result;
  251. }
  252. ///////////////////////////////////
  253. public function test(){
  254. //缓存,防重复
  255. $user_id = $this->auth->id;
  256. $user_id_redis = 'u_'.$user_id;
  257. $redis_ids = json_decode(Cache::get($user_id_redis),true);
  258. dump($redis_ids);
  259. }
  260. public function testrm(){
  261. $user_id = $this->auth->id;
  262. $user_id_redis = 'u_'.$user_id;
  263. Cache::rm($user_id_redis);
  264. }
  265. }