Eyemargin.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 模型
  7. */
  8. class Eyemargin extends Model
  9. {
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. /**
  13. * 获取携带标签的列表
  14. */
  15. public static function getDistanceList($user_lng,$user_lat,$field,$where,$user_id) {
  16. $results = \app\common\model\Eyemargin::alias("a")
  17. ->field($field)
  18. ->join("hx_user u","a.user_id = u.id")
  19. ->where($where)
  20. ->order("a.weight desc,a.createtime desc")
  21. ->select();
  22. if($results) {
  23. $userinfo = Db::name('user_info');
  24. foreach($results as $k => $v) {
  25. // 计算距离
  26. $results[$k]['distance'] = (int)self::getDistance($v['lng'],$v['lat'],$user_lng,$user_lat);
  27. // $results[$k]['distance_txt'] = $results[$k]['distance'] > 15 ? $v['city_name'].'·'.$v['district_name']:"距离:".$results[$k]['distance'].'km';
  28. $results[$k]['distance_txt'] = self::getDistanceTxt($v['lng'],$v['lat'],$user_lng,$user_lat,$v['city_name'],$v['district_name']);
  29. $results[$k]['right_info'] = self::getIsView($v['user_id'],$user_id);
  30. $v['cover'] || $results[$k]['cover'] = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/assets/img/video_cover.jpeg';
  31. $results[$k]['file_name'] = $v['eye_type'] == 1 ? self::getFileUniq($v['video']) : "";
  32. if ($v['eye_type'] == 2) {
  33. $album = explode(',', $v['album']);
  34. foreach ($album as &$vv) {
  35. $vv = $vv . config('oss.img_watermark');
  36. }
  37. $results[$k]['album'] = join('|', $album);
  38. }
  39. //查询在线状态和活跃状态
  40. $user_info = $userinfo->where(['user_id' => $v['user_id']])->find();
  41. $asktime = explode(',', $user_info['asktime']);
  42. if ($user_info && $asktime && time() - $asktime[count($asktime) - 1] <= 3600) {
  43. $results[$k]['is_online'] = 1; //0离线 1在线
  44. } else {
  45. $results[$k]['is_online'] = 0; //0离线 1在线
  46. }
  47. if ($user_info && count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
  48. $results[$k]['is_active'] = 1; //活跃
  49. } else {
  50. $results[$k]['is_active'] = 0; //不活跃
  51. }
  52. }
  53. }
  54. $distance = array_column($results,'distance');
  55. array_multisort($distance,SORT_ASC,$results);
  56. // 距离筛选
  57. return $results;
  58. }
  59. /**
  60. * 获取视频唯一识别码
  61. */
  62. public static function getFileUniq($file) {
  63. if(!$file) return false;
  64. $file_arr = explode('/',$file);
  65. end($file_arr);
  66. $file_name = $file_arr[key($file_arr)];
  67. $file_name_arr = explode(".",$file_name);
  68. if(!isset($file_name_arr[0])) return false;
  69. return $file_name_arr[0];
  70. }
  71. /**
  72. * 计算两点地理坐标之间的距离描述
  73. */
  74. public static function getDistanceTxt($longitude1, $latitude1, $longitude2, $latitude2,$city_name,$district_name){
  75. $distance = self::getDistance($longitude1, $latitude1, $longitude2, $latitude2, 1, 0);
  76. if($distance >= 15000) {
  77. $txt = -1;
  78. } elseif($distance >= 1000) {
  79. $distance = $distance / 1000;
  80. $txt = '距离:'.$distance."km";
  81. } else {
  82. $txt = '距离:'.$distance."米";
  83. }
  84. $txt = $txt == -1 ? $city_name.'·'.$district_name:$txt;
  85. return $txt;
  86. }
  87. /**
  88. * 获取是否眼缘/关注量/邀请
  89. */
  90. public static function getIsView($fate_user_id,$user_id) {
  91. $res = [];
  92. // 获取当前用户被关注数量
  93. $res['like_count'] = \app\common\model\UserLike::where(["user_id"=>$fate_user_id])->count();
  94. // 获取是否已有眼缘
  95. $is_fate = \app\common\model\UserFate::where(["user_id"=>$user_id,"fate_user_id"=>$fate_user_id])->find();
  96. $res['is_fate'] = $is_fate?1:0;
  97. // 获取是否关注
  98. $is_like = \app\common\model\UserLike::where(["fans_id"=>$user_id,"user_id"=>$fate_user_id])->find();
  99. $res['is_like'] = $is_like?1:0;
  100. // 获取是否显示分享
  101. $res['is_share'] = config("site.invite")?1:0;
  102. return $res;
  103. }
  104. /**
  105. * 计算两点地理坐标之间的距离
  106. * @param Decimal $longitude1 起点经度
  107. * @param Decimal $latitude1 起点纬度
  108. * @param Decimal $longitude2 终点经度
  109. * @param Decimal $latitude2 终点纬度
  110. * @param Int $unit 单位 1:米 2:公里
  111. * @param Int $decimal 精度 保留小数位数
  112. * @return Decimal
  113. */
  114. public static function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=1){
  115. if(!$longitude1 || !$latitude1 || !$longitude2 || !$latitude2) return 1000000;
  116. if($longitude1 <= 0 || $latitude1 <= 0 || $longitude2 <= 0 || $latitude2 <= 0) return 1000000;
  117. $EARTH_RADIUS = 6370.996; // 地球半径系数
  118. $PI = 3.1415926;
  119. $radLat1 = $latitude1 * $PI / 180.0;
  120. $radLat2 = $latitude2 * $PI / 180.0;
  121. $radLng1 = $longitude1 * $PI / 180.0;
  122. $radLng2 = $longitude2 * $PI /180.0;
  123. $a = $radLat1 - $radLat2;
  124. $b = $radLng1 - $radLng2;
  125. $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
  126. $distance = $distance * $EARTH_RADIUS * 1000;
  127. if($unit==2){
  128. $distance = $distance / 1000;
  129. }
  130. return round($distance, $decimal);
  131. }
  132. /**
  133. * 三数组合并
  134. */
  135. public static function arrayMerge($b,$c,$d) {
  136. $bcd = [];
  137. if($b && $c && $d){
  138. $eb = array_merge($b,$c);
  139. $bcd = array_merge($eb,$d);
  140. } elseif($b && $c) {
  141. $bcd = array_merge($b,$c);
  142. } elseif($b && $d) {
  143. $bcd = array_merge($b,$d);
  144. } elseif($c && $d) {
  145. $bcd = array_merge($c,$d);
  146. } else {
  147. if($b) {
  148. $bcd = $b;
  149. }
  150. if($c) {
  151. $bcd = $c;
  152. }
  153. if($d) {
  154. $bcd = $d;
  155. }
  156. }
  157. return self::second_array_unique_bykey($bcd,'id');
  158. }
  159. /**
  160. * 二维数组去重复
  161. * @param $arr
  162. * @param $key
  163. * @return mixed
  164. */
  165. public static function second_array_unique_bykey($arr, $key) {
  166. $tmp_arr = array();
  167. foreach ($arr as $k => $v) {
  168. if (in_array($v[$key], $tmp_arr)) { //搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
  169. unset($arr[$k]); //销毁一个变量 如果$tmp_arr中已存在相同的值就删除该值
  170. } else {
  171. $tmp_arr[$k] = $v[$key]; //将不同的值放在该数组中保存
  172. }
  173. }
  174. return array_values($arr);
  175. }
  176. }