ProductAttrDetail.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <template>
  2. <div style="margin-top: 50px">
  3. <el-form :model="value" ref="productAttrForm" label-width="120px" style="width: 720px" size="small">
  4. <el-form-item label="属性类型:">
  5. <el-select v-model="value.productAttributeCategoryId"
  6. placeholder="请选择属性类型"
  7. @change="handleProductAttrChange">
  8. <el-option
  9. v-for="item in productAttributeCategoryOptions"
  10. :key="item.value"
  11. :label="item.label"
  12. :value="item.value">
  13. </el-option>
  14. </el-select>
  15. </el-form-item>
  16. <el-form-item label="商品规格:">
  17. <el-card shadow="never" class="cardBg">
  18. <div v-for="(productAttr,idx) in selectProductAttr">
  19. {{productAttr.name}}:
  20. <el-checkbox-group v-if="productAttr.handAddStatus===0" v-model="selectProductAttr[idx].values">
  21. <el-checkbox v-for="item in getInputListArr(productAttr.inputList)" :label="item" :key="item"
  22. class="littleMarginLeft"></el-checkbox>
  23. </el-checkbox-group>
  24. <div v-else>
  25. <el-checkbox-group v-model="selectProductAttr[idx].values">
  26. <div v-for="(item,index) in selectProductAttr[idx].options" style="display: inline-block"
  27. class="littleMarginLeft">
  28. <el-checkbox :label="item" :key="item"></el-checkbox>
  29. <el-button type="text" class="littleMarginLeft" @click="handleRemoveProductAttrValue(idx,index)">删除
  30. </el-button>
  31. </div>
  32. </el-checkbox-group>
  33. <el-input v-model="addProductAttrValue" style="width: 160px;margin-left: 10px" clearable></el-input>
  34. <el-button class="littleMarginLeft" @click="handleAddProductAttrValue(idx)">增加</el-button>
  35. </div>
  36. </div>
  37. </el-card>
  38. <el-table style="width: 100%;margin-top: 20px"
  39. :data="value.skuStockList"
  40. border>
  41. <el-table-column
  42. v-for="(item,index) in selectProductAttr"
  43. :label="item.name"
  44. :key="item.id"
  45. align="center">
  46. <template slot-scope="scope">
  47. {{getProductSkuSp(scope.row,index)}}
  48. </template>
  49. </el-table-column>
  50. <el-table-column
  51. label="销售价格"
  52. width="80"
  53. align="center">
  54. <template slot-scope="scope">
  55. <el-input v-model="scope.row.price"></el-input>
  56. </template>
  57. </el-table-column>
  58. <el-table-column
  59. label="商品库存"
  60. width="80"
  61. align="center">
  62. <template slot-scope="scope">
  63. <el-input v-model="scope.row.stock"></el-input>
  64. </template>
  65. </el-table-column>
  66. <el-table-column
  67. label="库存预警值"
  68. width="80"
  69. align="center">
  70. <template slot-scope="scope">
  71. <el-input v-model="scope.row.lowStock"></el-input>
  72. </template>
  73. </el-table-column>
  74. <el-table-column
  75. label="SKU编号"
  76. align="center">
  77. <template slot-scope="scope">
  78. <el-input v-model="scope.row.skuCode"></el-input>
  79. </template>
  80. </el-table-column>
  81. <el-table-column
  82. label="操作"
  83. width="80"
  84. align="center">
  85. <template slot-scope="scope">
  86. <el-button
  87. type="text"
  88. @click="handleRemoveProductSku(scope.$index, scope.row)">删除
  89. </el-button>
  90. </template>
  91. </el-table-column>
  92. </el-table>
  93. <el-button
  94. type="primary"
  95. style="margin-top: 20px"
  96. @click="handleRefreshProductSkuList">刷新列表
  97. </el-button>
  98. </el-form-item>
  99. <el-form-item label="属性图片:" v-if="hasAttrPic">
  100. <el-card shadow="never" class="cardBg">
  101. <div v-for="(item,index) in selectProductAttrPics">
  102. <span>{{item.name}}:</span>
  103. <single-upload v-model="item.pic"
  104. style="width: 300px;display: inline-block;margin-left: 10px"></single-upload>
  105. </div>
  106. </el-card>
  107. </el-form-item>
  108. <el-form-item label="商品参数:">
  109. <el-card shadow="never" class="cardBg">
  110. <div v-for="(item,index) in selectProductParam" :class="{littleMarginTop:index!==0}">
  111. <div class="paramInputLabel">{{item.name}}:</div>
  112. <el-select v-if="item.inputType===1" class="paramInput" v-model="selectProductParam[index].value">
  113. <el-option
  114. v-for="item in getParamInputList(item.inputList)"
  115. :key="item"
  116. :label="item"
  117. :value="item">
  118. </el-option>
  119. </el-select>
  120. <el-input v-else class="paramInput" v-model="selectProductParam[index].value"></el-input>
  121. </div>
  122. </el-card>
  123. </el-form-item>
  124. <el-form-item label="商品相册:">
  125. <multi-upload v-model="selectProductPics"></multi-upload>
  126. </el-form-item>
  127. <el-form-item label="规格参数:">
  128. <el-tabs v-model="activeHtmlName" type="card">
  129. <el-tab-pane label="电脑端详情" name="pc">
  130. <tinymce :width="595" :height="300" v-model="value.detailHtml"></tinymce>
  131. </el-tab-pane>
  132. <el-tab-pane label="移动端详情" name="mobile">
  133. <tinymce :width="595" :height="300" v-model="value.detailMobileHtml"></tinymce>
  134. </el-tab-pane>
  135. </el-tabs>
  136. </el-form-item>
  137. <el-form-item style="text-align: center">
  138. <el-button size="medium" @click="handlePrev">上一步,填写商品促销</el-button>
  139. <el-button type="primary" size="medium" @click="handleNext">下一步,选择商品关联</el-button>
  140. </el-form-item>
  141. </el-form>
  142. </div>
  143. </template>
  144. <script>
  145. import {fetchList as fetchProductAttrCateList} from '@/api/productAttrCate'
  146. import {fetchList as fetchProductAttrList} from '@/api/productAttr'
  147. import SingleUpload from '@/components/Upload/singleUpload'
  148. import MultiUpload from '@/components/Upload/multiUpload'
  149. import Tinymce from '@/components/Tinymce'
  150. export default {
  151. name: "ProductAttrDetail",
  152. components: {SingleUpload, MultiUpload, Tinymce},
  153. props: {
  154. value: Object,
  155. isEdit: {
  156. type: Boolean,
  157. default: false
  158. }
  159. },
  160. data() {
  161. return {
  162. //编辑模式时是否初始化成功
  163. hasEditCreated:false,
  164. //商品属性分类下拉选项
  165. productAttributeCategoryOptions: [],
  166. //选中的商品属性
  167. selectProductAttr: [],
  168. //选中的商品参数
  169. selectProductParam: [],
  170. //选中的商品属性图片
  171. selectProductAttrPics: [],
  172. //可手动添加的商品属性
  173. addProductAttrValue: '',
  174. //商品富文本详情激活类型
  175. activeHtmlName: 'pc'
  176. }
  177. },
  178. computed: {
  179. //是否有商品属性图片
  180. hasAttrPic() {
  181. if (this.selectProductAttrPics.length < 1) {
  182. return false;
  183. }
  184. return true;
  185. },
  186. //商品的编号
  187. productId(){
  188. return this.value.id;
  189. },
  190. //商品的主图和画册图片
  191. selectProductPics:{
  192. get:function () {
  193. let pics=[];
  194. if(this.value.pic===undefined||this.value.pic==null||this.value.pic===''){
  195. return pics;
  196. }
  197. pics.push(this.value.pic);
  198. if(this.value.albumPics===undefined||this.value.albumPics==null||this.value.albumPics===''){
  199. return pics;
  200. }
  201. let albumPics = this.value.albumPics.split(',');
  202. for(let i=0;i<albumPics.length;i++){
  203. pics.push(albumPics[i]);
  204. }
  205. return pics;
  206. },
  207. set:function (newValue) {
  208. if (newValue == null || newValue.length === 0) {
  209. this.value.pic = null;
  210. this.value.albumPics = null;
  211. } else {
  212. this.value.pic = newValue[0];
  213. this.value.albumPics = '';
  214. if (newValue.length > 1) {
  215. for (let i = 1; i < newValue.length; i++) {
  216. this.value.albumPics += newValue[i];
  217. if (i !== newValue.length - 1) {
  218. this.value.albumPics += ',';
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. },
  226. created() {
  227. this.getProductAttrCateList();
  228. },
  229. watch: {
  230. productId:function (newValue) {
  231. if(!this.isEdit)return;
  232. if(this.hasEditCreated)return;
  233. if(newValue===undefined||newValue==null||newValue===0)return;
  234. this.handleEditCreated();
  235. }
  236. },
  237. methods: {
  238. handleEditCreated() {
  239. //根据商品属性分类id获取属性和参数
  240. if(this.value.productAttributeCategoryId!=null){
  241. this.handleProductAttrChange(this.value.productAttributeCategoryId);
  242. }
  243. this.hasEditCreated=true;
  244. },
  245. getProductAttrCateList() {
  246. let param = {pageNum: 1, pageSize: 100};
  247. fetchProductAttrCateList(param).then(response => {
  248. this.productAttributeCategoryOptions = [];
  249. let list = response.data.list;
  250. for (let i = 0; i < list.length; i++) {
  251. this.productAttributeCategoryOptions.push({label: list[i].name, value: list[i].id});
  252. }
  253. });
  254. },
  255. getProductAttrList(type, cid) {
  256. let param = {pageNum: 1, pageSize: 100, type: type};
  257. fetchProductAttrList(cid, param).then(response => {
  258. let list = response.data.list;
  259. if (type === 0) {
  260. this.selectProductAttr = [];
  261. for (let i = 0; i < list.length; i++) {
  262. let options = [];
  263. let values = [];
  264. if (this.isEdit) {
  265. if (list[i].handAddStatus === 1) {
  266. //编辑状态下获取手动添加编辑属性
  267. options = this.getEditAttrOptions(list[i].id);
  268. }
  269. //编辑状态下获取选中属性
  270. values = this.getEditAttrValues(i);
  271. }
  272. this.selectProductAttr.push({
  273. id: list[i].id,
  274. name: list[i].name,
  275. handAddStatus: list[i].handAddStatus,
  276. inputList: list[i].inputList,
  277. values: values,
  278. options: options
  279. });
  280. }
  281. if(this.isEdit){
  282. //编辑模式下刷新商品属性图片
  283. this.refreshProductAttrPics();
  284. }
  285. } else {
  286. this.selectProductParam = [];
  287. for (let i = 0; i < list.length; i++) {
  288. let value=null;
  289. if(this.isEdit){
  290. //编辑模式下获取参数属性
  291. value= this.getEditParamValue(list[i].id);
  292. }
  293. this.selectProductParam.push({
  294. id: list[i].id,
  295. name: list[i].name,
  296. value: value,
  297. inputType: list[i].inputType,
  298. inputList: list[i].inputList
  299. });
  300. }
  301. }
  302. });
  303. },
  304. //获取设置的可手动添加属性值
  305. getEditAttrOptions(id) {
  306. let options = [];
  307. for (let i = 0; i < this.value.productAttributeValueList.length; i++) {
  308. let attrValue = this.value.productAttributeValueList[i];
  309. if (attrValue.productAttributeId === id) {
  310. let strArr = attrValue.value.split(',');
  311. for (let j = 0; j < strArr.length; j++) {
  312. options.push(strArr[j]);
  313. }
  314. break;
  315. }
  316. }
  317. return options;
  318. },
  319. //获取选中的属性值
  320. getEditAttrValues(index) {
  321. let values = [];
  322. if (index === 0) {
  323. for (let i = 0; i < this.value.skuStockList.length; i++) {
  324. let sku = this.value.skuStockList[i];
  325. if (sku.sp1 != null && values.indexOf(sku.sp1) === -1) {
  326. values.push(sku.sp1);
  327. }
  328. }
  329. } else if (index === 1) {
  330. for (let i = 0; i < this.value.skuStockList.length; i++) {
  331. let sku = this.value.skuStockList[i];
  332. if (sku.sp2 != null && values.indexOf(sku.sp2) === -1) {
  333. values.push(sku.sp2);
  334. }
  335. }
  336. } else {
  337. for (let i = 0; i < this.value.skuStockList.length; i++) {
  338. let sku = this.value.skuStockList[i];
  339. if (sku.sp3 != null && values.indexOf(sku.sp3) === -1) {
  340. values.push(sku.sp3);
  341. }
  342. }
  343. }
  344. return values;
  345. },
  346. //获取属性的值
  347. getEditParamValue(id){
  348. for(let i=0;i<this.value.productAttributeValueList.length;i++){
  349. if(id===this.value.productAttributeValueList[i].productAttributeId){
  350. return this.value.productAttributeValueList[i].value;
  351. }
  352. }
  353. },
  354. handleProductAttrChange(value) {
  355. this.getProductAttrList(0, value);
  356. this.getProductAttrList(1, value);
  357. },
  358. getInputListArr(inputList) {
  359. return inputList.split(',');
  360. },
  361. handleAddProductAttrValue(idx) {
  362. let options = this.selectProductAttr[idx].options;
  363. if (this.addProductAttrValue == null || this.addProductAttrValue == '') {
  364. this.$message({
  365. message: '属性值不能为空',
  366. type: 'warning',
  367. duration: 1000
  368. });
  369. return
  370. }
  371. if (options.indexOf(this.addProductAttrValue) !== -1) {
  372. this.$message({
  373. message: '属性值不能重复',
  374. type: 'warning',
  375. duration: 1000
  376. });
  377. return;
  378. }
  379. this.selectProductAttr[idx].options.push(this.addProductAttrValue);
  380. this.addProductAttrValue = null;
  381. },
  382. handleRemoveProductAttrValue(idx, index) {
  383. this.selectProductAttr[idx].options.splice(index, 1);
  384. },
  385. getProductSkuSp(row, index) {
  386. if (index === 0) {
  387. return row.sp1;
  388. } else if (index === 1) {
  389. return row.sp2;
  390. } else {
  391. return row.sp3;
  392. }
  393. },
  394. handleRefreshProductSkuList() {
  395. this.$confirm('刷新列表将导致sku信息重新生成,是否要刷新', '提示', {
  396. confirmButtonText: '确定',
  397. cancelButtonText: '取消',
  398. type: 'warning'
  399. }).then(() => {
  400. this.refreshProductAttrPics();
  401. this.refreshProductSkuList();
  402. });
  403. },
  404. refreshProductSkuList() {
  405. this.value.skuStockList = [];
  406. let skuList = this.value.skuStockList;
  407. //只有一个属性时
  408. if (this.selectProductAttr.length === 1) {
  409. let values = this.selectProductAttr[0].values;
  410. for (let i = 0; i < values.length; i++) {
  411. skuList.push({
  412. sp1: values[i]
  413. });
  414. }
  415. } else if (this.selectProductAttr.length === 2) {
  416. let values0 = this.selectProductAttr[0].values;
  417. let values1 = this.selectProductAttr[1].values;
  418. for (let i = 0; i < values0.length; i++) {
  419. if (values1.length === 0) {
  420. skuList.push({
  421. sp1: values0[i]
  422. });
  423. continue;
  424. }
  425. for (let j = 0; j < values1.length; j++) {
  426. skuList.push({
  427. sp1: values0[i],
  428. sp2: values1[j]
  429. });
  430. }
  431. }
  432. } else {
  433. let values0 = this.selectProductAttr[0].values;
  434. let values1 = this.selectProductAttr[1].values;
  435. let values2 = this.selectProductAttr[2].values;
  436. for (let i = 0; i < values0.length; i++) {
  437. if (values1.length === 0) {
  438. skuList.push({
  439. sp1: values0[i]
  440. });
  441. continue;
  442. }
  443. for (let j = 0; j < values1.length; j++) {
  444. if (values2.length === 0) {
  445. skuList.push({
  446. sp1: values0[i],
  447. sp2: values1[j]
  448. });
  449. continue;
  450. }
  451. for (let k = 0; k < values2.length; k++) {
  452. skuList.push({
  453. sp1: values0[i],
  454. sp2: values1[j],
  455. sp3: values2[k]
  456. });
  457. }
  458. }
  459. }
  460. }
  461. },
  462. refreshProductAttrPics() {
  463. this.selectProductAttrPics = [];
  464. if (this.selectProductAttr.length >= 1) {
  465. let values = this.selectProductAttr[0].values;
  466. for (let i = 0; i < values.length; i++) {
  467. let pic=null;
  468. if(this.isEdit){
  469. //编辑状态下获取图片
  470. pic=this.getProductSkuPic(values[i]);
  471. }
  472. this.selectProductAttrPics.push({name: values[i], pic: pic})
  473. }
  474. }
  475. },
  476. //获取商品相关属性的图片
  477. getProductSkuPic(name){
  478. for(let i=0;i<this.value.skuStockList.length;i++){
  479. if(name===this.value.skuStockList[i].sp1){
  480. return this.value.skuStockList[i].pic;
  481. }
  482. }
  483. return null;
  484. },
  485. //合并商品属性
  486. mergeProductAttrValue() {
  487. this.value.productAttributeValueList = [];
  488. for (let i = 0; i < this.selectProductAttr.length; i++) {
  489. let attr = this.selectProductAttr[i];
  490. if (attr.handAddStatus === 1 && attr.options != null && attr.options.length > 0) {
  491. this.value.productAttributeValueList.push({
  492. productAttributeId: attr.id,
  493. value: this.getOptionStr(attr.options)
  494. });
  495. }
  496. }
  497. for (let i = 0; i < this.selectProductParam.length; i++) {
  498. let param = this.selectProductParam[i];
  499. this.value.productAttributeValueList.push({
  500. productAttributeId: param.id,
  501. value: param.value
  502. });
  503. }
  504. },
  505. //合并商品属性图片
  506. mergeProductAttrPics() {
  507. for (let i = 0; i < this.selectProductAttrPics.length; i++) {
  508. for (let j = 0; j < this.value.skuStockList.length; j++) {
  509. if (this.value.skuStockList[j].sp1 === this.selectProductAttrPics[i].name) {
  510. this.value.skuStockList[j].pic = this.selectProductAttrPics[i].pic;
  511. }
  512. }
  513. }
  514. },
  515. getOptionStr(arr) {
  516. let str = '';
  517. for (let i = 0; i < arr.length; i++) {
  518. str += arr[i];
  519. if (i != arr.length - 1) {
  520. str += ',';
  521. }
  522. }
  523. return str;
  524. },
  525. handleRemoveProductSku(index, row) {
  526. let list = this.value.skuStockList;
  527. if (list.length === 1) {
  528. list.pop();
  529. } else {
  530. list.splice(index, 1);
  531. }
  532. },
  533. getParamInputList(inputList) {
  534. return inputList.split(',');
  535. },
  536. handlePrev() {
  537. this.$emit('prevStep')
  538. },
  539. handleNext() {
  540. this.mergeProductAttrValue();
  541. this.mergeProductAttrPics();
  542. this.$emit('nextStep')
  543. }
  544. }
  545. }
  546. </script>
  547. <style scoped>
  548. .littleMarginLeft {
  549. margin-left: 10px;
  550. }
  551. .littleMarginTop {
  552. margin-top: 10px;
  553. }
  554. .paramInput {
  555. width: 250px;
  556. }
  557. .paramInputLabel {
  558. display: inline-block;
  559. width: 100px;
  560. text-align: right;
  561. padding-right: 10px
  562. }
  563. .cardBg {
  564. background: #F8F9FC;
  565. }
  566. </style>