common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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('validate_invoice_info')){
  109. function validate_invoice_info($data,&$return){
  110. $return=["code"=>0,"校验通过"];
  111. if (!isset($data['invoice_type'])||$data['invoice_type']==''){
  112. $return['code']=1004;
  113. $return['message']="发票类型不能为空";
  114. return false;
  115. }
  116. if (in_array($data['invoice_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  117. if (!isset($data['invoice_total'])||$data['invoice_total']==''){
  118. $return['code']=1004;
  119. $return['message']='发票税前金额不能为空';
  120. return false;
  121. }
  122. }else{
  123. if (!isset($data['invoice_code'])||$data['invoice_code']==''){
  124. $return['code']=1004;
  125. $return['message']='发票代码不能为空';
  126. return false;
  127. }
  128. if (!isset($data['invoice_subtotal'])||$data['invoice_subtotal']==''){
  129. $return['code']=1004;
  130. $return['message']='发票税后金额不能为空';
  131. return false;
  132. }
  133. }
  134. if (!isset($data['invoice_number'])||$data['invoice_number']==''){
  135. $return['code']=1004;
  136. $return['message']='发票号码不能为空';
  137. return false;
  138. }
  139. if(in_array($data['invoice_type'],['normal','roll','toll','electronic'])){
  140. if(!isset($data['check_code'])||$data['check_code']==''){
  141. $return['code']=1004;
  142. $return['message']='校验码不能为空';
  143. return false;
  144. }
  145. }
  146. }
  147. }