// +---------------------------------------------------------------------- // 应用公共文件 use think\facade\Db; // 高德地图Key define('AMAP_KEY', '937f431e40453c79c1c18af4a69c6b79'); function page_result1($code = 0, $msg = '', $data = []) { exit(json_encode([ 'code' => $code, 'msg' => $msg, 'data' => $data, ])); } /** * a.合成图片信息 复制一张图片的矩形区域到另外一张图片的矩形区域 * @param [type] $bg_image [目标图] * @param [type] $sub_image [被添加图] * @param [type] $add_x [目标图x坐标位置] * @param [type] $add_y [目标图y坐标位置] * @param [type] $add_w [目标图宽度区域] * @param [type] $add_h [目标图高度区域] * @param [type] $out_image [输出图路径] * @return [type] [description] */ function image_copy_image($bg_image, $sub_image, $add_x, $add_y, $add_w, $add_h, $out_image) { if ($sub_image) { $bg_image_c = imagecreatefromstring(file_get_contents($bg_image)); $sub_image_c = imagecreatefromstring(file_get_contents($sub_image)); imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c)); //保存到out_image imagejpeg($bg_image_c, $out_image, 80); imagedestroy($sub_image_c); imagedestroy($bg_image_c); return true; } } function image_copy_text($dst_path, $text, $font, $size, $picwith, $x, $y, $red, $grn, $blu) { $dst = imagecreatefromstring(file_get_contents($dst_path)); $arr = imagettfbbox($size, 0, $font, $text); $text_width = $arr[2] - $arr[0]; $x = $picwith == 0 ? $x : intval(($picwith - $text_width) / 2); //打上文字 $black = imagecolorallocate($dst, $red, $grn, $blu);//字体颜色0x00, 0x00, 0x00 imagefttext($dst, $size, 0, $x, $y, $black, $font, $text); //输出图片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($dst, $dst_path); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($dst, $dst_path); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst, $dst_path); break; default: break; } imagedestroy($dst); } function subtext($text, $length) { if (mb_strlen($text, 'utf8') > $length) { return mb_substr($text, 0, $length, 'utf8') . '...'; } else { return $text; } } /** * 公共数据导出实现功能 * @param $expTitle 导出文件名 * @param $expCellName 导出文件列名称 * @param $expTableData 导出数据 */ function export_excel($expTitle, $expCellName, $expTableData) { $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称 $fileName = $expTitle . date('_Ymd');//or $xlsTitle 文件名称可根据自己情况设定 $cellNum = count($expCellName); $dataNum = count($expTableData); $objPHPExcel = new PHPExcel();//方法一 $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ']; //设置头部导出时间备注 $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle . ' 导出时间:' . date('Y-m-d H:i:s')); //设置列名称 for ($i = 0; $i < $cellNum; $i++) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]); } //赋值 for ($i = 0; $i < $dataNum; $i++) { for ($j = 0; $j < $cellNum; $j++) { $keyarr = explode(".", $expCellName[$j][0]); $value = $expTableData[$i]; foreach ($keyarr as $k => $v) { $value = $value[$v]; } if (!empty($expCellName[$j][2])) { $value = $expCellName[$j][2][$value]; } $objPHPExcel->getActiveSheet(0)->setCellValueExplicit( $cellName[$j] . ($i + 3), $value, PHPExcel_Cell_DataType::TYPE_STRING ); } } ob_end_clean();//这一步非常关键,用来清除缓冲区防止导出的excel乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//"xls"参考下一条备注 $objWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel2007' );//"Excel2007"生成2007版本的xlsx,"Excel5"生成2003版本的xls $objWriter->save('php://output'); } /** * excel表格读取 * @param string $filename 文件路劲 */ function read_excel($filename) { //设置excel格式 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if ($ext == 'xlsx') { $reader = PHPExcel_IOFactory::createReader('Excel2007'); } else { $reader = PHPExcel_IOFactory::createReader('Excel5'); } //载入excel文件 $excel = $reader->load($filename); //读取第一张表 $sheet = $excel->getSheet(0); //获取总行数 $row_num = $sheet->getHighestRow(); //获取总列数 $col_num = $sheet->getHighestColumn(); $data = []; //数组形式获取表格数据 for ($col = 'A'; $col <= $col_num; $col++) { //从第二行开始,去除表头(若无表头则从第一行开始) for ($row = 2; $row <= $row_num; $row++) { $data[$row - 2][] = $sheet->getCell($col . $row)->getValue(); } } return $data; } //二维数组去重 function assoc_unique($arr, $key) { $tmp_arr = []; foreach ($arr as $k => $v) { if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true unset($arr[$k]); } else { $tmp_arr[] = $v[$key]; } } sort($arr); //sort函数对数组进行排序 return $arr; } /** * 阿里云身份证信息识别 */ function aliyun_ocr_idcard($file) { $url = "https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"; $appcode = config('wxconfig.aliAppCode'); // $file = "你的文件路径"; //如果输入带有inputs, 设置为True,否则设为False $is_old_format = false; //如果没有configure字段,config设为空 $config = [ "side" => "face", ]; //$config = array() if ($fp = fopen($file, "rb", 0)) { $binary = fread($fp, filesize($file)); // 文件读取 fclose($fp); $base64 = base64_encode($binary); // 转码 } $headers = []; array_push($headers, "Authorization:APPCODE " . $appcode); //根据API的要求,定义相对应的Content-Type array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8"); $querys = ""; if ($is_old_format == true) { $request = []; $request["image"] = [ "dataType" => 50, "dataValue" => "$base64", ]; if (count($config) > 0) { $request["configure"] = [ "dataType" => 50, "dataValue" => json_encode($config), ]; } $body = json_encode(["inputs" => [$request]]); } else { $request = [ "image" => "$base64", ]; if (count($config) > 0) { $request["configure"] = json_encode($config); } $body = json_encode($request); } $method = "POST"; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if (1 == strpos("$" . $url, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $result = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $rheader = substr($result, 0, $header_size); $rbody = substr($result, $header_size); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($httpCode == 200) { if ($is_old_format) { $output = json_decode($rbody, true); $result_str = $output["outputs"][0]["outputValue"]["dataValue"]; } else { $result_str = $rbody; } $result_arr = json_decode($result_str, true); if ($result_arr['success'] == false) { return false; } else { return [ "name" => $result_arr['name'], "nationality" => $result_arr['nationality'], "num" => $result_arr['num'], "sex" => $result_arr['sex'], "birth" => date('Y-m-d', strtotime($result_arr['birth'])), "nationality" => $result_arr['nationality'], "address" => $result_arr['address'], ]; } return $result_str; } else { return false; } } // 两个日期间数组 function periodDate($start_time, $end_time) { $start_time = strtotime($start_time); $end_time = strtotime($end_time); $i = 0; while ($start_time <= $end_time) { $arr[date('Y-m-d', $start_time)] = 0; $start_time = strtotime('+1 day', $start_time); $i++; } return $arr; } // 数组键值分开 function arrKeyVal($arr) { $keyArr = []; $valArr = []; if (!empty($arr)) { foreach ($arr as $k => $v) { $keyArr[] = $k; $valArr[] = $v; } } return ['keyarr' => $keyArr, 'valarr' => $valArr]; } //获取ip // function get_client_ip() { // $ip = $_SERVER['REMOTE_ADDR']; // if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) { // $ip = $_SERVER['HTTP_CLIENT_IP']; // } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) { // foreach ($matches[0] AS $xip) { // if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) { // $ip = $xip; // break; // } // } // } // return $ip; // } //能否发布 function is_released($workerid) { $comjobs_count = Db::name('comjobs') ->where('workerid', $workerid) ->count(); $demand_count = Db::name("demand") ->where('workerid', $workerid) ->count(); $supply_data = Db::name("supply") ->where('workerid', $workerid) ->count(); $count = $comjobs_count + $demand_count + $supply_data; if ($count >= 3) { $rtn['code'] = 1001; $rtn['msg'] = "您的审核还未通过,最多只能发3条信息"; return $rtn; } else { $rtn['code'] = 0; // $rtn['code'] = 1001; } return $rtn; } /** * 根据经纬度和半径计算出范围 * @param string $lat 纬度 * @param String $lng 经度 * @param float $radius 半径 * @return Array 范围数组 */ function calcScope($lat, $lng, $radius) { $degree = (24901 * 1609) / 360.0; $dpmLat = 1 / $degree; $radiusLat = $dpmLat * $radius; $minLat = $lat - $radiusLat; // 最小纬度 $maxLat = $lat + $radiusLat; // 最大纬度 $mpdLng = $degree * cos($lat * (PI / 180)); $dpmLng = 1 / $mpdLng; $radiusLng = $dpmLng * $radius; $minLng = $lng - $radiusLng; // 最小经度 $maxLng = $lng + $radiusLng; // 最大经度 /** 返回范围数组 */ $scope = [ 'minLat' => $minLat, 'maxLat' => $maxLat, 'minLng' => $minLng, 'maxLng' => $maxLng, ]; return $scope; } /** * 根据经纬度和半径查询在此范围内的所有的对象 * @param String $lat 纬度 * @param String $lng 经度 * @param float $radius 半径 * @return Array 计算出来的结果 */ //public function searchByLatAndLng($lat, $lng, $radius) { // $scope = $this->calcScope($lat, $lng, $radius); // 调用范围计算函数,获取最大最小经纬度 // /** 查询经纬度在 $radius 范围内的对象的详细地址 */ // $sql = 'SELECT `字段` FROM `表名` WHERE `Latitude` < '.$scope['maxLat'].' and `Latitude` > '.$scope['minLat'].' and `Longitude` < '.$scope['maxLng'].' and `Longitude` > '.$scope['minLng']; // $stmt = self::$db->query($sql); // $res = $stmt->fetchAll(PDO::FETCH_ASSOC); // 获取查询结果并返回 // return $res; //} /** * 获取两个经纬度之间的距离 * @param string $lat1 纬一 * @param String $lng1 经一 * @param String $lat2 纬二 * @param String $lng2 经二 * @return float 返回两点之间的距离 */ function calcDistance($lat1, $lng1, $lat2, $lng2) { /** 转换数据类型为 double */ $lat1 = doubleval($lat1); $lng1 = doubleval($lng1); $lat2 = doubleval($lat2); $lng2 = doubleval($lng2); /** 以下算法是 Google 出来的,与大多数经纬度计算工具结果一致 */ $theta = $lng1 - $lng2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; return ($miles * 1.609344); } //$lon1 用户当前经度 $lat1用户当前纬度 $lon2数据库经度的字段名 $lat2数据库纬度的字段名 function distance_sql($lon1 = '116.434164', $lat1 = '39.909843', $lon2 = 'longitude', $lat2 = 'latitude') { $sql = "round(6378.138*2*asin(sqrt(pow(sin( ({$lat1}*pi()/180-{$lat2}*pi()/180)/2),2)+cos({$lat1}*pi()/180)*cos({$lat2}*pi()/180)* pow(sin( ({$lon1}*pi()/180-{$lon2}*pi()/180)/2),2)))*1000) "; return $sql; } /** * CURL请求 * @param $url 请求url地址 * @param $method 请求方法 get post * @param null $postfields post数据数组 * @param array $headers 请求header信息 * @return mixed */ function http_request($url, $method = "GET", $postfields = null, $headers = []) { $method = strtoupper($method); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */ curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */ curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); switch ($method) { case "POST": curl_setopt($ci, CURLOPT_POST, true); if (!empty($postfields)) { $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields; curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr); } break; default: curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */ break; } $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE; curl_setopt($ci, CURLOPT_URL, $url); if ($ssl) { curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 } //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/ curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ci, CURLOPT_MAXREDIRS, 2); /* 指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的 */ curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLINFO_HEADER_OUT, true); /* curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */ $response = curl_exec($ci); curl_close($ci); return $response; } /** * 数据导入 * @param string $file excel文件 * @param string $crop * @param string $sheet * @return array 返回解析数据 * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ function importExecl($file = '', $cell = [], $crop = 0, $sheet = 0) { $file = iconv("utf-8", "gb2312", $file); //转码 if (empty($file) OR !file_exists($file)) { die('file not exists!'); } $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象 if (!$objRead->canRead($file)) { $objRead = new PHPExcel_Reader_Excel5(); if (!$objRead->canRead($file)) { die('No Excel!'); } } $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ']; $obj = $objRead->load($file); //建立excel对象 $currSheet = $obj->getSheet($sheet); //获取指定的sheet表 $columnH = $currSheet->getHighestColumn(); //取得最大的列号 $columnCnt = array_search($columnH, $cellName); $rowCnt = $currSheet->getHighestRow(); //获取总行数 $data = []; for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容 if ($_row > $crop) { for ($_column = 0; $_column <= $columnCnt; $_column++) { $cellId = $cellName[$_column] . $_row; $cellValue = $currSheet->getCell($cellId)->getValue(); //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值 if ($cellValue instanceof PHPExcel_RichText) { //富文本转换字符串 $cellValue = $cellValue->__toString(); } if (!empty($cell[$_column])) { $data[$_row][$cell[$_column]] = $cellValue; } else { $data[$_row][] = $cellValue; } } } } return array_values($data); }