M_purchase.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. appleProducts: [], // 苹果产品列表
  147. appleProductsLoaded: false // 苹果产品是否已加载
  148. };
  149. },
  150. onLoad() {
  151. // setTimeout(function() {
  152. // uni.setNavigationBarColor({
  153. // frontColor: '#ffffff',
  154. // backgroundColor: '#00000000',
  155. // animation: {
  156. // duration: 400,
  157. // timingFunc: 'easeIn'
  158. // }
  159. // })
  160. // }, 200);
  161. },
  162. onShow() {
  163. this.checkPlatform();
  164. this.loadData();
  165. let that = this;
  166. },
  167. methods: {
  168. onBack() {},
  169. goPage(page) {
  170. uni.navigateTo({
  171. url: page,
  172. });
  173. },
  174. selTA(item, se) {
  175. this.tid = item.id;
  176. this.sel = se;
  177. if (this.list != null && this.list != undefined) {
  178. this.money = this.list[se]["money"];
  179. }
  180. },
  181. selPay(se) {
  182. this.payType = se;
  183. },
  184. chkSel() {
  185. if (this.sel == 1) {
  186. this.sel = 0;
  187. } else {
  188. this.sel = 1;
  189. }
  190. },
  191. submitData() {
  192. const now = Date.now();
  193. if (now - this.lastClickTime < 3000) {
  194. uni.showToast({
  195. title: "请勿频繁点击",
  196. icon: "none"
  197. });
  198. return;
  199. }
  200. this.lastClickTime = now;
  201. if (this.isSubmitting) return;
  202. if (this.is_agree == 0) {
  203. uni.showToast({
  204. title: "请确认并选择协议",
  205. icon: "none",
  206. });
  207. return;
  208. }
  209. this.isSubmitting = true;
  210. let that = this;
  211. // iOS系统使用苹果内购
  212. if (this.isIOS) {
  213. this.submitAppleIap();
  214. return;
  215. }
  216. uni.request({
  217. url: this.$apiHost + "/Order/submit",
  218. data: {
  219. uuid: getApp().globalData.uuid,
  220. product_id: this.tid,
  221. type: "buyM",
  222. payType: this.payType,
  223. },
  224. header: {
  225. "content-type": "application/json",
  226. },
  227. success: (res) => {
  228. console.log("res-pay", res.data);
  229. if (res.data.success == "yes") {
  230. this.linkid = res.data.linkid;
  231. // 微信支付逻辑
  232. if (this.payType === "wechat") {
  233. uni.requestPayment({
  234. provider: "wxpay",
  235. orderInfo: {
  236. appid: res.data.wepay.appid,
  237. partnerid: res.data.wepay.partnerid,
  238. prepayid: res.data.wepay.prepayid,
  239. package: "Sign=WXPay",
  240. noncestr: res.data.wepay.noncestr,
  241. timestamp: res.data.wepay.timestamp,
  242. sign: res.data.wepay.sign
  243. },
  244. success(res) {
  245. console.log("微信支付成功:", res);
  246. setTimeout(function () {
  247. that.showPayCall();
  248. }, 1000);
  249. },
  250. fail(e) {
  251. console.log("微信支付失败:", e);
  252. uni.showToast({
  253. title: "支付失败,请重试",
  254. icon: "none"
  255. });
  256. }
  257. });
  258. }
  259. // 支付宝支付逻辑
  260. else if (this.payType === "alipay") {
  261. uni.requestPayment({
  262. provider: "alipay",
  263. orderInfo: res.data.ali_pay, // 直接使用后端返回的支付宝支付参数
  264. success(res) {
  265. console.log("支付宝支付成功:", res);
  266. setTimeout(function () {
  267. that.showPayCall();
  268. }, 1000);
  269. },
  270. fail(e) {
  271. console.log("支付宝支付失败:", e);
  272. uni.showToast({
  273. title: "支付失败,请重试",
  274. icon: "none"
  275. });
  276. }
  277. });
  278. }
  279. } else {
  280. uni.showToast({
  281. title: "创建订单失败,请联系客服",
  282. icon: "none",
  283. });
  284. }
  285. },
  286. fail: (err) => {
  287. console.log("请求失败:", err);
  288. uni.showToast({
  289. title: "网络错误,请重试",
  290. icon: "none"
  291. });
  292. },
  293. complete: () => {
  294. that.isSubmitting = false;
  295. }
  296. });
  297. },
  298. showPayCall() {
  299. let that = this;
  300. this.$refs["DialogBox"]
  301. .confirm({
  302. title: "提示",
  303. content: "我已经支付完成",
  304. DialogType: "inquiry",
  305. btn1: "否",
  306. btn2: "是",
  307. animation: 0,
  308. })
  309. .then((res) => {
  310. uni.request({
  311. url: this.$apiHost + "/Order/getstatus",
  312. data: {
  313. uuid: getApp().globalData.uuid,
  314. linkid: that.linkid,
  315. },
  316. header: {
  317. "content-type": "application/json",
  318. },
  319. success: (res) => {
  320. if (res.data.success == "yes") {
  321. uni.showToast({
  322. title: "充值成功",
  323. icon: "none",
  324. });
  325. } else {
  326. uni.showToast({
  327. title: "还未检测到充值状态,请稍后再试",
  328. icon: "none",
  329. });
  330. setTimeout(function () {
  331. that.showPayCall();
  332. }, 1000);
  333. }
  334. },
  335. complete: (com) => {},
  336. });
  337. });
  338. },
  339. loadData() {
  340. const that = this;
  341. // 获取M币数量
  342. uni.request({
  343. url: this.$apiHost + '/My/getnum',
  344. method: 'GET',
  345. header: {
  346. 'content-type': 'application/json',
  347. 'sign': getApp().globalData.headerSign
  348. },
  349. data: {
  350. uuid: getApp().globalData.uuid
  351. },
  352. success: (res) => {
  353. console.log("获取用户M币数量:", res.data);
  354. if (res.data && res.data.num_gmm) {
  355. this.myGoldCoin = res.data.num_gmm;
  356. }
  357. }
  358. });
  359. // 获取充值列表
  360. uni.request({
  361. url: this.$apiHost + "/User/getCzList",
  362. data: {
  363. uuid: getApp().globalData.uuid,
  364. },
  365. header: {
  366. "content-type": "application/json",
  367. },
  368. success: (res) => {
  369. console.log("res", res.data);
  370. this.num = res.data.num;
  371. if (res.data.list != null && res.data.list != undefined) {
  372. this.list = res.data.list;
  373. this.tid = this.list[1]["id"];
  374. this.sel = 1;
  375. this.money = this.list[1]["money"];
  376. // 充值列表已加载完成
  377. }
  378. },
  379. });
  380. },
  381. handleDropdownSelect(item) {
  382. switch (item.type) {
  383. case "vipRecord":
  384. uni.navigateTo({
  385. url: "/pages/vip/record?type=coin",
  386. });
  387. break;
  388. }
  389. },
  390. agreeChk() {
  391. if (this.is_agree == 0) {
  392. this.is_agree = 1;
  393. } else {
  394. this.is_agree = 0;
  395. }
  396. },
  397. // 检查平台类型
  398. checkPlatform() {
  399. const systemInfo = uni.getSystemInfoSync();
  400. this.isIOS = systemInfo.platform === 'ios';
  401. console.log('当前平台:', systemInfo.platform, '是否为iOS:', this.isIOS);
  402. // iOS系统已检测完成
  403. },
  404. // 验证苹果产品是否可用(简化版本)
  405. validateAppleProduct(productId) {
  406. // 对于M币充值,我们采用简化的验证方式
  407. // 在实际购买时如果产品ID无效,苹果会返回错误
  408. console.log('验证苹果产品ID:', productId);
  409. // 基本验证:检查产品ID是否符合苹果内购产品ID格式
  410. if (!productId || typeof productId !== 'string' || productId.length === 0) {
  411. return false;
  412. }
  413. // 标记已加载,避免重复检查
  414. this.appleProductsLoaded = true;
  415. return true;
  416. },
  417. // 苹果内购提交
  418. submitAppleIap() {
  419. let that = this;
  420. // 先创建订单
  421. uni.request({
  422. url: this.$apiHost + "/AppleIap/submit",
  423. data: {
  424. uuid: getApp().globalData.uuid,
  425. product_id: this.tid,
  426. },
  427. header: {
  428. "content-type": "application/json",
  429. },
  430. success: (res) => {
  431. console.log("苹果内购订单创建:", res.data);
  432. if (res.data.success == "yes") {
  433. that.currentOrderId = res.data.order_id;
  434. // 调用苹果内购
  435. that.requestApplePayment(res.data.apple_product_id);
  436. } else {
  437. uni.showToast({
  438. title: res.data.msg || "创建订单失败",
  439. icon: "none",
  440. });
  441. that.isSubmitting = false;
  442. }
  443. },
  444. fail: (err) => {
  445. console.log("创建苹果内购订单失败:", err);
  446. uni.showToast({
  447. title: "网络错误,请重试",
  448. icon: "none"
  449. });
  450. that.isSubmitting = false;
  451. }
  452. });
  453. },
  454. // 调用苹果内购
  455. requestApplePayment(productId) {
  456. let that = this;
  457. // 验证产品ID是否有效
  458. if (!this.validateAppleProduct(productId)) {
  459. console.log('苹果产品ID无效:', productId);
  460. uni.showToast({
  461. title: '产品ID无效',
  462. icon: 'none'
  463. });
  464. this.isSubmitting = false;
  465. return;
  466. }
  467. console.log('开始苹果内购,产品ID:', productId);
  468. // 请求苹果内购
  469. uni.requestPayment({
  470. provider: 'appleiap',
  471. orderInfo: {
  472. productid: productId
  473. },
  474. success: function (res) {
  475. console.log('苹果内购支付成功:', res);
  476. // 验证收据
  477. that.verifyAppleReceipt(res.transactionReceipt, res.transactionIdentifier);
  478. },
  479. fail: function (err) {
  480. console.log('苹果内购支付失败:', err);
  481. let errorMessage = '支付失败';
  482. // 根据错误类型给出更具体的提示
  483. if (err.errMsg) {
  484. if (err.errMsg.includes('cancel')) {
  485. errorMessage = '支付已取消';
  486. } else if (err.errMsg.includes('product')) {
  487. errorMessage = '商品不可用';
  488. } else if (err.errMsg.includes('network')) {
  489. errorMessage = '网络错误,请检查网络连接';
  490. }
  491. }
  492. uni.showToast({
  493. title: errorMessage,
  494. icon: 'none'
  495. });
  496. that.isSubmitting = false;
  497. }
  498. });
  499. },
  500. // 验证苹果收据
  501. verifyAppleReceipt(receiptData, transactionId) {
  502. let that = this;
  503. uni.request({
  504. url: that.$apiHost + "/AppleIap/verify",
  505. method: 'POST',
  506. data: {
  507. uuid: getApp().globalData.uuid,
  508. order_id: that.currentOrderId,
  509. receipt_data: receiptData,
  510. transaction_id: transactionId
  511. },
  512. header: {
  513. "content-type": "application/json",
  514. },
  515. success: (res) => {
  516. console.log("苹果收据验证结果:", res.data);
  517. if (res.data.success == "yes") {
  518. uni.showToast({
  519. title: "充值成功",
  520. icon: "success",
  521. });
  522. // 刷新M币数量
  523. that.loadData();
  524. } else {
  525. uni.showToast({
  526. title: res.data.msg || "验证失败",
  527. icon: "none",
  528. });
  529. }
  530. that.isSubmitting = false;
  531. },
  532. fail: (err) => {
  533. console.log("验证苹果收据失败:", err);
  534. uni.showToast({
  535. title: "验证失败,请联系客服",
  536. icon: "none"
  537. });
  538. that.isSubmitting = false;
  539. }
  540. });
  541. },
  542. },
  543. };
  544. </script>
  545. <style scoped lang="scss">
  546. @import "M_purchase.scss";
  547. .agree {
  548. width: 90%;
  549. margin: 0 auto;
  550. color: #666666;
  551. font-size: 24rpx;
  552. display: flex;
  553. align-items: center;
  554. text-align: left;
  555. line-height: 32rpx;
  556. position: absolute;
  557. bottom: 215rpx;
  558. left: 50%;
  559. transform: translateX(-50%);
  560. .agree2 {
  561. display: flex;
  562. flex-direction: row;
  563. justify-content: flex-start;
  564. align-items: center;
  565. padding-right: 8rpx;
  566. flex-shrink: 0;
  567. padding-bottom: 34rpx;
  568. }
  569. .xy {
  570. color: #0084ff;
  571. display: inline;
  572. }
  573. image {
  574. width: 32rpx;
  575. height: 32rpx;
  576. }
  577. }
  578. .btn_submit {
  579. width: 626rpx;
  580. height: 88rpx;
  581. background: linear-gradient(to left, #1f1f1f , #444444 );
  582. border-radius: 76rpx;
  583. margin: 0 auto;
  584. margin-top: 70rpx;
  585. color: #acf934;
  586. display: flex;
  587. align-items: center;
  588. justify-content: center;
  589. font-size: 32rpx;
  590. line-height: 0;
  591. position: relative;
  592. overflow: hidden;
  593. transition: all 0.3s ease;
  594. position: absolute;
  595. bottom: 100rpx;
  596. left: 50%;
  597. transform: translateX(-50%);
  598. &.btn-loading {
  599. opacity: 0.7;
  600. pointer-events: none;
  601. }
  602. .loading-spinner {
  603. width: 40rpx;
  604. height: 40rpx;
  605. border: 4rpx solid rgba(255, 255, 255, 0.3);
  606. border-radius: 50%;
  607. border-top-color: #fff;
  608. animation: spin 1s linear infinite;
  609. }
  610. text {
  611. display: inline-block;
  612. margin-right: 10rpx;
  613. }
  614. }
  615. @keyframes spin {
  616. to {
  617. transform: rotate(360deg);
  618. }
  619. }
  620. </style>