123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Model;
- /**
- * 模型
- */
- class Eyemargin extends Model
- {
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- /**
- * 获取携带标签的列表
- */
- public static function getDistanceList($user_lng,$user_lat,$field,$where,$user_id) {
- $results = \app\common\model\Eyemargin::alias("a")
- ->field($field)
- ->join("hx_user u","a.user_id = u.id")
- ->where($where)
- ->order("a.weight desc,a.createtime desc")
- ->select();
- if($results) {
- $userinfo = Db::name('user_info');
- $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
- wordwrap(config('public_key'), 64, PHP_EOL, true) .
- PHP_EOL."-----END PUBLIC KEY-----";
- foreach($results as $k => &$v) {
- // 计算距离
- $results[$k]['distance'] = (int)self::getDistance($v['lng'],$v['lat'],$user_lng,$user_lat);
- // $results[$k]['distance_txt'] = $results[$k]['distance'] > 15 ? $v['city_name'].'·'.$v['district_name']:"距离:".$results[$k]['distance'].'km';
- $results[$k]['distance_txt'] = self::getDistanceTxt($v['lng'],$v['lat'],$user_lng,$user_lat,$v['city_name'],$v['district_name']);
- $results[$k]['right_info'] = self::getIsView($v['user_id'],$user_id);
- $v['cover'] || $results[$k]['cover'] = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].'/assets/img/video_cover.jpeg';
- $results[$k]['file_name'] = $v['eye_type'] == 1 ? self::getFileUniq($v['video']) : "";
- if ($v['eye_type'] == 2) {
- $album = explode(',', $v['album']);
- foreach ($album as &$vv) {
- $vv = $vv . config('oss.img_watermark');
- }
- $results[$k]['album'] = join('|', $album);
- }
- //查询在线状态和活跃状态
- $user_info = $userinfo->where(['user_id' => $v['user_id']])->find();
- $asktime = explode(',', $user_info['asktime']);
- if ($user_info && $asktime && time() - $asktime[count($asktime) - 1] <= 3600) {
- $results[$k]['is_online'] = 1; //0离线 1在线
- } else {
- $results[$k]['is_online'] = 0; //0离线 1在线
- }
- if ($user_info && count($asktime) >= 20 && $asktime[count($asktime) - 1] - $asktime[0] <= 86400) {
- $results[$k]['is_active'] = 1; //活跃
- } else {
- $results[$k]['is_active'] = 0; //不活跃
- }
- if ($v['wechat']) {
- $wechat = "";
- openssl_public_encrypt($v['wechat'], $wechat, $public_key);
- $v['wechat'] = base64_encode($wechat);
- } else {
- $v['wechat'] = '';
- }
- $mobile = "";
- // openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
- openssl_public_encrypt($v['mobile'], $mobile, $public_key);
- $v['mobile'] = base64_encode($mobile);
- }
- }
- $distance = array_column($results,'distance');
- array_multisort($distance,SORT_ASC,$results);
- // 距离筛选
- return $results;
- }
- /**
- * 获取视频唯一识别码
- */
- public static function getFileUniq($file) {
- if(!$file) return false;
- $file_arr = explode('/',$file);
- end($file_arr);
- $file_name = $file_arr[key($file_arr)];
- $file_name_arr = explode(".",$file_name);
- if(!isset($file_name_arr[0])) return false;
- return $file_name_arr[0];
- }
- /**
- * 计算两点地理坐标之间的距离描述
- */
- public static function getDistanceTxt($longitude1, $latitude1, $longitude2, $latitude2,$city_name,$district_name){
- $distance = self::getDistance($longitude1, $latitude1, $longitude2, $latitude2, 1, 0);
- if($distance >= 15000) {
- $txt = -1;
- } elseif($distance >= 1000) {
- $distance = $distance / 1000;
- $txt = '距离:'.$distance."km";
- } else {
- $txt = '距离:'.$distance."米";
- }
- $txt = $txt == -1 ? $city_name.'·'.$district_name:$txt;
- return $txt;
- }
- /**
- * 获取是否眼缘/关注量/邀请
- */
- public static function getIsView($fate_user_id,$user_id) {
- $res = [];
- // 获取当前用户被关注数量
- $res['like_count'] = \app\common\model\UserLike::where(["user_id"=>$fate_user_id])->count();
- // 获取是否已有眼缘
- $is_fate = \app\common\model\UserFate::where(["user_id"=>$user_id,"fate_user_id"=>$fate_user_id])->find();
- $res['is_fate'] = $is_fate?1:0;
- // 获取是否关注
- $is_like = \app\common\model\UserLike::where(["fans_id"=>$user_id,"user_id"=>$fate_user_id])->find();
- $res['is_like'] = $is_like?1:0;
- // 获取是否显示分享
- $res['is_share'] = config("site.invite")?1:0;
- return $res;
- }
- /**
- * 计算两点地理坐标之间的距离
- * @param Decimal $longitude1 起点经度
- * @param Decimal $latitude1 起点纬度
- * @param Decimal $longitude2 终点经度
- * @param Decimal $latitude2 终点纬度
- * @param Int $unit 单位 1:米 2:公里
- * @param Int $decimal 精度 保留小数位数
- * @return Decimal
- */
- public static function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=1){
- if(!$longitude1 || !$latitude1 || !$longitude2 || !$latitude2) return 1000000;
- if($longitude1 <= 0 || $latitude1 <= 0 || $longitude2 <= 0 || $latitude2 <= 0) return 1000000;
- $EARTH_RADIUS = 6370.996; // 地球半径系数
- $PI = 3.1415926;
- $radLat1 = $latitude1 * $PI / 180.0;
- $radLat2 = $latitude2 * $PI / 180.0;
- $radLng1 = $longitude1 * $PI / 180.0;
- $radLng2 = $longitude2 * $PI /180.0;
- $a = $radLat1 - $radLat2;
- $b = $radLng1 - $radLng2;
- $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
- $distance = $distance * $EARTH_RADIUS * 1000;
- if($unit==2){
- $distance = $distance / 1000;
- }
- return round($distance, $decimal);
- }
- /**
- * 三数组合并
- */
- public static function arrayMerge($b,$c,$d) {
- $bcd = [];
- if($b && $c && $d){
- $eb = array_merge($b,$c);
- $bcd = array_merge($eb,$d);
- } elseif($b && $c) {
- $bcd = array_merge($b,$c);
- } elseif($b && $d) {
- $bcd = array_merge($b,$d);
- } elseif($c && $d) {
- $bcd = array_merge($c,$d);
- } else {
- if($b) {
- $bcd = $b;
- }
- if($c) {
- $bcd = $c;
- }
- if($d) {
- $bcd = $d;
- }
- }
- return self::second_array_unique_bykey($bcd,'id');
- }
- /**
- * 二维数组去重复
- * @param $arr
- * @param $key
- * @return mixed
- */
- public static function second_array_unique_bykey($arr, $key) {
- $tmp_arr = array();
- foreach ($arr as $k => $v) {
- if (in_array($v[$key], $tmp_arr)) { //搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
- unset($arr[$k]); //销毁一个变量 如果$tmp_arr中已存在相同的值就删除该值
- } else {
- $tmp_arr[$k] = $v[$key]; //将不同的值放在该数组中保存
- }
- }
- return array_values($arr);
- }
- }
|