com-add-cat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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">
  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. storeId: {
  120. type: Number,
  121. default: 0
  122. },
  123. newCats: {
  124. type: Array,
  125. default: function () {
  126. return []
  127. }
  128. },
  129. isAdd: {
  130. type: Boolean,
  131. default: function () {
  132. return false
  133. }
  134. }
  135. },
  136. data() {
  137. return {
  138. dialogVisible: false,
  139. options: [],// 商品分类列表
  140. children: [],
  141. third: [],
  142. cats: [], //用于前端已选的分类展示
  143. dialogLoading: false,
  144. title: '选择分类',
  145. current1: '',
  146. current2: '',
  147. current3: '',
  148. }
  149. },
  150. methods: {
  151. openDialog() {
  152. let self = this;
  153. this.getCats();
  154. self.title = self.mch_id > 0 ? '选择多商户分类' : '选择分类';
  155. this.cats = [];
  156. this.children = [];
  157. this.third = [];
  158. this.dialogVisible = true;
  159. },
  160. // 获取商品分类
  161. getCats() {
  162. let self = this;
  163. self.dialogLoading = true;
  164. params = {
  165. r: 'store/store-goods/tree'
  166. }
  167. if(self.isAdd){
  168. Object.assign(params, {is_add: 1, store_id: self.storeId})
  169. }
  170. request({
  171. params: params,
  172. method: 'get',
  173. data: {}
  174. }).then(e => {
  175. self.dialogLoading = false;
  176. if (e.data.code === 0) {
  177. self.options = e.data.data.list;
  178. self.setCats();
  179. } else {
  180. self.$message.error(e.data.msg);
  181. }
  182. }).catch(e => {
  183. self.dialogLoading = false;
  184. });
  185. },
  186. setCats() {
  187. let self = this;
  188. self.options.forEach(function (item) {
  189. item.checked = false;
  190. self.newCats.forEach(function (cItem) {
  191. if (item.id === cItem) {
  192. item.checked = true;
  193. self.cats.push({
  194. name: item.name,
  195. id: item.id,
  196. })
  197. }
  198. });
  199. if (item.children) {
  200. item.children.forEach(function (item2) {
  201. item2.checked = false;
  202. self.newCats.forEach(function (cItem) {
  203. if (item2.id === cItem) {
  204. item2.checked = true;
  205. self.cats.push({
  206. name: item2.name,
  207. id: item2.id,
  208. })
  209. }
  210. });
  211. if (item2.children) {
  212. item2.children.forEach(function (item3) {
  213. item3.checked = false;
  214. self.newCats.forEach(function (cItem) {
  215. if (item3.id === cItem) {
  216. item3.checked = true;
  217. self.cats.push({
  218. name: item3.name,
  219. id: item3.id,
  220. })
  221. }
  222. });
  223. })
  224. }
  225. })
  226. }
  227. })
  228. },
  229. deleteCat(option, index) {
  230. let self = this;
  231. self.cats.splice(index, 1);
  232. self.options.forEach(function (item) {
  233. if (item.id === option.id) {
  234. item.checked = false;
  235. }
  236. if (item.children) {
  237. item.children.forEach(function (item2) {
  238. if (item2.id === option.id) {
  239. item2.checked = false
  240. }
  241. if (item2.children) {
  242. item2.children.forEach(function (item3) {
  243. if (item3.id === option.id) {
  244. item3.checked = false
  245. }
  246. })
  247. }
  248. })
  249. }
  250. })
  251. },
  252. optionClick(option) {
  253. let self = this;
  254. let sign = true;
  255. self.cats.forEach(function (item, index) {
  256. // 移除分类
  257. if (option.id === item.id) {
  258. sign = false;
  259. self.cats.splice(index, 1);
  260. }
  261. });
  262. // 新增分类
  263. if (sign && option.checked) {
  264. self.cats.push({
  265. name: option.name,
  266. id: option.id,
  267. })
  268. }
  269. },
  270. // 选择分类
  271. selectCats(option, type) {
  272. let self = this;
  273. if (type === 1) {
  274. self.current1 = option.id;
  275. self.children = [];
  276. if (option.children) {
  277. option.children.forEach(function (item) {
  278. self.children.push(item);
  279. })
  280. }
  281. }
  282. if (type === 2) {
  283. self.current2 = option.id;
  284. self.third = [];
  285. if (option.children) {
  286. option.children.forEach(function (item) {
  287. self.third.push(item);
  288. })
  289. }
  290. }
  291. if (type === 3) {
  292. self.current3 = option.id;
  293. }
  294. },
  295. dialogSubmit() {
  296. this.dialogVisible = false;
  297. this.$emit('select', this.cats)
  298. }
  299. }
  300. })
  301. </script>