app-attr.vue 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. <template>
  2. <view class="app-attr">
  3. <view @click="alert">
  4. <slot name="button"></slot>
  5. </view>
  6. <view class="modal" v-if="display === 'block'" @click="close" >
  7. <view class="safe-area-inset-bottom u-attr-fixed" >
  8. <view class="content" @tap.native.stop="preventD">
  9. <image src="../../../static/image/icon/close.png" class="close" @click="close"></image>
  10. <view class="first dir-left-nowrap" :class="sign =='wholesale' ? 'no-border' : ''">
  11. <view class="box-grow-0 img" @click="clickImg(attrPic)">
  12. <app-image :img-src="attrPic" width="100%" height="100%"></app-image>
  13. </view>
  14. <view class="info">
  15. <view class="dir-left-nowrap cross-center" :class="priceColor">
  16. <view class="dir-left-nowrap cross-center">
  17. <view class="dir-left-nowrap cross-center" :class="theme+'-m-text ' + theme" v-if="selectAttr.extra || goods.extra">
  18. <view>{{selectAttr.extra ? selectAttr.extra.value + selectAttr.extra.name :
  19. goods.extra.value + goods.extra.name}}
  20. </view>
  21. <view v-if="!(goods.sign === 'integral_mall' && attrPrice == 0)">+</view>
  22. </view>
  23. <app-price :theme="theme" v-if="!(goods.sign === 'integral_mall' && attrPrice == 0) && sign != 'wholesale'" type="text-price-all" :price="attrPrice"
  24. :default-price="goods.price"></app-price>
  25. <app-price :theme="theme" v-if="sign == 'wholesale'" type="text-price-all" :price="attrPrice" :max="goods.level_show == 1 ? goods.price_member_max : goods.price_max" :min="goods.level_show == 1 ? goods.price_member_min : goods.price_min"></app-price>
  26. </view>
  27. <view v-if="goods.level_show === 1 && (selectAttr || sign == 'wholesale')">
  28. <app-member-mark :theme="themeObject"></app-member-mark>
  29. </view>
  30. </view>
  31. <view class="stock">库存:{{attrNum}}</view>
  32. </view>
  33. </view>
  34. <view class="second" :class="sign =='wholesale' ? 'no-padding' : ''">
  35. <slot name="extra"></slot>
  36. <view class="wholesale" v-if="sign =='wholesale'">
  37. <view class="wholesale-attr-list dir-left-nowrap cross-center" v-if="index != goods.attr_groups.length -1" v-for="(item, index) in goods.attr_groups" :key="index">
  38. <view class="wholesale-attr-group t-omit-two">{{item.attr_group_name}}</view>
  39. <scroll-view @scroll="scrollGet($event,index)" :scroll-left="item.scrollLeft" class="wholesale-attr-item" scroll-x="true">
  40. <view class="attr-name" v-for="(attr, key) in item.attr_list" :key="key" :class="attr.active ? theme + '-m-back ' + theme : 'attr-background'" @click="chooseAttr(index,attr)">{{attr.attr_name}}
  41. <view class="attr-number" :style="{'right':`${attr.length + 'rpx'}`}" :class="theme + '-m-back ' + theme" v-if="index == 0 && attr.number > 0">{{attr.number}}</view>
  42. </view>
  43. <view class="attr-name attr-background" style="visibility: hidden">占位
  44. </view>
  45. </scroll-view>
  46. <view @click.stop="toBottom(index)" class="right-icon">
  47. <view></view>
  48. <image src="/static/image/icon/right.png"></image>
  49. </view>
  50. </view>
  51. <view class="wholesale-attr-group-list">
  52. <view class="wholesale-attr-group" v-for="(item, index) in goodsAttr" :key="index">
  53. <view class="dir-left-nowrap">
  54. <view class="wholesale-attr-item main-between">
  55. <view class="dir-top-nowrap main-center attr-name t-omit">
  56. <view class="t-omit">{{item.attr_list[item.attr_list.length - 1].attr_name}}</view>
  57. <view class="attr-price">¥{{goods.level_show === 1 ? item.price_member : item.price}}</view>
  58. </view>
  59. <view class="dir-left-nowrap wholesale-number-box cross-center">
  60. <image :src="item.number <= 0 ? '/static/image/plugins/un-low.png' : '/static/image/plugins/low.png'" class="block box-grow-0 cross-center main-center" @click="wholesaleNumberSub(index)">
  61. </image>
  62. <view class="wholesale-number-input box-grow-0 cross-center main-center">
  63. <app-input height="60" type="number" v-model="item.number" paddingLeft="0" :center="true" placeholder=" " @blur="wholesaleNumberBlur(index)" :focus="false" width="88"></app-input>
  64. </view>
  65. <image :src="item.number >= item.stock? '/static/image/plugins/un-add.png' : '/static/image/plugins/add.png'" class="block box-grow-0 cross-center main-center" @click="wholesaleNumberAdd(index)"></image>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. <view class="attr-group" v-if="goods.type === 'goods' && sign !='wholesale'" v-for="(item, index) in newAttrGroupList" :key="index">
  73. <view class="attr-group-name">{{item.attr_group_name}}</view>
  74. <view class="dir-left-wrap">
  75. <view v-for="(attr, key) in item.attr_list" :key="key" class="attr-item"
  76. :class="attr.checked && sign !== 'gift' ? theme + '-m-back active ' + theme : attr.checked && sign === 'gift' ? theme + '-background active' : 'attr-item-default' + (attr.attr_num_0 ? ' attr_num_0' : '')"
  77. @click="storeAttrClick(attr.attr_id, item.attr_group_id)">{{attr.attr_name}}
  78. </view>
  79. </view>
  80. </view>
  81. <view v-if="chooseNumber" class="dir-left-nowrap number-box cross-center">
  82. <view class="box-grow-1">数量</view>
  83. <image :src="number <= 1 ? '/static/image/icon/can-be-reduced.png' : '/static/image/cart/can-be-reduced.png'" class="block box-grow-0 cross-center main-center" @click="numberSub">
  84. </image>
  85. <view class="number-input box-grow-0 cross-center main-center">
  86. <app-input type="number" v-model="number" paddingLeft="0" :center="true" placeholder=" " @blur="numberBlur"
  87. :focus="false" width="88"></app-input>
  88. </view>
  89. <image src="/static/image/cart/can-be-added.png" class="block box-grow-0 cross-center main-center" @click="numberAdd"></image>
  90. </view>
  91. </view>
  92. <view v-if="sign =='wholesale'" class="total">已选<text>{{totalNumber}}</text>件 总计<text>¥{{totalPrice > 0? totalPrice : '0.00'}}</text></view>
  93. <view class="three dir-left-nowrap">
  94. <view class="box-grow-1 main-center cross-center" v-if="cartShow"
  95. :class="sign === 'pick' ? theme + '-m-back ' + 'buy ' + theme : theme === 'b' || theme === 'f' || theme === 'a' ? theme + '-s-back ' + 'buy ' + theme : theme + '-s-back ' + theme+ '-m-text ' + theme" @click="cart">{{addText}}
  96. </view>
  97. <view v-if="is_show_buy" class="box-grow-1 main-center cross-center buy" :class="sign !== 'gift' ? theme + '-m-back '+ theme : theme + '-background'" @click="buy">
  98. {{buyText}}
  99. </view>
  100. </view>
  101. </view>
  102. </view>
  103. </view>
  104. </view>
  105. </template>
  106. <script>
  107. import { mapState, mapGetters } from "vuex";
  108. import appPrice from "../../page-component/goods/app-price.vue";
  109. import appImage from "../../basic-component/app-image/app-image.vue";
  110. import appMemberMark from '../../page-component/app-member-mark/app-member-mark.vue';
  111. export default {
  112. name: "app-attr",
  113. components: {
  114. appPrice,
  115. 'app-image': appImage,
  116. appMemberMark
  117. },
  118. props: {
  119. goods: Object,
  120. attrGroupList: Array,
  121. attrCart: {
  122. type: Array,
  123. default() {
  124. return [];
  125. }
  126. },
  127. cartShow: {
  128. type: Boolean,
  129. default() {
  130. return true
  131. }
  132. },
  133. previewUrl: String,
  134. submitUrl: String,
  135. goodsId: {
  136. type:Number,
  137. default() {
  138. return 0
  139. }
  140. },
  141. show: Number,
  142. buyText: {
  143. type: String,
  144. default() {
  145. return '立即购买';
  146. }
  147. },
  148. plugin: {
  149. default: '',
  150. },
  151. theme: {
  152. type: String,
  153. default: 'a',
  154. },
  155. chooseNumber: {
  156. type: Boolean,
  157. default: true,
  158. },
  159. noPay: {
  160. type: Boolean,
  161. default: false,
  162. },
  163. buyClick: {
  164. type: Boolean,
  165. default: false,
  166. },
  167. addText: {
  168. type: String,
  169. default: '加入购物车',
  170. },
  171. is_show_buy: {
  172. type: Boolean,
  173. default: true,
  174. },
  175. sign: {
  176. type: String
  177. },
  178. totalPrice: {
  179. type: String,
  180. default: '0.00',
  181. },
  182. totalNumber: {
  183. type: Number,
  184. default: 0,
  185. },
  186. discount: {
  187. type: Number,
  188. default: 0,
  189. },
  190. wholesaleType: {
  191. type: Number,
  192. default: 0,
  193. }
  194. },
  195. data() {
  196. return {
  197. display: 'none',
  198. number: 1,
  199. selectAttr: null,
  200. newAttrGroupList: null,
  201. pic_url: null,
  202. // 商品批发
  203. activeAttr: [],
  204. goodsAttr: []
  205. };
  206. },
  207. watch: {
  208. show() {
  209. if (this.display === 'block') {
  210. this.selectAttr = null;
  211. this.close();
  212. } else if (this.display === 'none') {
  213. this.alert();
  214. }
  215. },
  216. newData: {
  217. handler() {
  218. this.$emit('attr', this.newData)
  219. },
  220. immediate: true,
  221. },
  222. attrGroupList: {
  223. handler() {
  224. this.newAttrGroupList = this.attrGroupList;
  225. if (this.display == 'block') {
  226. this.alert();
  227. }
  228. },
  229. immediate: true
  230. },
  231. goods: {
  232. handler() {
  233. if (this.display == 'block') {
  234. this.alert();
  235. }
  236. },
  237. immediate: true
  238. }
  239. },
  240. mounted() {
  241. if(this.sign == 'wholesale') {
  242. this.pic_url = this.goods.attr_groups[0].attr_list[0].pic_url
  243. if(this.goods.attr_groups.length == 1) {
  244. this.goodsAttr = this.goods.attr;
  245. }else {
  246. for(let item of this.goods.attr_groups) {
  247. let para = {
  248. attr_group_name: '',
  249. attr_group_id: '',
  250. attr_id: '',
  251. attr_name: ''
  252. };
  253. para.attr_group_name = item.attr_group_name
  254. para.attr_group_id = item.attr_group_id
  255. para.attr_id = item.attr_list[0].attr_id
  256. para.attr_name = item.attr_list[0].attr_name
  257. this.activeAttr.push(para)
  258. }
  259. for(let item of this.goods.attr) {
  260. let same = true;
  261. for(let i = 0;i < item.attr_list.length -1;i++) {
  262. let first = {
  263. attr_group_name: item.attr_list[i].attr_group_name,
  264. attr_group_id: item.attr_list[i].attr_group_id,
  265. attr_id: item.attr_list[i].attr_id,
  266. attr_name: item.attr_list[i].attr_name
  267. }
  268. if(JSON.stringify(first) != JSON.stringify(this.activeAttr[i])) {
  269. same = false;
  270. }
  271. }
  272. if(same) {
  273. this.goodsAttr.push(item)
  274. }
  275. }
  276. }
  277. }
  278. },
  279. methods: {
  280. alert() {
  281. if (this.attrGroupList.length === 0) {
  282. return;
  283. }
  284. if(this.sign != 'wholesale') {
  285. let attr_group_list = this.attrGroupList;
  286. let attrs = this.goods.attr;
  287. let select_attr = null;
  288. this.number = 1;
  289. if (attr_group_list.length === 1) {
  290. for (let i in attrs) {
  291. for (let j in attr_group_list[0].attr_list) {
  292. if (attr_group_list[0].attr_list[j].attr_id == attrs[i].attr_list[0].attr_id) {
  293. if (attrs[i].stock > 0) {
  294. if (attrs.length === 1) {
  295. attr_group_list[0].attr_list[j].checked = true;
  296. }
  297. attr_group_list[0].attr_list[j].attr_num_0 = false;
  298. this.pic_url = attr_group_list[0].attr_list[j].pic_url;
  299. } else {
  300. this.number = 0;
  301. attr_group_list[0].attr_list[j].checked = false;
  302. attr_group_list[0].attr_list[j].attr_num_0 = true;
  303. }
  304. }
  305. }
  306. }
  307. if (attrs.length === 1) {
  308. select_attr = attrs[0];
  309. this.$emit('attrtap', select_attr);
  310. }
  311. }
  312. this.newAttrGroupList = attr_group_list;
  313. if(this.goods.selectAttr) {
  314. this.selectAttr = this.goods.selectAttr
  315. }else {
  316. this.selectAttr = select_attr;
  317. }
  318. }
  319. this.display = 'block';
  320. },
  321. scrollGet(e,index) {
  322. this.goods.attr_groups[index].scrollLeft = e.detail.scrollLeft;
  323. this.$forceUpdate();
  324. },
  325. toBottom(index) {
  326. this.$nextTick().then(() => {
  327. this.goods.attr_groups[index].scrollLeft = 99999;
  328. this.$forceUpdate();
  329. })
  330. },
  331. chooseAttr(index,attrItem) {
  332. let that = this;
  333. for(let attr of that.goods.attr_groups[index].attr_list) {
  334. attr.active = false;
  335. if(attr.attr_id == attrItem.attr_id && attr.attr_name == attrItem.attr_name) {
  336. attr.active = true;
  337. }
  338. }
  339. if(index == 0) {
  340. that.pic_url = attrItem.pic_url;
  341. }
  342. that.activeAttr[index].attr_id = attrItem.attr_id
  343. that.activeAttr[index].attr_name = attrItem.attr_name
  344. that.goodsAttr = [];
  345. for(let item of that.goods.attr) {
  346. let same = true;
  347. for(let i = 0;i < item.attr_list.length -1;i++) {
  348. let first = {
  349. attr_group_name: item.attr_list[i].attr_group_name,
  350. attr_group_id: item.attr_list[i].attr_group_id,
  351. attr_id: item.attr_list[i].attr_id,
  352. attr_name: item.attr_list[i].attr_name
  353. }
  354. if(JSON.stringify(first) != JSON.stringify(this.activeAttr[i])) {
  355. same = false;
  356. }
  357. }
  358. if(same) {
  359. that.goodsAttr.push(item)
  360. }
  361. }
  362. that.$forceUpdate();
  363. that.count();
  364. },
  365. wholesaleNumberSub(index) {
  366. if(this.goodsAttr[index].number == 0) {
  367. return false;
  368. }
  369. this.goodsAttr[index].number--;
  370. if(this.goods.attr_groups.length == 1) {
  371. this.pic_url = this.goodsAttr[index].pic_url;
  372. }
  373. this.count(index);
  374. },
  375. wholesaleNumberAdd(index) {
  376. if(this.goodsAttr[index].number > this.goodsAttr[index].stock || this.goodsAttr[index].number == this.goodsAttr[index].stock) {
  377. return false
  378. }
  379. this.goodsAttr[index].number++;
  380. if(this.goods.attr_groups.length == 1) {
  381. this.pic_url = this.goodsAttr[index].pic_url;
  382. }
  383. this.count(index);
  384. },
  385. wholesaleNumberBlur(index) {
  386. if(+this.goodsAttr[index].number > +this.goodsAttr[index].stock) {
  387. this.goodsAttr[index].number = this.goodsAttr[index].stock
  388. }
  389. if(this.goods.attr_groups.length == 1) {
  390. this.pic_url = this.goodsAttr[index].pic_url;
  391. }
  392. this.count(index);
  393. },
  394. count(index) {
  395. let that =this;
  396. this.$emit('attrtap', that.goods, that.goodsAttr);
  397. setTimeout(()=>{
  398. that.selectAttr = that.goodsAttr[index]
  399. })
  400. },
  401. close() {
  402. this.display = 'none';
  403. this.$emit('close', false)
  404. },
  405. preventD() {
  406. },
  407. storeAttrClick(attr_id, attr_group_id) {
  408. let attr_group_list = JSON.parse(JSON.stringify(this.newAttrGroupList));
  409. let attrs = this.goods.attr;
  410. let checkedAttr = [];
  411. let attr_cart = this.attrCart;
  412. for (let i in attr_group_list) {
  413. for (let j in attr_group_list[i].attr_list) {
  414. let temp = attr_group_list[i].attr_list[j];
  415. if (parseInt(attr_group_list[i].attr_group_id) == parseInt(attr_group_id)) {
  416. if (parseInt(temp.attr_id) === parseInt(attr_id)) {
  417. if (temp.checked) {
  418. temp.checked = false;
  419. } else {
  420. temp.checked = true;
  421. }
  422. if (temp.attr_num_0) {
  423. return;
  424. }
  425. } else {
  426. temp.checked = false;
  427. }
  428. }
  429. if (temp.checked) {
  430. if (i == 0) {
  431. this.pic_url = attr_group_list[0].attr_list[j].pic_url;
  432. }
  433. checkedAttr.push(attr_group_list[i].attr_group_id + '-' + temp.attr_id);
  434. }
  435. }
  436. }
  437. function inArray(val, arr) {
  438. return arr.some(function (v) {
  439. return val == v;
  440. })
  441. }
  442. let attrNum_0 = [];
  443. let select_attr = null;
  444. let number = 1;
  445. for (let i in attrs) {
  446. let arr = [];
  447. let sign = 0;
  448. for (let j in attrs[i].attr_list) {
  449. let param = attrs[i].attr_list[j].attr_group_id + '-' + attrs[i].attr_list[j].attr_id;
  450. if (!inArray(param, checkedAttr)) {
  451. sign += 1;
  452. arr.push(param);
  453. }
  454. }
  455. if (attrs[i].stock == 0 && sign <= 1) {
  456. attrNum_0 = attrNum_0.concat(arr);
  457. }
  458. if (sign == 0) {
  459. if (!select_attr) {
  460. select_attr = {};
  461. }
  462. select_attr = attrs[i];
  463. attr_cart.forEach(item => {
  464. if (item.attr_id == select_attr.id) {
  465. number = item.num;
  466. }
  467. });
  468. if (select_attr.stock <= 0) {
  469. uni.showToast({
  470. title: '库存不足',
  471. icon: 'none'
  472. });
  473. return;
  474. }
  475. if (select_attr.stock <= number) {
  476. number = select_attr.stock;
  477. }
  478. }
  479. }
  480. if (checkedAttr.length == 0) {
  481. select_attr = null;
  482. }
  483. //库存为0的规格添加标识
  484. for (let i in attr_group_list) {
  485. for (let j in attr_group_list[i].attr_list) {
  486. let cAttr = attr_group_list[i].attr_list[j];
  487. let cParam = attr_group_list[i].attr_group_id + '-' + cAttr.attr_id;
  488. if (inArray(cParam, attrNum_0) && !inArray(cParam, checkedAttr)) {
  489. cAttr.attr_num_0 = true;
  490. } else {
  491. cAttr.attr_num_0 = false;
  492. }
  493. }
  494. }
  495. this.newAttrGroupList = attr_group_list;
  496. this.selectAttr = select_attr;
  497. this.number = number;
  498. this.$emit('attrtap', this.selectAttr);
  499. },
  500. numberBlur(number) {
  501. number = parseInt(number.value);
  502. if (number > this.attrNum) {
  503. uni.showToast({
  504. title: '库存不足',
  505. icon: 'none'
  506. });
  507. number = this.attrNum;
  508. }
  509. this.$emit('attrtap', this.selectAttr);
  510. return this.number = number;
  511. },
  512. numberSub() {
  513. let number = this.number;
  514. if (number <= 1) {
  515. return true;
  516. }
  517. number--;
  518. this.number = number;
  519. this.$emit('attrtap', this.selectAttr);
  520. },
  521. numberAdd() {
  522. let number = this.number;
  523. number++;
  524. if (number > this.attrNum) {
  525. uni.showToast({
  526. title: '库存不足',
  527. icon: 'none'
  528. });
  529. this.number = this.attrNum;
  530. return;
  531. }
  532. this.number = number;
  533. this.$emit('attrtap', this.selectAttr);
  534. },
  535. cart() {
  536. if (!this.submit()) {
  537. return false;
  538. }
  539. let select_attr = this.selectAttr;
  540. if (this.goods.sign === 'pick' || this.goods.sign === 'community') {
  541. this.$emit('add', select_attr, this.number);
  542. return;
  543. }
  544. // 普通商品
  545. if (this.goods.type === 'goods') {
  546. if (this.goods.sign === 'miaosha') {
  547. this.$request({
  548. url: this.$api.miaosha.add_cart,
  549. data: {
  550. miaosha_goods_id: select_attr.goods_id,
  551. attr_id: select_attr.id,
  552. num: this.number
  553. },
  554. method: 'post'
  555. }).then(e => {
  556. uni.showToast({
  557. title: e.msg,
  558. type: 'success'
  559. });
  560. this.display = 'none';
  561. this.selectAttr.number = this.number;
  562. this.$emit('selectNumber', this.selectAttr);
  563. }).catch(() => {
  564. this.display = 'none';
  565. });
  566. } else if (this.goods.sign === 'flash_sale') {
  567. this.$request({
  568. url: this.$api.flash_sale.add_cart,
  569. data: {
  570. flash_goods_id: select_attr.goods_id,
  571. attr_id: select_attr.id,
  572. num: this.number
  573. },
  574. method: 'post'
  575. }).then(e => {
  576. uni.showToast({
  577. title: e.msg,
  578. type: 'success'
  579. });
  580. this.display = 'none';
  581. this.selectAttr.number = this.number;
  582. this.$emit('selectNumber', this.selectAttr);
  583. }).catch(() => {
  584. this.display = 'none';
  585. });
  586. }else if(this.goods.sign == 'wholesale') {
  587. if(this.totalNumber < this.goods.wholesaleGoods.rise_num) {
  588. uni.showToast({
  589. title: '至少采购' + this.goods.wholesaleGoods.rise_num + this.goods.unit,
  590. image: '/static/image/plugins/tip.png',
  591. duration: 1000
  592. });
  593. return false
  594. }
  595. let para = [];
  596. for(let item of this.goods.attr) {
  597. if(item.number > 0) {
  598. para.push(item)
  599. }
  600. }
  601. this.$request({
  602. url: this.$api.wholesale.cart,
  603. data: {
  604. attr: JSON.stringify(para)
  605. },
  606. method:'post'
  607. }).then(response => {
  608. this.display = 'none';
  609. if (response.code === 0) {
  610. for(let item of this.goods.attr) {
  611. item.number = '0'
  612. }
  613. for(let item of this.goodsAttr) {
  614. item.number = '0'
  615. }
  616. this.count();
  617. uni.hideLoading();
  618. uni.showToast({
  619. title: '添加成功',
  620. duration: 1000
  621. });
  622. }
  623. }).catch(response => {
  624. this.display = 'none';
  625. })
  626. } else {
  627. this.$request({
  628. url: this.$api.cart.add,
  629. data: {
  630. goods_id: select_attr.goods_id,
  631. attr: select_attr.id,
  632. num: this.number
  633. },
  634. method: 'post'
  635. }).then(e => {
  636. if (e.code === 0) {
  637. uni.showToast({
  638. title: e.msg,
  639. type: 'success'
  640. });
  641. this.display = 'none';
  642. this.selectAttr.number = this.number;
  643. this.$emit('selectNumber', this.selectAttr);
  644. } else {
  645. uni.showToast({
  646. title: e.msg,
  647. icon: "none",
  648. duration: 2500
  649. });
  650. this.display = 'none';
  651. }
  652. }).catch(() => {
  653. this.display = 'none';
  654. });
  655. }
  656. // 虚拟商品
  657. } else if (this.goods.type === 'ecard') {
  658. uni.showToast({
  659. title: '虚拟商品不允许加入购物车',
  660. icon: 'none'
  661. });
  662. }
  663. },
  664. buy() {
  665. if (!this.submit()) return false;
  666. if (this.noPay) {
  667. this.$emit('pay', this.number);
  668. return;
  669. }
  670. if (this.buyClick) {
  671. this.display = 'none';
  672. this.selectAttr.number = this.number;
  673. this.$emit('buyClick', this.selectAttr);
  674. return false;
  675. }
  676. if(this.goods.sign == 'wholesale') {
  677. if(this.totalNumber < this.goods.wholesaleGoods.rise_num) {
  678. uni.showToast({
  679. title: '至少采购' + this.goods.wholesaleGoods.rise_num + this.goods.unit,
  680. image: '/static/image/plugins/tip.png',
  681. duration: 1000
  682. });
  683. return false
  684. }
  685. let para = {};
  686. let mch_list = [{
  687. mch_id: 0,
  688. goods_list: []
  689. }];
  690. for(let item of this.goods.attr) {
  691. if(item.number > 0) {
  692. para = {
  693. id: item.goods_id,
  694. attr: [],
  695. num: item.number,
  696. cat_id: 0,
  697. goods_attr_id: item.id
  698. }
  699. for(let attr of item.attr_list) {
  700. let attrList = {
  701. attr_id: attr.attr_id,
  702. attr_group_id: attr.attr_group_id
  703. }
  704. para.attr.push(attrList)
  705. }
  706. mch_list[0].goods_list.push(para)
  707. }
  708. }
  709. let url = `/pages/order-submit/order-submit?mch_list=${JSON.stringify(mch_list)}`;
  710. if (this.submitUrl && this.previewUrl) {
  711. url += `&preview_url=${encodeURIComponent(this.previewUrl)}&submit_url=${encodeURIComponent(this.submitUrl)}&plugin=${this.plugin}`;
  712. }
  713. uni.navigateTo({
  714. url: url
  715. })
  716. }else {
  717. let goods = this.goods;
  718. let number = this.number;
  719. let select_attr = this.selectAttr;
  720. let goods_attr_id = select_attr.id;
  721. let attr = [];
  722. for (let i in select_attr.attr_list) {
  723. attr.push({
  724. attr_id: select_attr.attr_list[i].attr_id,
  725. attr_group_id: select_attr.attr_list[i].attr_group_id
  726. });
  727. }
  728. let mch_list = [{
  729. mch_id: goods.mch_id ? goods.mch_id : 0,
  730. goods_list: [{
  731. id: this.goodsId ? this.goodsId: goods.id,
  732. attr: attr,
  733. num: number,
  734. cat_id: 0,
  735. goods_attr_id: goods_attr_id
  736. }]
  737. }];
  738. let url = `/pages/order-submit/order-submit?mch_list=${JSON.stringify(mch_list)}`;
  739. if (this.submitUrl && this.previewUrl) {
  740. url += `&preview_url=${encodeURIComponent(this.previewUrl)}&submit_url=${encodeURIComponent(this.submitUrl)}&plugin=${this.plugin}`;
  741. }
  742. uni.navigateTo({
  743. url: url
  744. })
  745. }
  746. },
  747. submit() {
  748. let select_attr = this.selectAttr;
  749. if(this.goods.sign === 'wholesale') {
  750. return true;
  751. }
  752. if (!select_attr) {
  753. uni.showToast({
  754. title: '请先选规格',
  755. icon: 'none'
  756. });
  757. return false;
  758. }
  759. if (select_attr.stock <= 0) {
  760. uni.showToast({
  761. title: '库存不足',
  762. icon: 'none'
  763. });
  764. return false;
  765. }
  766. if (this.number <= 0) {
  767. uni.showToast({
  768. title: '数量不能为0',
  769. icon: 'none'
  770. });
  771. return false;
  772. }
  773. if (!this.goods) {
  774. return false;
  775. }
  776. return true;
  777. },
  778. clickImg(src) {
  779. uni.previewImage({
  780. current: 0,
  781. urls: [src]
  782. });
  783. },
  784. },
  785. computed: {
  786. ...mapState({
  787. gConfig: state => state.gConfig,
  788. }),
  789. attrPic() {
  790. if (this.pic_url) {
  791. return this.pic_url;
  792. } else if (this.goods) {
  793. return this.goods.cover_pic;
  794. } else {
  795. return ``;
  796. }
  797. },
  798. priceColor() {
  799. if (this.goods && this.goods.level_show === 1) {
  800. return `member`;
  801. } else {
  802. return this.theme + '-color';
  803. }
  804. },
  805. attrNum() {
  806. if (this.selectAttr) {
  807. return this.selectAttr.stock;
  808. } else if (this.goods) {
  809. return this.goods.goods_num;
  810. } else {
  811. return 0;
  812. }
  813. },
  814. attrPrice() {
  815. if (this.selectAttr) {
  816. let price;
  817. if (this.goods.level_show === 1) {
  818. price = this.selectAttr.price_member
  819. } else {
  820. price = this.selectAttr.price;
  821. }
  822. if(this.sign == 'wholesale') {
  823. if(this.selectAttr.number == 0) {
  824. return 'undefined'
  825. }
  826. if(this.wholesaleType == 0) {
  827. price = (price*(this.discount/10)).toFixed(2);
  828. return price
  829. }else {
  830. price = (price - this.discount).toFixed(2);
  831. return price
  832. }
  833. }else {
  834. return price
  835. }
  836. } else if(this.sign == 'wholesale') {
  837. return 'undefined';
  838. } else if (this.goods) {
  839. if (this.goods.hasOwnProperty('price_min')) {
  840. return this.goods.price_min;
  841. } else {
  842. return this.goods.price;
  843. }
  844. } else {
  845. return 0;
  846. }
  847. },
  848. newData() {
  849. const { number, display, selectAttr } = this;
  850. return {
  851. number,
  852. display,
  853. selectAttr
  854. }
  855. },
  856. themeObject:function() {
  857. return {
  858. back: this.theme + '-m-back ' + this.theme,
  859. theme: this.theme,
  860. color: this.theme + '-m-text ' + this.theme,
  861. sBack: this.theme + '-s-back ' + this.theme
  862. }
  863. },
  864. ...mapGetters('iPhoneX', {
  865. boolEmpty: 'getBoolEmpty'
  866. })
  867. }
  868. }
  869. </script>
  870. <style scoped lang="scss">
  871. .app-attr {
  872. background-color: #ffffff;
  873. .modal {
  874. background-color: rgba(0, 0, 0, 0.5);
  875. position: fixed;
  876. top: 0;
  877. left: 0;
  878. width: 100%;
  879. height: 100%;
  880. z-index: 1603;
  881. }
  882. .content {
  883. width: 100%;
  884. background-color: #ffffff;
  885. border-radius: #{16rpx} #{16rpx} 0 0;
  886. .close {
  887. width: #{30rpx};
  888. height: #{30rpx};
  889. position: absolute;
  890. right: #{24rpx};
  891. top: #{24rpx};
  892. background-color: #ffffff;
  893. }
  894. .first {
  895. margin: 0 #{24rpx};
  896. border-bottom: #{1rpx} solid #e2e2e2;
  897. &.no-border {
  898. border-bottom: 0;
  899. }
  900. .img {
  901. width: #{200rpx};
  902. height: #{200rpx};
  903. border: #{4rpx} solid #ffffff;
  904. border-radius: #{8rpx};
  905. margin-top: #{-64rpx};
  906. display: block;
  907. }
  908. .info {
  909. margin: #{36rpx} 0 #{26rpx} #{24rpx};
  910. font-size: $uni-font-size-import-two;
  911. line-height: 1;
  912. .stock {
  913. font-size: $uni-font-size-weak-one;
  914. color: $uni-general-color-two;
  915. margin-top: #{18rpx};
  916. }
  917. view {
  918. &:first-child {
  919. margin-right: #{12rpx};
  920. }
  921. }
  922. .member {
  923. color: #f39800;
  924. }
  925. }
  926. }
  927. .second {
  928. max-height: #{650rpx};
  929. overflow-y: auto;
  930. overflow-x: hidden;
  931. padding: #{4rpx} #{24rpx};
  932. font-size: $uni-font-size-general-two;
  933. &.no-padding {
  934. padding: 0;
  935. }
  936. .attr-group {
  937. padding: #{32rpx} 0;
  938. border-bottom: #{1rpx} solid #e2e2e2;
  939. .attr-group-name {
  940. color: $uni-general-color-one;
  941. margin-bottom: #{20rpx};
  942. }
  943. .attr-item {
  944. margin-right: #{20rpx};
  945. padding: #{15rpx 24rpx};
  946. border-radius: #{8rpx};
  947. margin-bottom: #{20rpx};
  948. &.attr-item-default {
  949. background-color: #f2f2f2;
  950. color: $uni-important-color-black;
  951. }
  952. &.active {
  953. color: #ffffff;
  954. }
  955. &.attr_num_0 {
  956. color: #cdcdcd;
  957. background-color: #f7f7f7;
  958. }
  959. }
  960. }
  961. .number-box {
  962. color: $uni-general-color-one;
  963. height: #{124rpx};
  964. .block {
  965. width: #{60rpx};
  966. height: #{60rpx};
  967. margin: 0 #{4rpx};
  968. &.disabled {
  969. background-color: #fbfbfb;
  970. color: $uni-general-color-two;
  971. }
  972. }
  973. .number-input {
  974. width: #{88rpx};
  975. height: #{60rpx};
  976. color: $uni-important-color-black;
  977. font-size: $uni-font-size-general-one;
  978. background-color: $uni-weak-color-two;
  979. }
  980. }
  981. }
  982. .three {
  983. height: #{110rpx};
  984. width: 100%;
  985. font-size: $uni-font-size-general-one;
  986. }
  987. }
  988. }
  989. .buy {
  990. color: #ffffff;
  991. }
  992. .wholesale {
  993. .wholesale-attr-list {
  994. height: 88rpx;
  995. margin: 0 24rpx;
  996. border-bottom: 1rpx solid #e2e2e2;
  997. position: relative;
  998. &:first-of-type {
  999. border-top: 1rpx solid #e2e2e2;
  1000. }
  1001. .wholesale-attr-group {
  1002. max-width: 200rpx;
  1003. flex-shrink: 0;
  1004. margin-right: 4rpx;
  1005. font-size: 20rpx;
  1006. color: #666666;
  1007. }
  1008. .right-icon {
  1009. position: absolute;
  1010. right: -24rpx;
  1011. top: 0;
  1012. height: 84rpx;
  1013. padding-left: 10rpx;
  1014. width: 50rpx;
  1015. z-index: 10000;
  1016. background-color: #fff;
  1017. view {
  1018. height: 40rpx;
  1019. width: 3rpx;
  1020. background-color: #e2e2e2;
  1021. position: absolute;
  1022. left: 0;
  1023. top: 24rpx;
  1024. }
  1025. image {
  1026. margin-top: 33rpx;
  1027. height: 22rpx;
  1028. width: 16rpx;
  1029. }
  1030. }
  1031. .wholesale-attr-item {
  1032. height: 100%;
  1033. white-space : nowrap;
  1034. .attr-name {
  1035. margin-top: 16rpx;
  1036. color: #fff;
  1037. padding: 0 22rpx;
  1038. height: 56rpx;
  1039. line-height: 56rpx;
  1040. border-radius: 8rpx;
  1041. font-size: 26rpx;
  1042. margin-left: 20rpx;
  1043. display: inline-block;
  1044. position: relative;
  1045. .attr-number {
  1046. color: #fff;
  1047. position: absolute;
  1048. top: -15rpx;
  1049. height: 30rpx;
  1050. line-height: 26rpx;
  1051. border: 2rpx solid #fff;
  1052. padding: 0 10rpx;
  1053. border-radius: 15rpx;
  1054. font-size: 20rpx;
  1055. z-index: 100;
  1056. }
  1057. &.attr-background {
  1058. background-color: #f2f2f2;
  1059. color: #353535;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. .wholesale-attr-group-list {
  1065. margin-top: 20rpx;
  1066. .wholesale-attr-group {
  1067. margin-bottom: 20rpx;
  1068. .wholesale-attr-item {
  1069. padding: 20rpx;
  1070. background-color: #f7f7f7;
  1071. width: 100%;
  1072. .attr-name {
  1073. width: 60%;
  1074. .attr-price {
  1075. color: #999999;
  1076. font-size: 24rpx;
  1077. }
  1078. }
  1079. .wholesale-number-box {
  1080. color: $uni-general-color-one;
  1081. .block {
  1082. width: #{60rpx};
  1083. height: #{60rpx};
  1084. margin: 0 #{4rpx};
  1085. &.disabled {
  1086. background-color: #fbfbfb;
  1087. color: $uni-general-color-two;
  1088. }
  1089. }
  1090. .wholesale-number-input {
  1091. width: #{88rpx};
  1092. height: #{60rpx};
  1093. color: $uni-important-color-black;
  1094. font-size: $uni-font-size-general-one;
  1095. background-color: #fff;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. .total {
  1103. width: 100%;
  1104. height: 80rpx;
  1105. line-height: 80rpx;
  1106. text-align: right;
  1107. padding: 0 24rpx;
  1108. color: #353535;
  1109. font-size: 28rpx;
  1110. border-top: 1rpx solid #e2e2e2;
  1111. text {
  1112. color: #ff4544;
  1113. }
  1114. }
  1115. .u-attr-fixed {
  1116. position: fixed;
  1117. bottom: 0;
  1118. left: 0;
  1119. width: 100%;
  1120. background-color: #ffffff;
  1121. }
  1122. </style>