M_purchase.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. const currentItem = this.list[this.sel];
  422. console.log("当前选中的充值项目:", currentItem);
  423. if (!currentItem || !currentItem.spec) {
  424. uni.showToast({
  425. title: "产品配置错误,请联系客服",
  426. icon: "none",
  427. });
  428. that.isSubmitting = false;
  429. return;
  430. }
  431. console.log("准备创建苹果内购订单 - 产品ID:", this.tid, "苹果产品ID:", currentItem.spec);
  432. // 先创建订单
  433. uni.request({
  434. url: this.$apiHost + "/AppleIap/submit",
  435. data: {
  436. uuid: getApp().globalData.uuid,
  437. product_id: this.tid,
  438. type: "buyM"
  439. },
  440. header: {
  441. "content-type": "application/json",
  442. },
  443. success: (res) => {
  444. console.log("苹果内购订单创建响应:", res.data);
  445. if (res.data.success == "yes") {
  446. that.currentOrderId = res.data.order_id;
  447. // 使用数据库中配置的spec作为苹果产品ID
  448. const appleProductId = res.data.apple_product_id || currentItem.spec;
  449. console.log("使用苹果产品ID进行支付:", appleProductId);
  450. // 调用苹果内购
  451. that.requestApplePayment(appleProductId);
  452. } else {
  453. uni.showToast({
  454. title: res.data.msg || "创建订单失败",
  455. icon: "none",
  456. });
  457. that.isSubmitting = false;
  458. }
  459. },
  460. fail: (err) => {
  461. console.log("创建苹果内购订单失败:", err);
  462. uni.showToast({
  463. title: "网络错误,请重试",
  464. icon: "none"
  465. });
  466. that.isSubmitting = false;
  467. }
  468. });
  469. },
  470. // 调用苹果内购
  471. requestApplePayment(productId) {
  472. let that = this;
  473. // 验证产品ID是否有效
  474. if (!this.validateAppleProduct(productId)) {
  475. console.log('苹果产品ID无效:', productId);
  476. uni.showToast({
  477. title: '产品ID无效',
  478. icon: 'none'
  479. });
  480. this.isSubmitting = false;
  481. return;
  482. }
  483. console.log('开始苹果内购,产品ID:', productId);
  484. console.log('苹果内购参数:', {
  485. provider: 'appleiap',
  486. orderInfo: {
  487. productid: productId
  488. }
  489. });
  490. // 请求苹果内购
  491. uni.requestPayment({
  492. provider: 'appleiap',
  493. orderInfo: {
  494. productid: productId
  495. },
  496. success: function (res) {
  497. console.log('苹果内购支付成功:', res);
  498. // 验证收据
  499. that.verifyAppleReceipt(res.transactionReceipt, res.transactionIdentifier);
  500. },
  501. fail: function (err) {
  502. console.log('苹果内购支付失败详情:', err);
  503. let errorMessage = '支付失败';
  504. // 根据错误类型给出更具体的提示
  505. if (err.errMsg) {
  506. if (err.errMsg.includes('cancel')) {
  507. errorMessage = '支付已取消';
  508. } else if (err.errMsg.includes('订单的ID不存在') || err.errMsg.includes('product')) {
  509. errorMessage = '产品ID不存在,请检查App Store Connect配置';
  510. console.error('产品ID配置错误 - 当前产品ID:', productId);
  511. console.error('请确认以下事项:');
  512. console.error('1. App Store Connect中是否已创建此产品ID');
  513. console.error('2. 产品状态是否为"Ready for Sale"');
  514. console.error('3. Bundle ID是否匹配');
  515. console.error('4. 是否在正确的环境(沙盒/生产)');
  516. } else if (err.errMsg.includes('network')) {
  517. errorMessage = '网络错误,请检查网络连接';
  518. } else {
  519. errorMessage = '支付失败: ' + (err.errMsg || JSON.stringify(err));
  520. }
  521. }
  522. uni.showToast({
  523. title: errorMessage,
  524. icon: 'none',
  525. duration: 3000
  526. });
  527. that.isSubmitting = false;
  528. }
  529. });
  530. },
  531. // 验证苹果收据
  532. verifyAppleReceipt(receiptData, transactionId) {
  533. let that = this;
  534. uni.request({
  535. url: that.$apiHost + "/AppleIap/verify",
  536. method: 'POST',
  537. data: {
  538. uuid: getApp().globalData.uuid,
  539. order_id: that.currentOrderId,
  540. receipt_data: receiptData,
  541. transaction_id: transactionId
  542. },
  543. header: {
  544. "content-type": "application/json",
  545. },
  546. success: (res) => {
  547. console.log("苹果收据验证结果:", res.data);
  548. if (res.data.success == "yes") {
  549. uni.showToast({
  550. title: "充值成功",
  551. icon: "success",
  552. });
  553. // 刷新M币数量
  554. that.loadData();
  555. } else {
  556. uni.showToast({
  557. title: res.data.msg || "验证失败",
  558. icon: "none",
  559. });
  560. }
  561. that.isSubmitting = false;
  562. },
  563. fail: (err) => {
  564. console.log("验证苹果收据失败:", err);
  565. uni.showToast({
  566. title: "验证失败,请联系客服",
  567. icon: "none"
  568. });
  569. that.isSubmitting = false;
  570. }
  571. });
  572. },
  573. },
  574. };
  575. </script>
  576. <style scoped lang="scss">
  577. @import "M_purchase.scss";
  578. .agree {
  579. width: 90%;
  580. margin: 0 auto;
  581. color: #666666;
  582. font-size: 24rpx;
  583. display: flex;
  584. align-items: center;
  585. text-align: left;
  586. line-height: 32rpx;
  587. position: absolute;
  588. bottom: 215rpx;
  589. left: 50%;
  590. transform: translateX(-50%);
  591. .agree2 {
  592. display: flex;
  593. flex-direction: row;
  594. justify-content: flex-start;
  595. align-items: center;
  596. padding-right: 8rpx;
  597. flex-shrink: 0;
  598. padding-bottom: 34rpx;
  599. }
  600. .xy {
  601. color: #0084ff;
  602. display: inline;
  603. }
  604. image {
  605. width: 32rpx;
  606. height: 32rpx;
  607. }
  608. }
  609. .btn_submit {
  610. width: 626rpx;
  611. height: 88rpx;
  612. background: linear-gradient(to left, #1f1f1f , #444444 );
  613. border-radius: 76rpx;
  614. margin: 0 auto;
  615. margin-top: 70rpx;
  616. color: #acf934;
  617. display: flex;
  618. align-items: center;
  619. justify-content: center;
  620. font-size: 32rpx;
  621. line-height: 0;
  622. position: relative;
  623. overflow: hidden;
  624. transition: all 0.3s ease;
  625. position: absolute;
  626. bottom: 100rpx;
  627. left: 50%;
  628. transform: translateX(-50%);
  629. &.btn-loading {
  630. opacity: 0.7;
  631. pointer-events: none;
  632. }
  633. .loading-spinner {
  634. width: 40rpx;
  635. height: 40rpx;
  636. border: 4rpx solid rgba(255, 255, 255, 0.3);
  637. border-radius: 50%;
  638. border-top-color: #fff;
  639. animation: spin 1s linear infinite;
  640. }
  641. text {
  642. display: inline-block;
  643. margin-right: 10rpx;
  644. }
  645. }
  646. @keyframes spin {
  647. to {
  648. transform: rotate(360deg);
  649. }
  650. }
  651. </style>