common.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use think\Db;
  3. use think\File;
  4. /**
  5. * @param $account
  6. * @return string
  7. * @throws \think\db\exception\DataNotFoundException
  8. * @throws \think\db\exception\DbException
  9. * @throws \think\db\exception\ModelNotFoundException
  10. * @throws \think\exception\DbException
  11. */
  12. function makeToken($account){
  13. $now=time();
  14. $str = $account['username'].$account['salt'].$now;
  15. $token = base64_encode($str);
  16. $has = Db::name("admin_token")->where(["adminid"=>$account['id']])->find();
  17. if($has){
  18. Db::name("admin_token")->where(["adminid"=>$account['id']])->update(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800)]);
  19. }else{
  20. Db::name("admin_token")->insert(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800),"addtime"=>date("Y-m-d H:i:s",$now+1800),
  21. "adminid"=>$account['id']]);
  22. }
  23. return $token;
  24. }
  25. /**
  26. * @param $token
  27. */
  28. function verifyToken($token){
  29. $has = Db::name("admin_token")->where(["token"=>$token])->find();
  30. if(!$has){
  31. return ["code"=>101,"msg"=>"token不存在"];
  32. }
  33. if(strtotime($has['expiretime'])<=time()){
  34. return ["code"=>102,"msg"=>"token已失效"];
  35. }
  36. $account = Db::name("admin")->where(["id"=>$has['adminid'],"is_del"=>0])->find();
  37. if(!$account){
  38. return ["code"=>103,"msg"=>"未找到账户"];
  39. }
  40. if($account['status']!=1){
  41. return ["code"=>104,"msg"=>"账户已禁用"];
  42. }
  43. $token_str = base64_decode($token);
  44. $account_str= substr($token_str,0,-10);
  45. if($account_str==$account['username'].$account['salt']){
  46. Db::name("admin_token")->where(["token"=>$token])->update(["expiretime"=>date("Y-m-d H:i:s",time()+1800)]);
  47. return ["code"=>0,"msg"=>"账户有效"];
  48. }else{
  49. return ["code"=>105,"msg"=>"账户token无效"];
  50. }
  51. }
  52. /**
  53. * @param $username
  54. * @return bool 账户正则匹配
  55. */
  56. function checkAccount($username){
  57. $match ='/^(1745)([\d]{6})$/';
  58. return preg_match($match,$username)?true:false;
  59. }
  60. /**
  61. * @param $pawd
  62. * @return bool 账户正则匹配
  63. */
  64. function checkPasswd($pawd){
  65. $match ='/^([a-zA-z]{2})([\d]{4})$/';
  66. return preg_match($match,$pawd)?true:false;
  67. }
  68. function UploadImg($files){
  69. $savename = [];
  70. $files= !is_array($files) ? [$files] : $files;
  71. // var_dump($files);
  72. try{
  73. //验证
  74. //validate(['imgFile'=>['fileSize'=>10240000,'fileExt'=>'jpg,jpeg,png,bmp,gif', 'fileMime'=>'image/jpeg,image/png,image/gif']])->check(['imgFile'=>$files]);
  75. foreach($files as $file){
  76. // $url= File::disk('public')->putFile( 'topic/'.date("Ymd"), $file,function ()use($file){
  77. // return str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName()."_".date('YmdHis'));
  78. // });
  79. // $name = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName());
  80. $info= $file->validate(['size'=>10240000,'ext'=>'jpg,jpeg,png,bmp,gif'])->move(ROOT_PATH .'public' .DS .'upload');
  81. if($info){
  82. $temp = ['url'=>'upload/'. $info->getSaveName()];
  83. $savename[]=$temp;
  84. // echo 'upload/'. $info->getSaveName();
  85. }else{
  86. return "";
  87. // echo $file->getError();
  88. }
  89. }
  90. return $savename;
  91. }catch (\think\exception\ValidateException $e) {
  92. return $e->getMessage();
  93. }
  94. }
  95. function QueuePush($data,$queue="createOrderJob"){
  96. //当前任务将由哪个类来负责处理
  97. $jobHandlerClassName = 'app\admin\JobInv';
  98. //业务数据 对象需要手动转序列化
  99. $jobQueueName = $queue;
  100. $isPushed = Queue::push($jobHandlerClassName, $data,$jobQueueName);
  101. if( $isPushed !== false ){
  102. Log::write("{$jobQueueName} 任务失败:{$data['id']}");
  103. }
  104. }
  105. function checkRole($roleid,$menu){
  106. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  107. if($roleinfo['private_data']!=""){
  108. $private = explode(",",$roleinfo['private_data']);
  109. if(in_array($menu,$private)){
  110. return true;
  111. }
  112. }
  113. return false;
  114. }