spec.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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)" />
  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)">确认</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">重新生成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' }">
  56. <Upload v-model="sku.picUrlArr" :limit="1" :low="true" style="height: 40px;overflow: hidden" />
  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" />
  82. </el-form-item>
  83. </td>
  84. <td style="padding: 0px 10px">
  85. <el-form-item :prop="`skuList.${index}.status`" :rules="{ required: true, message: '请选择状态', trigger: 'blur' }">
  86. <el-switch v-model="sku.status" active-color="#13ce66" inactive-color="#ff4949" />
  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})?|([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 = specObj[p.split(':')[0]]
  130. if (!specObj[key] instanceof Array) {
  131. specObj[key] = []
  132. }
  133. specObj[key].push(p.split(':')[1])
  134. })
  135. })
  136. this.specList = Object.keys(specObj).map(key => {
  137. return {
  138. name: key,
  139. vals: [...new Set(specObj[key])],
  140. edit: false,
  141. editVals: false
  142. }
  143. })
  144. this.specListTmp = JSON.parse(JSON.stringify(this.specList))
  145. this.skuList = sku.map(item => {
  146. const value = accDiv(item.value, 100)
  147. const cost = accDiv(item.cost, 100)
  148. item.value = value
  149. item.cost = cost
  150. return item
  151. })
  152. },
  153. formatObj(properties) {
  154. if (properties) {
  155. let obj = {}
  156. properties.split(';').forEach(item => {
  157. obj[item.split(':')[0]] = item.split(':')[1]
  158. })
  159. return obj
  160. }
  161. return {}
  162. },
  163. checkSpecVal(spec) {
  164. if (!this.specValTmp) {
  165. this.$message.warning('请输入规格值')
  166. return
  167. }
  168. if (spec.vals.find(item => item === this.specValTmp)) {
  169. this.$message.warning('规格值已存在')
  170. return
  171. }
  172. spec.vals.push(this.specValTmp)
  173. this.specValTmp = ''
  174. spec.editVals = false
  175. },
  176. checkSpecName(spec) {
  177. if (this.specList.filter(item => item.name === spec.name).length > 1) {
  178. this.$message.warning('规格名已存在')
  179. } else {
  180. spec.edit = false
  181. }
  182. },
  183. genSku() {
  184. this.$refs.spec.validate((valid, items) => {
  185. if (valid) {
  186. this.genSkuList()
  187. }
  188. })
  189. },
  190. genSkuList() {
  191. let specificationsTmp = []
  192. this.specList.forEach(spec => {
  193. spec.vals.forEach(val => {
  194. specificationsTmp.push({
  195. specification: spec.name,
  196. value: val
  197. })
  198. })
  199. })
  200. var specValues = []
  201. var spec = specificationsTmp[0].specification
  202. var values = []
  203. values.push(0)
  204. for (var i = 1; i < specificationsTmp.length; i++) {
  205. const aspec = specificationsTmp[i].specification
  206. if (aspec === spec) {
  207. values.push(i)
  208. } else {
  209. specValues.push(values)
  210. spec = aspec
  211. values = []
  212. values.push(i)
  213. }
  214. }
  215. specValues.push(values)
  216. var productsIndex = 0
  217. var skuList = []
  218. var combination = []
  219. var n = specValues.length
  220. for (var s = 0; s < n; s++) {
  221. combination[s] = 0
  222. }
  223. var index = 0
  224. var isContinue = false
  225. do {
  226. var specifications = []
  227. var properties = ''
  228. for (var x = 0; x < n; x++) {
  229. var z = specValues[x][combination[x]]
  230. specifications.push(specificationsTmp[z].value)
  231. properties = `${properties ? (properties + ';') : ''}${specificationsTmp[z].specification}:${specificationsTmp[z].value}`
  232. }
  233. skuList[productsIndex] = {
  234. idx: productsIndex,
  235. name: specifications.toString(),
  236. picUrlArr: [],
  237. valueY: 0.00,
  238. costY: '',
  239. quantity: 0,
  240. status: true,
  241. properties
  242. }
  243. productsIndex++
  244. index++
  245. combination[n - 1] = index
  246. for (var j = n - 1; j >= 0; j--) {
  247. if (combination[j] >= specValues[j].length) {
  248. combination[j] = 0
  249. index = 0
  250. if (j - 1 >= 0) {
  251. combination[j - 1] = combination[j - 1] + 1
  252. }
  253. }
  254. }
  255. isContinue = false
  256. for (var p = 0; p < n; p++) {
  257. if (combination[p] !== 0) {
  258. isContinue = true
  259. }
  260. }
  261. } while (isContinue)
  262. this.skuList = skuList
  263. this.specListTmp = JSON.parse(JSON.stringify(this.specList))
  264. },
  265. getSkuList() {
  266. if (this.skuList.length === 0) {
  267. this.$message.warning('请完善规格并生成sku表格')
  268. return false
  269. }
  270. this.$refs.sku.validate((valid, items) => {
  271. if (valid) {
  272. this.skuList.forEach(item => {
  273. const { valueY, costY } = item
  274. item.value = accMul(valueY, 100)
  275. item.exchangePrice = item.value
  276. item.cost = accMul(costY, 100)
  277. item.picUrl = item.picUrlArr.map(item => { return item.fileName }).toString()
  278. })
  279. this.$emit('valid', this.skuList)
  280. } else {
  281. if (items && Object.keys(items).length>0) {
  282. this.$message({
  283. message: items[Object.keys(items)[0]][0].message,
  284. type: 'warning'
  285. })
  286. }
  287. }
  288. })
  289. }
  290. }
  291. }
  292. </script>
  293. <style lang="scss" scoped>
  294. .spec-table {
  295. border-collapse: collapse;
  296. width: 100%;
  297. line-height: 32px;
  298. color: #606266;
  299. font-size: 14px;
  300. th {
  301. background-color: #ECECEC;
  302. }
  303. td {
  304. text-align: center;
  305. }
  306. .required {
  307. position: relative;
  308. span {
  309. position: relative;
  310. top: -6px;
  311. left: 2px;
  312. font-size: 10px;
  313. color: red;
  314. }
  315. }
  316. }
  317. </style>