common.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // 应用公共文件
  3. //失败返回值
  4. if(!function_exists("error")){
  5. /**
  6. * @param $message
  7. * @param int $code
  8. * @param null $data
  9. * @return \think\response\Json
  10. */
  11. function error($message,$code=1004,$data=null){
  12. return json(["code"=>$code,"message"=>$message,"data"=>$data]);
  13. }
  14. }
  15. //成功返回值
  16. if(!function_exists('success')){
  17. /**
  18. * @param $message
  19. * @param int $code
  20. * @param null $data
  21. * @return \think\response\Json
  22. */
  23. function success($message,$data=null,$code=0){
  24. return json(['code'=>$code,'message'=>$message,'data'=>$data]);
  25. }
  26. }
  27. if (!function_exists('action_in_arr')) {
  28. /**
  29. * 检测一个访问的方式是否在白名单内 白名单不验token
  30. * @param array $arr
  31. * @return bool
  32. */
  33. function action_in_arr(array $arr = []): bool
  34. {
  35. $arr = is_array($arr) ? $arr : explode(',', $arr);
  36. if (!$arr) {
  37. return false;
  38. }
  39. $arr = array_map('strtolower', $arr);
  40. if (in_array(strtolower(request()->action()), $arr) || in_array('*', $arr)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. if(!function_exists("startTime")){
  47. function startTime($time){
  48. return date('Y-m-d 00:00:00',strtotime($time));
  49. }
  50. }
  51. if(!function_exists('endTime')){
  52. function endTime($time){
  53. return date('Y-m-d 23:59:59',strtotime($time));
  54. }
  55. }
  56. if(!function_exists('makeToken')){
  57. function makeToken($strs,$salt='',$type='md5'){
  58. if($salt=='') $salt=time();
  59. $type = is_callable($type) ? $type : 'md5';
  60. $token = call_user_func($type,$strs.$salt);
  61. return $token;
  62. }
  63. };
  64. if(!function_exists('makeNo')){
  65. function makeNo($strs,$salt=''){
  66. $date=date('mdHis');
  67. $year = date('Y')-2000;
  68. if($salt=="")$salt=\think\helper\Str::random(4,1);
  69. return $strs.$year.$date.$salt;
  70. }
  71. };
  72. if(!function_exists('__')){
  73. function __($msg){
  74. return $msg;
  75. }
  76. };