common.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. //  />  フ
  3. //     |  _  _|
  4. //     /` ミ_꒳ノ
  5. //     /      |
  6. //    /  ヽ   ノ
  7. //    │  | | |
  8. // / ̄|   | | |
  9. // | ( ̄ヽ__ヽ_)__)
  10. // \二つ
  11. // 应用公共文件
  12. //失败返回值
  13. use think\facade\Filesystem;
  14. if(!function_exists("error")){
  15. /**
  16. * @param $message
  17. * @param int $code
  18. * @param null $data
  19. * @return \think\response\Json
  20. */
  21. function error($message,$code=1004,$data=null){
  22. return json(["code"=>$code,"message"=>$message,"data"=>$data]);
  23. }
  24. }
  25. //成功返回值
  26. if(!function_exists('success')){
  27. /**
  28. * @param $message
  29. * @param int $code
  30. * @param null $data
  31. * @return \think\response\Json
  32. */
  33. function success($message,$data=null,$code=0){
  34. return json(['code'=>$code,'message'=>$message,'data'=>$data]);
  35. }
  36. }
  37. if (!function_exists('action_in_arr')) {
  38. /**
  39. * 检测一个访问的方式是否在白名单内 白名单不验token
  40. * @param array $arr
  41. * @return bool
  42. */
  43. function action_in_arr(array $arr = []): bool
  44. {
  45. $arr = is_array($arr) ? $arr : explode(',', $arr);
  46. if (!$arr) {
  47. return false;
  48. }
  49. $arr = array_map('strtolower', $arr);
  50. if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. }
  56. if(!function_exists("startTime")){
  57. function startTime($time){
  58. return date('Y-m-d 00:00:00',strtotime($time));
  59. }
  60. }
  61. if(!function_exists('endTime')){
  62. function endTime($time){
  63. return date('Y-m-d 23:59:59',strtotime($time));
  64. }
  65. }
  66. if(!function_exists('makeToken')){
  67. function makeToken($strs,$salt='',$type='md5'){
  68. if($salt=='') $salt=time();
  69. $type = is_callable($type) ? $type : 'md5';
  70. $token = call_user_func($type,$strs.$salt);
  71. return $token;
  72. }
  73. };
  74. if(!function_exists('makeNo')){
  75. function makeNo($strs,$salt=''){
  76. $date=date('mdHis');
  77. $year = date('Y')-2000;
  78. if($salt=="")$salt=\think\helper\Str::random(4,1);
  79. return $strs.$year.$date.$salt;
  80. }
  81. };
  82. if(!function_exists('__')){
  83. function __($msg){
  84. return $msg;
  85. }
  86. };
  87. if(!function_exists('UploadImg')){
  88. function UploadImg($files){
  89. $savename = [];
  90. $files = !is_array($files) ? [$files] : $files;
  91. try {
  92. //验证
  93. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  94. foreach ($files as $file) {
  95. $url = Filesystem::disk('public')->putFile('topic/' . date('Ymd'), $file, function () use ($file) {
  96. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . '_' . date('YmdHis'));
  97. });
  98. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  99. $temp = ['url' => $url, 'name' => $name];
  100. $savename[] = $temp;
  101. }
  102. return $savename;
  103. } catch (\think\exception\ValidateException $e) {
  104. throw new \think\Exception($e->getMessage());
  105. }
  106. }
  107. }
  108. if(!function_exists('exportExcel')){
  109. /**
  110. * @param $data 数据集和 二维数组
  111. * @param $header 表头 一维数组
  112. * @param $filename 文件名
  113. * @return \PHPExcel
  114. */
  115. function exportExcel($data,$header, $filename){
  116. $objPHPExcel = new \PHPExcel();
  117. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  118. ->setLastModifiedBy("Maarten Balliauw")
  119. ->setTitle("Office 2007 XLSX Test Document")
  120. ->setSubject("Office 2007 XLSX Test Document")
  121. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  122. ->setKeywords("office 2007 openxml php")
  123. ->setCategory("Test result file");
  124. $objPHPExcel->setActiveSheetIndex(0);
  125. //制作表头第一行 header为一维数组
  126. $i=0;
  127. foreach ($header as $key=>$value){
  128. //列超过26个 列号为A-Z
  129. $colum = PHPExcel_Cell::stringFromColumnIndex($i);
  130. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $value);
  131. $i++;
  132. }
  133. //制作数据 data为二维数组
  134. $j=2;
  135. $list = is_array($data)? $data : $data->toArray();
  136. foreach ($list as $key=>$value){
  137. $k=0;
  138. foreach ($value as $k1=>$v1){
  139. //设置单元格类型为文本 否则数字会被识别成科学计数法
  140. $objPHPExcel->getActiveSheet()->getCell(PHPExcel_Cell::stringFromColumnIndex($k).$j)->getStyle()->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  141. $objPHPExcel->getActiveSheet()->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($k).$j, strval($v1));
  142. //设置纯文本格式字符串
  143. $k++;
  144. }
  145. $j++;
  146. }
  147. //文件导出
  148. header('Content-Type: application/vnd.ms-excel');
  149. header("Content-Disposition: attachment;filename=$filename.xlsx");
  150. header('Cache-Control: max-age=0');
  151. //允许跨域请求
  152. header('Access-Control-Allow-Origin:*');
  153. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  154. $objWriter->save('php://output');
  155. exit;
  156. }
  157. }
  158. if(!function_exists('validate_invoice_info')){
  159. function validate_invoice_info($data,&$return){
  160. $return=["code"=>0,"校验通过"];
  161. if (!isset($data['invoice_type'])||$data['invoice_type']==''){
  162. $return['code']=1004;
  163. $return['message']="发票类型不能为空";
  164. return false;
  165. }
  166. if (in_array($data['invoice_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  167. if (!isset($data['invoice_total'])||$data['invoice_total']==''){
  168. $return['code']=1004;
  169. $return['message']='发票税前金额不能为空';
  170. return false;
  171. }
  172. }else{
  173. if (!isset($data['invoice_code'])||$data['invoice_code']==''){
  174. $return['code']=1004;
  175. $return['message']='发票代码不能为空';
  176. return false;
  177. }
  178. if (!isset($data['invoice_subtotal'])||$data['invoice_subtotal']==''){
  179. $return['code']=1004;
  180. $return['message']='发票税后金额不能为空';
  181. return false;
  182. }
  183. }
  184. if (!isset($data['invoice_number'])||$data['invoice_number']==''){
  185. $return['code']=1004;
  186. $return['message']='发票号码不能为空';
  187. return false;
  188. }
  189. if(in_array($data['invoice_type'],['normal','roll','toll','electronic'])){
  190. if(!isset($data['check_code'])||$data['check_code']==''){
  191. $return['code']=1004;
  192. $return['message']='校验码不能为空';
  193. return false;
  194. }
  195. }
  196. }
  197. }