上一篇文章完成了租户套餐模块的编写,这篇文章将完成创建租户的相关代码
创建租户的时候要求输入租户的相关信息,超级管理员账号密码并且给创建的租户管理员进行菜单权限管理,刷新套餐等操作。
首先 前端页面需要加上管理员账号、密码的input框,选择套餐就调用MineAdmin的通用远程接口(太方便了,安全性的话emmm)
<template>
<div class="ma-content-block lg:flex justify-between p-4">
<!-- CRUD 组件 -->
<ma-crud :options="options" :columns="columns" ref="crudRef">
<template #status="{ record }">
<a-switch
checked-value="1"
unchecked-value="2"
:default-checked="record.status == 1"
@change="switchStatus($event, record.id, 'status')"
></a-switch>
</template>
</ma-crud>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import tenantInfo from '@/api/tenant/tenantInfo'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
import dayjs from 'dayjs'
const crudRef = ref()
const switchStatus = (statusValue, id, statusName) => {
tenantInfo.changeStatus({ id, statusName, statusValue }).then( res => {
res.success && Message.success(res.message)
}).catch( e => { console.log(e) } )
}
const options = reactive({
id: 'tenant_info',
rowSelection: {
showCheckedAll: true
},
pk: 'tenant_id',
operationColumn: true,
operationColumnWidth: 160,
formOption: {
viewType: 'modal',
width: 600
},
api: tenantInfo.getList,
add: {
show: true,
api: tenantInfo.save,
auth: ['tenant:info:save']
},
edit: {
show: true,
api: tenantInfo.update,
auth: ['tenant:info:update']
},
delete: {
show: true,
api: tenantInfo.deletes,
auth: ['tenant:info:delete']
}
})
const columns = reactive([
{
title: "编号",
dataIndex: "tenant_id",
formType: "input",
addDisplay: false,
editDisplay: false,
hide: true,
commonRules: {
required: true,
message: "请输入编号"
}
},
{
title: "管理员账号",
dataIndex: "admin_username",
formType: "input",
search: false,
commonRules: {
required: true,
message: "请输入管理员账号"
},
hide: true,
editDisplay: false,
},
{
title: "管理员密码",
dataIndex: "admin_password",
formType: "input",
search: false,
commonRules: {
required: true,
message: "请输入管理员密码"
},
hide: true,
editDisplay: false,
},
{
title: "公司名称",
dataIndex: "company_name",
formType: "input",
search: true,
commonRules: {
required: true,
message: "请输入公司名称"
}
},
{
title: "联系人",
dataIndex: "contract_name",
formType: "input",
search: true
},
{
title: "联系电话",
dataIndex: "contract_phone",
formType: "input",
search: true
},
{
title: "套餐",
dataIndex: "package_id",
formType: "select",
search: true,
commonRules: {
required: true,
message: "请输入套餐"
},
dict: {
remote: "tenant/package/remote",
props: { label: 'package_name', value: 'package_id'},
remoteOption: {
select: ['package_id', 'package_name'],
filter: {
status: ['=', 1]
}
}
}
},
{
title: "到期时间",
dataIndex: "exprie_time",
formType: "date",
search: true,
showTime: true,
shortcuts: [
{
label: '季度',
value: () => dayjs().add(3, 'month')
},
{
label: '半年',
value: () => dayjs().add(6, 'month')
},
{
label: '一年',
value: () => dayjs().add(1, 'year')
},
]
},
{
title: "用户数量",
dataIndex: "account_num",
formType: "input-number",
placeholder: "-1为不限制",
search: true
},
{
title: "租户状态",
dataIndex: "status",
formType: "radio",
search: true,
commonRules: {
required: true,
message: "请选择租户状态"
},
search: true,
dict: {
name: "data_status",
props: { label: "title", value: "key"}
},
addDefaultValue : "1",
},
{
title: "备注",
dataIndex: "remark",
formType: "textarea",
},
{
title: "管理员ID",
dataIndex: "user_id",
formType: "input",
addDisplay: false,
editDisplay: false,
hide: true
},
{
title: "创建人",
dataIndex: "created_by",
formType: "input",
addDisplay: false,
editDisplay: false,
hide: true
},
{
title: "创建时间",
dataIndex: "created_at",
formType: "date",
addDisplay: false,
editDisplay: false,
hide: true,
showTime: true
},
{
title: "修改人",
dataIndex: "updated_by",
formType: "input",
addDisplay: false,
editDisplay: false,
hide: true
},
{
title: "修改时间",
dataIndex: "updated_at",
formType: "date",
addDisplay: false,
editDisplay: false,
hide: true,
showTime: true
}
])
</script>
<script> export default { name: 'tenant:info' } </script>
前端处理完之后,我们来写后端的一些逻辑
首先是Validate,添加admin_username和admin_password都为必填
将saveRules和attributes都加上
app/Tenant/Request/TenantInfoRequest.php
public function saveRules(): array
{
return [
//公司名称 验证
'company_name' => 'required',
//套餐 验证
'package_id' => 'required',
//租户状态 验证
'status' => 'required',
'admin_username' => 'required',
'admin_password' => 'required',
];
}
public function attributes(): array
{
return [
'tenant_id' => '编号',
'company_name' => '公司名称',
'package_id' => '套餐',
'status' => '租户状态',
'admin_username' => '管理员账号',
'admin_password' => '管理员密码'
];
}
然后接下来就是重写save方法了 来到app/Tenant/Service/TenantInfoService.php