common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. //  />  フ
  3. //     |  _  _|
  4. //     /` ミ_꒳ノ
  5. //     /      |
  6. //    /  ヽ   ノ
  7. //    │  | | |
  8. // / ̄|   | | |
  9. // | ( ̄ヽ__ヽ_)__)
  10. // \二つ
  11. // 应用公共文件
  12. //失败返回值
  13. use think\facade\Filesystem;if(!function_exists("error")){
  14. /**
  15. * @param $message
  16. * @param int $code
  17. * @param null $data
  18. * @return \think\response\Json
  19. */
  20. function error($message,$code=1004,$data=null){
  21. return json(["code"=>$code,"message"=>$message,"data"=>$data]);
  22. }
  23. }
  24. //成功返回值
  25. if(!function_exists('success')){
  26. /**
  27. * @param $message
  28. * @param int $code
  29. * @param null $data
  30. * @return \think\response\Json
  31. */
  32. function success($message,$data=null,$code=0){
  33. return json(['code'=>$code,'message'=>$message,'data'=>$data]);
  34. }
  35. }
  36. if (!function_exists('action_in_arr')) {
  37. /**
  38. * 检测一个访问的方式是否在白名单内 白名单不验token
  39. * @param array $arr
  40. * @return bool
  41. */
  42. function action_in_arr(array $arr = []): bool
  43. {
  44. $arr = is_array($arr) ? $arr : explode(',', $arr);
  45. if (!$arr) {
  46. return false;
  47. }
  48. $arr = array_map('strtolower', $arr);
  49. if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. }
  55. if(!function_exists("startTime")){
  56. function startTime($time){
  57. return date('Y-m-d 00:00:00',strtotime($time));
  58. }
  59. }
  60. if(!function_exists('endTime')){
  61. function endTime($time){
  62. return date('Y-m-d 23:59:59',strtotime($time));
  63. }
  64. }
  65. if(!function_exists('makeToken')){
  66. function makeToken($strs,$salt='',$type='md5'){
  67. if($salt=='') $salt=time();
  68. $type = is_callable($type) ? $type : 'md5';
  69. $token = call_user_func($type,$strs.$salt);
  70. return $token;
  71. }
  72. };
  73. if(!function_exists('makeNo')){
  74. function makeNo($strs,$salt=''){
  75. $date=date('mdHis');
  76. $year = date('Y')-2000;
  77. if($salt=="")$salt=\think\helper\Str::random(4,1);
  78. return $strs.$year.$date.$salt;
  79. }
  80. };
  81. if(!function_exists('__')){
  82. function __($msg){
  83. return $msg;
  84. }
  85. };
  86. if(!function_exists('UploadImg')){
  87. function UploadImg($files){
  88. $savename = [];
  89. $files = !is_array($files) ? [$files] : $files;
  90. try {
  91. //验证
  92. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  93. foreach ($files as $file) {
  94. $url = Filesystem::disk('public')->putFile('topic/' . date('Ymd'), $file, function () use ($file) {
  95. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . '_' . date('YmdHis'));
  96. });
  97. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  98. $temp = ['url' => $url, 'name' => $name];
  99. $savename[] = $temp;
  100. }
  101. return $savename;
  102. } catch (\think\exception\ValidateException $e) {
  103. throw new \think\Exception($e->getMessage());
  104. }
  105. }
  106. }
  107. if(!function_exists('validate_invoice_info')){
  108. function validate_invoice_info($data,&$return){
  109. $return=["code"=>0,"校验通过"];
  110. if (!isset($data['invoice_type'])||$data['invoice_type']==''){
  111. $return['code']=1004;
  112. $return['message']="发票类型不能为空";
  113. return false;
  114. }
  115. if (in_array($data['invoice_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  116. if (!isset($data['invoice_total'])||$data['invoice_total']==''){
  117. $return['code']=1004;
  118. $return['message']='发票税前金额不能为空';
  119. return false;
  120. }
  121. }else{
  122. if (!isset($data['invoice_code'])||$data['invoice_code']==''){
  123. $return['code']=1004;
  124. $return['message']='发票代码不能为空';
  125. return false;
  126. }
  127. if (!isset($data['invoice_subtotal'])||$data['invoice_subtotal']==''){
  128. $return['code']=1004;
  129. $return['message']='发票税后金额不能为空';
  130. return false;
  131. }
  132. }
  133. if (!isset($data['invoice_number'])||$data['invoice_number']==''){
  134. $return['code']=1004;
  135. $return['message']='发票号码不能为空';
  136. return false;
  137. }
  138. if(in_array($data['invoice_type'],['normal','roll','toll','electronic'])){
  139. if(!isset($data['check_code'])||$data['check_code']==''){
  140. $return['code']=1004;
  141. $return['message']='校验码不能为空';
  142. return false;
  143. }
  144. }
  145. }
  146. }