pay.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="box">
  3. <view class="mt-85 text-center size-26">
  4. <text class="mr-10">支付剩余时间</text>
  5. <u-count-down :timestamp="time" font-size="26"></u-count-down>
  6. </view>
  7. <view class="yellow bold mt-15 text-center">
  8. <text>¥</text>
  9. <text class="size-40">{{price}}</text>
  10. </view>
  11. <view class="bg-white pt-30 mt-80">
  12. <view class="size-34 bold plr-36 mb-20">
  13. 支付方式
  14. </view>
  15. <view class="plr-36">
  16. <u-radio-group v-model="value" active-color="#00C28E">
  17. <u-radio name="1" class="w100 bb ptb-30">
  18. <view class="flex_l">
  19. <image style="width: 55rpx;height: 55rpx; margin-right: 20rpx;" src="../../static/wx.png" mode=""></image>
  20. 微信支付
  21. </view>
  22. </u-radio>
  23. <!-- #ifdef APP-PLUS -->
  24. <u-radio name="2" class="w100 ptb-30">
  25. <view class="flex_l">
  26. <image style="width: 55rpx;height: 55rpx; margin-right: 20rpx;" src="../../static/zfb.png" mode=""></image>
  27. 支付宝支付
  28. </view>
  29. </u-radio>
  30. <!-- #endif -->
  31. <!-- <u-radio name="3" class="w100 ptb-30">
  32. <view class="flex_l">
  33. <image style="width: 55rpx;height: 55rpx; margin-right: 20rpx;" src="../../static/wallet.png" mode=""></image>
  34. 余额支付
  35. </view>
  36. </u-radio> -->
  37. </u-radio-group>
  38. </view>
  39. </view>
  40. <view class="fixed-bottom pd-36">
  41. <u-button type="primary" @click="pay">确认支付</u-button>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. mapState
  48. } from 'vuex'
  49. export default {
  50. data() {
  51. return {
  52. order_id: '',
  53. coupon_id:'',
  54. price: '',
  55. time: 0,
  56. value: 1,
  57. code: '',
  58. };
  59. },
  60. computed: {
  61. ...mapState(['url'])
  62. },
  63. onLoad(option) {
  64. this.order_id = option.id
  65. this.coupon_id = option.coupon_id
  66. this.init()
  67. // #ifdef H5
  68. console.log(location.href);
  69. // 回调回来接收code,然后调取支付
  70. if (location.href.includes('code')) {
  71. this.code = location.href.split('?')[1].split('&')[0].split('=')[1]
  72. this.payAPI()
  73. } else {
  74. this.$store.commit('getUrl', encodeURIComponent(location.href))
  75. }
  76. // #endif
  77. },
  78. methods: {
  79. init() {
  80. this.$http('/addons/ddrive/hyorder/info', {
  81. order_id: this.order_id
  82. }).then(data => {
  83. this.price = data.discount_price
  84. this.time = data.createtime + 15 * 60 - (new Date()).getTime() / 1000
  85. })
  86. },
  87. pay() {
  88. // value 1 微信 2 支付宝 3 余额
  89. // 如果是H5的微信支付,需要先授权,授权回调回来获取code,再调取支付
  90. if (this.value == 1) {
  91. // #ifdef H5
  92. location.href =
  93. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfa24b924871985a8&redirect_uri=${this.url}&response_type=code&scope=snsapi_base&state=STATE#wechat_re`
  94. // #endif
  95. // #ifdef MP-WEIXIN
  96. let _this = this
  97. uni.login({
  98. provider: 'weixin',
  99. success(loginRes) {
  100. console.log(loginRes);
  101. _this.code = loginRes.code
  102. _this.payAPI()
  103. }
  104. });
  105. // #endif
  106. // #ifdef APP-PLUS
  107. this.payAPI()
  108. // #endif
  109. } else {
  110. this.payAPI('alipay')
  111. }
  112. },
  113. payAPI(query='') {
  114. this.$http('/addons/ddrive/hyorder/driver_pay', {
  115. order_id: this.order_id,
  116. coupon_id:this.coupon_id,
  117. // #ifdef APP-PLUS
  118. method: 'app',
  119. type: query?query:'user_wechat',
  120. // #endif
  121. // #ifdef MP-WEIXIN
  122. type: 'mini_wechat',
  123. code: this.code,
  124. method:'miniapp',
  125. // #endif
  126. // #ifdef H5
  127. method: 'mp',
  128. code: this.code,
  129. // #endif
  130. }, "POST").then(data => {
  131. console.log('支付参数1', data, this.value);
  132. //{
  133. // appid: "wxd5cdb23fa8a8a8a4"
  134. // noncestr: "ElHzMeZUqs676yRK"
  135. // package: "Sign=WXPay"
  136. // partnerid: "1601210852"
  137. // prepayid: "wx271544341817098433edb8b12397d20000"
  138. // sign: "5E26B16FE8B6995D333F09A44A5C7EC3"
  139. // timestamp: "1603784674"
  140. //}
  141. // 调取支付接口获取支付参数
  142. // 1、余额支付直接支付成功
  143. // 2、微信支付:则判断支付环境,分别走app支付、小程序支付和H5支付
  144. // 3、支付宝支付:走app支付
  145. if (this.value == 3) {
  146. uni.showToast({
  147. title: '支付成功'
  148. })
  149. setTimeout(() => {
  150. uni.navigateTo({
  151. url: '/pages/home/pay-success'
  152. })
  153. }, 1000)
  154. } else if (this.value == 1) {
  155. // #ifdef APP-PLUS
  156. console.log(999999);
  157. let _this = this
  158. uni.requestPayment({
  159. provider: 'wxpay',
  160. orderInfo: data, //微信、支付宝订单数据
  161. success: function(res) {
  162. console.log('success:' + JSON.stringify(res));
  163. uni.showToast({
  164. title: '支付成功'
  165. })
  166. setTimeout(() => {
  167. uni.redirectTo({
  168. url: '/pages/huoyun/order-detail?id=' + _this.order_id
  169. })
  170. }, 1000)
  171. },
  172. fail: function(err) {
  173. console.log('fail:' + JSON.stringify(err));
  174. uni.showToast({
  175. title: '支付失败,请重新支付',
  176. icon: 'none'
  177. })
  178. }
  179. });
  180. // #endif
  181. // #ifdef MP-WEIXIN
  182. uni.requestPayment({
  183. provider: 'wxpay',
  184. timeStamp: data.timeStamp,
  185. nonceStr: data.nonceStr,
  186. package: data.package,
  187. signType: data.signType,
  188. paySign: data.paySign,
  189. success: function(res) {
  190. console.log('success:' + JSON.stringify(res));
  191. uni.showToast({
  192. title: '支付成功'
  193. })
  194. setTimeout(() => {
  195. uni.redirectTo({
  196. url: '/pages/huoyun/order-detail?id=' + _this.order_id
  197. })
  198. }, 1000)
  199. },
  200. fail: function(err) {
  201. console.log('fail:' + JSON.stringify(err));
  202. uni.showToast({
  203. title: '支付失败,请重新支付',
  204. icon: 'none'
  205. })
  206. }
  207. });
  208. // #endif
  209. // #ifdef H5
  210. WeixinJSBridge.invoke(
  211. 'getBrandWCPayRequest', {
  212. appId: data.appId, //公众号名称,由商户传入
  213. nonceStr: data.nonceStr, //随机串
  214. package: data.package,
  215. signType: data.signType, //微信签名方式:
  216. timeStamp: data.timeStamp, //时间戳,自1970年以来的秒数
  217. paySign: data.paySign //微信签名
  218. },
  219. function(res) {
  220. if (res.err_msg == "get_brand_wcpay_request:ok") {
  221. // 使用以上方式判断前端返回,微信团队郑重提示:
  222. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  223. uni.showToast({
  224. title: '支付成功'
  225. })
  226. setTimeout(() => {
  227. uni.redirectTo({
  228. url: '/pages/huoyun/order-detail?id=' + _this.order_id
  229. })
  230. }, 1000)
  231. } else {
  232. uni.showToast({
  233. title: '支付失败,请重新支付',
  234. icon: 'none'
  235. })
  236. }
  237. });
  238. // #endif
  239. } else if (this.value == 2) {
  240. // #ifdef APP-PLUS
  241. let _this = this
  242. uni.requestPayment({
  243. provider: 'alipay',
  244. orderInfo: data, //微信、支付宝订单数据
  245. success: function(res) {
  246. console.log('success:' + JSON.stringify(res));
  247. uni.showToast({
  248. title: '支付成功'
  249. })
  250. setTimeout(() => {
  251. uni.redirectTo({
  252. url: '/pages/huoyun/order-detail?id=' + _this.order_id
  253. })
  254. }, 1000)
  255. },
  256. fail: function(err) {
  257. console.log('fail:' + JSON.stringify(err));
  258. uni.showToast({
  259. title: '支付失败,请重新支付',
  260. icon: 'none'
  261. })
  262. }
  263. });
  264. // #endif
  265. }
  266. })
  267. },
  268. radioChange(e) {
  269. this.value = e.detail.value
  270. },
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. /deep/.u-primary-hover {
  276. background: linear-gradient(to right,#00b6b4,#00d496) !important;
  277. }
  278. /deep/.u-radio {
  279. display: flex;
  280. flex-direction: row-reverse;
  281. }
  282. /deep/.u-radio-group {
  283. width: 100% !important;
  284. }
  285. /deep/.u-radio__label {
  286. width: 100%;
  287. }
  288. </style>