sku副本.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <div class="container add_edit">
  3. <el-form label-width="150px">
  4. <div>
  5. <h3 class="margin_b_20">SKU</h3>
  6. <div>
  7. <el-form-item label="开启SKU">
  8. <el-switch @change="open_switch_sku" v-model="switch_sku" active-color="#ff6a6c"
  9. inactive-color="#dcdfe6"></el-switch>
  10. </el-form-item>
  11. <el-button v-if="skuGroups.length<2 && switch_sku" class="button-new-tag" size="small"
  12. @click="addSKU" style="margin:0 80px 20px;">添加SKU</el-button>
  13. <div v-for="(item,index) in skuGroups">
  14. <el-form-item :label="`编辑SKU${index+1}`" v-if="switch_sku">
  15. <div>
  16. <!-- <el-checkbox @change="open_spec" class="margin_r_10"
  17. v-model="checked_spec"></el-checkbox> -->
  18. <el-input v-model="item.attrName" placeholder="编辑SKU"></el-input>
  19. </div>
  20. <div class="dflex margin_t_20">
  21. <el-tag type="info" class="tag-new-tag" :key="idx" v-for="(tag, idx) in item.attrValues"
  22. :closable="checked_spec" :disable-transitions="false"
  23. @close="handleClose_spec(tag,index,idx)">
  24. {{ tag.attrValue }}
  25. </el-tag>
  26. <el-input class="input-new-tag" v-if="tagindex == item.attrName && inputVisible_spec"
  27. v-model="inputValue_spec" ref="saveTagInput_spec"
  28. @keyup.enter.native="handleInputConfirm_spec(item,index)"
  29. @blur="handleInputConfirm_spec(item,index)"></el-input>
  30. <el-button :disabled="!checked_spec" v-else class="button-new-tag" size="small"
  31. @click="showInput_spec(item,index)">添加</el-button>
  32. </div>
  33. </el-form-item>
  34. </div>
  35. <el-form-item v-if="switch_sku">
  36. <el-table :data="tableSkuData" border>
  37. <el-table-column label="图片" align="center">
  38. <template slot-scope="scope">
  39. <image style="width: 80px;height: 80px;" width="60%" :src="scope.row.skuImg" alt="">
  40. </image>
  41. <el-upload action="" list-type="picture" :on-remove="handleRemove"
  42. :on-change="handleChange" :http-request="
  43. (file) => {
  44. return handleUpload(file, scope.row);
  45. }
  46. ">
  47. <template #trigger>
  48. <view>+</view>
  49. </template>
  50. </el-upload>
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="销售价格" align="center">
  54. <template slot-scope="scope">
  55. <el-input class="sort-container"
  56. oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" min="0.01"
  57. v-model="scope.row.price">
  58. </el-input>
  59. </template>
  60. </el-table-column>
  61. <el-table-column label="市场价格" align="center">
  62. <template slot-scope="scope">
  63. <el-input class="sort-container"
  64. oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" min="0.01"
  65. v-model="scope.row.marketPrice">
  66. </el-input>
  67. </template>
  68. </el-table-column>
  69. <el-table-column label="库存" align="center">
  70. <template slot-scope="scope">
  71. <el-input class="sort-container" type="number" v-model.number="scope.row.stockNum">
  72. </el-input>
  73. </template>
  74. </el-table-column>
  75. <template v-if="skuGroups.length==1">
  76. <el-table-column prop="spec" :label="skuGroups[0].attrName" align="center">
  77. </el-table-column>
  78. </template>
  79. <template v-if="skuGroups.length==2">
  80. <el-table-column prop="spec" :label="skuGroups[0].attrName" align="center">
  81. </el-table-column>
  82. <el-table-column prop="spec2" :label="skuGroups[1].attrName" align="center">
  83. </el-table-column>
  84. </template>
  85. </el-table>
  86. </el-form-item>
  87. </div>
  88. </div>
  89. </el-form>
  90. </div>
  91. </template>
  92. <script>
  93. export default {
  94. data() {
  95. return {
  96. switch_sku: true,
  97. skus: [],
  98. goods: {},
  99. skuGroups: [],
  100. tagindex: "", // 点击那个规格
  101. inputVisible_spec: false,
  102. inputValue_spec: '',
  103. checked_spec: true,
  104. tableSkuData: [],
  105. form: {}
  106. };
  107. },
  108. watch: {
  109. checked_spec: function(arrs) {
  110. for (var i = 0; i < arrs.length; i++) {
  111. for (var j = i + 1; j < arrs.length; j++) {
  112. if (arrs[i] == arrs[j]) {
  113. //第一个等同于第二个,splice方法删除第二个
  114. arrs.splice(j, 1);
  115. j--;
  116. }
  117. }
  118. }
  119. }
  120. },
  121. methods: {
  122. submitData(callback) {
  123. this.skus = [];
  124. console.log(this.tableSkuData)
  125. this.tableSkuData.forEach(x => {
  126. this.skus.push(x);
  127. });
  128. if (typeof callback === 'function') {
  129. callback(
  130. this.skus, this.switch_sku,
  131. JSON.stringify({
  132. })
  133. );
  134. }
  135. },
  136. getData(callback) {
  137. this.submitData(callback);
  138. },
  139. setData(res) {
  140. console.log(res, 'goods')
  141. if (!res.skus) {
  142. this.switch_sku = false
  143. }
  144. this.form = res;
  145. console.log(this.form.skuGroups, 'skus')
  146. this.form.skuGroups.forEach(item => {
  147. item.attrValues.forEach(i => {
  148. if (item.attrName) {
  149. i.attrName = item.attrName
  150. }
  151. })
  152. })
  153. this.skuGroups = this.form.skuGroups
  154. console.log(this.skuGroups, "skuGroups数据");
  155. let skus = this.form.skuGroups
  156. this.tableSkuData = res.skus;
  157. console.log(skus, 'skus', skus[0].attrName)
  158. for (var i = 0; i < this.tableSkuData.length; i++) {
  159. if (this.tableSkuData[i].saleAttrs.length == 1) {
  160. this.tableSkuData[i].spec = this.tableSkuData[i].saleAttrs[0].attrValue
  161. } else if (this.tableSkuData[i].saleAttrs.length == 2) {
  162. this.tableSkuData[i].spec = this.tableSkuData[i].saleAttrs[0].attrValue
  163. this.tableSkuData[i].spec2 = this.tableSkuData[i].saleAttrs[1].attrValue
  164. } else if (this.tableSkuData[i].saleAttrs.length == 0) {
  165. this.tableSkuData[i].spec = ''
  166. }
  167. }
  168. console.log(this.tableSkuData)
  169. var dynamicTags_spec=[]
  170. skus[0].attrValues.forEach(data => {
  171. dynamicTags_spec.push(data.attrValue)
  172. })
  173. this.checked_spec = dynamicTags_spec.length > 0;
  174. },
  175. setGoods(res) {
  176. this.goods = res;
  177. },
  178. affirm() {
  179. var imgsku = ''
  180. if (!this.form.skus) {
  181. if (!this.goods.imgs) {
  182. imgsku = ''
  183. } else {
  184. if (this.goods.imgs.search(',') == (-1)) {
  185. imgsku = this.goods.imgs
  186. } else {
  187. var a = this.goods.imgs.indexOf(',')
  188. imgsku = this.goods.imgs.substring(0, a)
  189. }
  190. }
  191. } else {
  192. imgsku = this.goods.imgs
  193. }
  194. // 规格
  195. this.tableSkuData = []
  196. let arr1 = this.skuGroups[0].attrValues;
  197. let arr2 = this.skuGroups[1].attrValues;
  198. if (arr1.length > 0) {
  199. if (arr2.length > 0) {
  200. arr1.forEach((item, index) => {
  201. arr2.forEach((i, ind) => {
  202. this.tableSkuData.push({
  203. skuId: '',
  204. skuName: item.attrValue + '+' + i.attrValue,
  205. skuDesc: '',
  206. skuImg: imgsku,
  207. price: this.goods.price,
  208. marketPrice: this.goods.marketPrice,
  209. stockNum: this.goods.stockNum,
  210. spec: item.attrValue,
  211. spec_s: index,
  212. spec2: i.attrValue,
  213. spec_s2: ind,
  214. state: '1',
  215. saleAttrs: [],
  216. });
  217. var shuju1=[]
  218. shuju1=this.tableSkuData[index].saleAttrs
  219. shuju1.push({
  220. attrId: '',
  221. skuId: '',
  222. attrName: item.attrName,
  223. attrValue: item.attrValue,
  224. })
  225. var shuju2=[]
  226. shuju2=this.tableSkuData[ind].saleAttrs
  227. shuju2.push({
  228. attrId: '',
  229. skuId: '',
  230. attrName: i.attrName,
  231. attrValue: i.attrValue,
  232. })
  233. })
  234. })
  235. } else {
  236. arr1.forEach((item, index) => {
  237. this.tableSkuData.push({
  238. skuId: '',
  239. skuName: item.attrValue,
  240. skuDesc: '',
  241. skuImg: imgsku,
  242. price: this.goods.price,
  243. marketPrice: this.goods.marketPrice,
  244. stockNum: this.goods.stockNum,
  245. spec: item.attrValue,
  246. spec_s: index,
  247. spec2: "",
  248. spec_s2: "",
  249. state: '1',
  250. saleAttrs: [],
  251. });
  252. var shuju1=[]
  253. shuju1=this.tableSkuData[index].saleAttrs
  254. shuju1.push({
  255. attrId: '',
  256. skuId: '',
  257. attrName: item.attrName,
  258. attrValue: item.attrValue,
  259. })
  260. })
  261. }
  262. } else {
  263. if (arr2.length > 0) {
  264. arr2.forEach((i, ind) => {
  265. this.tableSkuData.push({
  266. skuId: '',
  267. skuName: i.attrValue,
  268. skuDesc: '',
  269. skuImg: imgsku,
  270. price: this.goods.price,
  271. marketPrice: this.goods.marketPrice,
  272. stockNum: this.goods.stockNum,
  273. spec: "",
  274. spec_s: "",
  275. spec2: i.attrValue,
  276. spec_s2: ind,
  277. state: '1',
  278. saleAttrs: [],
  279. })
  280. var shuju2=[]
  281. shuju2=this.tableSkuData[ind].saleAttrs
  282. shuju2.push({
  283. attrId: '',
  284. skuId: '',
  285. attrName: i.attrName,
  286. attrValue: i.attrValue,
  287. })
  288. })
  289. } else {
  290. this.tableSkuData = []
  291. }
  292. }
  293. },
  294. // 添加SKU
  295. addSKU() {
  296. this.skuGroups.push({
  297. attrName: "",
  298. attrValues: []
  299. })
  300. },
  301. //规格
  302. handleClose_spec(tag, index,idx) {
  303. console.log(tag.attrValue,index,idx);
  304. console.log(this.skuGroups);
  305. console.log(this.skuGroups[index].attrValues.indexOf(tag.attrValue))
  306. this.skuGroups[index].attrValues=this.skuGroups[index].attrValues.filter((item,index)=>{
  307. return item.attrValue!=tag.attrValue
  308. }),
  309. this.affirm()
  310. },
  311. showInput_spec(item, index) {
  312. this.inputVisible_spec = true;
  313. this.tagindex = item.attrName;
  314. this.$nextTick(() => {
  315. // console.log(this.$refs.saveTagInput_spec);
  316. this.$refs.saveTagInput_spec[0].focus();
  317. });
  318. },
  319. handleInputConfirm_spec(item) {
  320. console.log(item);
  321. if (this.inputValue_spec) {
  322. if (item.attrValues.find(x => x.attrValue == this.inputValue_spec)) {
  323. this.$message({
  324. message: this.inputValue_spec + '已存在',
  325. type: 'warning'
  326. });
  327. return;
  328. }
  329. let arr = {
  330. attrValue: this.inputValue_spec,
  331. skuIds: "",
  332. attrName: item.attrName,
  333. }
  334. item.attrValues.push(arr);
  335. this.affirm();
  336. console.log(this.skuGroups, "skuGroups数据");
  337. }
  338. this.inputVisible_spec = false;
  339. this.inputValue_spec = '';
  340. },
  341. open_spec() {
  342. if (this.checked_spec) {
  343. return;
  344. };
  345. this.$confirm(`此操作将清除【${this.spec_s}】数据, 是否继续?`, '提示', {
  346. confirmButtonText: '确定',
  347. cancelButtonText: '取消',
  348. type: 'warning'
  349. })
  350. .then(() => {
  351. this.$message({
  352. type: 'success',
  353. message: '清除成功!'
  354. });
  355. this.tableSkuData = [];
  356. })
  357. .catch(() => {
  358. this.checked_spec = true;
  359. });
  360. },
  361. open_switch_sku() {
  362. console.log(this.switch_sku,'是否')
  363. if (this.switch_sku) {
  364. this.setData()
  365. return;
  366. };
  367. this.$confirm(`此操作将清除数据, 是否继续?`, '提示', {
  368. confirmButtonText: '确定',
  369. cancelButtonText: '取消',
  370. type: 'warning'
  371. })
  372. .then(() => {
  373. this.$message({
  374. type: 'success',
  375. message: '清除成功!'
  376. });
  377. })
  378. .catch(() => {
  379. this.switch_sku = true;
  380. });
  381. },
  382. handleChange(file, fileLists) {
  383. },
  384. // 可以获取图片参数(封面图上传图片)
  385. handleUpload(file2, row) {
  386. console.log(file2, "2222", row);
  387. const file = file2.file;
  388. const formData = new FormData();
  389. formData.append('file', file);
  390. // 文件上传
  391. this.$axios.post("/file/open/", formData, {
  392. headers: {
  393. 'Content-type': 'multipart/form-data'
  394. }
  395. }).then(response => {
  396. let res = response
  397. if (res.success) {
  398. row.skuImg = res.data
  399. } else {
  400. uni.showToast({
  401. icon: 'error',
  402. title: '请重新上传!'
  403. });
  404. }
  405. }).catch(res => {});
  406. },
  407. handleRemove(e) {
  408. // 删除文件
  409. this.$axios.delete("/file/open/?url=" + this.tableSkuData.skuImg, {}).then(response => {
  410. let res = response
  411. if (res.success) {
  412. this.tableSkuData.skuImg = ''
  413. } else {
  414. }
  415. }).catch(res => {});
  416. },
  417. imgDelete(e) {
  418. console.log(e)
  419. var img = e.tempFiles[0].file;
  420. const file = img;
  421. const formData = new FormData();
  422. formData.append('file', file);
  423. // 文件上传
  424. this.$axios.post("/file/open/", formData, {
  425. headers: {
  426. 'Content-type': 'multipart/form-data'
  427. }
  428. }).then(response => {
  429. let res = response
  430. if (res.success) {
  431. this.tableSkuData.skuImg = res.data
  432. console.log(this.tableSkuData.skuImg)
  433. } else {
  434. uni.showToast({
  435. icon: 'error',
  436. title: '请重新上传!'
  437. });
  438. }
  439. }).catch(res => {});
  440. },
  441. Delete(e) {
  442. // 删除文件
  443. this.$axios.delete("/file/open/?url=" + this.tableSkuData.skuImg, {}).then(response => {
  444. let res = response
  445. if (res.success) {
  446. this.tableSkuData.skuImg = ''
  447. } else {
  448. }
  449. }).catch(res => {});
  450. },
  451. }
  452. };
  453. </script>
  454. <style>
  455. input[type='file'] {
  456. position: absolute;
  457. top: 0;
  458. right: 0;
  459. bottom: 0;
  460. left: 0;
  461. width: 100%;
  462. cursor: pointer;
  463. opacity: 0;
  464. z-index: 99;
  465. }
  466. </style>
  467. <style>
  468. .tag-new-tag {
  469. margin-left: 24px;
  470. }
  471. .button-new-tag,
  472. .input-new-tag {
  473. margin-left: 24px;
  474. height: 32px;
  475. margin-top: 0;
  476. }
  477. .input-new-tag {
  478. width: 100px;
  479. line-height: 32px;
  480. }
  481. >>>.input-new-tag .el-input__inner {
  482. height: 100%;
  483. }
  484. .el-button--small {
  485. border: 1px solid #dcdfe6;
  486. }
  487. >>>.el-upload-list {
  488. display: none;
  489. }
  490. </style>