index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <up-popup :show="showPopup" :closeOnClickOverlay="true" @close="close" closeable>
  3. <view
  4. class="product-window"
  5. :class="(iSbnt ? 'join' : '') + ' ' + (iScart ? 'joinCart' : '')"
  6. >
  7. <view class="popup-title">请选择规格</view>
  8. <view class="textpic acea-row">
  9. <view class="pictrue">
  10. <image :src="attr.productSelect.image"></image>
  11. </view>
  12. <view class="text">
  13. <!-- <view class="line1">-->
  14. <!-- {{ attr.productSelect.storeName }}-->
  15. <!-- </view>-->
  16. <view class="money font-color">
  17. ¥<text class="num">{{ calcNumPrice }}</text>
  18. </view>
  19. <view class="font-color" >
  20. <text class="stock"
  21. >工费: {{ attr.productSelect.price || 0 }}元/g</text
  22. >
  23. <text class="stock"
  24. >附加费: {{ attr.productSelect.additionalAmount || "0" }}元</text
  25. >
  26. </view>
  27. <view class="font-color" style="display: flex;align-items: center;margin-top: 24rpx;" >
  28. <view class="carnum acea-row row-left">
  29. <view
  30. class="item reduce"
  31. :class="attr.productSelect.cart_num <= 1 ? 'on' : ''"
  32. @click="CartNumDes"
  33. >
  34. -
  35. </view>
  36. <view class="item num">
  37. <input
  38. type="number"
  39. v-model="attr.productSelect.cart_num"
  40. data-name="productSelect.cart_num"
  41. @input="bindCode(attr.productSelect.cart_num)"
  42. />
  43. </view>
  44. <view
  45. v-if="iSplus"
  46. class="item plus"
  47. :class="
  48. attr.productSelect.cart_num >= attr.productSelect.stock
  49. ? 'on'
  50. : ''
  51. "
  52. @click="CartNumAdd"
  53. >
  54. +
  55. </view>
  56. <view
  57. v-else
  58. class="item plus"
  59. :class="
  60. attr.productSelect.cart_num >= attr.productSelect.quota ||
  61. attr.productSelect.cart_num >= attr.productSelect.stock ||
  62. attr.productSelect.cart_num >= attr.productSelect.num
  63. ? 'on'
  64. : ''
  65. "
  66. @click="CartNumAdd"
  67. >+</view
  68. >
  69. </view>
  70. <text class="stock font333" v-if="isShow"
  71. >库存: {{ attr.productSelect.stock }}</text
  72. >
  73. <text class="stock font333" v-if="limitNum"
  74. >限量: {{ attr.productSelect.quota }}</text
  75. >
  76. </view>
  77. </view>
  78. <view class="iconfont icon-guanbi" @click="close"></view>
  79. </view>
  80. <view class="rollTop">
  81. <view class="productWinList">
  82. <view
  83. class="item"
  84. v-for="(item, indexw) in attr.productAttr"
  85. :key="indexw"
  86. >
  87. <view class="title">{{ item.attrName }}</view>
  88. <view class="listn acea-row row-middle">
  89. <view
  90. class="itemn"
  91. :class="item.index === itemn ? 'on' : ''"
  92. v-for="(itemn, indexn) in item.attrValues"
  93. @click="tapAttr(indexw, indexn)"
  94. :key="indexn"
  95. >
  96. {{ itemn }}
  97. </view>
  98. </view>
  99. </view>
  100. </view>
  101. <!-- <view class="cart acea-row row-between-wrapper">-->
  102. <!-- <view class="title">数量</view>-->
  103. <!-- <view class="carnum acea-row row-left">-->
  104. <!-- <view-->
  105. <!-- class="item reduce"-->
  106. <!-- :class="attr.productSelect.cart_num <= 1 ? 'on' : ''"-->
  107. <!-- @click="CartNumDes"-->
  108. <!-- >-->
  109. <!-- - -->
  110. <!-- </view>-->
  111. <!-- <view class="item num">-->
  112. <!-- <input-->
  113. <!-- type="number"-->
  114. <!-- v-model="attr.productSelect.cart_num"-->
  115. <!-- data-name="productSelect.cart_num"-->
  116. <!-- @input="bindCode(attr.productSelect.cart_num)"-->
  117. <!-- />-->
  118. <!-- </view>-->
  119. <!-- <view-->
  120. <!-- v-if="iSplus"-->
  121. <!-- class="item plus"-->
  122. <!-- :class="-->
  123. <!-- attr.productSelect.cart_num >= attr.productSelect.stock-->
  124. <!-- ? 'on'-->
  125. <!-- : ''-->
  126. <!-- "-->
  127. <!-- @click="CartNumAdd"-->
  128. <!-- >-->
  129. <!-- +-->
  130. <!-- </view>-->
  131. <!-- <view-->
  132. <!-- v-else-->
  133. <!-- class="item plus"-->
  134. <!-- :class="-->
  135. <!-- attr.productSelect.cart_num >= attr.productSelect.quota ||-->
  136. <!-- attr.productSelect.cart_num >= attr.productSelect.stock ||-->
  137. <!-- attr.productSelect.cart_num >= attr.productSelect.num-->
  138. <!-- ? 'on'-->
  139. <!-- : ''-->
  140. <!-- "-->
  141. <!-- @click="CartNumAdd"-->
  142. <!-- >+</view-->
  143. <!-- >-->
  144. <!-- </view>-->
  145. <!-- </view>-->
  146. </view>
  147. <view class="footer-box">
  148. <button
  149. v-if="attr.productSelect.stock > 0"
  150. class="confirm-btn"
  151. @click="submit"
  152. type="primary"
  153. >确定</button>
  154. <button
  155. v-else
  156. class="confirm-btn"
  157. @click="submit"
  158. type="primary"
  159. >已售罄</button>
  160. </view>
  161. </view>
  162. </up-popup>
  163. </template>
  164. <script setup>
  165. import { computed,watch } from "vue";
  166. const props = defineProps({
  167. attr: {
  168. type: Object,
  169. default: () => ({}),
  170. },
  171. showPopup: {
  172. type: Boolean,
  173. default: false,
  174. },
  175. limitNum: {
  176. type: Number,
  177. default: 0,
  178. },
  179. isShow: {
  180. type: Number,
  181. default: 0,
  182. },
  183. iSbnt: {
  184. type: Number,
  185. default: 0,
  186. },
  187. iSplus: {
  188. type: Number,
  189. default: 0,
  190. },
  191. iScart: {
  192. type: Number,
  193. default: 0,
  194. },
  195. });
  196. const emit = defineEmits([
  197. "goCat",
  198. "iptCartNum",
  199. "myevent",
  200. "ChangeCartNum",
  201. "attrVal",
  202. "ChangeAttr",
  203. "submit",
  204. "closePopup",
  205. "updatePrice"
  206. ]);
  207. const calcNumPrice = computed(() => {
  208. // 检查数据是否存在
  209. if (!props.attr?.productSelect) {
  210. return "0.00";
  211. }
  212. const { price, cart_num, additionalAmount, weight } =
  213. props.attr.productSelect;
  214. // 计算总价并保留两位小数
  215. const total =
  216. (Number(price) * Number(weight) + Number(additionalAmount || 0)) *
  217. Number(cart_num);
  218. // 向上取整到小数点后一位
  219. const roundedTotal = Math.ceil(total * 10) / 10;
  220. return roundedTotal.toFixed(2);
  221. });
  222. // 监听 calcNumPrice 变化并传递给父组件
  223. watch(calcNumPrice, (newPrice) => {
  224. console.log('newPrice',newPrice)
  225. emit("updatePrice", newPrice);
  226. }, { immediate: true });
  227. function goCat() {
  228. emit("goCat");
  229. }
  230. function bindCode() {
  231. emit("iptCartNum", props.attr.productSelect.cart_num);
  232. }
  233. function close() {
  234. emit("closePopup");
  235. }
  236. function submit() {
  237. emit("submit");
  238. }
  239. function CartNumDes() {
  240. emit("ChangeCartNum", false);
  241. }
  242. function CartNumAdd() {
  243. emit("ChangeCartNum", true);
  244. }
  245. function tapAttr(indexw, indexn) {
  246. emit("attrVal", { indexw, indexn });
  247. // 直接修改 props 不推荐,建议父组件传递响应式对象
  248. if (props.attr.productAttr && props.attr.productAttr[indexw]) {
  249. props.attr.productAttr[indexw].index =
  250. props.attr.productAttr[indexw].attrValues[indexn];
  251. }
  252. const value = getCheckedValue().join(",");
  253. emit("ChangeAttr", value);
  254. }
  255. function getCheckedValue() {
  256. const productAttr = props.attr.productAttr || [];
  257. const value = [];
  258. for (let i = 0; i < productAttr.length; i++) {
  259. for (let j = 0; j < productAttr[i].attrValues.length; j++) {
  260. if (productAttr[i].index === productAttr[i].attrValues[j]) {
  261. value.push(productAttr[i].attrValues[j]);
  262. }
  263. }
  264. }
  265. return value;
  266. }
  267. </script>
  268. <style scoped lang="scss">
  269. .product-window {
  270. width: 100%;
  271. background-color: #fff;
  272. border-radius: 16rpx 16rpx 0 0;
  273. padding-bottom: 40rpx;
  274. &.join,
  275. &.joinCart {
  276. padding-bottom: 30rpx;
  277. }
  278. &.joinCart {
  279. z-index: 999;
  280. }
  281. .textpic {
  282. padding: 0 30rpx 0 30rpx;
  283. margin-top: 29rpx;
  284. position: relative;
  285. align-items: center;
  286. .pictrue {
  287. width: 150rpx;
  288. height: 150rpx;
  289. image {
  290. width: 100%;
  291. height: 100%;
  292. border-radius: 10rpx;
  293. }
  294. }
  295. .text {
  296. //width: 430rpx;
  297. width: 70%;
  298. font-size: 32rpx;
  299. color: #333333;
  300. padding-left: 30rpx;
  301. .money {
  302. font-size: 24rpx;
  303. //margin-top: 40rpx;
  304. }
  305. .num {
  306. font-size: 36rpx;
  307. }
  308. .stock {
  309. font-size: 24rpx;
  310. color: #666;
  311. margin-left: 18rpx;
  312. }
  313. }
  314. .iconfont {
  315. position: absolute;
  316. right: 30rpx;
  317. top: -5rpx;
  318. font-size: 35rpx;
  319. color: #8a8a8a;
  320. }
  321. }
  322. .rollTop {
  323. max-height: 500rpx;
  324. overflow: auto;
  325. margin-top: 36rpx;
  326. }
  327. .footer-box {
  328. padding: 0 30rpx;
  329. margin: 30rpx 0 0 0;
  330. .confirm-btn {
  331. height: 88rpx;
  332. line-height: 88rpx;
  333. color: #333;
  334. border-radius: 16rpx;
  335. background-color: #F8C008;
  336. border-color: #F8C008;
  337. }
  338. }
  339. .productWinList {
  340. .item {
  341. & + .item {
  342. margin-top: 36rpx;
  343. }
  344. .title {
  345. font-size: 28rpx;
  346. color: #333;
  347. padding: 0 30rpx;
  348. }
  349. .listn {
  350. padding: 0 30rpx 0 16rpx;
  351. .itemn {
  352. height: 60rpx;
  353. line-height: 56rpx;
  354. border: 1px solid #f2f2f2;
  355. font-size: 26rpx;
  356. color: #282828;
  357. padding: 0rpx 33rpx;
  358. border-radius: 10rpx;
  359. margin: 20rpx 0 0 14rpx;
  360. background-color: #f2f2f2;
  361. &.on {
  362. color: $theme-color;
  363. background: rgba(248, 192, 8, 0.10);
  364. border-color: $border-color;
  365. }
  366. &.limit {
  367. color: #999;
  368. text-decoration: line-through;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. .cart {
  375. margin-top: 36rpx;
  376. padding: 0 30rpx;
  377. .title {
  378. font-size: 30rpx;
  379. color: #999;
  380. }
  381. }
  382. .carnum {
  383. height: 54rpx;
  384. border: 2rpx solid #DCDFE6;
  385. display: inline-flex;
  386. border-radius: 4rpx;
  387. view {
  388. width: 84rpx;
  389. text-align: center;
  390. height: 100%;
  391. line-height: 54rpx;
  392. color: #282828;
  393. border-left: 2rpx solid #DCDFE6;
  394. border-right: 2rpx solid #DCDFE6;
  395. }
  396. .reduce {
  397. width: 58rpx;
  398. border: 0;
  399. //border-radius: 4rpx 0 0 4rpx;
  400. line-height: 48rpx;
  401. background: #F5F7FA;
  402. &.on {
  403. color: #8C8C8C;
  404. font-size: 44rpx;
  405. background-color: #DCDFE6;
  406. }
  407. }
  408. .plus {
  409. width: 58rpx;
  410. border: 0;
  411. //border-radius: 0 4rpx 4rpx 0;
  412. line-height: 48rpx;
  413. background: #F5F7FA;
  414. &.on {
  415. border-color: #e3e3e3;
  416. color: #8C8C8C;
  417. background-color: #DCDFE6;
  418. }
  419. }
  420. .num {
  421. background: #fff;
  422. color: #333;
  423. font-size: 24rpx;
  424. //border-radius: 12rpx;
  425. line-height: 29px;
  426. height: 50rpx;
  427. input {
  428. display: -webkit-inline-box;
  429. }
  430. }
  431. }
  432. .joinBnt {
  433. font-size: 30rpx;
  434. width: 620rpx;
  435. height: 86rpx;
  436. border-radius: 50rpx;
  437. text-align: center;
  438. line-height: 86rpx;
  439. color: #fff;
  440. margin: 21rpx auto 0 auto;
  441. &.on {
  442. background-color: #bbb;
  443. color: #fff;
  444. }
  445. }
  446. }
  447. .popup-title{
  448. height: 100rpx;
  449. line-height: 100rpx;
  450. font-size: 36rpx;
  451. color: #333;
  452. text-align: center;
  453. }
  454. </style>