com-add-cat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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">
  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.value ? true : false}" class="cat-item"
  36. flex="dir:left box:first"
  37. v-for="(option,index) in options"
  38. :key="option.value">
  39. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  40. :label="option.value" size="mini">
  41. <span style="display: none;">{{option.label}}</span>
  42. </el-checkbox>
  43. <div @click="selectCats(option,1)" flex="box:last cross:center">
  44. <span>{{option.label}}</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.value ? true : false}" class="cat-item"
  54. flex="dir:left box:first"
  55. v-for="(option,index) in children"
  56. :key="option.value">
  57. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  58. :label="option.value" size="mini">
  59. <span style="display: none;">{{option.label}}</span>
  60. </el-checkbox>
  61. <div @click="selectCats(option,2)" flex="box:last cross:center">
  62. <span>{{option.label}}</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.value ? true : false}" class="cat-item"
  72. flex="dir:left box:first"
  73. v-for="(option,index) in third"
  74. :key="option.value">
  75. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  76. :label="option.value" size="mini">
  77. <span style="display: none;">{{option.label}}</span>
  78. </el-checkbox>
  79. <div @click="selectCats(option,3)" flex="box:last cross:center">
  80. <span>{{option.label}}</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: 'plugin/o2o_store/store/goods/category/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.value">
  101. {{item.label}}
  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: 'plugin/o2o_store/store/goods/category/options',
  157. mch_id: self.mch_id
  158. },
  159. method: 'get',
  160. data: {}
  161. }).then(e => {
  162. self.dialogLoading = false;
  163. if (e.data.code === 0) {
  164. self.options = e.data.data.list;
  165. self.setCats();
  166. } else {
  167. self.$message.error(e.data.msg);
  168. }
  169. }).catch(e => {
  170. self.dialogLoading = false;
  171. });
  172. },
  173. setCats() {
  174. let self = this;
  175. self.options.forEach(function (item) {
  176. item.checked = false;
  177. self.newCats.forEach(function (cItem) {
  178. if (item.value === cItem) {
  179. item.checked = true;
  180. self.cats.push({
  181. label: item.label,
  182. value: item.value,
  183. })
  184. }
  185. });
  186. if (item.children) {
  187. item.children.forEach(function (item2) {
  188. item2.checked = false;
  189. self.newCats.forEach(function (cItem) {
  190. if (item2.value === cItem) {
  191. item2.checked = true;
  192. self.cats.push({
  193. label: item2.label,
  194. value: item2.value,
  195. })
  196. }
  197. });
  198. if (item2.children) {
  199. item2.children.forEach(function (item3) {
  200. item3.checked = false;
  201. self.newCats.forEach(function (cItem) {
  202. if (item3.value === cItem) {
  203. item3.checked = true;
  204. self.cats.push({
  205. label: item3.label,
  206. value: item3.value,
  207. })
  208. }
  209. });
  210. })
  211. }
  212. })
  213. }
  214. })
  215. },
  216. deleteCat(option, index) {
  217. let self = this;
  218. self.cats.splice(index, 1);
  219. self.options.forEach(function (item) {
  220. if (item.value === option.value) {
  221. item.checked = false;
  222. }
  223. if (item.children) {
  224. item.children.forEach(function (item2) {
  225. if (item2.value === option.value) {
  226. item2.checked = false
  227. }
  228. if (item2.children) {
  229. item2.children.forEach(function (item3) {
  230. if (item3.value === option.value) {
  231. item3.checked = false
  232. }
  233. })
  234. }
  235. })
  236. }
  237. })
  238. },
  239. optionClick(option) {
  240. let self = this;
  241. let sign = true;
  242. self.cats.forEach(function (item, index) {
  243. // 移除分类
  244. if (option.value === item.value) {
  245. sign = false;
  246. self.cats.splice(index, 1);
  247. }
  248. });
  249. // 新增分类
  250. if (sign && option.checked) {
  251. self.cats.push({
  252. label: option.label,
  253. value: option.value,
  254. })
  255. }
  256. },
  257. // 选择分类
  258. selectCats(option, type) {
  259. let self = this;
  260. if (type === 1) {
  261. self.current1 = option.value;
  262. self.children = [];
  263. if (option.children) {
  264. option.children.forEach(function (item) {
  265. self.children.push(item);
  266. })
  267. }
  268. }
  269. if (type === 2) {
  270. self.current2 = option.value;
  271. self.third = [];
  272. if (option.children) {
  273. option.children.forEach(function (item) {
  274. self.third.push(item);
  275. })
  276. }
  277. }
  278. if (type === 3) {
  279. self.current3 = option.value;
  280. }
  281. },
  282. dialogSubmit() {
  283. this.dialogVisible = false;
  284. this.$emit('select', this.cats)
  285. }
  286. }
  287. })
  288. </script>