u-attr.vue 21 KB

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