common.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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('__')){
  65. function __($msg){
  66. return $msg;
  67. }
  68. };