spec.vue 12 KB

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