category-add-or-update.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.categoryId ? '新增'+name : '修改'+name"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible"
  6. >
  7. <p v-if="parentName">{{typeName}}</p>
  8. <el-form
  9. :model="dataForm"
  10. :rules="dataRule"
  11. ref="dataForm"
  12. @keyup.enter.native="dataFormSubmit()"
  13. label-width="80px"
  14. >
  15. <el-form-item
  16. v-if="dataForm.type !== 2"
  17. label="分类名称"
  18. prop="categoryName"
  19. style="width:280px;"
  20. >
  21. <el-input
  22. v-model="dataForm.categoryName"
  23. controls-position="right"
  24. maxlength="8"
  25. show-word-limit
  26. label="分类名称"
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item
  30. label="分类图标"
  31. prop="icon"
  32. v-if="dataForm.grade==0"
  33. >
  34. <img-upload v-model="dataForm.icon" />
  35. </el-form-item>
  36. <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="seq">
  37. <el-input-number v-model="dataForm.seq" controls-position="right" :min="0" label="排序号"></el-input-number>
  38. </el-form-item>
  39. <el-form-item label="状态" size="mini" prop="status">
  40. <el-radio-group v-model="dataForm.status">
  41. <el-radio :label="0">下线</el-radio>
  42. <el-radio :label="1">正常</el-radio>
  43. </el-radio-group>
  44. </el-form-item>
  45. </el-form>
  46. <span slot="footer" class="dialog-footer">
  47. <el-button size="small" @click="visible = false">取消</el-button>
  48. <el-button size="small" type="primary" @click="dataFormSubmit()">确定</el-button>
  49. </span>
  50. </el-dialog>
  51. </template>
  52. <script>
  53. import { treeDataTranslate, idList } from '@/utils'
  54. // import PicUpload from '@/components/pic-upload'
  55. import ImgUpload from '@/components/img-upload'
  56. export default {
  57. data () {
  58. return {
  59. name:'',
  60. visible: false,
  61. dataForm: {
  62. categoryId: 0,
  63. grade: 0,
  64. categoryName: '',
  65. seq: 1,
  66. status: 1,
  67. parentId: 0,
  68. icon: ''
  69. },
  70. dataRule: {
  71. categoryName: [
  72. { required: true, message: '分类名称不能为空', trigger: 'blur' }
  73. ],
  74. icon: [
  75. { required: true, message: '分类图片不能为空', trigger: 'change' }
  76. ]
  77. },
  78. categoryList: [],
  79. selectedCategory: [],
  80. categoryTreeProps: {
  81. value: 'categoryId',
  82. label: 'categoryName'
  83. },
  84. typeName:'',
  85. parentName:'' ,
  86. nameList:['一级分类','二级分类','三级分类']
  87. }
  88. },
  89. components: {
  90. ImgUpload
  91. },
  92. methods: {
  93. resetData(){
  94. this.typeName = ''
  95. this.parentName = ''
  96. this.dataForm = {
  97. categoryId: 0,
  98. grade: 0,
  99. categoryName: '',
  100. seq: 1,
  101. status: 1,
  102. parentId: 0,
  103. icon: ''
  104. }
  105. },
  106. async init (data,type,list) {
  107. this.visible = true
  108. this.resetData()
  109. if(data){
  110. if(type == 'add'){
  111. this.dataForm.categoryId = 0
  112. this.dataForm.parentId = data.categoryID
  113. this.dataForm.grade = data.grade + 1
  114. this.parentName = data.categoryName
  115. console.log('二级三级新增');
  116. }else{
  117. this.dataForm.categoryId = data.categoryID
  118. console.log('yi二级三级修改');
  119. this.parentName =this.getParentName(list,data.categoryID)
  120. console.log(this.parentName);
  121. }
  122. }else{//新增一级分类
  123. this.dataForm.categoryId = 0
  124. console.log('1级新增');
  125. }
  126. if (this.dataForm.categoryId) {
  127. // 修改
  128. await this.$http({
  129. url: this.$http.adornUrl(`/prod/category/info/${this.dataForm.categoryId}`),
  130. method: 'get',
  131. params: this.$http.adornParams()
  132. }).then(({ data }) => {
  133. this.dataForm.categoryId = data.categoryId
  134. this.dataForm.categoryName = data.categoryName
  135. this.dataForm.seq = data.seq
  136. this.dataForm.icon = data.icon
  137. this.dataForm.grade = data.grade
  138. this.dataForm.parentId = data.parentId
  139. this.dataForm.status = data.status
  140. })
  141. }
  142. this.name = this.nameList[this.dataForm.grade]
  143. if(this.dataForm.grade == 1 || this.dataForm.grade == 2){
  144. this.typeName = `所属${this.nameList[this.dataForm.grade-1]}:${this.parentName}`
  145. }
  146. // this.dataForm.categoryId = id || 0
  147. // this.$http({
  148. // url: this.$http.adornUrl('/prod/category/listCategory'),
  149. // method: 'get',
  150. // params: this.$http.adornParams()
  151. // }).then(({ data }) => {
  152. // this.categoryList = treeDataTranslate(data, 'categoryId', 'parentId')
  153. // }).then(() => {
  154. // this.visible = true
  155. // this.$nextTick(() => {
  156. // this.$refs['dataForm'].resetFields()
  157. // this.selectedCategory = []
  158. // })
  159. // }).then(() => {
  160. // if (this.dataForm.categoryId) {
  161. // // 修改
  162. // this.$http({
  163. // url: this.$http.adornUrl(`/prod/category/info/${this.dataForm.categoryId}`),
  164. // method: 'get',
  165. // params: this.$http.adornParams()
  166. // }).then(({ data }) => {
  167. // this.dataForm.categoryId = data.categoryId
  168. // this.dataForm.categoryName = data.categoryName
  169. // this.dataForm.seq = data.seq
  170. // this.dataForm.pic = data.pic
  171. // this.dataForm.parentId = data.parentId
  172. // this.dataForm.status = data.status
  173. // this.selectedCategory = idList(this.categoryList, data.parentId, 'categoryId', 'children').reverse()
  174. // })
  175. // }
  176. // })
  177. },
  178. getParentName(treeData, targetId) {
  179. const idField = 'categoryID'
  180. const nameField = 'categoryName'
  181. const childrenField = 'children'
  182. // 标准化目标ID
  183. const targetValue = typeof targetId === 'object'
  184. ? targetId[idField]
  185. : targetId;
  186. // 递归查找上级
  187. function findParent(data, parent = null) {
  188. for (const node of data) {
  189. // 检查当前节点是否是目标节点
  190. if (node[idField] === targetValue) {
  191. return parent ? parent[nameField] : null;
  192. }
  193. // 如果有子节点,继续查找
  194. if (node[childrenField] && node[childrenField].length > 0) {
  195. const result = findParent(node[childrenField], node);
  196. if (result !== null) {
  197. return result;
  198. }
  199. }
  200. }
  201. // 未找到
  202. return null;
  203. }
  204. return findParent(treeData);
  205. },
  206. handleChange (val) {
  207. this.dataForm.parentId = val[val.length - 1]
  208. },
  209. // 表单提交
  210. dataFormSubmit () {
  211. // if (this.selectedCategory.length === 1) {
  212. // this.dataForm.grade = 0
  213. // }
  214. // if (this.selectedCategory.length === 2) {
  215. // this.dataForm.grade = 1
  216. // }
  217. // if (this.selectedCategory.length === 3) {
  218. // this.dataForm.grade = 2
  219. // }
  220. this.$refs['dataForm'].validate((valid) => {
  221. if (valid) {
  222. this.$http({
  223. url: this.$http.adornUrl(`/prod/category`),
  224. method: this.dataForm.categoryId ? 'put' : 'post',
  225. data: this.$http.adornData({
  226. 'categoryId': this.dataForm.categoryId || undefined,
  227. 'categoryName': this.dataForm.categoryName,
  228. 'status': this.dataForm.status,
  229. 'seq': this.dataForm.seq,
  230. 'grade': this.dataForm.grade,
  231. 'parentId': this.dataForm.parentId,
  232. 'icon': this.dataForm.icon
  233. })
  234. }).then(({ data }) => {
  235. this.$message({
  236. message: '操作成功',
  237. type: 'success',
  238. duration: 1500,
  239. onClose: () => {
  240. this.visible = false
  241. this.$emit('refreshDataList')
  242. }
  243. })
  244. })
  245. }
  246. })
  247. }
  248. }
  249. }
  250. </script>