添加人员-Thinkphp5.1开发后台管理系统
今天来说下添加人员(后台管理人员),昨天写了登录操作,今天我们来添加人员,为了追求真实性,字段设置为:id、create_time、update_time、display、username、userpass、userhead、sex、userphone、useremail、department、post、status,请求处理过程中使用到了视图层、控制器层、模型层、验证层、静态文件JS等,采用异步上传头像,POST方式提交数据。下面是各步骤代码:
数据库表结构:
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for lt_user -- ---------------------------- DROP TABLE IF EXISTS `lt_user`; CREATE TABLE `lt_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `create_time` int(11) NOT NULL COMMENT '插入时间', `update_time` int(11) NULL DEFAULT NULL COMMENT '修改时间', `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名', `userpass` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码', `display` tinyint(11) NOT NULL DEFAULT 1 COMMENT '数据状态1正常2删除', `userhead` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户头像', `sex` tinyint(1) NULL DEFAULT 1 COMMENT '性别', `userphone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号', `useremail` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', `department` tinyint(1) NULL DEFAULT NULL COMMENT '部门', `post` tinyint(1) NULL DEFAULT NULL COMMENT '岗位', `status` tinyint(1) NULL DEFAULT NULL COMMENT '状态', PRIMARY KEY (`id`) USING BTREE, INDEX `intime`(`create_time`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
视图层代码,请移步码云查看:立即前往
控制器层代码:
/** * 添加 */ public function getSave() { //数据 $data['display'] = 1; //用户状态,默认为1 正常 $data['username'] = input('post.username'); //用户名 $data['userpass'] = input('post.userpass'); //用户密码 $data['userhead'] = input('post.userhead'); //用户头像 $data['sex'] = input('post.sex'); //性别 $data['userphone'] = input('post.userphone'); //手机号 $data['useremail'] = input('post.useremail'); //邮箱 $data['department'] = input('post.department'); //部门 $data['post'] = input('post.post'); //岗位 $data['status'] = input('post.status'); //状态 //验证 $validate = new \app\common\validate\User; if(!$validate->scene('save')->check($data)) { $this->error($validate->getError()); } $data['userpass'] = md5($data['userpass']); //操作 $res = $this->user_model->getSave($data); if($res) { $this->success('添加成功'); } else { $this->error('添加失败'); } }
模型层代码:
/** * 添加数据 * @param $data * @return int|string */ public function getSave($data) { $user = new User; $res = $user->allowField(true)->save($data); return $res; }
验证层代码:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019\2\18 0018 * Time: 19:44 */ namespace app\common\validate; use think\Validate; class User extends Validate { protected $rule = [ 'display' => 'require|number|in:1,2', 'username' => 'require|max:96', 'userpass' => 'require|max:32', 'userhead' => 'require|max:255', 'sex' => 'require|number|in:1,2', 'userphone' => 'require|mobile', 'useremail' => 'require|email', 'department' => 'require|number|in:1,2,3,4,5', 'post' => 'require|number|in:1,2,3,4,5,6,7', 'status' => 'require|number|in:1,2,3,4', 'id' =>'require|number|max:11' ]; protected $message = [ 'display.require' => '请输入数据状态', 'display.number' => '数据状态类型错误', 'display.in' => '数据状态参数值错误', 'username.require' => '请填写姓名', 'username.max' => '姓名参数值最多为32个汉字', 'userpass.require' => '请输入密码', 'userpass.max' => '密码参数值最多为32个字符', 'userhead.require' => '请上传头像', 'userhead.max' => '头像参数值最多为255个字符', 'sex.require' => '请选择性别', 'sex.number' => '性别参数值类型错误', 'sex.in' => '性别参数值错误', 'userphone.require' => '请输入手机号', 'userphone.mobile' => '手机号格式错误', 'useremail.require' => '请输入邮箱', 'useremail.email' => '邮箱格式错误', 'department.require' => '请选择部门', 'department.number' => '部门参数值类型错误', 'department.in' => '部门参数值错误', 'post.require' => '请选择岗位', 'post.number' => '岗位参数值类型错误', 'post.in' => '岗位参数值错误', 'status.require' => '请选择状态', 'status.number' => '状态参数值类型错误', 'status.in' => '状态参数值错误', 'id.require' => '缺少ID参数值', 'id.number' => 'ID参数值类型错误', 'id.max' => 'ID参数值最多为11位', ]; protected $scene = [ 'save'=>['display','username','userpass','userhead','sex','userphone','useremail','department','post','status'], 'update'=>['display','username','userpass','userhead','sex','userphone','useremail','department','post','status','id'], ]; }
关于表单中图片异步上传:ThinkPHP+Layui图片异步上传