spec.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div v-if="multiSku === 1" style="padding: 16px 0px">
  3. <el-form ref="spec" :model="{ specList }" label-width="80px">
  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" 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="spec.editVals = true">添加规格值</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 }">
  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="height: 40px;overflow: hidden" @change="$refs.sku.validateField([`skuList.${index}.picUrlArr`])" />
  57. </el-form-item>
  58. </td>
  59. <td>
  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>
  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 }}</p>
  72. </el-form-item>
  73. </td>
  74. <td>
  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>
  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, message: '请输入兑换价格', trigger: 'blur' },{ pattern: /^([1-9]\d*)$/, message: "请输入正确的数字", trigger: ["blur", "change"]}]
  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. checkSpecName(spec) {
  180. if (this.specList.filter(item => item.name === spec.name).length > 1) {
  181. this.$message.warning('规格名已存在')
  182. } else {
  183. spec.edit = false
  184. }
  185. },
  186. genSku() {
  187. this.$refs.spec.validate((valid, items) => {
  188. if (valid) {
  189. this.genSkuList()
  190. }
  191. })
  192. },
  193. genSkuList() {
  194. let specificationsTmp = []
  195. this.specList.forEach(spec => {
  196. spec.vals.forEach(val => {
  197. specificationsTmp.push({
  198. specification: spec.name,
  199. value: val
  200. })
  201. })
  202. })
  203. var specValues = []
  204. var spec = specificationsTmp[0].specification
  205. var values = []
  206. values.push(0)
  207. for (var i = 1; i < specificationsTmp.length; i++) {
  208. const aspec = specificationsTmp[i].specification
  209. if (aspec === spec) {
  210. values.push(i)
  211. } else {
  212. specValues.push(values)
  213. spec = aspec
  214. values = []
  215. values.push(i)
  216. }
  217. }
  218. specValues.push(values)
  219. var productsIndex = 0
  220. var skuList = []
  221. var combination = []
  222. var n = specValues.length
  223. for (var s = 0; s < n; s++) {
  224. combination[s] = 0
  225. }
  226. var index = 0
  227. var isContinue = false
  228. do {
  229. var specifications = []
  230. var properties = ''
  231. for (var x = 0; x < n; x++) {
  232. var z = specValues[x][combination[x]]
  233. specifications.push(specificationsTmp[z].value)
  234. properties = `${properties ? (properties + ';') : ''}${specificationsTmp[z].specification}:${specificationsTmp[z].value}`
  235. }
  236. skuList[productsIndex] = {
  237. idx: productsIndex,
  238. name: specifications.toString(),
  239. picUrlArr: [],
  240. valueY: 0.00,
  241. costY: '',
  242. quantity: 0,
  243. status: true,
  244. properties
  245. }
  246. productsIndex++
  247. index++
  248. combination[n - 1] = index
  249. for (var j = n - 1; j >= 0; j--) {
  250. if (combination[j] >= specValues[j].length) {
  251. combination[j] = 0
  252. index = 0
  253. if (j - 1 >= 0) {
  254. combination[j - 1] = combination[j - 1] + 1
  255. }
  256. }
  257. }
  258. isContinue = false
  259. for (var p = 0; p < n; p++) {
  260. if (combination[p] !== 0) {
  261. isContinue = true
  262. }
  263. }
  264. } while (isContinue)
  265. this.skuList = skuList
  266. this.specListTmp = JSON.parse(JSON.stringify(this.specList))
  267. },
  268. getSkuList() {
  269. if (this.skuList.length === 0) {
  270. this.$message.warning('请完善规格并生成sku表格')
  271. return false
  272. }
  273. this.$refs.sku.validate((valid, items) => {
  274. if (valid) {
  275. this.skuList.forEach(item => {
  276. const { valueY, costY } = item
  277. item.value = accMul(valueY, 100)
  278. item.exchangePrice = item.value
  279. item.cost = accMul(costY, 100)
  280. item.picUrl = item.picUrlArr.map(item => { return item.fileName }).toString()
  281. })
  282. this.$emit('valid', this.skuList)
  283. } else {
  284. if (items && Object.keys(items).length>0) {
  285. this.$message({
  286. message: items[Object.keys(items)[0]][0].message,
  287. type: 'warning'
  288. })
  289. }
  290. }
  291. })
  292. }
  293. }
  294. }
  295. </script>
  296. <style lang="scss" scoped>
  297. .spec-table {
  298. border-collapse: collapse;
  299. width: 100%;
  300. line-height: 32px;
  301. color: #606266;
  302. font-size: 14px;
  303. th {
  304. background-color: #ECECEC;
  305. }
  306. td {
  307. text-align: center;
  308. }
  309. .required {
  310. position: relative;
  311. span {
  312. position: relative;
  313. top: -6px;
  314. left: 2px;
  315. font-size: 10px;
  316. color: red;
  317. }
  318. }
  319. }
  320. </style>