spec.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <div v-if="multiSku === 1" style="padding: 16px 0px">
  3. <el-form ref="spec" :model="{ specList }" :inline="false" label-width="80px" @submit.native.prevent>
  4. <div v-for="(spec, index) in specList" :key="index" style="margin-bottom: 20px">
  5. <el-row style="margin-bottom: 10px">
  6. <el-col :span="8">
  7. <el-form-item :label="`规格项${index + 1}`" :prop="`specList.${index}.name`" :rules="{ required: true, message: '请输入规格名称', trigger: 'blur' }">
  8. <el-input v-model="spec.name" :disabled="!spec.edit" @keyup.enter.native="checkSpecName(spec)" />
  9. </el-form-item>
  10. </el-col>
  11. <el-col :span="16">
  12. <el-button v-if="spec.edit" type="text" style="margin-left: 10px" @click="checkSpecName(spec)">确定</el-button>
  13. <template v-else>
  14. <el-button type="text" style="margin-left: 10px" @click="spec.edit = true">编辑</el-button>
  15. <el-button type="text" class="del" style="margin-left: 10px" @click="specList.splice(index, 1)">删除</el-button>
  16. </template>
  17. </el-col>
  18. </el-row>
  19. <el-row>
  20. <el-form-item :prop="`specList.${index}.vals`" :rules="{ required: true, message: '请完善规格值', trigger: ['blur', 'change'] }">
  21. <el-col :span="24">
  22. <el-tag v-for="(tag, index) in spec.vals" :key="index" closable style="margin-right: 10px" @close="spec.vals.splice(index, 1)">{{ tag }}</el-tag>
  23. <el-input v-if="spec.editVals" v-model="specValTmp" ref="specedit" style="display: inline-block;width: 120px; margin-right: 10px" size="mini" @keyup.enter.native="checkSpecVal(spec, `specList.${index}.vals`)" />
  24. <el-button v-if="!spec.edit&&!spec.editVals" type="text" style="margin-left: 10px" icon="el-icon-plus" size="small" @click="addSpec(spec)">添加规格值</el-button>
  25. <el-button v-if="spec.editVals" type="text" @click="checkSpecVal(spec, `specList.${index}.vals`)">确认</el-button>
  26. </el-col>
  27. </el-form-item>
  28. </el-row>
  29. </div>
  30. </el-form>
  31. <el-button type="primary" size="small" plain @click="specList.push({ name: '', vals: [], edit: true, editVals: false })" :disabled="!!specList.find(item => item.edit)" style="margin-bottom: 10px">添加规格</el-button>
  32. <el-button type="primary" size="small" style="margin-bottom: 10px;margin-left: 10px" :disabled="specList.length === 0 || !!specList.find(item => item.edit)" @click="genSku">{{ skuList.length > 0 ? '重新生成SKU表格' : '生成SKU表格' }}</el-button>
  33. <br>
  34. <el-form ref="sku" :model="{ skuList }" :inline="false">
  35. <table v-if="skuList instanceof Array && skuList.length > 0" class="spec-table" border="1" bordercolor="#CCC">
  36. <tr>
  37. <th :colspan="specListTmp.length">商品规格</th>
  38. <th rowspan="2" class="required"><span>*</span>SKU主图</th>
  39. <th rowspan="2" class="required"><span>*</span>名称</th>
  40. <th rowspan="2" class="required"><span>*</span>价格(元)</th>
  41. <th rowspan="2" class="required"><span>*</span>兑换盲豆数量</th>
  42. <th rowspan="2">采购价格(元)</th>
  43. <th rowspan="2" class="required"><span>*</span>库存</th>
  44. <th rowspan="2">启用</th>
  45. </tr>
  46. <tr>
  47. <th v-for="(spec, index) in specListTmp" :key="index" style="width: 80px">{{ spec.name }}</th>
  48. </tr>
  49. <!-- k1:v1;k2:v2 -->
  50. <tr v-for="(sku, index) in skuList" :key="index">
  51. <td v-for="(spec, i) in specListTmp" :key="i" style="min-width: 50px">
  52. {{ formatObj(sku.properties)[spec.name] }}
  53. </td>
  54. <td>
  55. <el-form-item :prop="`skuList.${index}.picUrlArr`" :rules="{ required: true, message: '请上传SKU图片', trigger: ['blur', 'change'] }">
  56. <Upload v-model="sku.picUrlArr" :limit="1" :low="true" style="width: 100%" @change="$refs.sku.validateField([`skuList.${index}.picUrlArr`])" />
  57. </el-form-item>
  58. </td>
  59. <td class="pd">
  60. <el-form-item :prop="`skuList.${index}.name`" :rules="{ required: true, message: '名称不能为空', trigger: 'blur' }">
  61. <el-input v-model="sku.name" />
  62. </el-form-item>
  63. </td>
  64. <td class="pd">
  65. <el-form-item :prop="`skuList.${index}.valueY`" :rules="valueYRules">
  66. <el-input v-model="sku.valueY" />
  67. </el-form-item>
  68. </td>
  69. <td>
  70. <el-form-item :prop="`skuList.${index}.valueY`" :rules="exchangeRules">
  71. <p>{{ (sku.valueY * 10).toFixed(0) }}</p>
  72. </el-form-item>
  73. </td>
  74. <td class="pd">
  75. <el-form-item :prop="`skuList.${index}.costY`" :rules="costYRules">
  76. <el-input v-model="sku.costY" />
  77. </el-form-item>
  78. </td>
  79. <td class="pd">
  80. <el-form-item :prop="`skuList.${index}.quantity`" :rules="{ required: true, message: '请输入库存', trigger: 'blur' }">
  81. <el-input-number v-model="sku.quantity" :min="0" @change="$event === 0 ? sku.status = false : sku.status = true" />
  82. </el-form-item>
  83. </td>
  84. <td style="padding: 0px 10px">
  85. <el-form-item :prop="`skuList.${index}.status`">
  86. <el-switch v-model="sku.status" active-color="#13ce66" inactive-color="#ff4949" @change="!$event ? sku.quantity = 0 : sku.quantity = 1" />
  87. </el-form-item>
  88. </td>
  89. </tr>
  90. </table>
  91. </el-form>
  92. </div>
  93. </template>
  94. <script>
  95. import Upload from '@/components/ImageUpload'
  96. import { accMul, accDiv } from '@/utils/util'
  97. export default {
  98. name: 'Spec',
  99. components: {
  100. Upload
  101. },
  102. props: {
  103. multiSku: {
  104. type: Number,
  105. default: 0
  106. },
  107. sku: {
  108. type: Array,
  109. default: () => []
  110. }
  111. },
  112. data() {
  113. return {
  114. specList: [],
  115. specListTmp: [],
  116. skuList: [],
  117. specValTmp: '',
  118. specifications: [],
  119. valueYRules: [{ required: true, message: '请输入价格', trigger: 'blur' },{ pattern: /^([1-9]\d*(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1}))))$/, message: "请输入正确的金额,最多两位小数", trigger: ["blur", "change"]}],
  120. costYRules: [{ required: false, message: '请输入采购价格', trigger: 'blur' },{ pattern: /^([1-9]\d*(\.\d{1,2}\d[0])?|0|([0](\.([0][1-9]|[1-9]\d{0,1}))))$/, message: "请输入正确的金额,最多两位小数", trigger: ["blur", "change"] }],
  121. exchangeRules: [{ required: true, trigger: 'blur' }]
  122. }
  123. },
  124. methods: {
  125. setSkuList(sku) {
  126. let specObj = {}
  127. sku.forEach(item => {
  128. item.properties.split(';').forEach(p => {
  129. const key = p.split(':')[0]
  130. // console.log(specObj[key] instanceof Array)
  131. if (!specObj[key]) {
  132. specObj[key] = []
  133. console.log(specObj[key])
  134. }
  135. specObj[key].push(p.split(':')[1])
  136. })
  137. })
  138. this.specList = Object.keys(specObj).map(key => {
  139. return {
  140. name: key,
  141. vals: [...new Set(specObj[key])],
  142. edit: false,
  143. editVals: false
  144. }
  145. })
  146. this.specListTmp = JSON.parse(JSON.stringify(this.specList))
  147. this.skuList = sku.map(item => {
  148. this.$set(item, 'valueY', accDiv(item.value, 100))
  149. this.$set(item, 'costY', accDiv(item.cost, 100))
  150. this.$set(item, 'picUrlArr', item.picUrl.split(',').map(item => { return { fileName: item }}))
  151. this.$set(item, 'status', !!item.quantity)
  152. return item
  153. })
  154. },
  155. formatObj(properties) {
  156. if (properties) {
  157. let obj = {}
  158. properties.split(';').forEach(item => {
  159. obj[item.split(':')[0]] = item.split(':')[1]
  160. })
  161. return obj
  162. }
  163. return {}
  164. },
  165. checkSpecVal(spec, prop) {
  166. if (!this.specValTmp) {
  167. this.$message.warning('请输入规格值')
  168. return
  169. }
  170. if (spec.vals.find(item => item === this.specValTmp)) {
  171. this.$message.warning('规格值已存在')
  172. return
  173. }
  174. spec.vals.push(this.specValTmp)
  175. this.specValTmp = ''
  176. spec.editVals = false
  177. this.$refs.spec.validateField([prop])
  178. },
  179. addSpec(spec) {
  180. spec.editVals = true
  181. console.log(this.$refs.specedit)
  182. setTimeout(() => {
  183. console.log(this.$refs.specedit)
  184. // if (this.$refs.specedit) {
  185. // this.$refs.edittmp.focus()
  186. // }
  187. }, 1000);
  188. },
  189. checkSpecName(spec) {
  190. if (this.specList.filter(item => item.name === spec.name).length > 1) {
  191. this.$message.warning('规格名已存在')
  192. } else {
  193. spec.edit = false
  194. }
  195. },
  196. genSku() {
  197. this.$refs.spec.validate((valid, items) => {
  198. if (valid) {
  199. this.genSkuList()
  200. }
  201. })
  202. },
  203. genSkuList() {
  204. let specificationsTmp = []
  205. this.specList.forEach(spec => {
  206. spec.vals.forEach(val => {
  207. specificationsTmp.push({
  208. specification: spec.name,
  209. value: val
  210. })
  211. })
  212. })
  213. var specValues = []
  214. var spec = specificationsTmp[0].specification
  215. var values = []
  216. values.push(0)
  217. for (var i = 1; i < specificationsTmp.length; i++) {
  218. const aspec = specificationsTmp[i].specification
  219. if (aspec === spec) {
  220. values.push(i)
  221. } else {
  222. specValues.push(values)
  223. spec = aspec
  224. values = []
  225. values.push(i)
  226. }
  227. }
  228. specValues.push(values)
  229. var productsIndex = 0
  230. var skuList = []
  231. var combination = []
  232. var n = specValues.length
  233. for (var s = 0; s < n; s++) {
  234. combination[s] = 0
  235. }
  236. var index = 0
  237. var isContinue = false
  238. do {
  239. var specifications = []
  240. var properties = ''
  241. for (var x = 0; x < n; x++) {
  242. var z = specValues[x][combination[x]]
  243. specifications.push(specificationsTmp[z].value)
  244. properties = `${properties ? (properties + ';') : ''}${specificationsTmp[z].specification}:${specificationsTmp[z].value}`
  245. }
  246. skuList[productsIndex] = {
  247. idx: productsIndex,
  248. name: specifications.toString(),
  249. picUrlArr: [],
  250. valueY: 0.00,
  251. costY: '',
  252. quantity: 0,
  253. status: true,
  254. properties
  255. }
  256. productsIndex++
  257. index++
  258. combination[n - 1] = index
  259. for (var j = n - 1; j >= 0; j--) {
  260. if (combination[j] >= specValues[j].length) {
  261. combination[j] = 0
  262. index = 0
  263. if (j - 1 >= 0) {
  264. combination[j - 1] = combination[j - 1] + 1
  265. }
  266. }
  267. }
  268. isContinue = false
  269. for (var p = 0; p < n; p++) {
  270. if (combination[p] !== 0) {
  271. isContinue = true
  272. }
  273. }
  274. } while (isContinue)
  275. this.skuList = skuList
  276. this.specListTmp = JSON.parse(JSON.stringify(this.specList))
  277. },
  278. getSkuList() {
  279. if (this.skuList.length === 0) {
  280. this.$message.warning('请完善规格并生成sku表格')
  281. return false
  282. }
  283. this.$refs.sku.validate((valid, items) => {
  284. if (valid) {
  285. this.skuList.forEach(item => {
  286. const { valueY, costY } = item
  287. item.value = accMul(valueY, 100)
  288. item.exchangePrice = item.value * 10
  289. item.cost = accMul(costY, 100)
  290. item.picUrl = item.picUrlArr.map(item => { return item.fileName }).toString()
  291. })
  292. this.$emit('valid', this.skuList)
  293. } else {
  294. if (items && Object.keys(items).length>0) {
  295. this.$message({
  296. message: items[Object.keys(items)[0]][0].message,
  297. type: 'warning'
  298. })
  299. }
  300. }
  301. })
  302. }
  303. }
  304. }
  305. </script>
  306. <style lang="scss" scoped>
  307. .spec-table {
  308. border-collapse: collapse;
  309. width: 100%;
  310. line-height: 32px;
  311. color: #606266;
  312. font-size: 14px;
  313. th {
  314. background-color: #ECECEC;
  315. }
  316. td {
  317. text-align: center;
  318. }
  319. .pd {
  320. padding: 15px;
  321. }
  322. .required {
  323. position: relative;
  324. span {
  325. position: relative;
  326. top: -6px;
  327. left: 2px;
  328. font-size: 10px;
  329. color: red;
  330. }
  331. }
  332. }
  333. </style>