rule.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view>
  3. <h2>管理规格</h2>
  4. <div style="border: 1px solid #000;margin-bottom: 20px;" v-for="(spec, index) in specs" :key="index">
  5. <div
  6. style="display: flex;justify-content: space-between;background: #ddd;height: 40px;align-items: center;padding: 0 20px;box-sizing: border-box;">
  7. <div>
  8. <label :for="`spec-${index}`">规格名称: </label>
  9. <input type="text" :id="`spec-${index}`" v-model="spec.name" @input="rulea" />
  10. <input type="text" :id="`spec-${index}`" v-model="spec.name" @input="rulea" />
  11. <input type="text" :id="`spec-${index}`" v-model="spec.name" @input="rulea" />
  12. <input type="text" :id="`spec-${index}`" v-model="spec.name" @input="rulea" />
  13. </div>
  14. <div>
  15. <button @click="removeSpec(index)" style="margin-right: 20px;">删除规格</button>
  16. <button @click="addValue(index)">添加值</button>
  17. </div>
  18. </div>
  19. <div style="display: flex;flex-wrap: wrap;padding: 0 20px;box-sizing: border-box;">
  20. <div style="margin: 10px 0;" v-for="(value, idx) in spec.values" :key="idx">
  21. <label :for="`value-${index}-${idx}`">规格值: </label>
  22. <input type="text" :id="`value-${idx}`" v-model="spec.values[idx].name" @input="rulea" />
  23. <input type="text" :id="`value-${idx}`" v-model="spec.values[idx].name_en" @input="rulea" />
  24. <input type="text" :id="`value-${idx}`" v-model="spec.values[idx].name_es" @input="rulea" />
  25. <input type="text" :id="`value-${idx}`" v-model="spec.values[idx].name_ita" @input="rulea" />
  26. <button style="margin:0 20px;" @click="removeValue(index,name)">删除值</button>
  27. </div>
  28. </div>
  29. </div>
  30. <button @click="addSpec">添加规格</button>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. selectArr: [],
  38. subIndex: [],
  39. specs: [],
  40. zu: [],
  41. formattedItems: []
  42. }
  43. },
  44. onLoad() {
  45. },
  46. created() {
  47. this.rulea()
  48. },
  49. methods: {
  50. addValue(index) {
  51. // this.specs[index].values.push('');
  52. this.specs[index].values.push('');
  53. },
  54. addSpec() {
  55. this.specs.push({
  56. name: '',
  57. values: []
  58. });
  59. },
  60. rulea() {
  61. var all = []
  62. console.log('specs', this.specs);
  63. this.specs.forEach((item) => {
  64. all.push(item.values)
  65. })
  66. this.$nextTick(() => {
  67. this.zu = this.cartesianProductOf.apply(this, all)
  68. // console.log(('ruleaaaa', this.zu));
  69. if (this.zu) {
  70. this.formatItems();
  71. }
  72. })
  73. },
  74. removeValue(specIndex, valueIndex) {
  75. console.log('specIndex', specIndex);
  76. console.log('valueIndex', valueIndex);
  77. this.specs[specIndex].values.splice(valueIndex, 1);
  78. this.rulea()
  79. },
  80. // 多规格组合
  81. cartesianProductOf() { //笛卡尔积
  82. return Array.prototype.reduce.call(arguments, (a, b) => {
  83. var ret = [];
  84. a.forEach((item) => {
  85. b.forEach((child) => {
  86. // var c = item.concat([child.name])
  87. // console.log(c);
  88. // ret.push({item:c});
  89. ret.push(item.concat([child]))
  90. });
  91. });
  92. console.log('ret', ret);
  93. return ret;
  94. }, [
  95. []
  96. ]);
  97. },
  98. formatItems() {
  99. // 遍历原始数组并转换格式
  100. var obj = {}
  101. this.formattedItems.map(item => {})
  102. console.log('this.formattedItems', this.formattedItems);
  103. let formlist = (JSON.parse(JSON.stringify(this.formattedItems)))
  104. this.formattedItems = []
  105. let resultList = []
  106. this.zu.forEach((item, index) => {
  107. if (this.zu) {
  108. let newlist = []
  109. newlist.push({
  110. height: item.height,
  111. stock_total: item.stock_total,
  112. weight: item.weight,
  113. length: item.length,
  114. width: item.width,
  115. price: item.price,
  116. image: item.image,
  117. is_sales: item.is_sales
  118. })
  119. let itemV0 = []
  120. let itemV1 = []
  121. let itemV2 = []
  122. let itemV3 = []
  123. // resultList.push(itemV)
  124. item.forEach((itemC, indexC) => {
  125. itemV0.push(itemC.name)
  126. itemV1.push(itemC.name_en)
  127. itemV2.push(itemC.name_es)
  128. itemV3.push(itemC.name_ita)
  129. })
  130. console.log('itemV1', itemV0);
  131. console.log('itemV2', itemV1);
  132. console.log('itemV3', itemV2);
  133. console.log('itemV4', itemV3);
  134. this.formattedItems.push({
  135. item: itemV0.join(","),
  136. item_en: itemV1.join(","),
  137. item_es: itemV2.join(","),
  138. item_ita: itemV3.join(","),
  139. // ...newlist[index]
  140. height: '0', // 假设高度为20
  141. stock_total: '0', // 库存
  142. weight: '0', // 重量
  143. length: '0', // 长度
  144. width: '0', // 宽度
  145. price: '0.00', // 价格
  146. image: '', // 图片
  147. is_sales: true, // 是否售卖
  148. });
  149. } else {
  150. this.formattedItems = []
  151. }
  152. });
  153. // this.formattedItems = newlist.concat(this.formattedItems)
  154. console.log('formlist', formlist);
  155. console.log('formattedItems', this.formattedItems);
  156. this.formattedItems.forEach((item, index) => {
  157. formlist.forEach((child, indexC) => {
  158. if (formlist.length == this.formattedItems.length) {
  159. if (index == indexC) {
  160. item.stock_total = child.stock_total // 库存
  161. item.height = child.height // 假设高度为20
  162. item.weight = child.weight // 重量
  163. item.weight = child.weight // 长度
  164. item.width = child.width // 宽度
  165. item.price = child.price // 价格
  166. item.image = child.image // 图片
  167. item.is_sales = child.is_sales // 是否售卖
  168. }
  169. } else {
  170. if (item.item == child.item) {
  171. item.height = child.height // 假设高度为20
  172. item.stock_total = child.stock_total // 库存
  173. item.weight = child.weight // 重量
  174. item.weight = child.weight // 长度
  175. item.width = child.width // 宽度
  176. item.price = child.price // 价格
  177. item.image = child.image // 图片
  178. item.is_sales = child.is_sales // 是否售卖
  179. }
  180. }
  181. })
  182. })
  183. },
  184. }
  185. }
  186. </script>
  187. <style lang="scss">
  188. .avatar {
  189. width: 60%;
  190. float: left;
  191. }
  192. .avatar-uploader .el-upload {
  193. border: 1px dashed #d9d9d9;
  194. border-radius: 6px;
  195. cursor: pointer;
  196. position: relative;
  197. overflow: hidden;
  198. }
  199. .avatar-uploader .el-upload:hover {
  200. border-color: #409EFF;
  201. }
  202. .avatar-uploader-icon {
  203. font-size: 28px;
  204. color: #8c939d;
  205. width: 178px;
  206. height: 178px;
  207. line-height: 178px;
  208. text-align: center;
  209. }
  210. .avatara {
  211. width: 178px;
  212. height: 178px;
  213. display: block;
  214. }
  215. .wp-80 {
  216. width: 80%;
  217. }
  218. .wp-47 {
  219. width: 47%;
  220. }
  221. .mg-auto {
  222. margin: 0 auto;
  223. }
  224. .pd-25 {
  225. padding: 25rpx;
  226. }
  227. .pd-b-20 {
  228. padding: 0 0 20rpx;
  229. }
  230. .pd-tb-40 {
  231. padding: 40rpx 0;
  232. }
  233. .bg-fff {
  234. background-color: #FFFFFF;
  235. }
  236. .radius5 {
  237. border-radius: 5rpx;
  238. }
  239. .radius50 {
  240. border-radius: 50rpx;
  241. }
  242. .flex-wrap {
  243. flex-wrap: wrap;
  244. }
  245. .fss {
  246. display: flex;
  247. justify-content: flex-start;
  248. align-items: flex-start;
  249. }
  250. .fbc {
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. }
  255. .fcc {
  256. display: flex;
  257. justify-content: center;
  258. align-items: center;
  259. }
  260. .safe_area {
  261. margin-bottom: constant(safe-area-inset-bottom);
  262. margin-bottom: env(safe-area-inset-bottom);
  263. }
  264. .dra_position {
  265. position: absolute;
  266. top: 0;
  267. bottom: 0;
  268. width: 100%;
  269. height: auto;
  270. }
  271. .scroll-view-box {
  272. font-size: 28rpx;
  273. bottom: 84rpx;
  274. }
  275. .dra_close {
  276. box-sizing: border-box;
  277. position: absolute;
  278. bottom: 0;
  279. width: 100%;
  280. padding: 15rpx 30rpx 20rpx;
  281. >view {
  282. height: 68rpx;
  283. line-height: 68rpx;
  284. text-align: center;
  285. }
  286. .btns {
  287. color: #a12a30;
  288. border: 1px solid #a12a30;
  289. }
  290. .btns1 {
  291. color: #FFFFFF;
  292. background-color: #a12a30;
  293. }
  294. }
  295. .scroll-view-div {
  296. >view:nth-child(3n-1) {
  297. margin: 0 10rpx;
  298. }
  299. >view {
  300. width: 32%;
  301. height: 72rpx;
  302. margin-bottom: 16rpx;
  303. text-align: center;
  304. background-color: #F5F5F5;
  305. }
  306. .active {
  307. color: #ff000b;
  308. background-color: #fff4f3;
  309. }
  310. }
  311. </style>