下面直接进入上传配置功能开发,废话不多说。
一、创建表
1、语句
CREATE TABLE `cluster_control`.`nc_param_upload` (`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '编号',`upload_type` tinyint(1) UNSIGNED NOT NULL COMMENT '上传类型 1:本站 2:阿里云 3:腾讯云 4:七牛云',`checked_images` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图片类型',`checked_videos` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '视频类型',`checked_filedocs` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '文件类型',`upload_number` smallint(5) UNSIGNED NOT NULL DEFAULT 0 COMMENT '文件大小',`example_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '实例名称',`access_key` varchar(100) NULL COMMENT 'accessKey',`secret_key` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT 'secretKey',`bucket_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '空间名称',`domain_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT 'Bucket域名',`update_time` datetime(0) NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '文件上传配置';
2、表结构
二、路由配置
因它是固定数据,所以只有读取与保存这两个接口。在根目录下的route文件夹中的app.php文件中,具体如下代码:
//上传配置Route::post('upload/get_info','param.Upload/getInfo');// 获取上传配置参数数据Route::post('upload/save_info','param.Upload/saveInfo');// 保存上传配置参数数据
三、控制层开发
在根目录下的app文件夹下的controller文件夹中,创建文件夹并命名param,在param文件夹中接着创建上传参数控制器并命名为Upload。具体代码如下:
<?php
/*** 上传参数控制管理* User: 龙哥·三年风水* Date: 2024/11/1* Time: 14:38*/
namespace app\controller\param;
use app\controller\Base;
use app\model\param\Upload as UploadModel;
class Upload extends Base
{// 获取上传参数public function getInfo(){if($this->request->param('id') !== 1)return err('请求参数错误');//验证请求参数$resPUpload = UploadModel::dataFind(['id' => 1],'upload_type,checked_images,checked_videos,checked_filedocs,upload_number,example_name,access_key,secret_key,bucket_name,domain_name');$resPUpload['upload_max_filesize'] = (int)ini_get('upload_max_filesize');return succ('操作成功',$resPUpload);}// 保存上传参数public function saveInfo(){$param = $this->request->param();$validate = new \app\validate\param\Upload;if(!$validate->check($param)) return err($validate->getError());$checked_images = implode(',', $param['checked_images']);$checked_videos = implode(',', $param['checked_videos']);$checked_filedocs = implode(',', $param['checked_filedocs']);UploadModel::save(['upload_type' => $param['upload_type'],'checked_images' => $checked_images,'checked_videos' => $checked_videos,'checked_filedocs' => $checked_filedocs,'upload_number' => $param['upload_number'],'example_name' => $param['upload_type'] > 1 ? $param['example_name'] : '','access_key' => $param['upload_type'] > 1 ? $param['access_key'] : '','secret_key' => $param['upload_type'] > 1 ? $param['secret_key'] : '','bucket_name' => $param['upload_type'] > 1 ? $param['bucket_name'] : '','domain_name' => $param['upload_type'] > 1 ? $param['domain_name'] : ''],[['id','=',1]]);return succ('操作成功', []);}
}
四、数据层开发
在根目录下app文件夹下model文件夹中创建文件夹并命名为param,在param文件夹中创建上传参数数据文件并命名为Upload。具体代码如下:
<?php
/*** 上传参数数据管理* User: 龙哥·三年风水* Date: 2024/11/1* Time: 14:42*/
namespace app\model\param;
use app\model\Tools;
use Redis\Redis;
class Upload extends Tools
{protected static $name = 'param_upload';protected static $connection = 'mysql';protected $pk = 'id';protected static $unsetField = ['update_time','create_time','status'];//获取单条数据public static function dataFind($where = [], $field='*', $unsetField=[]){$fieldL = 0; //预防字段少于数据库字段等情况下,加入等级进行区分。保障Redis存储最高性if($field == '*') {$res = Redis::select(config('cache.stores.redis.other_db'))->hgetall('param_upload1');}else{$res = Redis::select(config('cache.stores.redis.other_db'))->hmget('param_upload1',explode(',',$field));}foreach($res as $v){if(empty($v)){$fieldL = 1;break;}}if($fieldL === 1){$fields = $field;if($fieldL === 1)$fields = '*';$res = self::dataFinds(self::$name, $where, $fields, true, self::$connection);if(count($unsetField) > 0){//如果单独添加过滤数据,则优先并入数据foreach($unsetField as $v){array_push(self::$unsetField,$v);}}if(!empty(self::$unsetField)){//主动过滤修改时间等状态foreach(self::$unsetField as $v){if(isset($res[$v]))unset($res[$v]);}}//过滤好了之后存入RedisRedis::select(config('cache.stores.redis.other_db'))->hmset('param_upload1',$res);}if($field != '*'){//根据需要,将需要的字段过滤返回$data = [];$list = explode(',',$field);foreach($list as $k=>$v){if($fieldL === 1){$data[$v] = $res[$v];}else{$data[$v] = $res[$k];}}$res = $data;}return $res;}//保存数据public static function save($data, $where = [], $unsetField=[]){self::dataSaves(self::$name, $data, $where, self::$connection);if(count($unsetField) > 0){//如果单独添加过滤数据,则优先并入数据foreach($unsetField as $v){array_push(self::$unsetField,$v);}}if(!empty(self::$unsetField)){//主动过滤修改时间等状态foreach(self::$unsetField as $v){if(isset($data[$v]))unset($data[$v]);}}//过滤好了之后存入RedisRedis::select(config('cache.stores.redis.other_db'))->hmset('param_upload1',$data);}
}
五、验证层开发
在根目录下app文件夹下validate文件夹中创建文件夹并命名为param,在param文件夹中创建图片格式验证文件并命名为Upload。具体代码如下:
<?php
/*** 上传参数验证管理* User: 龙哥·三年风水* Date: 2024/11/1* Time: 14:54*/
namespace app\validate\param;
use app\validate\Tools;
class Upload extends Tools
{protected $rule = ['id' => 'require|number','upload_type' => 'require|number','checked_images' => 'require|array','checked_videos' => 'require|array','checked_filedocs' => 'require|array','upload_number' => 'require|number|between:1,300','example_name' => 'requireIf:upload_type,2,4|url|length:3,120','access_key' => 'requireIf:upload_type,2,4|length:3,120','secret_key' => 'requireIf:upload_type,2,4|length:3,120','bucket_name' => 'requireIf:upload_type,2,4|length:3,120','domain_name' => 'url|length:3,120'];protected $message = ['id.require' => '编号必须填写','id.number' => '编号必须是数字类型','upload_type.require' => '上传类型必须选择','upload_type.number' => '上传类型必须是数字类型','checked_images.require' => '图片类型必须选择','checked_images.array' => '图片类型必须是数组','checked_videos.require' => '视频类型必须选择','checked_videos.array' => '视频类型必须是数组','checked_filedocs.require' => '文件类型必须选择','checked_filedocs.array' => '文件类型必须是数组','upload_number.require' => '文件上传大小必须填写','upload_number.number' => '文件上传大小必须是数字','upload_number.between' => '文件上传大小必须是1到300之间','example_name.requireIf' => '实例名称必须填写','example_name.url' => '实例名称必须是url类型','example_name.length' => '实例名称必须是3到120位之间','access_key.requireIf' => 'accessKey必须填写','access_key.length' => 'accessKey必须是3到120位之间','secret_key.requireIf' => 'secretKey必须填写','secret_key.length' => 'secretKey必须是3到120位之间','bucket_name.requireIf' => '空间名称必须填写','bucket_name.length' => '空间名称必须是3到120位之间','domain_name.url' => 'Bucket域名必须是url类型','domain_name.length' => 'Bucket域名必须是3到120位之间'];
}
六、提前说明
在本功能开发的同时,我发现之前开发的系统配置文件命名及存放的位置不合理,所以需要修改一下前面开发的文档。明天将开发系统配置及文件上传工厂。