common.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. use think\facade\Db;
  3. // 应用公共文件
  4. //返回json格式的响应信息,方便中间件记录,目前在abutmenu应用中有用到
  5. if (!function_exists('json_show')) {
  6. function json_show(int $code = 0, string $message = '请求成功', array $data = [])
  7. {
  8. return json(['code' => $code, 'message' => $message, 'data' => $data]);
  9. }
  10. }
  11. function made($var,$data=[]){
  12. $str = Db::name('cat')->where(['id'=>$var])->find();
  13. if($str==false){
  14. return [];
  15. }
  16. $vmn =[];
  17. $vmn['id'] =$str['id'];
  18. $vmn['name'] =$str['cat_name'];
  19. array_unshift($data,$vmn);
  20. // $var['id']=made();
  21. if($str['pid']==0){
  22. // krsort($data);
  23. return $data;
  24. }else{
  25. return made($str['pid'],$data);
  26. }
  27. }
  28. //获取有赞分类信息
  29. function made_youzan(int $yz_cat_id=0,$data=[]){
  30. $rs = get_yz_cat_info($yz_cat_id);
  31. if(empty($rs)) return [];
  32. $vmn =[];
  33. $vmn['yz_cat_id'] =$rs['id'];
  34. $vmn['yz_cat_name'] =$rs['yz_cat_name'];
  35. array_unshift($data,$vmn);
  36. if($rs['parent_cid']==0) return $data;
  37. else return made_youzan($rs['parent_cid'],$data);
  38. }
  39. function makeExcluse($var,$data=[]){
  40. $str = Db::name('exclusive')->where(['id'=>$var])->find();
  41. if($str==false){
  42. return [];
  43. }
  44. $vmn =[];
  45. $vmn['id'] =$str['id'];
  46. $vmn['name'] =$str['name'];
  47. array_unshift($data,$vmn);
  48. // $var['id']=made();
  49. if($str['pid']==0){
  50. // krsort($data);
  51. return $data;
  52. }else{
  53. return makeExcluse($str['pid'],$data);
  54. }
  55. }
  56. function makeNo($str){
  57. $date=date("mdHis");
  58. $year = date("Y")-2000;
  59. $msec=rand(1000,9999);
  60. return $str.$year.$date.$msec;
  61. }
  62. function randomkeys($length) {
  63. $returnStr='';
  64. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  65. for($i = 0; $i < $length; $i ++) {
  66. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  67. }
  68. return $returnStr;
  69. }