list.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <a-card size="small" :bordered="false" class="shoppingManagementList-wrap">
  3. <a-spin :spinning="spinning" tip="Loading...">
  4. <!-- 操作按钮 -->
  5. <div ref="tableSearch" class="table-operator" style="border: 0;">
  6. <a-button id="shoppingManagementList-addType" v-if="$hasPermissions('B_addCategory')" type="primary" @click="addCategory('add')">新增类目</a-button>
  7. </div>
  8. <!-- 列表 -->
  9. <a-table
  10. class="sTable fixPagination"
  11. ref="table"
  12. :style="{ height: tableHeight+70+'px'}"
  13. size="small"
  14. :rowKey="(record) => record.categorySn"
  15. rowKeyName="categorySn"
  16. :columns="columns"
  17. :pagination="false"
  18. :dataSource="loadDataList"
  19. :scroll="{ y: tableHeight+70 }"
  20. @expand="expandChild"
  21. :expanded-row-keys.sync="expandedRowKeys"
  22. bordered>
  23. <!-- 图片 -->
  24. <template slot="imgs" slot-scope="text, record">
  25. <div class="addType-img">
  26. <img :src="record.iconUrl" alt="图片走丢啦" width="60" height="60" :id="'shoppingManagementList-img-'+record.id"/>
  27. </div>
  28. </template>
  29. <!-- 状态 -->
  30. <template slot="enabledFlag" slot-scope="text, record">
  31. <a-switch
  32. :id="'shoppingManagementList-enableFlag-'+record.id"
  33. v-if="$hasPermissions('B_shoppingManagement_enabled')"
  34. checkedChildren="启用"
  35. unCheckedChildren="禁用"
  36. v-model="record.enableFlag"
  37. @change="e=>changeFlagHandle(e,record)"/>
  38. <span v-else :style="{color:(record.status==1?'#00aa00':'#999')}"> {{ record.status==1? '已启用': '已禁用' }} </span>
  39. </template>
  40. <!-- 操作 -->
  41. <template slot="action" slot-scope="text, record">
  42. <a-button
  43. size="small"
  44. type="link"
  45. class="button-info"
  46. @click="handleEdit(record,'edit')"
  47. v-if="$hasPermissions('B_addCategory_edit')"
  48. :id="'shoppingManagementList-edit-btn'+record.id">编辑</a-button>
  49. <a-button
  50. size="small"
  51. type="link"
  52. class="button-info"
  53. @click="handleEdit(record,'addChild')"
  54. v-if="$hasPermissions('B_addChildCategory')&&(record.type && record.type=='root')&&(record.shopCategoryCount>=0&&record.shopProductCount==0)"
  55. :id="'shoppingManagementList-addChild-btn'+record.id">添加子类目</a-button>
  56. <a-button
  57. size="small"
  58. type="link"
  59. v-if="$hasPermissions('B_addProduct')&&(record.shopProductCount>=0&&record.shopCategoryCount==0)||record.type!='root'"
  60. class="button-success"
  61. @click="handleAddProduct(record)"
  62. :id="'shoppingManagementList-addProduct-btn'+record.id">添加产品</a-button>
  63. <a-button
  64. size="small"
  65. type="link"
  66. class="button-error"
  67. @click="handleDel(record)"
  68. v-if="$hasPermissions('B_delCategory')&&!record.enableFlag"
  69. :id="'shoppingManagementList-del-btn'+record.id">删除</a-button>
  70. </template>
  71. </a-table>
  72. </a-spin>
  73. <!-- 新增类目 -->
  74. <addCategoryModal
  75. v-drag
  76. ref="categoryModal"
  77. :pageType="pageType"
  78. :openModal="openCategoryModal"
  79. :parentData="parentData"
  80. @ok="handleCategoryOk"
  81. @close="closeCategory"></addCategoryModal>
  82. </a-card>
  83. </template>
  84. <script>
  85. import { commonMixin } from '@/utils/mixin'
  86. // 组件
  87. import { VSelect } from '@/components'
  88. import addCategoryModal from './addCategoryModal.vue'
  89. // 接口
  90. import { shopCategoryList, getChildList, shopCategoryUpdateStatus, shopCategoryDel } from '@/api/shopCategory'
  91. export default {
  92. name: 'ShoppingManagementList',
  93. mixins: [commonMixin],
  94. components: { VSelect, addCategoryModal },
  95. data () {
  96. return {
  97. spinning: false,
  98. tableHeight: 0, // 表格高度
  99. openCategoryModal: false, // 打开新增类目弹窗
  100. // 表头
  101. columns: [
  102. { title: '类目名称', dataIndex: 'categoryName', width: '20%', align: 'left', customRender: function (text) { return text || '--' } },
  103. { title: '图片', scopedSlots: { customRender: 'imgs' }, width: '15%', align: 'center' },
  104. { title: '优先级', dataIndex: 'sort', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
  105. { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
  106. { title: '状态', scopedSlots: { customRender: 'enabledFlag' }, width: '15%', align: 'center' },
  107. { title: '操作', scopedSlots: { customRender: 'action' }, width: '20%', align: 'center' }
  108. ],
  109. parentData: null, // 父级类目一行数据
  110. loadDataList: [], // 表格数据
  111. pageType: null, // 按钮类型
  112. expandedRowKeys: []// 展开的类目sn合集
  113. }
  114. },
  115. methods: {
  116. // 展开子级 获取子级数据
  117. expandChild (expanded, record) {
  118. if (expanded) {
  119. const _this = this
  120. _this.spinning = true
  121. getChildList({ categorySn: record.categorySn }).then(res => {
  122. if (res.status == 200) {
  123. const pos = this.loadDataList.findIndex(item => item.categorySn === record.categorySn)
  124. _this.loadDataList[pos].children = pos != -1 ? (res.data.shopCategoryList && res.data.shopCategoryList.length > 0) ? res.data.shopCategoryList : [] : []
  125. // 处理显示状态
  126. if (_this.loadDataList[pos].children && _this.loadDataList[pos].children.length > 0) {
  127. _this.loadDataList[pos].children.map(con => con.enableFlag = con.status == 1)
  128. }
  129. }
  130. _this.spinning = false
  131. })
  132. }
  133. },
  134. // 新增类目
  135. addCategory (typeVal) {
  136. this.pageType = typeVal
  137. this.openCategoryModal = true
  138. },
  139. // 新增类目成功
  140. handleCategoryOk () {
  141. this.getTabData()
  142. },
  143. // 关闭新增类目弹窗
  144. closeCategory () {
  145. this.pageType = null
  146. this.parentData = null
  147. this.openCategoryModal = false
  148. },
  149. // 编辑 添加子类目
  150. handleEdit (row, typeVal) {
  151. this.pageType = typeVal
  152. this.openCategoryModal = true
  153. this.parentData = row
  154. if (typeVal === 'edit') {
  155. this.$refs.categoryModal.getDetailData({ categorySn: row.categorySn })
  156. }
  157. },
  158. // 更新状态
  159. changeFlagHandle (e, row) {
  160. const _this = this
  161. _this.spinning = true
  162. const params = { categorySn: row.categorySn }
  163. params.status = row.status == 1 ? '0' : '1'
  164. shopCategoryUpdateStatus(params).then(res => {
  165. if (res.status == 200) {
  166. _this.$message.success(res.message)
  167. row.enableFlag = e
  168. _this.spinning = false
  169. } else {
  170. _this.spinning = false
  171. }
  172. })
  173. },
  174. // 添加产品
  175. handleAddProduct (row) {
  176. this.$router.push({ name: 'chooseProductList', params: { sn: row.categorySn } })
  177. },
  178. // 删除
  179. handleDel (row) {
  180. const _this = this
  181. this.$confirm({
  182. title: '提示',
  183. content: '删除后,关联的产品会同步删除,确认删除吗?',
  184. centered: true,
  185. onOk () {
  186. _this.spinning = true
  187. shopCategoryDel({ categorySn: row.categorySn }).then(res => {
  188. if (res.status == 200) {
  189. _this.$message.success(res.message)
  190. if (row.type === 'root') {
  191. const pos = _this.loadDataList.findIndex(item => item.categorySn === row.categorySn)
  192. _this.loadDataList.splice(pos, 1)
  193. } else {
  194. const posNum = _this.loadDataList.findIndex(item => item.categorySn === row.parentSn)
  195. const indexNum = _this.loadDataList[posNum].children.findIndex(con => con.categorySn === row.categorySn)
  196. _this.loadDataList[posNum].children.splice(indexNum, 1)
  197. }
  198. _this.spinning = false
  199. } else {
  200. _this.spinning = false
  201. }
  202. })
  203. }
  204. })
  205. },
  206. // 获取表格数据
  207. getTabData () {
  208. this.spinning = true
  209. shopCategoryList({}).then(res => {
  210. let data
  211. if (res.status == 200) {
  212. data = res.data
  213. for (var i = 0; i < data.length; i++) {
  214. data[i].enableFlag = data[i].status == 1
  215. data[i].children = []
  216. data[i].type = 'root'
  217. }
  218. }
  219. this.loadDataList = res.data
  220. if (this.expandedRowKeys && this.expandedRowKeys.length > 0) {
  221. this.expandedRowKeys.forEach(item => {
  222. this.expandChild(true, { categorySn: item })
  223. })
  224. }
  225. this.spinning = false
  226. })
  227. },
  228. // 初始化
  229. pageInit () {
  230. const _this = this
  231. _this.getTabData()
  232. this.$nextTick(() => { // 页面渲染完成后的回调
  233. _this.setTableH()
  234. })
  235. },
  236. // 计算表格高度
  237. setTableH () {
  238. const tableSearchH = this.$refs.tableSearch.offsetHeight
  239. this.tableHeight = window.innerHeight - tableSearchH - 230
  240. }
  241. },
  242. watch: {
  243. '$store.state.app.winHeight' (newValue, oldValue) { // 窗口变更时,需同时更改表格高度
  244. this.setTableH()
  245. }
  246. },
  247. mounted () {
  248. if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
  249. this.pageInit()
  250. }
  251. },
  252. activated () {
  253. // 如果是新页签打开,则重置当前页面
  254. if (this.$store.state.app.isNewTab) {
  255. this.pageInit()
  256. }
  257. // 仅刷新列表,不重置页面
  258. if (this.$store.state.app.updateList) {
  259. this.pageInit()
  260. }
  261. },
  262. beforeRouteEnter (to, from, next) {
  263. next(vm => {})
  264. }
  265. }
  266. </script>
  267. <style lang="less">
  268. .shoppingManagementList-wrap{
  269. height: 99%;
  270. .addType-img{
  271. img{
  272. object-fit: cover;
  273. }
  274. }
  275. }
  276. </style>