request-merchant-transfer.uvue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="content">
  3. <button class="button" @click="pay">支付0.2元</button>
  4. <button class="button" @click="transfer" :disabled="disabled">接受0.1元转账</button>
  5. <view class="tips" v-if="disabled" style="color: #ff5a5f;">请先支付 0.2 元,才能体验接收转账 0.1 元</view>
  6. <!-- #ifdef MP-WEIXIN -->
  7. <view class="tips" v-else><text style="color: #42b983;">若已支付 0.2 元,请点击“接受0.1元转账”按钮</text></view>
  8. <!-- #endif -->
  9. <!-- #ifndef MP-WEIXIN -->
  10. <view class="tips" v-else><text style="color: #42b983;">已支付 0.2 元,请点击“接受0.1元转账”按钮</text></view>
  11. <!-- #endif -->
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. outTradeNo: "",
  19. disabled: true,
  20. openid: ""
  21. }
  22. },
  23. onLoad() {
  24. // this.outTradeNo = "test17404775193789726";
  25. // this.disabled = false;
  26. // #ifdef MP-WEIXIN
  27. this.getOpenId();
  28. // #endif
  29. },
  30. methods: {
  31. pay() {
  32. let bundleId = this.getBundleId();
  33. let random = Math.floor(Math.random() * 9000) + 1000;
  34. this.outTradeNo = `test${Date.now()}${random}`;
  35. console.log('outTradeNo: ', this.outTradeNo)
  36. this.disabled = true;
  37. uni.showLoading({
  38. title: "请求中..."
  39. });
  40. // #ifdef APP
  41. const getProviderRes = uni.getProviderSync({
  42. service: "payment"
  43. });
  44. if (getProviderRes.providerIds.indexOf("wxpay") == -1) {
  45. uni.showToast({
  46. title: "未添加微信支付依赖",
  47. icon: 'error'
  48. });
  49. uni.hideLoading();
  50. return;
  51. }
  52. // #endif
  53. uni.request({
  54. url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOrderInfo",
  55. method: "GET",
  56. data: {
  57. outTradeNo: this.outTradeNo,
  58. bundleId,
  59. openid: this.openid
  60. },
  61. success: (res) => {
  62. uni.hideLoading();
  63. let data = res.data as UTSJSONObject;
  64. let errCode = data['errCode'] as number;
  65. if (errCode != 0) {
  66. uni.showModal({
  67. title: "提示",
  68. content: data['errMsg'] as string,
  69. showCancel: false
  70. });
  71. return;
  72. }
  73. let orderInfo = data["orderInfo"] as string;
  74. console.log('orderInfo: ', orderInfo)
  75. // #ifdef MP-WEIXIN
  76. setTimeout(() => {
  77. this.disabled = false;
  78. }, 3000);
  79. // #endif
  80. uni.requestPayment({
  81. provider: "wxpay",
  82. orderInfo: orderInfo,
  83. // #ifdef MP-WEIXIN
  84. ...JSON.parse(orderInfo),
  85. // #endif
  86. success: (res) => {
  87. console.log('res: ', res)
  88. uni.showToast({
  89. title: "支付成功",
  90. icon: 'success'
  91. });
  92. this.disabled = false;
  93. },
  94. fail: (err) => {
  95. console.error("err", err);
  96. uni.hideLoading();
  97. uni.showToast({
  98. title: "支付失败",
  99. icon: 'error'
  100. });
  101. }
  102. });
  103. },
  104. fail: (err) => {
  105. uni.hideLoading();
  106. console.error("request-err", err);
  107. }
  108. });
  109. },
  110. transfer() {
  111. uni.showLoading({
  112. title: "请求中..."
  113. });
  114. try {
  115. let bundleId = this.getBundleId();
  116. uni.request({
  117. url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/transfer",
  118. method: "GET",
  119. data: {
  120. outTradeNo: this.outTradeNo,
  121. bundleId
  122. },
  123. success: (res) => {
  124. let data = res.data as UTSJSONObject;
  125. if (data['errCode'] != null && typeof data['errCode'] == "number" && data['errCode'] == 0) {
  126. let options = data['options'] as UTSJSONObject;
  127. uni.requestMerchantTransfer({
  128. mchId: options['mchId'] as string,
  129. appId: options['appId'] as string,
  130. package: options['package'] as string,
  131. success: (res) => {
  132. uni.hideLoading();
  133. console.log('res: ', res)
  134. uni.showToast({
  135. title: "转账成功",
  136. icon: 'success'
  137. });
  138. this.disabled = true;
  139. },
  140. fail: (err) => {
  141. uni.hideLoading();
  142. let errMsg = err.errMsg;
  143. if (errMsg == "requestMerchantTransfer:fail cancel") {
  144. errMsg = "取消转账";
  145. }
  146. uni.showToast({
  147. title: errMsg,
  148. icon: 'none'
  149. });
  150. console.error("转账失败", err);
  151. }
  152. });
  153. } else {
  154. uni.hideLoading();
  155. if (data['errMsg'] != null) {
  156. uni.showModal({
  157. title: "提示",
  158. content: data['errMsg'] as string,
  159. showCancel: false
  160. });
  161. }
  162. return;
  163. }
  164. },
  165. fail: (err) => {
  166. console.error("request-err", err);
  167. uni.hideLoading();
  168. }
  169. });
  170. } catch (err) {
  171. console.error('err: ', err)
  172. uni.showToast({
  173. title: "运行异常",
  174. icon: 'error'
  175. });
  176. uni.hideLoading();
  177. }
  178. },
  179. getBundleId() : string | null {
  180. let baseInfo = uni.getAppBaseInfo();
  181. let bundleId : string | null;
  182. // #ifdef APP-ANDROID
  183. bundleId = baseInfo.packageName;
  184. // #endif
  185. // #ifdef APP-IOS
  186. bundleId = baseInfo.bundleId;
  187. // #endif
  188. // #ifdef MP-WEIXIN
  189. bundleId = uni.getAccountInfoSync().miniProgram.appId;
  190. // #endif
  191. return bundleId;
  192. },
  193. // #ifdef MP-WEIXIN
  194. getOpenId() {
  195. uni.showLoading({
  196. title: "请稍等...",
  197. mask: true
  198. });
  199. let bundleId = this.getBundleId();
  200. uni.login({
  201. provider: 'weixin',
  202. success: (res) => {
  203. uni.request({
  204. url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOpenId",
  205. method: "GET",
  206. data: {
  207. code: res.code,
  208. bundleId
  209. },
  210. success: (res) => {
  211. uni.hideLoading();
  212. this.openid = res.data.openid;
  213. console.log('openid: ', this.openid);
  214. },
  215. fail: (err) => {
  216. uni.hideLoading();
  217. console.error("request-err", err);
  218. }
  219. });
  220. }
  221. })
  222. },
  223. // #endif
  224. }
  225. }
  226. </script>
  227. <style>
  228. .content {
  229. display: flex;
  230. flex-direction: column;
  231. align-items: center;
  232. justify-content: center;
  233. padding: 20px;
  234. }
  235. .button {
  236. width: 100%;
  237. margin-bottom: 20px;
  238. border-radius: 5px;
  239. }
  240. .tips {
  241. color: #999;
  242. font-size: 14px;
  243. }
  244. </style>