sku.vue 16 KB

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