| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <template>
- <div class="message-main">
- <h4>基本信息</h4>
- <el-form
- ref="ruleFormRef"
- :model="ruleForm"
- :rules="rules"
- label-width="auto"
- class="demo-ruleForm"
- :size="formSize"
- :inline="true"
- status-icon
- >
- <el-row :gutter="24">
- <el-col :span="9">
- <el-form-item label="标题" prop="title">
- <el-input v-model="ruleForm.title" clearable style="width: 400px" />
- </el-form-item>
- </el-col>
- <el-col :span="9">
- <el-form-item label="发布机构" prop="orgId">
- <el-cascader
- filterable
- clearable
- style="width: 400px"
- v-model="ruleForm.orgId"
- :options="institutionList"
- :props="tableProps"
- @change="deptChange"
- />
- </el-form-item>
- </el-col>
- </el-row>
- <el-row :gutter="24">
- <el-col :span="9">
- <el-form-item label="URL" prop="url">
- <el-input v-model="ruleForm.url" clearable style="width: 400px" />
- </el-form-item>
- </el-col>
- <el-col :span="9">
- <el-form-item label="子机构是否发布" prop="isLevel">
- <el-radio-group v-model="ruleForm.isLevel">
- <el-radio :value="1">是</el-radio>
- <el-radio :value="0">否</el-radio>
- </el-radio-group>
- </el-form-item>
- </el-col>
- </el-row>
- <el-form-item label="广告图" prop="image">
- <ImgCropper @uploadImg="uploadImg" :imgUrl="imgUrl" :imgName="imgName" v-if="imgUrl"></ImgCropper>
- <ImgCropper @uploadImg="uploadImg" :imgUrl="imgUrl" :imgName="imgName" v-else></ImgCropper>
- <!-- <el-upload
- class="avatar-uploader"
- action="#"
- :show-file-list="false"
- :on-success="handleAvatarSuccess"
- :before-upload="beforeAvatarUpload"
- >
- <img v-if="imageUrl" :src="imageUrl" class="avatar" />
- <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
- </el-upload> -->
- </el-form-item>
- <el-row style="justify-content: center">
- <el-form-item>
- <el-button @click="resetForm(ruleFormRef)">取消</el-button>
- <el-button type="primary" @click="submitForm(ruleFormRef)">
- 保存
- </el-button>
- </el-form-item>
- </el-row>
- </el-form>
- </div>
- </template>
- <script setup lang="ts">
- import { ref, reactive, onMounted } from "vue";
- import {ElMessage, ElMessageBox} from "element-plus"
- import type { ComponentSize, FormInstance, FormRules, } from "element-plus";
- import api from "@/api";
- import router from "@/router";
- import { useRoute, useRouter } from "vue-router";
- const myRouter = useRouter()
- function uploadImg(file:any){
- ruleForm.image=file.id
- }
- const imgUrl=ref('')
- const imgName=ref('')
- interface RuleForm {
- title: string;
- orgId: string;
- sendDeptName: string;
- image: string;
- isLevel: string | number;
- isForce: string;
- url:string;
- }
- const tableProps = ref({
- value: "id",
- label: "name",
- children: "children",
- checkStrictly: true,
- });
- const route=useRoute()
- onMounted(() => {
- getInstitutionList(); //机构列表
- // console.log(route)
- if(route.query?.id){
- api.advertisementApi.getById({id:route.query?.id}).then(res=>{
- if(res.code===200){
- ruleForm.title=res.data.title
- ruleForm.orgId=res.data.orgId
- orgId.value=res.data.orgId
- ruleForm.isLevel=res.data.isLevel
- ruleForm.url=res.data.url
- imgUrl.value=res.data.previewUrl
- ruleForm.image=res.data.uploadFiles.id
- imgName.value=res.data.uploadFiles.fileName
- }
- })
- } else {
- ruleForm.orgId = localStorage.getItem('userDeptId')
- }
- });
- const formSize = ref<ComponentSize>("default");
- const ruleFormRef = ref<FormInstance>();
- const ruleForm = reactive<RuleForm>({
- title: "",
- orgId: "",
- sendDeptName: "",
- isLevel: 0,
- isForce: "",
- image: "",
- url:"",
- });
- const rules = reactive<FormRules<RuleForm>>({
- title: [
- { required: true, message: "请输入标题", trigger: "blur" },
- { max: 50, message: "最大长度不可以超过50个字符", trigger: "blur" },
- ],
- orgId: [
- {
- required: true,
- message: "请选择发布机构",
- trigger: "change",
- },
- ],
- url: [
- {
- pattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
- message: '请填写正确的URL地址',
- trigger: 'change'
- }
- ],
- image: [
- {
- required: true,
- message: "请上传图片",
- trigger: "blur",
- },
- ],
- });
- const orgId=ref('')
- //机构选择
- function deptChange(e:any){
- if(e){
- orgId.value=e[e.length-1]
- ruleForm.sendDeptName=getID(institutionList.value,orgId.value).name
- // console.log(getID(institutionList.value,orgId.value))
- }else{
- orgId.value=''
- }
- }
- const submitForm = async (formEl: FormInstance | undefined) => {
- if (!formEl) return;
- await formEl.validate((valid, fields) => {
- if (valid) {
- if(route.query?.id){
- api.advertisementApi.posterEdit({
- ...ruleForm,
- orgId:orgId.value,
- id:route.query?.id
- }).then(res=>{
- if(res.code===200){
- ElMessage.success(res.msg)
- router.back()
- }
- })
- }else{
- if(route.query?.total&&route.query?.total >=5) {
- ElMessageBox({
- title: '温馨提示',
- message: '当前已有5条广告图,添加时会将时间排序最晚的数据进行替换,是否继续保存?',
- type: 'warning',
- showCancelButton: true,
- confirmButtonText: '继续保存',
- cancelButtonText: '取消',
- }).then((action) => {
- if(action === 'confirm') {
- api.advertisementApi.posterAdd({
- ...ruleForm,
- orgId:orgId.value
- }).then(res=>{
- if(res.code===200){
- ElMessage.success(res.msg)
- router.back()
- }
- })
- }
- })
- } else {
- api.advertisementApi.posterAdd({
- ...ruleForm,
- orgId:orgId.value
- }).then(res=>{
- if(res.code===200){
- ElMessage.success(res.msg)
- router.back()
- }
- })
- }
- }
- }
- });
- };
- const resetForm = (formEl: FormInstance | undefined) => {
- if (!formEl) return;
- formEl.resetFields();
- myRouter.push({
- path: '/content/advertisement'
- })
- };
- //机构列表
- const institutionList = ref([]);
- const getInstitutionList = () => {
- api.institutionApi.institutionList({}).then((res: any) => {
- if (res.code === 200) institutionList.value = res.data;
- });
- };
- function getID(json:any, id:string) {
- var o = {}; //专门用来保存筛选后的数组
- //定义一个函数getID(json,id),形参json用来接受传入的数组,形参id用来接受id号
- //核心:利用arry.forEach(function(currentValue),index,arr)遍历数组
- json.forEach(function(item:any) {
- //if用来判断外层,else if用来判断里层,调用递归函数的有2个判断条件在“有goods属性并且不为空”情况下才调用
- if (item.id == id) {
- return o = item;
- } else if (item.children && item.children.length > 0) {
- o = getID(item.children, id)//递归
- }
- });
- return o; //全部遍历完之后返回对象o
- }
- </script>
- <style lang="scss" scoped>
- .message-main {
- flex: 1;
- width: 100%;
- height: 100%;
- background: #fff;
- padding: 15px;
- box-sizing: border-box;
- }
- .avatar-uploader {
- width: 178px;
- height: 178px;
- display: block;
- border: 1px dashed #8c939d !important;
- }
- .avatar-uploader .el-upload {
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- // transition: var(--el-transition-duration-fast);
- }
- .avatar-uploader .el-upload:hover {
- border-color: var(--el-color-primary);
- }
- .el-icon.avatar-uploader-icon {
- font-size: 28px;
- color: #8c939d;
- width: 178px;
- height: 178px;
- text-align: center;
- }
- </style>
|