buy.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <template>
  2. <view class="container">
  3. <!-- 顶部背景区域 -->
  4. <view class="header-bg">
  5. <view class="balance-info">
  6. <text class="balance-title">可用余额</text>
  7. <text class="balance-amount">{{ appStore.$userInfo.nowMoney }}</text>
  8. </view>
  9. </view>
  10. <!-- 主要内容区域 -->
  11. <view class="content">
  12. <!-- Tab栏(已移入content顶部) -->
  13. <view class="tab-container">
  14. <view
  15. class="tab-item"
  16. :class="{ active: activeTab.index === index }"
  17. v-for="(tab, index) in tabs"
  18. :key="index"
  19. @click="switchTab(tab)"
  20. >
  21. {{ tab.name }}
  22. </view>
  23. </view>
  24. <!-- 权益标签 -->
  25. <!-- <view class="rights-tag">权益 -0</view> -->
  26. <!-- 选择克重标题 -->
  27. <view class="section-title">
  28. <text class="title-text">选择克重</text>
  29. <view class="real-price-wrapper">
  30. <view class="label">
  31. <text class="real-price-label">实时金价</text>
  32. <text class="real-price-value" v-show="activeTab.index === 0">{{
  33. realGoldprice
  34. }}</text>
  35. <text class="real-price-value" v-show="activeTab.index === 1">{{
  36. realPtprice
  37. }}</text>
  38. <text class="real-price-value" v-show="activeTab.index === 2">{{
  39. realAgprice
  40. }}</text>
  41. <text class="real-price-unit">/g</text>
  42. </view>
  43. <view class="rights-tag-ellipse">
  44. <text v-show="activeTab.index === 0"
  45. >权益 -{{ goldAdjustPrice }}</text
  46. >
  47. <text v-show="activeTab.index === 1"
  48. >权益 -{{ PtAdjustPrice }}</text
  49. >
  50. <text v-show="activeTab.index === 2"
  51. >权益 -{{ AgAdjustPrice }}</text
  52. >
  53. <view class="ellipse-arrow"></view>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 克重选择按钮 -->
  58. <view class="weight-grid">
  59. <view
  60. class="weight-item"
  61. :class="{ active: selectedWeight === weight }"
  62. v-for="weight in weightOptions"
  63. :key="weight"
  64. @click="selectWeight(weight)"
  65. >
  66. {{ weight }}克
  67. </view>
  68. </view>
  69. <!-- 自定义输入区域 -->
  70. <view class="custom-input-section">
  71. <view class="input-container">
  72. <input
  73. class="custom-input"
  74. type="number"
  75. v-model="customWeight"
  76. placeholder="请输入克数"
  77. @input="onInputChange"
  78. />
  79. <text class="input-unit">g</text>
  80. </view>
  81. </view>
  82. <!-- 预计金额 -->
  83. <view class="total-amount">
  84. <text>预计金额 {{ estimatePrice }} 元</text>
  85. </view>
  86. <!-- 协议选择 -->
  87. <view
  88. class="agreement-section"
  89. style="display: flex; align-items: center"
  90. >
  91. <up-checkbox
  92. v-model:checked="agreedToTerms"
  93. shape="square"
  94. activeColor="#e9c279"
  95. usedAlone
  96. :customStyle="{ marginRight: '12rpx' }"
  97. >
  98. <template #label>
  99. <text class="agreement-text"> 阅读并同意 </text>
  100. </template>
  101. </up-checkbox>
  102. <view class="agreement-link" @click="showAgreement">《余料买入协议》</view>
  103. </view>
  104. <!-- 提交按钮 -->
  105. <view class="submit-section">
  106. <button
  107. class="submit-btn"
  108. :class="{ disabled: !canSubmit }"
  109. :disabled="!canSubmit"
  110. @click="handleSubmit"
  111. >
  112. 立即购买
  113. </button>
  114. </view>
  115. </view>
  116. <!-- 协议弹窗 -->
  117. <up-popup
  118. :show="showAgreementPopup"
  119. mode="center"
  120. border-radius="20"
  121. width="80%"
  122. height="60%"
  123. >
  124. <view class="popup-content">
  125. <view class="popup-header">
  126. <!-- <text class="popup-title">买金协议</text> -->
  127. <!-- <text class="popup-close" @click="closeAgreement">×</text> -->
  128. </view>
  129. <scroll-view scroll-y scroll-left="50" class="popup-body">
  130. <view class="agreement-content">
  131. <Aggrement />
  132. </view>
  133. </scroll-view>
  134. <view class="popup-footer">
  135. <button class="popup-btn" @click="closeAgreement">
  136. 我已详细知悉
  137. </button>
  138. </view>
  139. </view>
  140. </up-popup>
  141. <!-- 未实名提示 -->
  142. <up-modal
  143. :show="showDetectModal"
  144. title="当前用户未实名"
  145. :showCancelButton="true"
  146. confirmText="去认证"
  147. @confirm="toFaceVerify"
  148. @cancel="showDetectModal = false"
  149. @close="showDetectModal = false"
  150. />
  151. </view>
  152. </template>
  153. <script setup>
  154. import { ref, computed, watch } from "vue";
  155. import { onShow } from "@dcloudio/uni-app";
  156. import useRealGoldPrice from "@/hooks/useRealGoldPrice";
  157. import { buyGold } from "@/api/vault";
  158. import { getUserInfo } from "@/api/user";
  159. import { useAppStore } from "@/stores/app";
  160. import { useToast } from '@/hooks/useToast'
  161. import Aggrement from './aggrement.vue'
  162. const appStore = useAppStore();
  163. const { Toast } = useToast()
  164. const {
  165. realGoldprice,
  166. realPtprice,
  167. realAgprice,
  168. fetchGoldPrice,
  169. goldAdjustPrice,
  170. PtAdjustPrice,
  171. AgAdjustPrice,
  172. } = useRealGoldPrice();
  173. onShow(() => {
  174. fetchGoldPrice();
  175. });
  176. // const tabs = ["黄金", "铂金", "白银"];
  177. const tabs = [
  178. { name: "黄金", mentalType: 1, index: 0 },
  179. { name: "铂金", mentalType: 2, index: 1 },
  180. { name: "白银", mentalType: 3, index: 2 },
  181. ];
  182. const activeTab = ref(tabs[0]);
  183. const selectedWeight = ref(0);
  184. const customWeight = ref("");
  185. const currentPrice = ref("775.53");
  186. const agreedToTerms = ref(false);
  187. const showAgreementPopup = ref(false);
  188. const showDetectModal = ref(false)
  189. // 克重选项
  190. const weightOptions = [10, 20, 30, 50, 100, 200, 300, 500, 1000];
  191. // 计算总金额
  192. const totalAmount = computed(() => {
  193. const weight = selectedWeight.value || parseFloat(customWeight.value) || 0;
  194. const price = parseFloat(currentPrice.value);
  195. return (weight * price).toFixed(1);
  196. });
  197. // 是否可以提交
  198. const canSubmit = computed(() => {
  199. const hasWeight =
  200. selectedWeight.value > 0 ||
  201. (customWeight.value && parseFloat(customWeight.value) > 0);
  202. return hasWeight;
  203. });
  204. // 切换黄金类型
  205. const switchTab = (tab) => {
  206. console.log("tab", tab.name);
  207. if (activeTab.value.name === tab.name) return;
  208. activeTab.value = tab;
  209. if (tab.name === "铂金" && PtAdjustPrice.value === 0) {
  210. fetchGoldPrice("RTJ_Pt");
  211. } else if (tab.name === "白银" && AgAdjustPrice.value === 0) {
  212. fetchGoldPrice("RTJ_Ag");
  213. }
  214. };
  215. const selectWeight = (weight) => {
  216. selectedWeight.value = weight;
  217. customWeight.value = "";
  218. };
  219. const onInputChange = (e) => {
  220. const value = e.detail.value;
  221. customWeight.value = value;
  222. if (value) {
  223. selectedWeight.value = 0;
  224. }
  225. };
  226. const realWeight = computed(() => {
  227. return selectedWeight.value === 0 ? customWeight.value : selectedWeight.value;
  228. });
  229. // 预估价
  230. const estimatePrice = computed(() => {
  231. if (!realWeight.value) return 0;
  232. if (activeTab.value.name === "黄金") {
  233. return (realWeight.value * realGoldprice.value).toFixed(2);
  234. } else if (activeTab.value.name === "铂金") {
  235. return (realWeight.value * realPtprice.value).toFixed(2);
  236. } else if (activeTab.value.name === "白银") {
  237. return (realWeight.value * realAgprice.value).toFixed(2);
  238. }
  239. return 0;
  240. });
  241. const showAgreement = () => {
  242. showAgreementPopup.value = true;
  243. };
  244. const closeAgreement = () => {
  245. showAgreementPopup.value = false;
  246. };
  247. function toFaceVerify() {
  248. showDetectModal.value = false
  249. uni.navigateTo({ url: '/pages/users/face_detect/index' })
  250. }
  251. const handleSubmit = async () => {
  252. if (!canSubmit.value) return;
  253. try {
  254. const priceMap = {
  255. 0: realGoldprice.value,
  256. 1: realPtprice.value,
  257. 2: realAgprice.value,
  258. };
  259. if (!agreedToTerms.value) {
  260. return Toast({ title: "请阅读并同意协议" });
  261. }
  262. // 判断是否认证
  263. if (!appStore.userPanelInfoGetter.realNameVerified) {
  264. showDetectModal.value = true
  265. return
  266. }
  267. if (Number(priceMap[activeTab.value.index]) > Number(appStore.$userInfo.nowMoney)) {
  268. return Toast({ title: "余额不足" });
  269. }
  270. uni.showLoading({title: '加载中'})
  271. const weight = selectedWeight.value || parseFloat(customWeight.value);
  272. const params = {
  273. userId: appStore.uid,
  274. operationType: 4, // 4 - 买金
  275. weight,
  276. price: priceMap[activeTab.value.index],
  277. metalType: activeTab.value.mentalType,
  278. type: 1,
  279. };
  280. await buyGold(params);
  281. const { data } = await getUserInfo()
  282. appStore.UPDATE_USERINFO(data);
  283. uni.showToast({
  284. title: `购买成功`,
  285. icon: "success",
  286. });
  287. } catch (error) {
  288. console.error("buyGold", error);
  289. const title = typeof error === 'string' ? error : '购买失败'
  290. uni.showToast({
  291. title,
  292. icon: "error",
  293. });
  294. } finally {
  295. uni.hideLoading()
  296. }
  297. };
  298. // 监听自定义输入
  299. watch(customWeight, (newVal) => {
  300. if (newVal) {
  301. selectedWeight.value = 0;
  302. }
  303. });
  304. </script>
  305. <style lang="scss" scoped>
  306. // $header-color: #e9c279;
  307. // $primary-color: #e9c279;
  308. // $text-color: #333;
  309. // $light-text: #666;
  310. // $border-color: #f0f0f0;
  311. .container {
  312. min-height: 90vh;
  313. background-color: #f8f8f8;
  314. }
  315. .header-bg {
  316. background-image: linear-gradient(
  317. to bottom,
  318. #e9c279 0%,
  319. #e9c279 10%,
  320. #f0dab2 70%,
  321. transparent 100%
  322. );
  323. padding: 40rpx 30rpx 30rpx;
  324. height: 400rpx;
  325. position: relative;
  326. }
  327. .balance-info {
  328. text-align: center;
  329. margin-bottom: 60rpx;
  330. .balance-title {
  331. display: block;
  332. color: white;
  333. font-size: 28rpx;
  334. margin-bottom: 20rpx;
  335. }
  336. .balance-amount {
  337. display: block;
  338. color: white;
  339. font-size: 80rpx;
  340. font-weight: bold;
  341. }
  342. }
  343. .tab-container {
  344. display: flex;
  345. justify-content: center;
  346. gap: 60rpx;
  347. margin-top: 0;
  348. margin-bottom: 40rpx;
  349. // border-bottom: 2rpx solid #f0f0f0;
  350. }
  351. .tab-item {
  352. color: #333;
  353. font-size: 32rpx;
  354. padding: 20rpx 0;
  355. position: relative;
  356. cursor: pointer;
  357. background: transparent;
  358. font-weight: normal;
  359. transition: color 0.2s;
  360. &.active {
  361. font-weight: bold;
  362. color: #e9c279;
  363. &::after {
  364. content: "";
  365. position: absolute;
  366. left: 0;
  367. right: 0;
  368. bottom: -2rpx;
  369. height: 6rpx;
  370. background: #e9c279;
  371. border-radius: 3rpx 3rpx 0 0;
  372. z-index: 1;
  373. }
  374. }
  375. }
  376. .content {
  377. background: white;
  378. margin: -90rpx 0 0;
  379. border-radius: 20rpx;
  380. padding: 10rpx 30rpx;
  381. position: relative;
  382. z-index: 10;
  383. // box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  384. }
  385. .section-title {
  386. display: flex;
  387. justify-content: space-between;
  388. align-items: center;
  389. margin-bottom: 40rpx;
  390. .title-text {
  391. font-size: 32rpx;
  392. font-weight: bold;
  393. color: #333;
  394. }
  395. }
  396. .real-price-wrapper {
  397. position: relative;
  398. align-items: center;
  399. height: 60rpx;
  400. .label {
  401. display: flex;
  402. }
  403. }
  404. .real-price-label {
  405. font-size: 28rpx;
  406. margin-right: 6rpx;
  407. color: #e9c279;
  408. }
  409. .real-price-value {
  410. font-size: 32rpx;
  411. color: #e9c279;
  412. font-weight: bold;
  413. margin: 0 2rpx;
  414. }
  415. .real-price-unit {
  416. font-size: 28rpx;
  417. margin-left: 2rpx;
  418. color: #e9c279;
  419. }
  420. .rights-tag-ellipse {
  421. position: absolute;
  422. display: inline-block;
  423. left: 70%;
  424. top: -100rpx;
  425. transform: translateX(-50%);
  426. background: #e9c279;
  427. color: white;
  428. padding: 0 14rpx;
  429. border-radius: 12rpx;
  430. font-size: 22rpx;
  431. z-index: 2;
  432. white-space: nowrap;
  433. text-align: center;
  434. min-width: 90rpx;
  435. min-height: 40rpx;
  436. line-height: 40rpx;
  437. position: relative;
  438. }
  439. .ellipse-arrow {
  440. position: absolute;
  441. left: 40%;
  442. bottom: -14rpx;
  443. transform: translateX(-50%) rotate(20deg);
  444. width: 0;
  445. height: 0;
  446. border-left: 16rpx solid transparent;
  447. border-right: 16rpx solid transparent;
  448. border-top: 28rpx solid #e9c279;
  449. z-index: -1;
  450. }
  451. .weight-grid {
  452. display: grid;
  453. grid-template-columns: repeat(3, 1fr);
  454. gap: 20rpx;
  455. margin-bottom: 40rpx;
  456. }
  457. .weight-item {
  458. background: #f8f8f8;
  459. border: 2rpx solid #f0f0f0;
  460. border-radius: 12rpx;
  461. padding: 30rpx;
  462. text-align: center;
  463. font-size: 32rpx;
  464. color: #666;
  465. cursor: pointer;
  466. transition: all 0.3s;
  467. &.active {
  468. background: #e9c279;
  469. color: white;
  470. border-color: #e9c279;
  471. }
  472. &:hover {
  473. border-color: #e9c279;
  474. }
  475. }
  476. .custom-input-section {
  477. margin-bottom: 40rpx;
  478. }
  479. .input-container {
  480. display: flex;
  481. align-items: center;
  482. background: #f8f8f8;
  483. border-radius: 12rpx;
  484. padding: 20rpx;
  485. .custom-input {
  486. flex: 1;
  487. background: transparent;
  488. border: none;
  489. font-size: 32rpx;
  490. color: #333;
  491. &::placeholder {
  492. color: #ccc;
  493. }
  494. }
  495. .input-unit {
  496. font-size: 32rpx;
  497. color: #666;
  498. margin-left: 20rpx;
  499. }
  500. }
  501. .total-amount {
  502. text-align: center;
  503. font-size: 36rpx;
  504. color: #e9c279;
  505. font-weight: bold;
  506. margin-bottom: 60rpx;
  507. }
  508. .agreement-section {
  509. margin-bottom: 40rpx;
  510. display: flex;
  511. align-items: center;
  512. }
  513. .agreement-text {
  514. font-size: 28rpx;
  515. color: #666;
  516. }
  517. .agreement-link {
  518. color: #e9c279;
  519. text-decoration: underline;
  520. margin-left: 4rpx;
  521. }
  522. .submit-section {
  523. margin-top: 40rpx;
  524. }
  525. .submit-btn {
  526. width: 100%;
  527. background: #e9c279;
  528. color: white;
  529. border: none;
  530. border-radius: 12rpx;
  531. padding: 30rpx;
  532. font-size: 32rpx;
  533. font-weight: bold;
  534. &.disabled {
  535. background: #ccc;
  536. cursor: not-allowed;
  537. }
  538. }
  539. // 弹窗样式
  540. .popup-content {
  541. width: 80vw;
  542. padding: 40rpx;
  543. height: 80vh;
  544. // display: flex;
  545. // flex-direction: column;
  546. }
  547. .popup-header {
  548. display: flex;
  549. justify-content: space-between;
  550. align-items: center;
  551. margin-bottom: 30rpx;
  552. .popup-title {
  553. font-size: 36rpx;
  554. font-weight: bold;
  555. color: #333;
  556. }
  557. .popup-close {
  558. font-size: 48rpx;
  559. color: #666;
  560. cursor: pointer;
  561. }
  562. }
  563. .popup-body {
  564. height: 62vh;
  565. // flex: 1;
  566. // min-height: 0; // 关键,防止flex塌陷
  567. .agreement-content {
  568. font-size: 28rpx;
  569. line-height: 1.6;
  570. color: #666;
  571. }
  572. }
  573. .popup-footer {
  574. margin-top: 30rpx;
  575. .popup-btn {
  576. width: 100%;
  577. background: #e9c279;
  578. color: white;
  579. border: none;
  580. border-radius: 12rpx;
  581. padding: 10rpx 30rpx;
  582. font-size: 32rpx;
  583. }
  584. }
  585. </style>