com-add-cat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <style>
  2. .com-add-cat .com-goods-cat-list {
  3. border: 1px solid #E8EAEE;
  4. border-radius: 5px;
  5. margin-top: -5px;
  6. padding: 10px 0;
  7. overflow: scroll;
  8. height: 400px;
  9. }
  10. .com-add-cat .com-goods-cat-list .cat-item {
  11. cursor: pointer;
  12. padding: 5px 10px;
  13. }
  14. .com-add-cat .com-goods-cat-list .active {
  15. background: #FAFAFA;
  16. }
  17. .com-add-cat .el-checkbox {
  18. margin-right: 0;
  19. }
  20. .com-add-cat .tag-box {
  21. margin: 20px 0;
  22. }
  23. .com-add-cat .tag-box .tag-item {
  24. margin-right: 5px;
  25. }
  26. </style>
  27. <template id="com-add-cat">
  28. <div class="com-add-cat">
  29. <el-dialog :title="title" width="1100px" :visible.sync="dialogVisible" :show-close="false" :close-on-click-modal="false">
  30. <el-row v-loading="dialogLoading" :gutter="20" style="margin-top: -30px;">
  31. <template v-if="options.length > 0">
  32. <el-col :span="8">
  33. <h3>一级分类</h3>
  34. <div class="com-goods-cat-list active">
  35. <div :class="{'active': current1 == option.id ? true : false}" class="cat-item"
  36. flex="dir:left box:first"
  37. v-for="(option,index) in options"
  38. :key="option.id">
  39. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  40. :label="option.id" size="mini">
  41. <span style="display: none;">{{option.name}}</span>
  42. </el-checkbox>
  43. <div @click="selectCats(option,1)" flex="box:last cross:center">
  44. <span>{{option.name}}</span>
  45. <i v-if="option.children" class="el-icon-arrow-right"></i>
  46. </div>
  47. </div>
  48. </div>
  49. </el-col>
  50. <el-col :span="8" v-if="children.length > 0">
  51. <h3>二级分类</h3>
  52. <div class="com-goods-cat-list">
  53. <div :class="{'active': current2 == option.id ? true : false}" class="cat-item"
  54. flex="dir:left box:first"
  55. v-for="(option,index) in children"
  56. :key="option.id">
  57. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  58. :label="option.id" size="mini">
  59. <span style="display: none;">{{option.name}}</span>
  60. </el-checkbox>
  61. <div @click="selectCats(option,2)" flex="box:last cross:center">
  62. <span>{{option.name}}</span>
  63. <i v-if="option.children" class="el-icon-arrow-right"></i>
  64. </div>
  65. </div>
  66. </div>
  67. </el-col>
  68. <el-col :span="8" v-if="third.length > 0">
  69. <h3>三级分类</h3>
  70. <div class="com-goods-cat-list">
  71. <div :class="{'active': current3 == option.id ? true : false}" class="cat-item"
  72. flex="dir:left box:first"
  73. v-for="(option,index) in third"
  74. :key="option.id">
  75. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  76. :label="option.id" size="mini">
  77. <span style="display: none;">{{option.name}}</span>
  78. </el-checkbox>
  79. <div @click="selectCats(option,3)" flex="box:last cross:center">
  80. <span>{{option.name}}</span>
  81. <i v-if="option.children" class="el-icon-arrow-right"></i>
  82. </div>
  83. </div>
  84. </div>
  85. </el-col>
  86. </template>
  87. <template v-else>
  88. <div flex="main:center" style="align-items: center;margin-top: 20px;">
  89. <span>无系统分类</span>
  90. <el-button style="display: inline-block;margin-left: 10px" flex="main:center" type="primary"
  91. size="small"
  92. @click="$navigate({r: 'mall/cate/edit'})">
  93. 请先添加商品分类
  94. </el-button>
  95. </div>
  96. </template>
  97. </el-row>
  98. <div class="tag-box" v-if="cats.length > 0">
  99. <el-tag type="warning" class="tag-item" @close="deleteCat(item, index)" closable v-for="(item, index) in cats"
  100. :key="item.id">
  101. {{item.name}}
  102. </el-tag>
  103. </div>
  104. <span slot="footer" class="dialog-footer">
  105. <el-button size='small' @click="dialogVisible = false">取 消</el-button>
  106. <el-button size='small' type="primary" @click="dialogSubmit">确 定</el-button>
  107. </span>
  108. </el-dialog>
  109. </div>
  110. </template>
  111. <script>
  112. Vue.component('com-add-cat', {
  113. template: '#com-add-cat',
  114. props: {
  115. mch_id: {
  116. type: Number,
  117. default: 0
  118. },
  119. newCats: {
  120. type: Array,
  121. default: function () {
  122. return []
  123. }
  124. }
  125. },
  126. data() {
  127. return {
  128. dialogVisible: false,
  129. options: [],// 商品分类列表
  130. children: [],
  131. third: [],
  132. cats: [], //用于前端已选的分类展示
  133. dialogLoading: false,
  134. title: '选择分类',
  135. current1: '',
  136. current2: '',
  137. current3: '',
  138. }
  139. },
  140. methods: {
  141. openDialog() {
  142. let self = this;
  143. this.getCats();
  144. self.title = self.mch_id > 0 ? '选择多商户分类' : '选择分类';
  145. this.cats = [];
  146. this.children = [];
  147. this.third = [];
  148. this.dialogVisible = true;
  149. },
  150. // 获取商品分类
  151. getCats() {
  152. let self = this;
  153. self.dialogLoading = true;
  154. request({
  155. params: {
  156. r: 'mall/cate/tree'
  157. },
  158. method: 'get',
  159. data: {}
  160. }).then(e => {
  161. self.dialogLoading = false;
  162. if (e.data.code === 0) {
  163. self.options = e.data.data.list;
  164. self.setCats();
  165. } else {
  166. self.$message.error(e.data.msg);
  167. }
  168. }).catch(e => {
  169. self.dialogLoading = false;
  170. });
  171. },
  172. setCats() {
  173. let self = this;
  174. self.options.forEach(function (item) {
  175. item.checked = false;
  176. self.newCats.forEach(function (cItem) {
  177. if (item.id === cItem) {
  178. item.checked = true;
  179. self.cats.push({
  180. name: item.name,
  181. id: item.id,
  182. })
  183. }
  184. });
  185. if (item.children) {
  186. item.children.forEach(function (item2) {
  187. item2.checked = false;
  188. self.newCats.forEach(function (cItem) {
  189. if (item2.id === cItem) {
  190. item2.checked = true;
  191. self.cats.push({
  192. name: item2.name,
  193. id: item2.id,
  194. })
  195. }
  196. });
  197. if (item2.children) {
  198. item2.children.forEach(function (item3) {
  199. item3.checked = false;
  200. self.newCats.forEach(function (cItem) {
  201. if (item3.id === cItem) {
  202. item3.checked = true;
  203. self.cats.push({
  204. name: item3.name,
  205. id: item3.id,
  206. })
  207. }
  208. });
  209. })
  210. }
  211. })
  212. }
  213. })
  214. },
  215. deleteCat(option, index) {
  216. let self = this;
  217. self.cats.splice(index, 1);
  218. self.options.forEach(function (item) {
  219. if (item.id === option.id) {
  220. item.checked = false;
  221. }
  222. if (item.children) {
  223. item.children.forEach(function (item2) {
  224. if (item2.id === option.id) {
  225. item2.checked = false
  226. }
  227. if (item2.children) {
  228. item2.children.forEach(function (item3) {
  229. if (item3.id === option.id) {
  230. item3.checked = false
  231. }
  232. })
  233. }
  234. })
  235. }
  236. })
  237. },
  238. optionClick(option) {
  239. let self = this;
  240. let sign = true;
  241. self.cats.forEach(function (item, index) {
  242. // 移除分类
  243. if (option.id === item.id) {
  244. sign = false;
  245. self.cats.splice(index, 1);
  246. }
  247. });
  248. // 新增分类
  249. if (sign && option.checked) {
  250. self.cats.push({
  251. name: option.name,
  252. id: option.id,
  253. })
  254. }
  255. },
  256. // 选择分类
  257. selectCats(option, type) {
  258. let self = this;
  259. if (type === 1) {
  260. self.current1 = option.id;
  261. self.children = [];
  262. if (option.children) {
  263. option.children.forEach(function (item) {
  264. self.children.push(item);
  265. })
  266. }
  267. }
  268. if (type === 2) {
  269. self.current2 = option.id;
  270. self.third = [];
  271. if (option.children) {
  272. option.children.forEach(function (item) {
  273. self.third.push(item);
  274. })
  275. }
  276. }
  277. if (type === 3) {
  278. self.current3 = option.id;
  279. }
  280. },
  281. dialogSubmit() {
  282. this.dialogVisible = false;
  283. this.$emit('select', this.cats)
  284. }
  285. }
  286. })
  287. </script>