spec.vue 13 KB

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