sku.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <template>
  2. <view class="sku-view">
  3. <!-- 是否是多规格 -->
  4. <view class="switch-view clearfix">
  5. <view class="float_left">启用多规格</view>
  6. <view class="float_right"><u-switch size="40" v-model="specType"></u-switch></view>
  7. </view>
  8. <view class="sku-name-view" v-if="specType">
  9. <view class="sku-ul">
  10. <view class="sku-li" v-for="(item, index) in spec_group" :key="index">
  11. <view class="sku-label clearfix">
  12. <view class="float_left">
  13. {{ item.groupSpec.specName }}
  14. <text class="custom-icon custom-icon-delete" @click="deletSpecGroup(index)"></text>
  15. </view>
  16. <view class="float_right sku-val-add-btn" @click="openSkuLabelPop('sku_val_pop', index, item.groupSpec.specId)">
  17. <text class="custom-icon custom-icon-xinzeng"></text>
  18. 新增规格值
  19. </view>
  20. </view>
  21. <view class="sku-val-ul">
  22. <view class="sku-val-li" v-for="(ic, inc) in item.params" :key="inc">
  23. {{ ic.specValueName }}
  24. <text class="custom-icon custom-icon-delete" @click="deletSpecValue(index, inc)"></text>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="sku-group" v-if="specType">
  31. <view class="sku-group-tit clearfix">
  32. <text>规格组合</text>
  33. <view class="rest-btn float_right" @click="openSkuLabelPop('sku_label_pop')">添加规格组合</view>
  34. </view>
  35. <!-- 同步规格 -->
  36. <view class="sku-group-main">
  37. <view class="sku-top">
  38. <view class="sku-top-li">
  39. <view class="label">规格图片</view>
  40. <view class="sku-main"><upload :images="specImage ? [specImage] : []" @handleRemove="imgRemoveTotal" @uploadSuccess="uploadSuccessTotal" /></view>
  41. <u-button size="mini" type="primary" @click="synchro('specImage')">同步</u-button>
  42. </view>
  43. <view class="sku-top-li">
  44. <view class="label">商品条码</view>
  45. <view class="input-view">
  46. <view v-for="(item, index) in unit_data" :key="index">
  47. <text>{{ item.unitName }}:</text>
  48. <input type="text" placeholder="请输入" placeholder-class="input-pl" v-model="barCode[item.unitId]" />
  49. </view>
  50. </view>
  51. <u-button size="mini" type="primary" @click="synchro('barCode')">同步</u-button>
  52. </view>
  53. <view class="sku-top-li">
  54. <view class="label">重量/g</view>
  55. <view class="input-view">
  56. <view v-for="(item, index) in unit_data" :key="index">
  57. <text>{{ item.unitName }}:</text>
  58. <input type="digit" placeholder="请输入" placeholder-class="input-pl" v-model="weight[item.unitId]" />
  59. </view>
  60. </view>
  61. <u-button size="mini" type="primary" @click="synchro('weight')">同步</u-button>
  62. </view>
  63. </view>
  64. </view>
  65. <!-- 单个规格 -->
  66. <view class="sku-group-main" v-for="(sku, index) in specMultiple" :key="index">
  67. <view class="sku-group-label">{{ sku.specValueName }}</view>
  68. <view class="sku-top">
  69. <view class="sku-top-li">
  70. <view class="label">规格图片</view>
  71. <view class="sku-main">
  72. <upload :images="sku.specImage ? [sku.specImage] : []" @uploadSuccess="uploadSuccess($event, index)" @handleRemove="imgRemove(index)" />
  73. </view>
  74. </view>
  75. <view class="sku-top-li">
  76. <view class="label">商品条码</view>
  77. <view class="input-view">
  78. <view v-for="(item, index) in unit_data" :key="index">
  79. <text>{{ item.unitName }}:</text>
  80. <input type="text" placeholder="请输入" placeholder-class="input-pl" v-model="sku.barCode[item.unitId]" />
  81. </view>
  82. </view>
  83. </view>
  84. <view class="sku-top-li">
  85. <view class="label">重量/g</view>
  86. <view class="input-view">
  87. <view v-for="(item, index) in unit_data" :key="index">
  88. <text>{{ item.unitName }}:</text>
  89. <input type="digit" placeholder="请输入" placeholder-class="input-pl" v-model="sku.weight[item.unitId]" />
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. <!-- 不启用多规格 单位 -->
  97. <view class="sku-group unit-group" v-else>
  98. <view class="sku-group-main" v-for="(item, index) in unit_data" :key="index">
  99. <view class="sku-group-label">{{ item.unitName }}</view>
  100. <view class="sku-top">
  101. <view class="sku-top-li">
  102. <view class="label">商品条码</view>
  103. <input type="text" placeholder="请输入" placeholder-class="input-pl" v-model="item.barCode" />
  104. </view>
  105. <view class="sku-top-li">
  106. <view class="label">重量/g</view>
  107. <input type="digit" placeholder="请输入" placeholder-class="input-pl" v-model="item.weight" />
  108. </view>
  109. <!-- <view class="sku-top-li">
  110. <view class="label">默认属性</view>
  111. <view class="sku-main">
  112. <u-switch size="30" v-model="item.isDefault" @change='unit'></u-switch>
  113. </view>
  114. </view> -->
  115. </view>
  116. </view>
  117. </view>
  118. <view class="submit-btn"><view class="btn-min" @click="skuConfirm">确定</view></view>
  119. <!-- 选择规格组 -->
  120. <u-popup v-model="sku_label_pop" border-radius="16" mode="bottom">
  121. <view class="pop-view">
  122. <view class="handel-btn clearfix">
  123. <text class="float_left" @click="cancelSkuLabelPop('sku_label_pop')">取消</text>
  124. <text class="float_right" @click="specConfitm">确定</text>
  125. </view>
  126. <view class="pop-cont">
  127. <block v-for="(item, index) in spec_list" :key="index">
  128. <view class="sku-li clearfix" :class="[spec_group.find(its => its.groupSpec.specId === item.id) ? 'sku-on' : '']" @click="changeSpec(item.id)">
  129. <text class="sku-val-label float_left">{{ item.specName }}</text>
  130. <text class="custom-icon custom-icon-radio float_right" style="color: #999999;" v-if="spec_group.find(its => its.groupSpec.specId === item.id)"></text>
  131. <text class="custom-icon float_right" v-else :class="[temporary_spec_id === item.id ? 'custom-icon-xuanze_xuanzhong' : 'custom-icon-xuanze']"></text>
  132. </view>
  133. </block>
  134. </view>
  135. </view>
  136. </u-popup>
  137. <!-- 选择规格值 -->
  138. <u-popup v-model="sku_val_pop" border-radius="16" mode="bottom">
  139. <view class="pop-view">
  140. <view class="handel-btn clearfix">
  141. <text class="float_left" @click="cancelSkuLabelPop('sku_val_pop')">取消</text>
  142. <text class="float_right" @click="specValueConfirm">确定</text>
  143. </view>
  144. <view class="pop-cont">
  145. <view class="sku-li clearfix" v-for="(item, index) in spec_data_child" :key="index" @click="selSpecChild(item.id)">
  146. <text class="sku-val-label float_left">{{ item.specName }}</text>
  147. <text
  148. class="custom-icon float_right"
  149. :class="[temporary_spec_chid.indexOf(item.id) > -1 ? 'custom-icon-xuanze_xuanzhong' : 'custom-icon-weixuanze']"
  150. ></text>
  151. </view>
  152. </view>
  153. </view>
  154. </u-popup>
  155. </view>
  156. </template>
  157. <script>
  158. import upload from '@/components/qiniu/QiniuUpload.vue';
  159. export default {
  160. components: {
  161. upload
  162. },
  163. data() {
  164. return {
  165. specMultiple: [], //组合好的规格明细
  166. spec_group_index: '', // 当前规格下标
  167. spec_data_child: [],
  168. specImage: '',
  169. barCode: {},
  170. weight: {},
  171. specType: false,
  172. current: 0, // tabs组件的current值,表示当前活动的tab选项
  173. sku_label_pop: false,
  174. sku_val_pop: false,
  175. unit_data: [],
  176. spec_list: [],
  177. spec_group: [], // 属性组
  178. temporary_spec_id: '', // 临时存储规格id
  179. temporary_spec_chid: [] // 临时存储属性值id
  180. };
  181. },
  182. watch: {
  183. specType(val) {
  184. if (val) {
  185. this.specGroupRest();
  186. }
  187. }
  188. },
  189. onLoad(options) {
  190. this.specType = parseInt(options.specType) === 2;
  191. this.unit_data = JSON.parse(options.unitData);
  192. // console.log(this.unit_data);
  193. const obj = {};
  194. this.unit_data.forEach(item => {
  195. obj[item.unitId] = '';
  196. item.isDefault = item.isDefault === 5;
  197. });
  198. this.barCode = this.$u.deepClone(obj);
  199. this.weight = this.$u.deepClone(obj);
  200. if (this.specType) {
  201. this.specMultiple = JSON.parse(options.specMultiple).map(item => {
  202. const groupSpecId = item.specGroup.map(itemP => itemP.specId).join('_');
  203. const groupSpecName = item.specGroup.map(itemP => itemP.specName).join('_');
  204. const specValueId = item.specGroup.map(itemP => itemP.specValueId).join('_');
  205. const specValueName = item.specGroup.map(itemP => itemP.specValueName).join('_');
  206. return {
  207. ...item,
  208. groupSpecId: groupSpecId,
  209. groupSpecName: groupSpecName,
  210. specValueId: specValueId,
  211. specValueName: specValueName
  212. };
  213. });
  214. this.spec_group = JSON.parse(options.specGroup);
  215. }
  216. this.getAllSpecManage();
  217. },
  218. methods: {
  219. // 获取属性列表
  220. getAllSpecManage() {
  221. this.$u.api
  222. .getAllSpecManage({
  223. page: 1,
  224. pageSize: 100
  225. })
  226. .then(res => {
  227. this.spec_list = res.data;
  228. });
  229. },
  230. openSkuLabelPop(key, index, specId) {
  231. this[key] = true;
  232. if (key === 'sku_val_pop') {
  233. this.spec_group_index = index;
  234. const obj = this.spec_list.find(item => item.id === specId);
  235. // console.log(obj, specId);
  236. this.spec_data_child = obj.child || [];
  237. this.temporary_spec_chid = this.spec_group[index].params.map(item => item.specValueId);
  238. }
  239. },
  240. cancelSkuLabelPop(key) {
  241. this[key] = false;
  242. },
  243. // 单位切换
  244. tabsChange(index) {
  245. this.current = index;
  246. },
  247. uploadSuccess(imgUrl, index) {
  248. this.specMultiple[index].specImage = imgUrl;
  249. },
  250. imgRemove(index) {
  251. this.specMultiple[index].specImage = '';
  252. },
  253. uploadSuccessTotal(imgUrl) {
  254. this.specImage = imgUrl;
  255. },
  256. imgRemoveTotal(arr) {
  257. this.specImage = '';
  258. },
  259. changeSpec(id) {
  260. if (this.spec_group.find(its => its.groupSpec.specId === id)) {
  261. return;
  262. }
  263. if (this.temporary_spec_id !== id) {
  264. this.temporary_spec_id = id;
  265. }
  266. },
  267. // 选择属性名
  268. specConfitm() {
  269. const obj = this.spec_list.find(item => item.id === this.temporary_spec_id);
  270. if (obj) {
  271. this.spec_group.push({
  272. groupSpec: {
  273. specName: obj.specName,
  274. specId: obj.id
  275. },
  276. params: []
  277. });
  278. }
  279. this.sku_label_pop = false;
  280. this.temporary_spec_id = '';
  281. },
  282. // 选择属性值
  283. selSpecChild(id) {
  284. const index = this.temporary_spec_chid.indexOf(id);
  285. if (index === -1) {
  286. this.temporary_spec_chid.push(id);
  287. } else {
  288. this.temporary_spec_chid.splice(index, 1);
  289. }
  290. },
  291. // 选择属性值确定
  292. specValueConfirm() {
  293. const target = this.$u.deepClone(this.temporary_spec_chid);
  294. this.spec_group[this.spec_group_index].params = target.map(item => {
  295. const specValueName = this.spec_data_child.find(itemf => itemf.id === item).specName;
  296. return {
  297. specValueId: item,
  298. specValueName: specValueName
  299. };
  300. });
  301. this.sku_val_pop = false;
  302. this.temporary_spec_chid = [];
  303. this.specGroupRest();
  304. },
  305. //重排规格组合
  306. specGroupRest() {
  307. // 备份一份规格明细,用来填充重排规格组之前的数据
  308. const cloneSpecGroup = this.$u.deepClone(this.specMultiple);
  309. const skuArr = this.spec_group.map(item => {
  310. item.params = item.params.map(itemP => {
  311. return {
  312. ...itemP,
  313. groupSpecName: item.groupSpec.specName,
  314. groupSpecId: item.groupSpec.specId
  315. };
  316. });
  317. return item.params;
  318. });
  319. this.specMultiple = this.doExchange(skuArr);
  320. this.specMultiple.forEach(item => {
  321. const obj = cloneSpecGroup.find(itemC => itemC.groupSpecId === item.groupSpecId);
  322. if (obj) {
  323. item.barCode = obj.barCode;
  324. item.weight = obj.weight;
  325. item.specImage = obj.specImage;
  326. }
  327. });
  328. },
  329. // 规格组合
  330. doExchange(doubleArrays) {
  331. const len = doubleArrays.length;
  332. if (len >= 2) {
  333. const len1 = doubleArrays[0].length;
  334. const len2 = doubleArrays[1].length;
  335. const newlen = len1 * len2;
  336. const temp = new Array(newlen);
  337. let index = 0;
  338. for (let i = 0; i < len1; i++) {
  339. let item = doubleArrays[0][i];
  340. for (let j = 0; j < len2; j++) {
  341. let item_J = doubleArrays[1][j];
  342. temp[index] = {
  343. groupSpecId: item.groupSpecId + '_' + item_J.groupSpecId,
  344. groupSpecName: item.groupSpecName + '_' + item_J.groupSpecName,
  345. specValueId: item.specValueId + '_' + item_J.specValueId,
  346. specValueName: item.specValueName + '_' + item_J.specValueName
  347. };
  348. index++;
  349. }
  350. }
  351. let newArray = new Array(len - 1);
  352. newArray[0] = temp;
  353. if (len > 2) {
  354. let _count = 1;
  355. for (let i = 2; i < len; i++) {
  356. newArray[_count] = doubleArrays[i];
  357. _count++;
  358. }
  359. }
  360. return this.doExchange(newArray);
  361. } else {
  362. if (!doubleArrays.length) {
  363. return [];
  364. }
  365. const obj = {};
  366. this.unit_data.forEach(item => {
  367. obj[item.unitId] = '';
  368. });
  369. return doubleArrays[0].map(item => {
  370. return {
  371. ...item,
  372. specImage: '',
  373. barCode: this.$u.deepClone(obj),
  374. isDefault: this.$u.deepClone(obj),
  375. weight: this.$u.deepClone(obj)
  376. };
  377. });
  378. }
  379. },
  380. // 删除规格组
  381. deletSpecGroup(index) {
  382. this.spec_group.splice(index, 1);
  383. this.specGroupRest();
  384. },
  385. // 删除规格组
  386. deletSpecValue(index, iVal) {
  387. this.spec_group[index].params.splice(index, 1);
  388. this.specGroupRest();
  389. },
  390. // 同步
  391. synchro(key) {
  392. this.specMultiple.forEach(item => {
  393. item[key] = this[key];
  394. });
  395. },
  396. // 规格确定
  397. skuConfirm() {
  398. if (!this.specType) {
  399. const data = this.unit_data.map(item => {
  400. return {
  401. ...item,
  402. isDefault: item.isDefault ? 5 : 4
  403. };
  404. });
  405. this._prePage().form.unitData = data;
  406. } else {
  407. const specMultiple = this.specMultiple.map(item => {
  408. let specGroup = [];
  409. if (item.groupSpecName.indexOf('_') > -1) {
  410. const specIdArr = item.groupSpecId.split('_');
  411. const specNameArr = item.groupSpecName.split('_');
  412. const specValueNameArr = item.specValueName.split('_');
  413. const specValueIdArr = item.specValueId.split('_');
  414. specGroup = specNameArr.map((im, index) => {
  415. return {
  416. specId: specIdArr[index],
  417. specName: specNameArr[index],
  418. specValueName: specValueNameArr[index],
  419. specValueId: specValueIdArr[index]
  420. };
  421. });
  422. } else {
  423. specGroup = [
  424. {
  425. specId: item.groupSpecId,
  426. specName: item.groupSpecName,
  427. specValueName: item.specValueName,
  428. specValueId: item.specValueId
  429. }
  430. ];
  431. }
  432. return {
  433. specImage: item.specImage,
  434. barCode: item.barCode,
  435. isDefault: item.isDefault,
  436. weight: item.weight,
  437. specGroup: specGroup
  438. };
  439. });
  440. // console.log(specMultiple);
  441. this._prePage().form.specMultiple = specMultiple;
  442. this._prePage().form.specGroup = this.spec_group;
  443. }
  444. this._prePage().form.specType = this.specType ? 2 : 1;
  445. uni.navigateBack();
  446. }
  447. }
  448. };
  449. </script>
  450. <style scoped lang="scss">
  451. .sku-view {
  452. padding-bottom: 140rpx;
  453. .switch-view {
  454. line-height: 100rpx;
  455. padding: 0 24rpx;
  456. background-color: #ffffff;
  457. border-bottom: 1px solid #f5f5f5;
  458. }
  459. }
  460. .sku-name-view {
  461. padding: 0 24rpx;
  462. .sku-ul {
  463. padding-top: 30rpx;
  464. .sku-li {
  465. margin-bottom: 30rpx;
  466. .sku-label {
  467. .float_left {
  468. padding: 0 20rpx;
  469. line-height: 50rpx;
  470. border-radius: 8rpx;
  471. font-size: 24rpx;
  472. display: inline-block;
  473. border: 1px dotted $uni-color-primary;
  474. color: $uni-color-primary;
  475. .custom-icon {
  476. margin-left: 20rpx;
  477. }
  478. }
  479. .sku-val-add-btn {
  480. font-size: 24rpx;
  481. color: $uni-color-primary;
  482. .custom-icon-xinzeng {
  483. font-size: 24rpx;
  484. }
  485. }
  486. }
  487. .sku-val-ul {
  488. padding-top: 14rpx;
  489. .sku-val-li {
  490. padding: 0 20rpx;
  491. line-height: 50rpx;
  492. border-radius: 8rpx;
  493. font-size: 24rpx;
  494. display: inline-block;
  495. background-color: $uni-color-primary;
  496. color: #ffffff;
  497. margin-right: 10rpx;
  498. .custom-icon {
  499. margin-left: 20rpx;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. }
  506. .sku-group {
  507. .sku-group-tit {
  508. padding: 20rpx 24rpx;
  509. line-height: 50rpx;
  510. font-size: 24rpx;
  511. .rest-btn {
  512. line-height: 50rpx;
  513. background-color: $uni-color-primary;
  514. border-radius: 6rpx;
  515. padding: 0 20rpx;
  516. color: #ffffff;
  517. }
  518. }
  519. .sku-group-main {
  520. background-color: #ffffff;
  521. margin-bottom: 30rpx;
  522. .sku-group-label {
  523. line-height: 74rpx;
  524. height: 70rpx;
  525. padding: 0 46rpx;
  526. position: relative;
  527. border-bottom: 1px solid #f5f5f5;
  528. &::before {
  529. content: '';
  530. position: absolute;
  531. display: block;
  532. width: 6rpx;
  533. height: 34rpx;
  534. background-color: $uni-color-primary;
  535. left: 24rpx;
  536. top: 50%;
  537. transform: translateY(-50%);
  538. }
  539. }
  540. .sku-top {
  541. font-size: 24rpx;
  542. padding: 20rpx 0;
  543. display: flex;
  544. .sku-top-li {
  545. flex: 5;
  546. text-align: center;
  547. border-right: 1px solid #f5f5f5;
  548. .label {
  549. .yuan {
  550. color: $uni-color-primary;
  551. }
  552. }
  553. .sku-main {
  554. margin: 20rpx 0;
  555. }
  556. .input-view {
  557. margin: 20rpx 0;
  558. min-height: 128rpx;
  559. view {
  560. padding-bottom: 10rpx;
  561. text {
  562. margin-right: 10rpx;
  563. }
  564. input {
  565. border: 1px solid #f5f5f5;
  566. line-height: 50rpx;
  567. height: 50rpx;
  568. width: 150rpx;
  569. vertical-align: middle;
  570. display: inline-block;
  571. }
  572. }
  573. }
  574. &:last-child {
  575. border-bottom: 0 none;
  576. }
  577. }
  578. }
  579. }
  580. }
  581. .unit-group {
  582. margin-top: 20rpx;
  583. .sku-group-main {
  584. .sku-top {
  585. padding-bottom: 0;
  586. .sku-top-li {
  587. input {
  588. border: 1px solid #f5f5f5;
  589. line-height: 60rpx;
  590. height: 60rpx;
  591. display: block;
  592. margin: 20rpx;
  593. }
  594. .sku-main {
  595. line-height: 60rpx;
  596. height: 60rpx;
  597. margin: 20rpx;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. .pop-view {
  604. .handel-btn {
  605. padding: 0 30rpx;
  606. line-height: 90rpx;
  607. border-bottom: 1px solid #f5f5f5;
  608. .float_right {
  609. color: $uni-color-primary;
  610. }
  611. }
  612. .pop-cont {
  613. max-height: 800rpx;
  614. overflow: auto;
  615. padding: 0 30rpx;
  616. .sku-li {
  617. line-height: 80rpx;
  618. border-bottom: 1px solid #f5f5f5;
  619. .custom-icon {
  620. font-size: 40rpx;
  621. }
  622. .custom-icon-xuanze {
  623. color: #999999;
  624. }
  625. .custom-icon-weixuanze {
  626. color: #999999;
  627. }
  628. .custom-icon-xuanze1 {
  629. color: $uni-color-primary;
  630. }
  631. .custom-icon-radio {
  632. color: $uni-color-primary;
  633. }
  634. }
  635. .sku-on {
  636. color: #999999;
  637. }
  638. }
  639. }
  640. </style>