request-payment.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view style="background:#FFF; padding:50rpx 0;">
  6. <view class="uni-hello-text uni-center">支付金额</text></view>
  7. <view class="uni-h1 uni-center uni-common-mt">
  8. <text class="rmbLogo">¥</text>
  9. <input class="price" type="digit" :value="price" maxlength="4" @input="priceChange" />
  10. </view>
  11. </view>
  12. <view class="uni-btn-v uni-common-mt">
  13. <!-- #ifdef MP-WEIXIN -->
  14. <button type="primary" @click="weixinPay" :loading="loading">微信支付</button>
  15. <!-- #endif -->
  16. <!-- #ifdef APP-PLUS -->
  17. <template v-if="providerList.length > 0">
  18. <button v-for="(item,index) in providerList" :key="index" @click="requestPayment(item,index)" :loading="item.loading">{{item.name}}支付</button>
  19. </template>
  20. <!-- #endif -->
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. title: 'request-payment',
  31. loading: false,
  32. price: 1,
  33. providerList: []
  34. }
  35. },
  36. onLoad: function() {
  37. // #ifdef APP-PLUS
  38. uni.getProvider({
  39. service: "payment",
  40. success: (e) => {
  41. console.log("payment success:" + JSON.stringify(e));
  42. let providerList = [];
  43. e.provider.map((value) => {
  44. switch (value) {
  45. case 'alipay':
  46. providerList.push({
  47. name: '支付宝',
  48. id: value,
  49. loading: false
  50. });
  51. break;
  52. case 'wxpay':
  53. providerList.push({
  54. name: '微信',
  55. id: value,
  56. loading: false
  57. });
  58. break;
  59. default:
  60. break;
  61. }
  62. })
  63. this.providerList = providerList;
  64. },
  65. fail: (e) => {
  66. console.log("获取支付通道失败:", e);
  67. }
  68. });
  69. // #endif
  70. },
  71. methods: {
  72. loginMpWeixin() {
  73. return new Promise((resolve, reject) => {
  74. uni.login({
  75. provider: 'weixin',
  76. success(res) {
  77. uni.request({
  78. url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/user-center',
  79. method: 'POST',
  80. data: {
  81. action: 'loginByWeixin',
  82. params: {
  83. code: res.code,
  84. platform: 'mp-weixin'
  85. }
  86. },
  87. success(res) {
  88. if (res.data.code !== 0) {
  89. reject(new Error('获取openid失败:', res.result.msg))
  90. return
  91. }
  92. uni.setStorageSync('openid', res.data.openid)
  93. resolve(res.data.openid)
  94. },
  95. fail(err) {
  96. reject(new Error('获取openid失败:' + err))
  97. }
  98. })
  99. },
  100. fail(err) {
  101. reject(err)
  102. }
  103. })
  104. })
  105. },
  106. async weixinPay() {
  107. let openid = uni.getStorageSync('openid')
  108. console.log("发起支付");
  109. this.loading = true;
  110. if (!openid) {
  111. try {
  112. openid = await this.loginMpWeixin()
  113. } catch (e) {
  114. console.error(e)
  115. }
  116. if (!openid) {
  117. uni.showModal({
  118. content: '获取openid失败',
  119. showCancel: false
  120. })
  121. this.loading = false
  122. return
  123. }
  124. }
  125. this.openid = openid
  126. let orderInfo = await this.getOrderInfo('wxpay')
  127. if (!orderInfo) {
  128. uni.showModal({
  129. content: '获取支付信息失败',
  130. showCancel: false
  131. })
  132. return
  133. }
  134. uni.requestPayment({
  135. ...orderInfo,
  136. success: (res) => {
  137. uni.showToast({
  138. title: "感谢您的赞助!"
  139. })
  140. },
  141. fail: (res) => {
  142. uni.showModal({
  143. content: "支付失败,原因为: " + res
  144. .errMsg,
  145. showCancel: false
  146. })
  147. },
  148. complete: () => {
  149. this.loading = false;
  150. }
  151. })
  152. },
  153. async requestPayment(e, index) {
  154. this.providerList[index].loading = true;
  155. const provider = e.id
  156. let orderInfo = await this.getOrderInfo(provider);
  157. if (!orderInfo) {
  158. uni.showModal({
  159. content: '获取支付信息失败',
  160. showCancel: false
  161. })
  162. return
  163. }
  164. console.log('--------orderInfo--------')
  165. console.log(orderInfo);
  166. uni.requestPayment({
  167. provider,
  168. orderInfo: orderInfo,
  169. success: (e) => {
  170. console.log("success", e);
  171. uni.showToast({
  172. title: "感谢您的赞助!"
  173. })
  174. },
  175. fail: (e) => {
  176. console.log("fail", e);
  177. uni.showModal({
  178. content: "支付失败,原因为: " + e.errMsg,
  179. showCancel: false
  180. })
  181. },
  182. complete: () => {
  183. this.providerList[index].loading = false;
  184. }
  185. })
  186. },
  187. getOrderInfo(provider) {
  188. return new Promise((resolve, reject) => {
  189. if (!this.price) {
  190. reject(new Error('请输入金额'))
  191. }
  192. uni.request({
  193. method: 'POST',
  194. url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/pay',
  195. data: {
  196. provider,
  197. openid: this.openid,
  198. totalFee: Number(this.price) * 100, // 转为以分为单位
  199. // #ifdef APP-PLUS
  200. platform: 'app-plus',
  201. // #endif
  202. // #ifdef MP-WEIXIN
  203. platform: 'mp-weixin',
  204. // #endif
  205. },
  206. success(res) {
  207. if (res.data.code === 0) {
  208. resolve(res.data.orderInfo)
  209. } else {
  210. reject(new Error('获取支付信息失败' + res.data.msg))
  211. }
  212. },
  213. fail(err) {
  214. reject(new Error('请求支付接口失败' + err))
  215. }
  216. })
  217. })
  218. },
  219. priceChange(e) {
  220. console.log(e.detail.value)
  221. this.price = e.detail.value;
  222. }
  223. }
  224. }
  225. </script>
  226. <style>
  227. .rmbLogo {
  228. font-size: 40rpx;
  229. }
  230. button {
  231. background-color: #007aff;
  232. color: #ffffff;
  233. }
  234. .uni-h1.uni-center {
  235. display: flex;
  236. flex-direction: row;
  237. justify-content: center;
  238. align-items: flex-end;
  239. }
  240. .price {
  241. border-bottom: 1px solid #eee;
  242. width: 200rpx;
  243. height: 80rpx;
  244. padding-bottom: 4rpx;
  245. }
  246. .ipaPayBtn {
  247. margin-top: 30rpx;
  248. }
  249. </style>