index.vue 9.0 KB

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