M_purchase.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <template>
  2. <view class="page">
  3. <view class="topBody">
  4. <PageHeader title="M币充值" class="PageHeader">
  5. <template v-slot:right>
  6. <DropdownMenu
  7. :options="dropdownOptions"
  8. @select="handleDropdownSelect"
  9. />
  10. </template>
  11. </PageHeader>
  12. <view class="reserveASeat"></view>
  13. <view class="myGoldCoin-box">
  14. <view>我的M币</view>
  15. <view class="myGoldCoin-box-content">
  16. <image src="../../static/icon/coin_m.png" mode="widthFix" />
  17. <text>{{ myGoldCoin }}</text>
  18. </view>
  19. </view>
  20. <view class="myinfo">
  21. <view class="purchaseList" style="margin-top: 32rpx">
  22. <view
  23. class="item"
  24. :class="index == sel ? 'itemSel' : ''"
  25. v-for="(item, index) in list"
  26. @click="selTA(item, index)"
  27. :key="index"
  28. >
  29. <view class="num1">
  30. <image src="../../static/icon/coin_m.png" mode="widthFix" />
  31. <view class="name">{{ item.num_gmm| formatNumberToK }} </view>
  32. </view>
  33. <view class="num2">
  34. <view class="name">{{ $t("txt.¥") }}{{ item.money }}</view>
  35. </view>
  36. </view>
  37. </view>
  38. <view v-if="!isIOS" class="jinchu">
  39. <text>{{ $t("txt.支付方式") }}</text>
  40. </view>
  41. <view v-if="isWeChatPay && !isIOS" class="mingxiList" @click="selPay('wechat')">
  42. <view class="left">
  43. <image
  44. class="icon"
  45. src="../../static/me/icon_wechat.png"
  46. mode="widthFix"
  47. />
  48. <text style="font-size: 28rpx; margin-left: 20rpx">{{
  49. $t("txt.微信支付")
  50. }}</text>
  51. </view>
  52. <view class="right">
  53. <image
  54. class="icon"
  55. :src="
  56. payType != 'wechat'
  57. ? '../../static/icon/wd_icon_gouxuan04.png'
  58. : '../../static/icon/wd_icon_gouxuan05.png'
  59. "
  60. />
  61. </view>
  62. </view>
  63. <!-- <view class="line"></view> -->
  64. <view v-if="!isIOS" class="mingxiList" @click="selPay('alipay')">
  65. <view class="left">
  66. <image
  67. class="icon"
  68. src="../../static/me/icon_alipay.png"
  69. mode="widthFix"
  70. />
  71. <text style="font-size: 28rpx; margin-left: 20rpx">{{
  72. $t("txt.支付宝支付")
  73. }}</text>
  74. </view>
  75. <view class="right">
  76. <image
  77. class="icon"
  78. :src="
  79. payType != 'alipay'
  80. ? '../../static/icon/wd_icon_gouxuan04.png'
  81. : '../../static/icon/wd_icon_gouxuan05.png'
  82. "
  83. />
  84. </view>
  85. </view>
  86. <!-- <view class="line"></view> -->
  87. </view>
  88. <view class="agree">
  89. <view class="agree2" @click="agreeChk()">
  90. <image
  91. src="../../static/icon/wd_icon_gouxuan04.png"
  92. v-if="is_agree == 0"
  93. ></image>
  94. <image
  95. src="../../static/icon/wd_icon_gouxuan05.png"
  96. v-if="is_agree == 1"
  97. ></image>
  98. </view>
  99. <view>
  100. 同意
  101. <text class="xy" @click="goPage('/pages/AboutUs/pay_xy')">
  102. 《充值服务协议》 </text
  103. >,充值M币仅【萌创星球】使用,点击查看
  104. <text class="xy" @click="goPage('/pages/vip/record?type=coin')"> 充值记录 </text>
  105. </view>
  106. </view>
  107. <view class="btn_submit" :class="{'btn-loading': isSubmitting}" @click="submitData">
  108. <text v-if="!isSubmitting">¥{{ money }}</text>
  109. <text v-if="!isSubmitting">确认充值</text>
  110. <view v-if="isSubmitting" class="loading-spinner"></view>
  111. </view>
  112. <view class="blankHeight"></view>
  113. </view>
  114. <!-- 提示框 -->
  115. <DialogBox ref="DialogBox"></DialogBox>
  116. </view>
  117. </template>
  118. <script>
  119. import DropdownMenu from '@/components/DropdownMenu.vue'
  120. import { mapState } from 'vuex'
  121. export default {
  122. components: {
  123. DropdownMenu
  124. },
  125. computed: {
  126. ...mapState('hideModule', ['isWeChatPay'])
  127. },
  128. data() {
  129. return {
  130. title: "",
  131. sel: 1,
  132. payType: "wechat",
  133. list: [],
  134. money: 0,
  135. tid: 0,
  136. linkid: "",
  137. is_agree: 0,
  138. myGoldCoin: 0,
  139. dropdownOptions: [
  140. { label: '购买记录', type: 'vipRecord' }
  141. ],
  142. isSubmitting: false,
  143. lastClickTime: 0,
  144. isIOS: false, // 是否为iOS系统
  145. currentOrderId: "" // 当前苹果内购订单ID
  146. };
  147. },
  148. onLoad() {
  149. // setTimeout(function() {
  150. // uni.setNavigationBarColor({
  151. // frontColor: '#ffffff',
  152. // backgroundColor: '#00000000',
  153. // animation: {
  154. // duration: 400,
  155. // timingFunc: 'easeIn'
  156. // }
  157. // })
  158. // }, 200);
  159. },
  160. onShow() {
  161. this.checkPlatform();
  162. this.loadData();
  163. let that = this;
  164. },
  165. methods: {
  166. onBack() {},
  167. goPage(page) {
  168. uni.navigateTo({
  169. url: page,
  170. });
  171. },
  172. selTA(item, se) {
  173. this.tid = item.id;
  174. this.sel = se;
  175. if (this.list != null && this.list != undefined) {
  176. this.money = this.list[se]["money"];
  177. }
  178. },
  179. selPay(se) {
  180. this.payType = se;
  181. },
  182. chkSel() {
  183. if (this.sel == 1) {
  184. this.sel = 0;
  185. } else {
  186. this.sel = 1;
  187. }
  188. },
  189. submitData() {
  190. const now = Date.now();
  191. if (now - this.lastClickTime < 3000) {
  192. uni.showToast({
  193. title: "请勿频繁点击",
  194. icon: "none"
  195. });
  196. return;
  197. }
  198. this.lastClickTime = now;
  199. if (this.isSubmitting) return;
  200. if (this.is_agree == 0) {
  201. uni.showToast({
  202. title: "请确认并选择协议",
  203. icon: "none",
  204. });
  205. return;
  206. }
  207. this.isSubmitting = true;
  208. let that = this;
  209. // iOS系统使用苹果内购
  210. if (this.isIOS) {
  211. this.submitAppleIap();
  212. return;
  213. }
  214. uni.request({
  215. url: this.$apiHost + "/Order/submit",
  216. data: {
  217. uuid: getApp().globalData.uuid,
  218. product_id: this.tid,
  219. type: "buyM",
  220. payType: this.payType,
  221. },
  222. header: {
  223. "content-type": "application/json",
  224. },
  225. success: (res) => {
  226. console.log("res-pay", res.data);
  227. if (res.data.success == "yes") {
  228. this.linkid = res.data.linkid;
  229. // 微信支付逻辑
  230. if (this.payType === "wechat") {
  231. uni.requestPayment({
  232. provider: "wxpay",
  233. orderInfo: {
  234. appid: res.data.wepay.appid,
  235. partnerid: res.data.wepay.partnerid,
  236. prepayid: res.data.wepay.prepayid,
  237. package: "Sign=WXPay",
  238. noncestr: res.data.wepay.noncestr,
  239. timestamp: res.data.wepay.timestamp,
  240. sign: res.data.wepay.sign
  241. },
  242. success(res) {
  243. console.log("微信支付成功:", res);
  244. setTimeout(function () {
  245. that.showPayCall();
  246. }, 1000);
  247. },
  248. fail(e) {
  249. console.log("微信支付失败:", e);
  250. uni.showToast({
  251. title: "支付失败,请重试",
  252. icon: "none"
  253. });
  254. }
  255. });
  256. }
  257. // 支付宝支付逻辑
  258. else if (this.payType === "alipay") {
  259. uni.requestPayment({
  260. provider: "alipay",
  261. orderInfo: res.data.ali_pay, // 直接使用后端返回的支付宝支付参数
  262. success(res) {
  263. console.log("支付宝支付成功:", res);
  264. setTimeout(function () {
  265. that.showPayCall();
  266. }, 1000);
  267. },
  268. fail(e) {
  269. console.log("支付宝支付失败:", e);
  270. uni.showToast({
  271. title: "支付失败,请重试",
  272. icon: "none"
  273. });
  274. }
  275. });
  276. }
  277. } else {
  278. uni.showToast({
  279. title: "创建订单失败,请联系客服",
  280. icon: "none",
  281. });
  282. }
  283. },
  284. fail: (err) => {
  285. console.log("请求失败:", err);
  286. uni.showToast({
  287. title: "网络错误,请重试",
  288. icon: "none"
  289. });
  290. },
  291. complete: () => {
  292. that.isSubmitting = false;
  293. }
  294. });
  295. },
  296. showPayCall() {
  297. let that = this;
  298. this.$refs["DialogBox"]
  299. .confirm({
  300. title: "提示",
  301. content: "我已经支付完成",
  302. DialogType: "inquiry",
  303. btn1: "否",
  304. btn2: "是",
  305. animation: 0,
  306. })
  307. .then((res) => {
  308. uni.request({
  309. url: this.$apiHost + "/Order/getstatus",
  310. data: {
  311. uuid: getApp().globalData.uuid,
  312. linkid: that.linkid,
  313. },
  314. header: {
  315. "content-type": "application/json",
  316. },
  317. success: (res) => {
  318. if (res.data.success == "yes") {
  319. uni.showToast({
  320. title: "充值成功",
  321. icon: "none",
  322. });
  323. } else {
  324. uni.showToast({
  325. title: "还未检测到充值状态,请稍后再试",
  326. icon: "none",
  327. });
  328. setTimeout(function () {
  329. that.showPayCall();
  330. }, 1000);
  331. }
  332. },
  333. complete: (com) => {},
  334. });
  335. });
  336. },
  337. loadData() {
  338. // 获取M币数量
  339. uni.request({
  340. url: this.$apiHost + '/My/getnum',
  341. method: 'GET',
  342. header: {
  343. 'content-type': 'application/json',
  344. 'sign': getApp().globalData.headerSign
  345. },
  346. data: {
  347. uuid: getApp().globalData.uuid
  348. },
  349. success: (res) => {
  350. console.log("获取用户M币数量:", res.data);
  351. if (res.data && res.data.num_gmm) {
  352. this.myGoldCoin = res.data.num_gmm;
  353. }
  354. }
  355. });
  356. // 获取充值列表
  357. uni.request({
  358. url: this.$apiHost + "/User/getCzList",
  359. data: {
  360. uuid: getApp().globalData.uuid,
  361. },
  362. header: {
  363. "content-type": "application/json",
  364. },
  365. success: (res) => {
  366. console.log("res", res.data);
  367. this.num = res.data.num;
  368. if (res.data.list != null && res.data.list != undefined) {
  369. this.list = res.data.list;
  370. this.tid = this.list[1]["id"];
  371. this.sel = 1;
  372. this.money = this.list[1]["money"];
  373. }
  374. },
  375. });
  376. },
  377. handleDropdownSelect(item) {
  378. switch (item.type) {
  379. case "vipRecord":
  380. uni.navigateTo({
  381. url: "/pages/vip/record?type=coin",
  382. });
  383. break;
  384. }
  385. },
  386. agreeChk() {
  387. if (this.is_agree == 0) {
  388. this.is_agree = 1;
  389. } else {
  390. this.is_agree = 0;
  391. }
  392. },
  393. // 检查平台类型
  394. checkPlatform() {
  395. const systemInfo = uni.getSystemInfoSync();
  396. this.isIOS = systemInfo.platform === 'ios';
  397. console.log('当前平台:', systemInfo.platform, '是否为iOS:', this.isIOS);
  398. },
  399. // 苹果内购提交
  400. submitAppleIap() {
  401. let that = this;
  402. // 先创建订单
  403. uni.request({
  404. url: this.$apiHost + "/AppleIap/submit",
  405. data: {
  406. uuid: getApp().globalData.uuid,
  407. product_id: this.tid,
  408. },
  409. header: {
  410. "content-type": "application/json",
  411. },
  412. success: (res) => {
  413. console.log("苹果内购订单创建:", res.data);
  414. if (res.data.success == "yes") {
  415. that.currentOrderId = res.data.order_id;
  416. // 调用苹果内购
  417. that.requestApplePayment(res.data.apple_product_id);
  418. } else {
  419. uni.showToast({
  420. title: res.data.msg || "创建订单失败",
  421. icon: "none",
  422. });
  423. that.isSubmitting = false;
  424. }
  425. },
  426. fail: (err) => {
  427. console.log("创建苹果内购订单失败:", err);
  428. uni.showToast({
  429. title: "网络错误,请重试",
  430. icon: "none"
  431. });
  432. that.isSubmitting = false;
  433. }
  434. });
  435. },
  436. // 调用苹果内购
  437. requestApplePayment(productId) {
  438. let that = this;
  439. // 请求苹果内购
  440. uni.requestPayment({
  441. provider: 'appleiap',
  442. orderInfo: {
  443. productid: productId
  444. },
  445. success: function (res) {
  446. console.log('苹果内购支付成功:', res);
  447. // 验证收据
  448. that.verifyAppleReceipt(res.transactionReceipt, res.transactionIdentifier);
  449. },
  450. fail: function (err) {
  451. console.log('苹果内购支付失败:', err);
  452. uni.showToast({
  453. title: '支付失败',
  454. icon: 'none'
  455. });
  456. that.isSubmitting = false;
  457. }
  458. });
  459. },
  460. // 验证苹果收据
  461. verifyAppleReceipt(receiptData, transactionId) {
  462. let that = this;
  463. uni.request({
  464. url: that.$apiHost + "/AppleIap/verify",
  465. method: 'POST',
  466. data: {
  467. uuid: getApp().globalData.uuid,
  468. order_id: that.currentOrderId,
  469. receipt_data: receiptData,
  470. transaction_id: transactionId
  471. },
  472. header: {
  473. "content-type": "application/json",
  474. },
  475. success: (res) => {
  476. console.log("苹果收据验证结果:", res.data);
  477. if (res.data.success == "yes") {
  478. uni.showToast({
  479. title: "充值成功",
  480. icon: "success",
  481. });
  482. // 刷新M币数量
  483. that.loadData();
  484. } else {
  485. uni.showToast({
  486. title: res.data.msg || "验证失败",
  487. icon: "none",
  488. });
  489. }
  490. that.isSubmitting = false;
  491. },
  492. fail: (err) => {
  493. console.log("验证苹果收据失败:", err);
  494. uni.showToast({
  495. title: "验证失败,请联系客服",
  496. icon: "none"
  497. });
  498. that.isSubmitting = false;
  499. }
  500. });
  501. },
  502. },
  503. };
  504. </script>
  505. <style scoped lang="scss">
  506. @import "M_purchase.scss";
  507. .agree {
  508. width: 90%;
  509. margin: 0 auto;
  510. color: #666666;
  511. font-size: 24rpx;
  512. display: flex;
  513. align-items: center;
  514. text-align: left;
  515. line-height: 32rpx;
  516. position: absolute;
  517. bottom: 215rpx;
  518. left: 50%;
  519. transform: translateX(-50%);
  520. .agree2 {
  521. display: flex;
  522. flex-direction: row;
  523. justify-content: flex-start;
  524. align-items: center;
  525. padding-right: 8rpx;
  526. flex-shrink: 0;
  527. padding-bottom: 34rpx;
  528. }
  529. .xy {
  530. color: #0084ff;
  531. display: inline;
  532. }
  533. image {
  534. width: 32rpx;
  535. height: 32rpx;
  536. }
  537. }
  538. .btn_submit {
  539. width: 626rpx;
  540. height: 88rpx;
  541. background: linear-gradient(to left, #1f1f1f , #444444 );
  542. border-radius: 76rpx;
  543. margin: 0 auto;
  544. margin-top: 70rpx;
  545. color: #acf934;
  546. display: flex;
  547. align-items: center;
  548. justify-content: center;
  549. font-size: 32rpx;
  550. line-height: 0;
  551. position: relative;
  552. overflow: hidden;
  553. transition: all 0.3s ease;
  554. position: absolute;
  555. bottom: 100rpx;
  556. left: 50%;
  557. transform: translateX(-50%);
  558. &.btn-loading {
  559. opacity: 0.7;
  560. pointer-events: none;
  561. }
  562. .loading-spinner {
  563. width: 40rpx;
  564. height: 40rpx;
  565. border: 4rpx solid rgba(255, 255, 255, 0.3);
  566. border-radius: 50%;
  567. border-top-color: #fff;
  568. animation: spin 1s linear infinite;
  569. }
  570. text {
  571. display: inline-block;
  572. margin-right: 10rpx;
  573. }
  574. }
  575. @keyframes spin {
  576. to {
  577. transform: rotate(360deg);
  578. }
  579. }
  580. </style>