企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 一键清空缓存 通用清除缓存函数申明,调用不详述。具体参考我学习笔记: https://www.kancloud.cn/wayanbao/thinkphp/811941 ### 1、清空数据缓存 ``` /** * 清空数据缓存 不删出cache目录 * 2018.10.22 By wyb */ function clear_file_cache($path) { $dh = opendir($path); while (($file = readdir($dh)) !== false ) { if ($file != "." && $file != "..") { $fullpath = $path . $file; if (!is_dir($fullpath)) { array_map( 'unlink', glob($fullpath)); //echo '【删除文件】'.$fullpath ; } else { //echo '【文件夹】'.$fullpath ; $newpath = $fullpath . DS; clear_file_cache($newpath); @rmdir($newpath); } } } closedir($dh); return true; //删除当前文件夹: //if(rmdir($path)) { // return true; //} else { // return false; //} } ``` ### 2、清空模板缓存 ``` /** * 清除模板缓存 不删除 temp目录 * 2018.10.22 By wyb */ function clear_tmp_cache($path) { if(array_map('unlink', glob( $path.'*.php' ))){ return true; } else { return false; } } ``` ### 3、清空日志缓存 ``` /** * 清除日志缓存 不删出log目录 * 2018.10.22 By wyb */ function clear_log_cache($path) { $dh = opendir($path); while (($file = readdir($dh)) !== false ) { if ($file != "." && $file != "..") { $fullpath = $path . $file; if (!is_dir($fullpath)) { array_map( 'unlink', glob($fullpath)); //echo '【删除文件】'.$fullpath ; } else { //echo '【文件夹】'.$fullpath ; $newpath = $fullpath . DS; clear_log_cache($newpath); @rmdir($newpath); } } } closedir($dh); return true; //删除当前文件夹: //if(rmdir($path)) { // return true; //} else { // return false; //} } ``` ### 4、一键清空缓存 ``` /** * 一键清空数据缓存cache、模板缓存tmp,不删除 cache、tmp相关目录 * 2018.10.22 By wyb */ public function clearAll() { // 首先,清空模板缓存tmp $path = TEMP_PATH; if(!@clear_tmp_cache($path)){ return error('清除模板缓存失败'); } // 其次,清空数据缓存cache $c_path = CACHE_PATH; if(!@clear_file_cache($c_path)){ return error('清空文件缓存失败'); } // 最后,信息提示 return success('一键清空缓存成功'); } ``` ## 自定义助手函数步骤 ### 1、新建common控制器(build文件创建) ~~~ return [ // 生成应用公共文件 '__file__' => ['common.php', 'config.php', 'database.php'], // 定义demo模块的自动生成 (按照实际定义的文件名生成) 'admin' => [ '__file__' => ['common.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index','User','Config','Category','Menu','Common'], 'model' => ['Models', 'ModelsField','Config','Category','Menu'], 'view' => ['index/index','config/index','category/index','menu/index'], ], // 其他更多的模块定义 ]; ~~~ ### 2、继承common控制器 ~~~ <?php namespace app\admin\controller; use think\Controller; use think\Db; use util\Tree; class Common extends Controller { …… } ~~~ ### 3、编写公共函数,如删除 ~~~ <?php namespace app\admin\controller; use think\Controller; use think\Db; use util\Tree; class Common extends Controller { // 删除分类 public function delete($id = 0,$tab = 1){ // 获取控制器(当前表名) $table = request()->controller(); if (Db::name($table)->where('id',$id)->delete()) { return success('分类成功',url('index',['tab'=>$tab])); }else{ $this->error('分类失败',url('index',['tab'=>$tab])); } } } ~~~ ## 常用公共函数 ### 自定义success助手函数 ~~~ function success($msg = '成功', $url = '') { $data['status'] = 200; $data['msg'] = $msg; $data['url'] = $url; return json($data); } ~~~ ### 自定义error助手函数 ~~~ function error($msg = '失败', $url = '') { $data['status'] = 202; $data['msg'] = $msg; $data['url'] = $url; return json($data); } ~~~ ### 检测数据表是否存在 ~~~ /** * [table_exists 检测数据表是否存在] * @param string $tablename [表名,不含前缀] * @return [bool] [存在返回true,不存在返回false] */ function table_exists($tablename='') { //获取所有数据表名 $tables = []; $data = Db::query('SHOW TABLES'); foreach ($data as $value) { $tables[] = $value['Tables_in_'.config('database.database')]; } //获取表前缀 $dbPrefix = config('database.prefix'); //当前的表名 $tablename=$dbPrefix.$tablename; if(in_array($tablename,$tables)){ return true; //存在 }else { return false; //不存在 } } ~~~ ### 检测数据表字段是否存在 ~~~ /** * [field_exists 检测数据表字段是否存在] * @param string $tablename [表名,不含前缀] * @param string $field [字段名] * @return [bool] [存在返回true,不存在返回false] */ function field_exists($tablename='', $field='') { //获取表前缀 $dbPrefix = config('database.prefix'); //先判断数据表是否存在 if(table_exists($tablename)){ $fieldArray = Db::query("Describe `{$dbPrefix}{$tablename}` `{$field}` ;"); if(is_array($fieldArray[0])){ return true; //存在 }else { return false; //不存在 } }else { return false; //不存在 } } ~~~ ### 根据分类ID获取相应分类信息 ~~~ /** * @foo 根据分类ID获取相应分类信息 * @Author w.y.b * @DateTime 2018-07-21 * @param integer $id [分类ID] * @param string $field [分类字段] * @return [type] [有第二个字段参数,则获取某个字段;否则获取单条分类数据] */ function getCatInfoById($id=0, $field=''){ if($field == ''){ //获取单条数据 return Db::name('category')->where('id',$id)->find(); }else{ //获取某个字段 return Db::name('category')->where('id',$id)->value($field); } } ~~~ ### 根据分类ID获取相应模型信息 ~~~ /** * @foo 根据分类ID获取相应模型信息 * @Author w.y.b * @DateTime 2018-07-21 * @param integer $id [分类ID] * @param string $field [模型字段] * @return [type] [有第二个模型字段参数,则获取某个字段;否则获取单条模型数据] */ function getModInfoById($id=0, $field=''){ //模型ID、 $modelId = getCatInfoById($id, 'modelid'); if($field == ''){ //获取单条数据 return Db::name('models')->where('id',$modelId)->find(); }else{ //获取某个字段 return Db::name('models')->where('id',$modelId)->value($field); } } ~~~ ### PHP异位或加密实现自动登陆 ``` /** * 异位或加密字符串 * @param [String] $value [需要加密的字符串] * @param [integer] $type [加密解密(0:加密,1:解密)] * @return [String] [加密或解密后的字符串] */ function encryption ($value, $type=0) { $key = md5(C('ENCTYPTION_KEY')); if (!$type) { return str_replace('=', '', base64_encode($value ^ $key)); } $value = base64_decode($value); return $value ^ $key; } ``` 详情见: [PHP异位或加密实现自动登陆](https://www.kancloud.cn/book/wayanbao/thinkphp/preview/PHP%E5%BC%82%E4%BD%8D%E6%88%96%E5%8A%A0%E5%AF%86%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E7%99%BB%E9%99%86.md) [自动登录完成](https://www.kancloud.cn/book/wayanbao/thinkphp/preview/%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E5%AE%8C%E6%88%90.md) ### 退出登录处理 ``` /** * 退出登录处理 */ Public function loginOut () { //卸载SESSION session_unset(); session_destroy(); //删除用于自动登录的COOKIE @setcookie('auto', '', time() - 3600, '/'); //跳转致登录页 redirect(U('Login/index')); } ``` ### 图片上传处理 首先,函数申明 ``` /** * 图片上传处理 * @param [String] $path [保存文件夹名称] * @param [String] $width [缩略图宽度多个用,号分隔] * @param [String] $height [缩略图高度多个用,号分隔(要与宽度一一对应)] * @return [Array] [图片上传信息] */ Private function _upload ($path, $width, $height) { import('ORG.Net.UploadFile'); //引入ThinkPHP文件上传类 $obj = new UploadFile(); //实例化上传类 $obj->maxSize = C('UPLOAD_MAX_SIZE'); //图片最大上传大小 $obj->savePath = C('UPLOAD_PATH') . $path . '/'; //图片保存路径 $obj->saveRule = 'uniqid'; //保存文件名 $obj->uploadReplace = true; //覆盖同名文件 $obj->allowExts = C('UPLOAD_EXTS'); //允许上传文件的后缀名 $obj->thumb = true; //生成缩略图 $obj->thumbMaxWidth = $width; //缩略图宽度 $obj->thumbMaxHeight = $height; //缩略图高度 $obj->thumbPrefix = 'max_,medium_,mini_'; //缩略图后缀名 $obj->thumbPath = $obj->savePath . date('Y_m') . '/'; //缩略图保存图径 $obj->thumbRemoveOrigin = true; //删除原图 $obj->autoSub = true; //使用子目录保存文件 $obj->subType = 'date'; //使用日期为子目录名称 $obj->dateFormat = 'Y_m'; //使用 年_月 形式 if (!$obj->upload()) { return array('status' => 0, 'msg' => $obj->getErrorMsg()); } else { $info = $obj->getUploadFileInfo(); $pic = explode('/', $info[0]['savename']); return array( 'status' => 1, 'path' => array( 'max' => $pic[0] . '/max_' . $pic[1], 'medium' => $pic[0] . '/medium_' . $pic[1], 'mini' => $pic[0] . '/mini_' . $pic[1] ) ); } } ``` 其次,函数调用 ``` /** * 头像上传 */ Public function uploadFace () { if (!$this->isPost()) { halt('页面不存在'); } $upload = $this->_upload('Face', '180,80,50', '180,80,50'); echo json_encode($upload); } ``` 第三,配置文件 ``` <?php return array( //图片上传 'UPLOAD_MAX_SIZE' => 2000000, //最大上传大小 'UPLOAD_PATH' => './Uploads/', //文件上传保存路径 'UPLOAD_EXTS' => array('jpg', 'jpeg', 'gif', 'png'), //允许上传文件的后缀 ); ?> ``` 第四,修改头像 ``` /** * 修改用户头像(后台处理) */ Public function editFace () { if (!$this->isPost()) { halt('页面不存在'); } $db = M('userinfo'); $where = array('uid' => session('uid')); $field = array('face50', 'face80', 'face180'); $old = $db->where($where)->field($field)->find(); if ($db->where($where)->save($_POST)) { if (!empty($old['face180'])) { @unlink('./Uploads/Face/' . $old['face180']); @unlink('./Uploads/Face/' . $old['face80']); @unlink('./Uploads/Face/' . $old['face50']); } $this->success('修改成功', U('index')); } else { $this->error('修改失败,请重试...'); } } ``` ### 格式化时间(评论与微博) ``` /** * 格式化时间 * @param [type] $time [要格式化的时间戳] * @return [type] [description] */ function time_format ($time) { //当前时间 $now = time(); //今天零时零分零秒 $today = strtotime(date('y-m-d', $now)); //传递时间与当前时秒相差的秒数 $diff = $now - $time; $str = ''; switch ($time) { case $diff < 60 : $str = $diff . '秒前'; break; case $diff < 3600 : $str = floor($diff / 60) . '分钟前'; break; case $diff < (3600 * 8) : $str = floor($diff / 3600) . '小时前'; break; case $time > $today : $str = '今天&nbsp;&nbsp;' . date('H:i', $time); break; default : $str = date('Y-m-d H:i:s', $time); } return $str; } ```