cardPayment_app.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="wallet_app" v-if="show">
  3. <view class="wallet_footer" :style="{ 'height': footerHeight + 48 + 'px' }">
  4. <view class="close">
  5. <view class="close_icon" @click.stop="close">
  6. <uni-icons type="closeempty" size="28" color="var(--text-01)"></uni-icons>
  7. </view>
  8. </view>
  9. </view>
  10. <!-- <GlobalLoading :msg="loadingStatus"></GlobalLoading> -->
  11. </view>
  12. </template>
  13. <script>
  14. import GlobalLoading from '@/components/GlobalLoading.vue'
  15. import { systemInfo, Toast } from "@/utils"
  16. import { SHOP_SUBMIT_PAY } from '@/api'
  17. import { getLan } from "@/locale"
  18. export default {
  19. comments: {
  20. GlobalLoading
  21. },
  22. props: {
  23. paramsObj: { type: Object }
  24. },
  25. emits: ['on-confirm', 'on-open', 'on-close', 'on-success', 'on-error'],
  26. data() {
  27. return {
  28. webview: null,
  29. show: false,
  30. footerHeight: 0,
  31. loadingStatus: { show: false }
  32. }
  33. },
  34. methods: {
  35. open(data) {
  36. this.show = true;
  37. this.fetchPaymentInfo(data);
  38. // this.initWebViewBelowElement();
  39. },
  40. close() {
  41. this.webview.close();
  42. this.webview = null;
  43. this.show = false;
  44. uni.hideKeyboard();
  45. this.$emit('on-close');
  46. },
  47. getRequest(url) {
  48. let theRequest = new Object();
  49. let index = url.indexOf("?");
  50. if (index != -1) {
  51. let str = url.substring(index + 1);
  52. let strs = str.split("&");
  53. for (let i = 0; i < strs.length; i++) {
  54. theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  55. }
  56. }
  57. return theRequest;
  58. },
  59. async initWebViewBelowElement(eventData) {
  60. const res = await systemInfo();
  61. try {
  62. let wvPath = '/static/hybrid/html/card.html';
  63. let wv = plus.webview.create("", wvPath, {
  64. 'uni-app': 'none',
  65. position: 'fixed',
  66. background: 'transparent',
  67. left: '24px',
  68. right: '24px',
  69. top: this.footerHeight - 24 + 28 + 'px',
  70. overflowY: 'auto',
  71. });
  72. wv.loadURL(wvPath);
  73. wv.overrideUrlLoading({ mode: 'reject' }, (e) => {
  74. let { str } = this.getRequest(e.url);
  75. str = unescape(str)
  76. return this.handleBack(str)
  77. });
  78. /* wv.evalJS(`
  79. window.dispatchEvent(new CustomEvent('appMessage',
  80. { detail: ${JSON.stringify({ type: 'lan', lan:getLan() })} }))`); */
  81. wv.evalJS(`
  82. window.dispatchEvent(new CustomEvent('appMessage',
  83. { detail: ${JSON.stringify({ ...eventData, lan: getLan() })} }))`);
  84. this.$root.$scope.$getAppWebview().append(wv);
  85. this.webview = wv;
  86. } catch (error) {
  87. console.error(error);
  88. }
  89. },
  90. async fetchPaymentInfo(data) {
  91. this.loadingStatus.show = true
  92. let obj = data
  93. try {
  94. const res = await SHOP_SUBMIT_PAY({
  95. payType: 'airwallex',
  96. ...obj
  97. })
  98. const { id, client_secret } = res.data;
  99. let params = {}
  100. params.intent_id = id;
  101. params.client_secret = client_secret;
  102. this.initWebViewBelowElement({ type: 'pay', ...params })
  103. } catch (error) {
  104. console.error(error);
  105. } finally {
  106. this.loadingStatus.show = false
  107. }
  108. },
  109. handleBack(str) {
  110. const { status, res } = JSON.parse(str);
  111. switch (status) {
  112. case 'success':
  113. console.log('成功参数', JSON.stringify(res));
  114. if (res.status == "SUCCEEDED" || res.status == "succeeded") {
  115. this.$emit('on-success', res)
  116. }
  117. else {
  118. Toast(res.status)
  119. }
  120. break;
  121. case 'error':
  122. console.log('----- 提交失败 ----- ', JSON.stringify(res))
  123. Toast(res.message)
  124. break;
  125. case 'loading':
  126. let s = res.state == true || res.state == 'true' ? true : false
  127. this.loadingStatus.show = s
  128. if (s) {
  129. uni.showLoading({
  130. title: '',
  131. mask: true
  132. })
  133. }
  134. else {
  135. uni.hideLoading();
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. },
  143. async mounted() {
  144. const res = await systemInfo();
  145. this.footerHeight = res.windowHeight / 2;
  146. const _this = this;
  147. uni.onKeyboardHeightChange(res => {
  148. _this.webview.setStyle({
  149. top: _this.footerHeight - 24 + 28 - res.height + 'px'
  150. })
  151. })
  152. }
  153. }
  154. </script>
  155. <style lang="less" scoped>
  156. .wallet_app {
  157. position: fixed;
  158. top: 0;
  159. left: 0;
  160. right: 0;
  161. bottom: 0;
  162. z-index: 9999999;
  163. background-color: rgba(0, 0, 0, .5);
  164. display: flex;
  165. align-items: flex-end;
  166. .wallet_footer {
  167. width: 100%;
  168. background-color: #fff;
  169. padding: 24px;
  170. border-radius: 10px 10px 0 0;
  171. .close {
  172. height: 28px;
  173. display: flex;
  174. justify-content: flex-end;
  175. }
  176. }
  177. }
  178. </style>