common.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. use think\Db;
  12. // 应用公共文件
  13. function makeSalt(){
  14. $str =rand(100000,999999);
  15. return $str;
  16. }
  17. /**
  18. * @param $file
  19. * @return string
  20. */
  21. function uplaod_avatar($file){
  22. // 移动到框架应用根目录/public/uploads/ 目录下
  23. $info = $file->validate(['size'=>15678,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/avatar');
  24. if($info){
  25. return $info->getSaveName();
  26. }else{
  27. return "";
  28. }
  29. }
  30. /**
  31. * 日志记录
  32. * @param $log
  33. * @param $id
  34. */
  35. function write_log($log,$userinfo,$module="",$action="",$role=0){
  36. $data=[
  37. "action_id"=>isset($userinfo['id'])? $userinfo['id']:"",
  38. "msg"=>$log,
  39. "addtime"=>date("y-m-d H:i:s"),
  40. "action_name"=>isset($userinfo['nickname'])? $userinfo['nickname']:"sys",
  41. "module"=>$module,
  42. "action"=>$action,
  43. "username"=>isset($userinfo['username'])? $userinfo['username']:"sys",
  44. "role"=>$role==0?(isset($userinfo['role_id'])&&$userinfo['role_id']!=0?$userinfo['role_id']:0):3,
  45. ];
  46. Db::name("system_log")->insert($data);
  47. }
  48. /**
  49. * @param $code
  50. * @param $msg
  51. */
  52. function error_show($code,$msg=""){
  53. $data = [
  54. "code"=>$code,
  55. "msg"=>$msg
  56. ];
  57. echo json_encode($data);
  58. die();
  59. }
  60. /**
  61. * @param $code
  62. * @param string $msg
  63. * @param array $list
  64. */
  65. function app_show($code,$msg="",$list=[]){
  66. $data = [
  67. "code"=>$code,
  68. "msg"=>$msg,
  69. "data"=>$list
  70. ];
  71. echo json_encode($data);
  72. die();
  73. }
  74. function makeNo($str){
  75. $date=date("mdHis");
  76. $year = date("Y")-2000;
  77. $msec=randomkeys(4);
  78. return $str.$msec.$year.$date;
  79. }
  80. function randomkeys($length) {
  81. $returnStr='';
  82. $pattern = 'abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  83. for($i = 0; $i < $length; $i ++) {
  84. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  85. }
  86. return $returnStr;
  87. }