Eyemargin.php 7.7 KB

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