addAdvertisement.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div class="message-main">
  3. <h4>基本信息</h4>
  4. <el-form
  5. ref="ruleFormRef"
  6. :model="ruleForm"
  7. :rules="rules"
  8. label-width="auto"
  9. class="demo-ruleForm"
  10. :size="formSize"
  11. :inline="true"
  12. status-icon
  13. >
  14. <el-row :gutter="24">
  15. <el-col :span="9">
  16. <el-form-item label="标题" prop="title">
  17. <el-input v-model="ruleForm.title" clearable style="width: 400px" />
  18. </el-form-item>
  19. </el-col>
  20. <el-col :span="9">
  21. <el-form-item label="发布机构" prop="orgId">
  22. <el-cascader
  23. filterable
  24. clearable
  25. style="width: 400px"
  26. v-model="ruleForm.orgId"
  27. :options="institutionList"
  28. :props="tableProps"
  29. @change="deptChange"
  30. />
  31. </el-form-item>
  32. </el-col>
  33. </el-row>
  34. <el-row :gutter="24">
  35. <el-col :span="9">
  36. <el-form-item label="URL" prop="url">
  37. <el-input v-model="ruleForm.url" clearable style="width: 400px" />
  38. </el-form-item>
  39. </el-col>
  40. <el-col :span="9">
  41. <el-form-item label="子机构是否发布" prop="isLevel">
  42. <el-radio-group v-model="ruleForm.isLevel">
  43. <el-radio :value="1">是</el-radio>
  44. <el-radio :value="0">否</el-radio>
  45. </el-radio-group>
  46. </el-form-item>
  47. </el-col>
  48. </el-row>
  49. <el-form-item label="广告图" prop="image">
  50. <ImgCropper @uploadImg="uploadImg" :imgUrl="imgUrl" :imgName="imgName" v-if="imgUrl"></ImgCropper>
  51. <ImgCropper @uploadImg="uploadImg" :imgUrl="imgUrl" :imgName="imgName" v-else></ImgCropper>
  52. <!-- <el-upload
  53. class="avatar-uploader"
  54. action="#"
  55. :show-file-list="false"
  56. :on-success="handleAvatarSuccess"
  57. :before-upload="beforeAvatarUpload"
  58. >
  59. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  60. <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  61. </el-upload> -->
  62. </el-form-item>
  63. <el-row style="justify-content: center">
  64. <el-form-item>
  65. <el-button @click="resetForm(ruleFormRef)">取消</el-button>
  66. <el-button type="primary" @click="submitForm(ruleFormRef)">
  67. 保存
  68. </el-button>
  69. </el-form-item>
  70. </el-row>
  71. </el-form>
  72. </div>
  73. </template>
  74. <script setup lang="ts">
  75. import { ref, reactive, onMounted } from "vue";
  76. import {ElMessage, ElMessageBox} from "element-plus"
  77. import type { ComponentSize, FormInstance, FormRules, } from "element-plus";
  78. import api from "@/api";
  79. import router from "@/router";
  80. import { useRoute, useRouter } from "vue-router";
  81. const myRouter = useRouter()
  82. function uploadImg(file:any){
  83. ruleForm.image=file.id
  84. }
  85. const imgUrl=ref('')
  86. const imgName=ref('')
  87. interface RuleForm {
  88. title: string;
  89. orgId: string;
  90. sendDeptName: string;
  91. image: string;
  92. isLevel: string | number;
  93. isForce: string;
  94. url:string;
  95. }
  96. const tableProps = ref({
  97. value: "id",
  98. label: "name",
  99. children: "children",
  100. checkStrictly: true,
  101. });
  102. const route=useRoute()
  103. onMounted(() => {
  104. getInstitutionList(); //机构列表
  105. // console.log(route)
  106. if(route.query?.id){
  107. api.advertisementApi.getById({id:route.query?.id}).then(res=>{
  108. if(res.code===200){
  109. ruleForm.title=res.data.title
  110. ruleForm.orgId=res.data.orgId
  111. orgId.value=res.data.orgId
  112. ruleForm.isLevel=res.data.isLevel
  113. ruleForm.url=res.data.url
  114. imgUrl.value=res.data.previewUrl
  115. ruleForm.image=res.data.uploadFiles.id
  116. imgName.value=res.data.uploadFiles.fileName
  117. }
  118. })
  119. } else {
  120. ruleForm.orgId = localStorage.getItem('userDeptId')
  121. }
  122. });
  123. const formSize = ref<ComponentSize>("default");
  124. const ruleFormRef = ref<FormInstance>();
  125. const ruleForm = reactive<RuleForm>({
  126. title: "",
  127. orgId: "",
  128. sendDeptName: "",
  129. isLevel: 0,
  130. isForce: "",
  131. image: "",
  132. url:"",
  133. });
  134. const rules = reactive<FormRules<RuleForm>>({
  135. title: [
  136. { required: true, message: "请输入标题", trigger: "blur" },
  137. { max: 50, message: "最大长度不可以超过50个字符", trigger: "blur" },
  138. ],
  139. orgId: [
  140. {
  141. required: true,
  142. message: "请选择发布机构",
  143. trigger: "change",
  144. },
  145. ],
  146. url: [
  147. {
  148. pattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
  149. message: '请填写正确的URL地址',
  150. trigger: 'change'
  151. }
  152. ],
  153. image: [
  154. {
  155. required: true,
  156. message: "请上传图片",
  157. trigger: "blur",
  158. },
  159. ],
  160. });
  161. const orgId=ref('')
  162. //机构选择
  163. function deptChange(e:any){
  164. if(e){
  165. orgId.value=e[e.length-1]
  166. ruleForm.sendDeptName=getID(institutionList.value,orgId.value).name
  167. // console.log(getID(institutionList.value,orgId.value))
  168. }else{
  169. orgId.value=''
  170. }
  171. }
  172. const submitForm = async (formEl: FormInstance | undefined) => {
  173. if (!formEl) return;
  174. await formEl.validate((valid, fields) => {
  175. if (valid) {
  176. if(route.query?.id){
  177. api.advertisementApi.posterEdit({
  178. ...ruleForm,
  179. orgId:orgId.value,
  180. id:route.query?.id
  181. }).then(res=>{
  182. if(res.code===200){
  183. ElMessage.success(res.msg)
  184. router.back()
  185. }
  186. })
  187. }else{
  188. if(route.query?.total&&route.query?.total >=5) {
  189. ElMessageBox({
  190. title: '温馨提示',
  191. message: '当前已有5条广告图,添加时会将时间排序最晚的数据进行替换,是否继续保存?',
  192. type: 'warning',
  193. showCancelButton: true,
  194. confirmButtonText: '继续保存',
  195. cancelButtonText: '取消',
  196. }).then((action) => {
  197. if(action === 'confirm') {
  198. api.advertisementApi.posterAdd({
  199. ...ruleForm,
  200. orgId:orgId.value
  201. }).then(res=>{
  202. if(res.code===200){
  203. ElMessage.success(res.msg)
  204. router.back()
  205. }
  206. })
  207. }
  208. })
  209. } else {
  210. api.advertisementApi.posterAdd({
  211. ...ruleForm,
  212. orgId:orgId.value
  213. }).then(res=>{
  214. if(res.code===200){
  215. ElMessage.success(res.msg)
  216. router.back()
  217. }
  218. })
  219. }
  220. }
  221. }
  222. });
  223. };
  224. const resetForm = (formEl: FormInstance | undefined) => {
  225. if (!formEl) return;
  226. formEl.resetFields();
  227. myRouter.push({
  228. path: '/content/advertisement'
  229. })
  230. };
  231. //机构列表
  232. const institutionList = ref([]);
  233. const getInstitutionList = () => {
  234. api.institutionApi.institutionList({}).then((res: any) => {
  235. if (res.code === 200) institutionList.value = res.data;
  236. });
  237. };
  238. function getID(json:any, id:string) {
  239. var o = {}; //专门用来保存筛选后的数组
  240. //定义一个函数getID(json,id),形参json用来接受传入的数组,形参id用来接受id号
  241. //核心:利用arry.forEach(function(currentValue),index,arr)遍历数组
  242. json.forEach(function(item:any) {
  243. //if用来判断外层,else if用来判断里层,调用递归函数的有2个判断条件在“有goods属性并且不为空”情况下才调用
  244. if (item.id == id) {
  245. return o = item;
  246. } else if (item.children && item.children.length > 0) {
  247. o = getID(item.children, id)//递归
  248. }
  249. });
  250. return o; //全部遍历完之后返回对象o
  251. }
  252. </script>
  253. <style lang="scss" scoped>
  254. .message-main {
  255. flex: 1;
  256. width: 100%;
  257. height: 100%;
  258. background: #fff;
  259. padding: 15px;
  260. box-sizing: border-box;
  261. }
  262. .avatar-uploader {
  263. width: 178px;
  264. height: 178px;
  265. display: block;
  266. border: 1px dashed #8c939d !important;
  267. }
  268. .avatar-uploader .el-upload {
  269. border-radius: 6px;
  270. cursor: pointer;
  271. position: relative;
  272. overflow: hidden;
  273. // transition: var(--el-transition-duration-fast);
  274. }
  275. .avatar-uploader .el-upload:hover {
  276. border-color: var(--el-color-primary);
  277. }
  278. .el-icon.avatar-uploader-icon {
  279. font-size: 28px;
  280. color: #8c939d;
  281. width: 178px;
  282. height: 178px;
  283. text-align: center;
  284. }
  285. </style>