u-attr.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <template>
  2. <view class="u-attr">
  3. <view @click="turnOn">
  4. <slot name="btn"></slot>
  5. </view>
  6. <u-popup v-model="newValue" mode="bottom" border-radius="14" :safeAreaInsetBottom="true" @close="close">
  7. <view class="u-model" @touchmove.stop.prevent>
  8. <view class="u-top dir-left-nowrap u-border-box">
  9. <view class="u-pic u-border-box" @click="clickImg(imgUrl)">
  10. <image class="u-img" :src="imgUrl"></image>
  11. </view>
  12. <view class="u-info">
  13. <view :class="['cross-center', 'dir-left-nowrap', themeObject.color]">
  14. <view class="dir-left-nowrap">
  15. <slot name="priceBefore"></slot>
  16. <view class="u-price">
  17. <app-price
  18. v-if="is_show_price"
  19. :theme="themeObject.theme"
  20. :userTheme="themeObject.userTheme"
  21. :sign="sign"
  22. :price="sellPrice"
  23. :default-price="goods.price"
  24. ></app-price>
  25. </view>
  26. </view>
  27. <app-member-mark
  28. v-if="goods.level_show === 1"
  29. :theme="themeObject"
  30. ></app-member-mark>
  31. </view>
  32. <view class="u-stock">库存:{{stock}}</view>
  33. </view>
  34. <view class="u-close-image" @click="close">
  35. <image class="bd-close-image u-border-box" src="/static/image/icon/icon-close.png" ></image>
  36. </view>
  37. </view>
  38. <view class="u-center">
  39. <scroll-view class="u-scroll-view" scroll-y="true">
  40. <slot name="extra"></slot>
  41. <view v-if="goods.type === 'goods'" class="u-attr-group u-border-box" v-for="(item, index) in newGroup" :key="index">
  42. <view class="u-group-name u-text">{{item.attr_group_name}}</view>
  43. <view class="dir-left-wrap" >
  44. <view :class="['u-group-item', attr.select ? 'u-checked ' + themeObject.back : 'u-unchecked', attr.num_0 ? 'u-attr_num_0' : '']"
  45. @click="storeAttr(attr.attr_id, item.attr_group_id, attr.num_0)"
  46. v-for="(attr, key) in item.attr_list" :key="key">
  47. {{attr.attr_name}}
  48. </view>
  49. </view>
  50. </view>
  51. <view class="u-number dir-left-nowrap main-between cross-center">
  52. <view class="u-text">数量</view>
  53. <view class="dir-left-nowrap u-input-box">
  54. <view @click.stop="numberSub" :class="[number <=1 ? 'u-reduced-1' : 'u-reduced-0', 'u-number-btn']"></view>
  55. <input @blur="numberBlur" type="number" class="u-input" v-model="number">
  56. <view @click.stop="numberAdd" class="u-number-btn u-added-1"></view>
  57. </view>
  58. </view>
  59. </scroll-view>
  60. </view>
  61. <view class="u-bottom dir-left-nowrap">
  62. <view
  63. v-if="is_show_left && (goods.type === 'goods' || is_must_left)"
  64. class="box-grow-1"
  65. :class="cartClass"
  66. @click="leftSubmit">
  67. <slot name="left_slot"></slot>
  68. <template v-if="!$slots.left_slot">{{leftText}}</template>
  69. </view>
  70. <view
  71. v-if="is_show_right"
  72. class="box-grow-1"
  73. @click="rightSubmit"
  74. :class="[!$slots.right_slot ? themeObject.back + ' u-btn u-btn-color' : '']"
  75. >
  76. <slot name="right_slot"></slot>
  77. <template v-if="!$slots.right_slot">{{rightText}}</template>
  78. </view>
  79. <template v-if="$slots.right">
  80. <slot name="right"></slot>
  81. </template>
  82. </view>
  83. </view>
  84. </u-popup>
  85. </view>
  86. </template>
  87. <script>
  88. import uPopup from '../../basic-component/u-popup/u-popup.vue';
  89. import appPrice from "../goods/app-price.vue";
  90. import appMemberMark from "../app-member-mark/app-member-mark.vue";
  91. export default {
  92. name: "u-attr",
  93. props: {
  94. value: {
  95. type: Boolean
  96. },
  97. goods: {
  98. type: Object
  99. },
  100. themeObject: {
  101. type: Object
  102. },
  103. checked: {
  104. type: Object
  105. },
  106. is_show_price: {
  107. type: Boolean,
  108. default: true
  109. },
  110. is_show_left: {
  111. type: Boolean,
  112. default: true
  113. },
  114. is_must_left: {
  115. type: Boolean,
  116. default: true
  117. },
  118. is_show_right: {
  119. type: Boolean,
  120. default: true
  121. },
  122. leftText: {
  123. type: String,
  124. default: '加入购物车'
  125. },
  126. rightText: {
  127. type: String,
  128. default: '立即购买'
  129. },
  130. leftFunc: {
  131. type: Boolean
  132. },
  133. rightFunc: {
  134. type: Boolean
  135. },
  136. sign: {
  137. type: String
  138. },
  139. again: {
  140. type: Number
  141. }
  142. },
  143. data() {
  144. return {
  145. newValue: false,
  146. picUrl: null,
  147. newGroup: [],
  148. number: 1
  149. }
  150. },
  151. methods: {
  152. close: function() {
  153. this.$emit('input', false);
  154. },
  155. turnOn: function() {
  156. this.$emit('input', true);
  157. },
  158. inArray: function(newVal, arr) {
  159. return arr.some(v => {
  160. return newVal === v;
  161. });
  162. },
  163. identifier: function(copyGroup, attrNum_0, select) {
  164. copyGroup.forEach(f => {
  165. f.attr_list.forEach(c => {
  166. let param = `${f.attr_group_id}-${c.attr_id}`;
  167. this.inArray(param, attrNum_0) && !this.inArray(param, select) ? c.num_0 = true : c.num_0 = false;
  168. });
  169. });
  170. },
  171. selectCheck: function(copyAttr, attrNum_0, select) {
  172. copyAttr.forEach(f => {
  173. let arr = [];
  174. let sign = 0;
  175. f.attr_list.forEach(c => {
  176. let param = `${c.attr_group_id}-${c.attr_id}`;
  177. if (!this.inArray(param, select)) {
  178. sign += 1;
  179. arr.push(param);
  180. }
  181. });
  182. if (f.stock === 0 && sign <= 1) Array.prototype.push.apply(attrNum_0, arr);
  183. if (sign === 0) {
  184. this.$emit('check', f, 1);
  185. }
  186. });
  187. },
  188. storeAttr: function(attr_id, group_id, num_0) {
  189. if (num_0 === true) return;
  190. let newGroup = this.newGroup;
  191. let copyAttr = this.copyAttr;
  192. let select = [];
  193. newGroup.forEach((i, index) => {
  194. let attr_list = i.attr_list;
  195. attr_list.forEach((k) => {
  196. if (i.attr_group_id === group_id) {
  197. if (k.attr_id === attr_id) {
  198. if (k.select === true) {
  199. k.select = false;
  200. } else {
  201. k.select = true;
  202. }
  203. } else {
  204. k.select = false;
  205. }
  206. // k.attr_id === attr_id ? k.select = true : k.select = false;
  207. }
  208. if (k.select === true) {
  209. select.push(`${i.attr_group_id}-${k.attr_id}`);
  210. if (index === 0) this.picUrl = k.pic_url;
  211. }
  212. });
  213. });
  214. let attrNum_0 = [];
  215. this.selectCheck(copyAttr, attrNum_0, select);
  216. this.$nextTick(() => {
  217. if (this.number > this.stock) this.number = this.stock;
  218. });
  219. this.identifier(newGroup, attrNum_0, select);
  220. if (select.length !== newGroup.length) {
  221. this.$emit('check', null, 1);
  222. }
  223. },
  224. firstSelect: function() {
  225. if (!this.copyGroup || !this.copyAttr) return;
  226. let copyGroup = this.copyGroup;
  227. let copyAttr = this.copyAttr;
  228. let groupLength = copyGroup.length;
  229. let select = [];
  230. copyAttr.forEach(i => {
  231. let attr_list = i.attr_list;
  232. attr_list.forEach(j => {
  233. let attr_group_id = j.attr_group_id;
  234. copyGroup.forEach(k => {
  235. if (attr_group_id === k.attr_group_id) {
  236. let groups_attr_list = k.attr_list;
  237. groups_attr_list.forEach(g => {
  238. if (g.attr_id === j.attr_id) {
  239. if (i.stock > 0) {
  240. if (groupLength > 0) {
  241. g.select = true;
  242. select.push(`${k.attr_group_id}-${g.attr_id}`);
  243. if (groupLength === copyGroup.length) {
  244. this.picUrl = g.pic_url;
  245. }
  246. groupLength--;
  247. }
  248. }
  249. }
  250. });
  251. }
  252. });
  253. });
  254. });
  255. let attrNum_0 = [];
  256. this.selectCheck(copyAttr, attrNum_0, select);
  257. this.identifier(copyGroup, attrNum_0, select);
  258. this.newGroup = copyGroup;
  259. },
  260. numberBlur: function (e) {
  261. let value = parseInt(e.detail.value);
  262. if (!value) value = 1;
  263. if (value > this.stock) {
  264. value = this.stock;
  265. uni.showToast({
  266. title: '库存不足',
  267. icon: 'none'
  268. });
  269. }
  270. this.number = value;
  271. },
  272. numberSub:function() {
  273. let value = this.number;
  274. if (value > 1) {
  275. value--;
  276. this.number = value;
  277. }
  278. },
  279. numberAdd: function() {
  280. let value = this.number;
  281. value++;
  282. if (value > this.stock) {
  283. value = this.stock;
  284. uni.showToast({
  285. title: '库存不足',
  286. icon: 'none'
  287. });
  288. }
  289. this.number = value;
  290. },
  291. leftSubmit:function() {
  292. if (this.leftFunc === true) {
  293. this.$emit('leftFunc', this.number);
  294. } else {
  295. this.cart();
  296. }
  297. this.close();
  298. },
  299. rightSubmit:function() {
  300. let attrs = [];
  301. if (!this.checked) {
  302. uni.showToast({
  303. title: '请先选规格',
  304. icon: 'none'
  305. });
  306. return false;
  307. }
  308. this.checked.attr_list.forEach(item => {
  309. attrs.push({
  310. attr_id: item.attr_id,
  311. attr_group_id: item.attr_group_id
  312. });
  313. });
  314. let goods = {
  315. mch_id: this.goods.mch_id ? this.goods.mch_id : 0,
  316. goods_list: [
  317. {
  318. id: this.goods.id,
  319. attrs,
  320. num: this.number,
  321. cat_id: 0,
  322. goods_attr_id: this.checked.id
  323. }
  324. ]
  325. }
  326. if (this.rightFunc === true) {
  327. this.$emit('rightFunc', goods);
  328. } else {
  329. this.shop(goods);
  330. }
  331. this.close();
  332. },
  333. shop: function(goods) {
  334. uni.navigateTo({
  335. url: `/pages/order-submit/order-submit?mch_list=${JSON.stringify([goods])}`
  336. });
  337. },
  338. cart: function() {
  339. if (!this.checked) {
  340. uni.showToast({
  341. title: '请先选规格',
  342. icon: 'none'
  343. });
  344. return false;
  345. }
  346. if (this.goods.type === 'ecard') return;
  347. this.$request({
  348. url: this.$api.cart.add,
  349. method: 'post',
  350. data: {
  351. goods_id: this.checked.goods_id,
  352. attr: this.checked.id,
  353. num: this.number
  354. }
  355. }).then(res => {
  356. if (res.code === 0) {
  357. uni.showToast({
  358. title: res.msg,
  359. icon: 'none'
  360. });
  361. this.$emit('cart', this.checked, this.number);
  362. this.close();
  363. } else {
  364. uni.showToast({
  365. title: res.msg,
  366. icon: "none",
  367. duration: 2500
  368. });
  369. }
  370. });
  371. },
  372. clickImg(src) {
  373. uni.previewImage({
  374. current: 0,
  375. urls: [src]
  376. });
  377. }
  378. },
  379. components: {
  380. uPopup,
  381. appPrice,
  382. appMemberMark
  383. },
  384. computed: {
  385. imgUrl: function() {
  386. if (this.picUrl) {
  387. return this.picUrl;
  388. } else if (this.goods) {
  389. return this.goods.cover_pic;
  390. } else {
  391. return '';
  392. }
  393. },
  394. stock: function() {
  395. if (!this.$validation.isEmpty(this.checked)) {
  396. return this.checked.stock;
  397. } else if (!this.$validation.isEmpty(this.goods)) {
  398. return this.goods.goods_num;
  399. }
  400. },
  401. sellPrice: function() {
  402. if (!this.$validation.isEmpty(this.checked)) {
  403. return this.goods.level_show === 1 ? this.checked.price_member : this.checked.price;
  404. } else if (!this.$validation.isEmpty(this.goods)) {
  405. return this.goods.hasOwnProperty('price_min') ? this.goods.price_min : this.goods.price;
  406. }
  407. },
  408. copyGroup: function() {
  409. if (!this.goods) return;
  410. let group = this.$utils.deepClone(this.goods.attr_groups);
  411. for (let i = 0; i < group.length; i++) {
  412. group[i].attr_list.forEach(item => {
  413. item.select = false;
  414. item.num_0 = false;
  415. });
  416. }
  417. return group;
  418. },
  419. copyAttr: function() {
  420. if (!this.goods) return;
  421. return this.$utils.deepClone(this.goods.attr);
  422. },
  423. cartClass: function() {
  424. if (!this.$slots.left_slot) {
  425. if (this.themeObject.theme === 'a' || this.themeObject.theme === 'b' || this.themeObject.theme === 'f') {
  426. return this.themeObject.sBack + ' u-btn u-btn-color ';
  427. } else {
  428. return this.themeObject.sBack + ' u-btn ' + this.themeObject.color;
  429. }
  430. } else {
  431. return '';
  432. }
  433. }
  434. },
  435. watch: {
  436. value: {
  437. handler(newVal) {
  438. this.newValue = newVal;
  439. if (newVal === false) return;
  440. if (this.$validation.isEmpty(this.checked)) this.$utils.throttle(this.firstSelect, 800);
  441. },
  442. immediate: true
  443. },
  444. number: {
  445. handler(newVal) {
  446. this.$emit('check', this.checked, newVal);
  447. }
  448. },
  449. again: {
  450. handler() {
  451. this.firstSelect();
  452. }
  453. }
  454. }
  455. }
  456. </script>
  457. <style scoped >
  458. .u-model {
  459. width: 750upx;
  460. }
  461. .u-top {
  462. margin: 0 24upx;
  463. border-bottom: 1upx solid #e2e2e2;
  464. height: 140upx;
  465. }
  466. .u-close-image {
  467. width: 54upx;
  468. height: 78upx;
  469. padding: 24upx 0 24rpx 24rpx;
  470. margin-left: 24rpx;
  471. }
  472. .bd-close-image {
  473. width: 30upx;
  474. height: 30upx;
  475. }
  476. .u-pic {
  477. width: 200rpx;
  478. height: 200rpx;
  479. padding: 4upx;
  480. border-radius: 8rpx;
  481. position: relative;
  482. top: -64upx;
  483. background-color: #ffffff;
  484. }
  485. .u-img {
  486. width: 192rpx;
  487. height: 192rpx;
  488. background-color: #ffffff;
  489. }
  490. .u-info {
  491. width: 424upx;
  492. height: 136upx;
  493. padding: 45upx 0 0 24upx;
  494. }
  495. .u-scroll-view {
  496. width: 100%;
  497. max-height: calc(80vh - 154upx);
  498. }
  499. .u-attr-group {
  500. margin:24upx 32upx 0 32upx;
  501. border-bottom: 1upx solid #e2e2e2;
  502. }
  503. .u-number {
  504. height: 124upx;
  505. margin: 0 32upx;
  506. }
  507. .u-group-name {
  508. margin-bottom: 20upx;
  509. }
  510. .u-group-item {
  511. padding: 15upx 24upx;
  512. border-radius: 8upx;
  513. margin:0 20upx 20upx 0;
  514. font-size: 26upx;
  515. }
  516. .u-checked {
  517. color: #ffffff;
  518. }
  519. .u-unchecked {
  520. background-color: #f2f2f2;
  521. color: #353535;
  522. }
  523. .u-attr_num_0 {
  524. background-color: #f7f7f7;
  525. color: #cdcdcd;
  526. }
  527. .u-stock {
  528. font-size: 24upx;
  529. color: #999999;
  530. }
  531. .u-price {
  532. margin-right: 12upx;
  533. }
  534. .u-input {
  535. width: 88upx;
  536. height: 60upx;
  537. background-color: #f7f7f7;
  538. font-size: 20upx;
  539. color: #353535;
  540. text-align: center;
  541. }
  542. .u-input-box {
  543. width: 218upx;
  544. }
  545. .u-number-btn {
  546. height: 60upx;
  547. width: 60upx;
  548. background-repeat: no-repeat;
  549. background-size: 100% 100%;
  550. background-position: center;
  551. }
  552. .u-number-btn:first-child {
  553. margin-right: 5upx;
  554. }
  555. .u-number-btn:last-child {
  556. margin-left: 5upx;
  557. }
  558. .u-reduced-1 {
  559. background-image: url("../../../static/image/icon/can-be-reduced.png");
  560. }
  561. .u-reduced-0 {
  562. background-image: url("../../../static/image/cart/can-be-reduced.png");
  563. }
  564. .u-added-1 {
  565. background-image: url("../../../static/image/cart/can-be-added.png");
  566. }
  567. .u-text {
  568. font-size: 26upx;
  569. color: #666666;
  570. }
  571. .u-bottom {
  572. height: 110upx;
  573. }
  574. .u-btn {
  575. text-align: center;
  576. line-height: 110upx;
  577. }
  578. .u-btn-color {
  579. color: #ffffff;
  580. }
  581. </style>