$code,"message"=>$message,"data"=>$data]); } } //成功返回值 if(!function_exists('success')){ /** * @param $message * @param int $code * @param null $data * @return \think\response\Json */ function success($message,$data=null,$code=0){ return json(['code'=>$code,'message'=>$message,'data'=>$data]); } } if (!function_exists('action_in_arr')) { /** * 检测一个访问的方式是否在白名单内 白名单不验token * @param array $arr * @return bool */ function action_in_arr(array $arr = []): bool { $arr = is_array($arr) ? $arr : explode(',', $arr); if (!$arr) { return false; } $arr = array_map('strtolower', $arr); if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) { return true; } return false; } } if(!function_exists("startTime")){ function startTime($time){ return date('Y-m-d 00:00:00',strtotime($time)); } } if(!function_exists('endTime')){ function endTime($time){ return date('Y-m-d 23:59:59',strtotime($time)); } } if(!function_exists('makeToken')){ function makeToken($strs,$salt='',$type='md5'){ if($salt=='') $salt=time(); $type = is_callable($type) ? $type : 'md5'; $token = call_user_func($type,$strs.$salt); return $token; } }; if(!function_exists('makeNo')){ function makeNo($strs,$salt=''){ $date=date('mdHis'); $year = date('Y')-2000; if($salt=="")$salt=\think\helper\Str::random(4,1); return $strs.$year.$date.$salt; } }; if(!function_exists('__')){ function __($msg){ return $msg; } }; if(!function_exists('UploadImg')){ function UploadImg($files){ $savename = []; $files = !is_array($files) ? [$files] : $files; try { //验证 validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]); foreach ($files as $file) { $url = Filesystem::disk('public')->putFile('topic/' . date('Ymd'), $file, function () use ($file) { return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . '_' . date('YmdHis')); }); $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName()); $temp = ['url' => $url, 'name' => $name]; $savename[] = $temp; } return $savename; } catch (\think\exception\ValidateException $e) { throw new \think\Exception($e->getMessage()); } } } if(!function_exists('exportExcel')){ /** * @param $data 数据集和 二维数组 * @param $header 表头 一维数组 * @param $filename 文件名 * @return \PHPExcel */ function exportExcel($data,$header, $filename){ $objPHPExcel = new \PHPExcel(); $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $objPHPExcel->setActiveSheetIndex(0); //制作表头第一行 header为一维数组 $i=0; foreach ($header as $key=>$value){ //列超过26个 列号为A-Z $colum = PHPExcel_Cell::stringFromColumnIndex($i); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $value); $i++; } //制作数据 data为二维数组 $j=2; $list = is_array($data)? $data : $data->toArray(); foreach ($list as $key=>$value){ $k=0; foreach ($value as $k1=>$v1){ //设置单元格类型为文本 否则数字会被识别成科学计数法 $objPHPExcel->getActiveSheet()->getCell(PHPExcel_Cell::stringFromColumnIndex($k).$j)->getStyle()->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT); $objPHPExcel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($k).$j, strval($v1)); //设置纯文本格式字符串 $k++; } $j++; } //文件导出 header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=$filename.xlsx"); header('Cache-Control: max-age=0'); //允许跨域请求 header('Access-Control-Allow-Origin:*'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; } } if(!function_exists('validate_invoice_info')){ function validate_invoice_info($data,&$return){ $return=["code"=>0,"校验通过"]; if (!isset($data['invoice_type'])||$data['invoice_type']==''){ $return['code']=1004; $return['message']="发票类型不能为空"; return false; } if (in_array($data['invoice_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){ if (!isset($data['invoice_total'])||$data['invoice_total']==''){ $return['code']=1004; $return['message']='发票税前金额不能为空'; return false; } }else{ if (!isset($data['invoice_code'])||$data['invoice_code']==''){ $return['code']=1004; $return['message']='发票代码不能为空'; return false; } if (!isset($data['invoice_subtotal'])||$data['invoice_subtotal']==''){ $return['code']=1004; $return['message']='发票税后金额不能为空'; return false; } } if (!isset($data['invoice_number'])||$data['invoice_number']==''){ $return['code']=1004; $return['message']='发票号码不能为空'; return false; } if(in_array($data['invoice_type'],['normal','roll','toll','electronic'])){ if(!isset($data['check_code'])||$data['check_code']==''){ $return['code']=1004; $return['message']='校验码不能为空'; return false; } } } }