sku111.vue 14 KB

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