index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <zj-tab-page ref="tabPage" :defaultActives="[{ key: 'list', label: $route.meta.title+'-列表', essential: true }]">
  3. <template slot-scope="{activeKey, data}">
  4. <div v-if="activeKey == 'list'" class="app-container">
  5. <div class="btn-group clearfix">
  6. <div class="fl">
  7. <el-button v-if="$restrict('add')" size="small" type="primary" icon="el-icon-plus" @click="addOrEditParent('add1')">添加分类</el-button>
  8. </div>
  9. <div class="fr">
  10. <el-input placeholder="请输入分类名称进行搜索" v-model="screenForm.keyword" size="small" style="width: 240px;" clearable>
  11. <el-button slot="append" icon="el-icon-search" size="small" @click="getListByScreen"></el-button>
  12. </el-input>
  13. </div>
  14. </div>
  15. <div class="table">
  16. <el-table :data="dataList" row-key="categoryId" border default-expand-all :tree-props="{children: 'children'}">
  17. <el-table-column prop="name" label="分类名称" min-width="150"></el-table-column>
  18. <el-table-column align="center" label="分类图片" prop="imgUrl" width="80">
  19. <template slot-scope="scope">
  20. <el-image style="width: 40px; height: 40px; display: block; margin: 0 auto;" :src="scope.row.imgUrl" :preview-src-list="[scope.row.imgUrl]" fit="cover">
  21. <div slot="error" style="height: 100%;font-size: 40px;">
  22. <i class="el-icon-picture-outline"></i>
  23. </div>
  24. </el-image>
  25. </template>
  26. </el-table-column>
  27. <el-table-column align="center" prop="createTime" label="创建时间" min-width="160"></el-table-column>
  28. <!-- <el-table-column align="center" prop="goodsNum" label="商品数"></el-table-column> -->
  29. <el-table-column align="center" prop="sortNum" label="排序"></el-table-column>
  30. <el-table-column align="center" prop="companyName" label="商户" min-width="120"></el-table-column>
  31. <el-table-column align="center" label="状态" class-name="status-col">
  32. <template slot-scope="scope">
  33. <el-tag :type="scope.row.status ? 'success':'danger'">{{ scope.row.status ? '显示':'隐藏' }}</el-tag>
  34. </template>
  35. </el-table-column>
  36. <el-table-column align="right" label="操作" width="150" fixed="right">
  37. <template slot-scope="scope">
  38. <el-button type="primary" size="mini" icon="el-icon-plus" v-if="scope.row.level == 1 && $restrict('add')" @click="addOrEditChild('add2', scope.row.categoryId)"></el-button>
  39. <el-button type="primary" size="mini" icon="el-icon-edit" v-if="scope.row.level == 1 && $restrict('edit')" @click="addOrEditParent('edit1', scope.row.categoryId)"></el-button>
  40. <el-button type="primary" size="mini" icon="el-icon-edit" v-if="scope.row.level == 2 && $restrict('edit')" @click="addOrEditChild('edit2', scope.row.categoryId)"></el-button>
  41. <el-popconfirm v-if="$restrict('del')" style="margin-left: 10px;" title="确定删除吗?" @confirm="handleDelete(scope.row.categoryId)" >
  42. <el-button slot="reference" size="mini" icon="el-icon-delete"></el-button>
  43. </el-popconfirm>
  44. </template>
  45. </el-table-column>
  46. </el-table>
  47. </div>
  48. </div>
  49. <!-- 新增编辑 一级分类 -->
  50. <div v-if="addParentFormVisible" class="app-container">
  51. <el-form ref="addParentForm" :model="addParentForm" :rules="addParentFormRules" label-position="left" label-width="80px">
  52. <el-row :gutter="20">
  53. <el-col :span="6">
  54. <el-form-item label="一级分类" prop="oneClassify">
  55. <el-input type="text" placeholder="请输入一级分类名称" v-model="addParentForm.oneClassify" maxlength="10" show-word-limit style="width: 250px;"></el-input>
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="6">
  59. <el-form-item label="状态" prop="status">
  60. <el-radio-group v-model="addParentForm.status">
  61. <el-radio :label="true">显示</el-radio>
  62. <el-radio :label="false">隐藏</el-radio>
  63. </el-radio-group>
  64. </el-form-item>
  65. </el-col>
  66. <el-col :span="6">
  67. <el-form-item label="排序" prop="sort">
  68. <el-input placeholder="请输入排序" v-model.number="addParentForm.sort" style="width: 250px;"></el-input>
  69. </el-form-item>
  70. </el-col>
  71. <el-col :span="24">
  72. <el-form-item label="分类图片" prop="imgUrl">
  73. <el-upload
  74. class="avatar-uploader"
  75. style="height:122px"
  76. :action="baseURL + 'common/upload'"
  77. :headers="myHeaders"
  78. :show-file-list="false"
  79. :on-success="uploadSuccess"
  80. :before-upload="beforeUpload">
  81. <img v-if="addParentForm.imgUrl" :src="addParentForm.imgUrl" class="avatar">
  82. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  83. </el-upload>
  84. </el-form-item>
  85. </el-col>
  86. </el-row>
  87. </el-form>
  88. <div slot="footer" class="dialog-footer">
  89. <el-button @click="data.removeTab()">取 消</el-button>
  90. <el-button type="primary" @click="submitAddParentForm(data.removeTab)">确 定</el-button>
  91. </div>
  92. </div>
  93. <!-- 新增编辑 二级分类 -->
  94. <div v-if="addChildFormVisible" class="app-container">
  95. <el-form ref="addChildForm" :model="addChildForm" :rules="addChildFormRules" label-position="left" label-width="80px">
  96. <el-row :gutter="20">
  97. <el-col :span="6">
  98. <el-form-item label="一级分类" prop="oneClassify">
  99. <el-select v-model="addChildForm.oneClassify" placeholder="请选择一级分类" disabled style="width: 250px;">
  100. <el-option :label="item.name" :value="item.categoryId" v-for="(item, index) in classifyList" :key="index"></el-option>
  101. </el-select>
  102. </el-form-item>
  103. </el-col>
  104. <el-col :span="6">
  105. <el-form-item label="二级分类" prop="twoClassify">
  106. <el-input type="text" placeholder="请输入二级分类名称" v-model="addChildForm.twoClassify" maxlength="10" show-word-limit style="width: 250px;"></el-input>
  107. </el-form-item>
  108. </el-col>
  109. <el-col :span="6">
  110. <el-form-item label="状态" prop="status">
  111. <el-radio-group v-model="addChildForm.status">
  112. <el-radio :label="true">显示</el-radio>
  113. <el-radio :label="false">隐藏</el-radio>
  114. </el-radio-group>
  115. </el-form-item>
  116. </el-col>
  117. <el-col :span="6">
  118. <el-form-item label="排序" prop="sort">
  119. <el-input placeholder="请输入排序" v-model.number="addChildForm.sort" style="width: 250px;"></el-input>
  120. </el-form-item>
  121. </el-col>
  122. <el-col :span="24">
  123. <el-form-item label="分类图片" prop="imgUrl">
  124. <el-upload
  125. class="avatar-uploader"
  126. style="height:122px"
  127. :action="baseURL + 'common/upload'"
  128. :headers="myHeaders"
  129. :show-file-list="false"
  130. :on-success="uploadSuccess"
  131. :before-upload="beforeUpload">
  132. <img v-if="addChildForm.imgUrl" :src="addChildForm.imgUrl" class="avatar">
  133. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  134. </el-upload>
  135. </el-form-item>
  136. </el-col>
  137. </el-row>
  138. </el-form>
  139. <div slot="footer" class="dialog-footer">
  140. <el-button @click="data.removeTab()">取 消</el-button>
  141. <el-button type="primary" @click="submitAddChildForm(data.removeTab)">确 定</el-button>
  142. </div>
  143. </div>
  144. </template>
  145. </zj-tab-page>
  146. </template>
  147. <script>
  148. import { getToken } from '@/utils/auth'
  149. import { getClassifyList, addClassify, editClassify, deleteClassify, getClassifyDetail, getSmallType } from '@/api/goods'
  150. import { ORDER_MAIN_TYPE } from "@/utils/select_data";
  151. import { findElem } from '@/utils/util.js';
  152. export default {
  153. data() {
  154. return {
  155. baseURL: process.env.VUE_APP_BASE_API,
  156. myHeaders: {'x-token': getToken()},
  157. dataList: [], // 列表数据
  158. listLoading: true, // 列表加载loading
  159. screenForm: { // 筛选表单数据
  160. keyword: '', // 关键词
  161. },
  162. editParentId: null,
  163. addParentFormType: 'add',
  164. addParentFormVisible: false,
  165. addParentForm: {
  166. oneClassify: '',
  167. twoClassify: '',
  168. imgUrl: '',
  169. status: true,
  170. sort: 0,
  171. },
  172. addParentFormRules: {
  173. oneClassify: [
  174. { required: true, message: '请填写一级分类名称', trigger: 'blur' },
  175. ],
  176. imgUrl: [
  177. { required: true, message: '请上传分类图片', trigger: 'change' }
  178. ]
  179. },
  180. editChildId: null,
  181. addChildFormType: 'add',
  182. addChildFormVisible: false,
  183. addChildForm: {
  184. oneClassify: '',
  185. twoClassify: '',
  186. imgUrl: '',
  187. status: true,
  188. sort: 0,
  189. },
  190. addChildFormRules: {
  191. oneClassify: [
  192. { required: true, message: '请选择一级分类', trigger: 'change' }
  193. ],
  194. twoClassify: [
  195. { required: true, message: '请填写二级分类', trigger: 'blur' }
  196. ],
  197. imgUrl: [
  198. { required: true, message: '请上传分类图片', trigger: 'change' }
  199. ],
  200. },
  201. classifyList: [],
  202. orderMainType: ORDER_MAIN_TYPE,
  203. orderSmallType: [],
  204. formType: 'add',
  205. formVisible: false,
  206. }
  207. },
  208. created() {
  209. this.getList();
  210. },
  211. methods: {
  212. getList() {
  213. this.listLoading = true;
  214. let params = {
  215. type: 2,
  216. name: this.screenForm.keyword,
  217. };
  218. getClassifyList(params).then(res => {
  219. this.dataList = res.data;
  220. })
  221. },
  222. // 筛选后重新获取列表
  223. getListByScreen() {
  224. this.currentPage = 1;
  225. this.getList();
  226. },
  227. // 打开 新增编辑 一级分类
  228. addOrEditParent(type, cid) {
  229. this.$refs.tabPage.addTab({
  230. // 对应显示的模块
  231. activeKey: type,
  232. // 唯一标识
  233. key: type,
  234. // 页签名称
  235. label: ({ edit1: "编辑一级分类", add1: "添加一级分类" })[type],
  236. // 打开时事件
  237. triggerEvent: () => {
  238. this.$nextTick(()=>{
  239. this.formType = type
  240. this.formVisible = true
  241. this.addParentFormType = type;
  242. this.addParentFormVisible = true;
  243. if(type == 'edit1'){
  244. this.editParentId = cid;
  245. getClassifyDetail({categoryId: cid}).then(res => {
  246. this.addParentForm = {
  247. oneClassify: res.data.name,
  248. status: res.data.status,
  249. imgUrl: res.data.imgUrl,
  250. sort: res.data.sortNum
  251. }
  252. })
  253. }
  254. })
  255. },
  256. // 关闭时事件
  257. closeEvent: () => {
  258. this.cancelAddParentForm()
  259. }
  260. })
  261. },
  262. // 取消 新增编辑 一级分类
  263. cancelAddParentForm(){
  264. this.addParentFormVisible = false;
  265. this.$refs?.addParentForm?.resetFields();
  266. },
  267. // 提交 一级分类
  268. submitAddParentForm(cancel) {
  269. this.$refs.addParentForm.validate((valid) => {
  270. if (valid) {
  271. let params = {
  272. type: 2,
  273. name: this.addParentForm.oneClassify,
  274. imgUrl: this.addParentForm.imgUrl,
  275. status: this.addParentForm.status,
  276. sortNum: this.addParentForm.sort,
  277. }
  278. if(this.addParentFormType == 'edit1') {
  279. params.categoryId = this.editParentId;
  280. editClassify(params).then(res => {
  281. cancel('list')
  282. this.cancelAddParentForm();
  283. this.getList();
  284. this.$successMsg('编辑成功');
  285. })
  286. }else {
  287. addClassify(params).then(res => {
  288. cancel('list')
  289. this.cancelAddParentForm();
  290. this.getList();
  291. this.$successMsg('添加成功');
  292. })
  293. }
  294. }
  295. })
  296. },
  297. // 改变大类
  298. changeOrderType() {
  299. this.addChildForm.smallType = '';
  300. this.getSmallType();
  301. },
  302. getSmallType() {
  303. getSmallType({
  304. mainNumber: this.addChildForm.orderType,
  305. categoryId: this.addChildForm.oneClassify
  306. }).then(res => {
  307. this.orderSmallType = res.data;
  308. })
  309. },
  310. // 打开 新增编辑 二级分类
  311. addOrEditChild(type, cid) {
  312. this.$refs.tabPage.addTab({
  313. // 对应显示的模块
  314. activeKey: type,
  315. // 唯一标识
  316. key: type,
  317. // 页签名称
  318. label: ({ edit2: "编辑二级分类", add2: "添加二级分类" })[type],
  319. // 打开时事件
  320. triggerEvent: () => {
  321. this.$nextTick(()=>{
  322. this.formType = type
  323. this.formVisible = true
  324. this.addChildFormType = type;
  325. this.addChildFormVisible = true;
  326. getClassifyList({name: '', type: 2}).then(res => {
  327. this.classifyList = res.data;
  328. })
  329. if(type == 'add2') {
  330. this.addChildForm.oneClassify = cid;
  331. }
  332. if(type == 'edit2'){
  333. this.editChildId = cid;
  334. getClassifyDetail({categoryId: cid}).then(res => {
  335. this.addChildForm = {
  336. oneClassify: res.data.parentId,
  337. twoClassify: res.data.name,
  338. imgUrl: res.data.imgUrl,
  339. status: res.data.status,
  340. sort: res.data.sortNum
  341. }
  342. this.getSmallType();
  343. })
  344. }
  345. })
  346. },
  347. // 关闭时事件
  348. closeEvent: () => {
  349. this.cancelAddChildForm()
  350. }
  351. })
  352. },
  353. // 取消 新增编辑 二级分类
  354. cancelAddChildForm(){
  355. this.addChildFormVisible = false;
  356. this.$refs?.addChildForm?.resetFields();
  357. },
  358. // 提交 二级分类
  359. submitAddChildForm(cancel) {
  360. this.$refs.addChildForm.validate((valid) => {
  361. if (valid) {
  362. let params = {}
  363. if(this.addChildFormType == 'edit2') {
  364. params = {
  365. type: 2,
  366. categoryId: this.editChildId,
  367. name: this.addChildForm.twoClassify,
  368. imgUrl: this.addChildForm.imgUrl,
  369. status: this.addChildForm.status,
  370. sortNum: this.addChildForm.sort
  371. }
  372. editClassify(params).then(res => {
  373. cancel('list')
  374. this.cancelAddChildForm();
  375. this.getList();
  376. this.$successMsg('编辑成功');
  377. })
  378. }else {
  379. params = {
  380. children: [{
  381. type: 2,
  382. parentId: this.addChildForm.oneClassify,
  383. name: this.addChildForm.twoClassify,
  384. imgUrl: this.addChildForm.imgUrl,
  385. status: this.addChildForm.status,
  386. sortNum: this.addChildForm.sort
  387. }]
  388. }
  389. addClassify(params).then(res => {
  390. cancel('list')
  391. this.cancelAddChildForm();
  392. this.getList();
  393. this.$successMsg('添加成功');
  394. })
  395. }
  396. }
  397. })
  398. },
  399. // 操作 - 删除
  400. handleDelete(id) {
  401. console.log(id)
  402. deleteClassify({categoryId: id}).then(res => {
  403. this.getList();
  404. this.$successMsg();
  405. })
  406. },
  407. uploadSuccess(res, file, index) {
  408. if (this.addParentFormVisible) {
  409. this.addParentForm.imgUrl = res.data.url
  410. } else {
  411. this.addChildForm.imgUrl = res.data.url
  412. }
  413. },
  414. beforeUpload(file) {
  415. const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  416. const whiteList = ['jpg', 'jpeg', 'png'];
  417. if (whiteList.indexOf(fileSuffix) === -1) {
  418. this.$errorMsg('只支持上传jpg/png文件!');
  419. return false;
  420. }
  421. },
  422. }
  423. }
  424. </script>
  425. <style scoped lang="scss">
  426. </style>
  427. <style lang="scss">
  428. .el-image-viewer__wrapper .el-icon-circle-close {
  429. color: #ffffff !important;
  430. font-size: 60px;
  431. }
  432. .el-table__row.expanded {
  433. background: #f5f5f5;
  434. }
  435. th:first-child, th:last-child {
  436. text-align: center !important;
  437. }
  438. </style>