request-payment.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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>支付金额</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. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. title: 'request-payment',
  30. loading: false,
  31. price: 1,
  32. providerList: []
  33. }
  34. },
  35. onLoad: function() {
  36. // #ifdef APP-PLUS
  37. uni.getProvider({
  38. service: "payment",
  39. success: (e) => {
  40. console.log("payment success:" + JSON.stringify(e));
  41. let providerList = [];
  42. e.provider.map((value) => {
  43. switch (value) {
  44. case 'alipay':
  45. providerList.push({
  46. name: '支付宝',
  47. id: value,
  48. loading: false
  49. });
  50. break;
  51. case 'wxpay':
  52. providerList.push({
  53. name: '微信',
  54. id: value,
  55. loading: false
  56. });
  57. break;
  58. default:
  59. break;
  60. }
  61. })
  62. this.providerList = providerList;
  63. },
  64. fail: (e) => {
  65. console.log("获取支付通道失败:", e);
  66. }
  67. });
  68. // #endif
  69. },
  70. methods: {
  71. loginMpWeixin() {
  72. return new Promise((resolve, reject) => {
  73. uni.login({
  74. provider: 'weixin',
  75. success(res) {
  76. console.warn('此处使用uni-id处理微信小程序登录,详情参考: https://uniapp.dcloud.net.cn/uniCloud/uni-id')
  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. console.warn('此处使用uni-pay处理支付,详情参考: https://uniapp.dcloud.io/uniCloud/unipay')
  193. uni.request({
  194. method: 'POST',
  195. url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/pay',
  196. data: {
  197. provider,
  198. openid: this.openid,
  199. totalFee: Number(this.price) * 100, // 转为以分为单位
  200. // #ifdef APP-PLUS
  201. platform: 'app-plus',
  202. // #endif
  203. // #ifdef MP-WEIXIN
  204. platform: 'mp-weixin',
  205. // #endif
  206. },
  207. success(res) {
  208. if (res.data.code === 0) {
  209. resolve(res.data.orderInfo)
  210. } else {
  211. reject(new Error('获取支付信息失败' + res.data.msg))
  212. }
  213. },
  214. fail(err) {
  215. reject(new Error('请求支付接口失败' + err))
  216. }
  217. })
  218. })
  219. },
  220. priceChange(e) {
  221. console.log(e.detail.value)
  222. this.price = e.detail.value;
  223. }
  224. }
  225. }
  226. </script>
  227. <style>
  228. .rmbLogo {
  229. font-size: 40rpx;
  230. }
  231. button {
  232. background-color: #007aff;
  233. color: #ffffff;
  234. }
  235. .uni-h1.uni-center {
  236. display: flex;
  237. flex-direction: row;
  238. justify-content: center;
  239. align-items: flex-end;
  240. }
  241. .price {
  242. border-bottom: 1px solid #eee;
  243. width: 200rpx;
  244. height: 80rpx;
  245. padding-bottom: 4rpx;
  246. }
  247. .ipaPayBtn {
  248. margin-top: 30rpx;
  249. }
  250. </style>