addMessage.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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="sendDeptId">
  22. <el-cascader
  23. filterable
  24. clearable
  25. style="width: 400px"
  26. v-model="ruleForm.sendDeptId"
  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="子机构是否发布" prop="isChildren">
  37. <el-radio-group v-model="ruleForm.isChildren">
  38. <el-radio :value="1">是</el-radio>
  39. <el-radio :value="0">否</el-radio>
  40. </el-radio-group>
  41. </el-form-item>
  42. </el-col>
  43. <el-col :span="9">
  44. <el-form-item label="是否强制查看" prop="isForce">
  45. <el-radio-group v-model="ruleForm.isForce">
  46. <el-radio :value="1">是</el-radio>
  47. <el-radio :value="0">否</el-radio>
  48. </el-radio-group>
  49. </el-form-item>
  50. </el-col>
  51. </el-row>
  52. <el-form-item label="消息内容" prop="content">
  53. <el-input
  54. style="width: 400px"
  55. v-model="ruleForm.content"
  56. type="textarea"
  57. clearable
  58. />
  59. </el-form-item>
  60. <el-row style="justify-content: center">
  61. <el-form-item>
  62. <el-button @click="resetForm(ruleFormRef)">取消</el-button>
  63. <el-button type="primary" @click="submitForm(ruleFormRef)">
  64. 保存
  65. </el-button>
  66. </el-form-item>
  67. </el-row>
  68. </el-form>
  69. </div>
  70. </template>
  71. <script setup lang="ts">
  72. import { ref, reactive, onMounted } from "vue";
  73. import {ElMessage} from "element-plus"
  74. import type { ComponentSize, FormInstance, FormRules, } from "element-plus";
  75. import api from "@/api";
  76. import router from "@/router";
  77. import { useRoute, useRouter } from "vue-router";
  78. import useUserStore from "@/store/modules/user.ts";
  79. interface RuleForm {
  80. title: string;
  81. sendDeptId: string;
  82. sendDeptName: string;
  83. content: string;
  84. isChildren: string | number;
  85. isForce: string | number;
  86. }
  87. const tableProps = ref({
  88. value: "id",
  89. label: "name",
  90. children: "children",
  91. checkStrictly: true,
  92. });
  93. const route=useRoute()
  94. const myRouter = useRouter()
  95. onMounted(() => {
  96. getInstitutionList(); //机构列表
  97. // const userInfo = useUserStore()
  98. if(route.query?.id){
  99. api.contentApi.noticeGet({id:route.query?.id}).then(res=>{
  100. if(res.code===200){
  101. ruleForm.title=res.data.title
  102. ruleForm.sendDeptId=res.data.sendDeptId
  103. sendDeptId.value=res.data.sendDeptId
  104. ruleForm.isChildren=res.data.isChildren
  105. ruleForm.sendDeptName=res.data.sendDeptName
  106. ruleForm.isForce=res.data.isForce
  107. ruleForm.content=res.data.content
  108. }
  109. })
  110. } else {
  111. ruleForm.sendDeptId = localStorage.getItem('userDeptId')
  112. sendDeptId.value = localStorage.getItem('userDeptId')
  113. }
  114. });
  115. const formSize = ref<ComponentSize>("default");
  116. const ruleFormRef = ref<FormInstance>();
  117. const ruleForm = reactive<RuleForm>({
  118. title: "",
  119. sendDeptId: "",
  120. sendDeptName: "",
  121. isChildren: 0,
  122. isForce: 0,
  123. content: "",
  124. });
  125. const rules = reactive<FormRules<RuleForm>>({
  126. title: [
  127. { required: true, message: "请输入标题", trigger: "blur" },
  128. { max: 50, message: "最大长度不可以超过50个字符", trigger: "blur" },
  129. ],
  130. sendDeptId: [
  131. {
  132. required: true,
  133. message: "请选择发布机构",
  134. trigger: "change",
  135. },
  136. ],
  137. content: [
  138. {
  139. required: true,
  140. message: "请输入消息内容",
  141. trigger: "blur",
  142. },
  143. ],
  144. });
  145. const sendDeptId=ref('')
  146. //机构选择
  147. function deptChange(e:any){
  148. if(e){
  149. sendDeptId.value=e[e.length-1]
  150. ruleForm.sendDeptName=getID(institutionList.value,e[e.length-1]).name
  151. }else{
  152. sendDeptId.value=''
  153. }
  154. }
  155. const submitForm = async (formEl: FormInstance | undefined) => {
  156. if (!formEl) return;
  157. await formEl.validate((valid, fields) => {
  158. if (valid) {
  159. if(route.query?.id){
  160. api.contentApi.noticeEdit({
  161. ...ruleForm,
  162. sendDeptId:sendDeptId.value,
  163. id:route.query?.id
  164. }).then(res=>{
  165. if(res.code===200){
  166. ElMessage.success(res.msg)
  167. router.back()
  168. }
  169. })
  170. }else{
  171. api.contentApi.noticeAdd({
  172. ...ruleForm,
  173. sendDeptId:sendDeptId.value
  174. }).then(res=>{
  175. if(res.code===200){
  176. ElMessage.success(res.msg)
  177. router.back()
  178. }
  179. })
  180. }
  181. }
  182. });
  183. };
  184. const resetForm = (formEl: FormInstance | undefined) => {
  185. // if (!formEl) return;
  186. // formEl.resetFields();
  187. myRouter.push({
  188. path: '/content/message'
  189. })
  190. };
  191. //机构列表
  192. const institutionList = ref([]);
  193. const getInstitutionList = () => {
  194. api.institutionApi.institutionList({}).then((res: any) => {
  195. if (res.code === 200) {
  196. institutionList.value = res.data;
  197. ruleForm.sendDeptName=getID(res.data,ruleForm.sendDeptId).name
  198. console.log(123, getID(res.data,ruleForm.sendDeptId))
  199. }
  200. });
  201. };
  202. function getID(json:any, id:string) {
  203. // var o = {};
  204. // //专门用来保存筛选后的数组
  205. // //定义一个函数getID(json,id),形参json用来接受传入的数组,形参id用来接受id号
  206. // //核心:利用arry.forEach(function(currentValue),index,arr)遍历数组
  207. // // json.forEach(function(item:any) {
  208. // // console.log(123, item)
  209. // // //if用来判断外层,else if用来判断里层,调用递归函数的有2个判断条件在“有goods属性并且不为空”情况下才调用
  210. // // if (item.id == id) {
  211. // // o = item
  212. // // return o;
  213. // // } else if (item.children && item.children.length > 0) {
  214. // // o = getID(item.children, id)//递归
  215. // // }
  216. // // });
  217. // // return o; //全部遍历完之后返回对象o
  218. // for (let i=0; i<json.length-1; i++) {
  219. // if(json[i].id === id) {
  220. // o = json[i]
  221. // break
  222. // } else if(json[i].children && json[i].children.length > 0) {
  223. // getID(json[i].children, id)
  224. // }
  225. // }
  226. // return o;
  227. for (let item of json) {
  228. if (item.id === id) {
  229. return item;
  230. }
  231. if (item.children && item.children.length > 0) {
  232. const found = getID(item.children, id);
  233. if (found) {
  234. return found;
  235. }
  236. }
  237. }
  238. return null;
  239. }
  240. </script>
  241. <style lang="scss" scoped>
  242. .message-main {
  243. flex: 1;
  244. width: 100%;
  245. height: 100%;
  246. background: #fff;
  247. padding: 15px;
  248. box-sizing: border-box;
  249. }
  250. :deep(.el-form-item.is-error .el-input__validateIcon) {
  251. display: none;
  252. }
  253. </style>