Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pay.vue 8.3 KiB

4 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{total}}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <view class="type-item b-b" @click="changePayType(1)" v-if="payTypeList.wxpay && total > 0">
  9. <text class="icon yticon icon-wxpay"></text>
  10. <view class="con">
  11. <text class="tit">微信支付</text>
  12. </view>
  13. <label class="radio">
  14. <radio value="" color="#fa436a" :checked='payType == 1' />
  15. </radio>
  16. </label>
  17. </view>
  18. <view class="type-item b-b" @click="changePayType(2)" v-if="payTypeList.alipay && total > 0">
  19. <text class="icon yticon icon-alipay"></text>
  20. <view class="con">
  21. <text class="tit">支付宝支付</text>
  22. </view>
  23. <label class="radio">
  24. <radio value="" color="#fa436a" :checked='payType == 2' />
  25. </radio>
  26. </label>
  27. </view>
  28. <view class="type-item b-b" @click="changePayType(3)" v-if="payTypeList.offline">
  29. <text class="icon yticon icon-pay"></text>
  30. <view class="con">
  31. <text class="tit">货到付款</text>
  32. </view>
  33. <label class="radio">
  34. <radio value="" color="#fa436a" :checked='payType == 3' />
  35. </radio>
  36. </label>
  37. </view>
  38. </view>
  39. <text class="mix-btn" @click="confirm">确认支付</text>
  40. </view>
  41. </template>
  42. <script>
  43. // #ifdef H5
  44. var jweixin = require('jweixin-module');
  45. // #endif
  46. export default {
  47. data() {
  48. return {
  49. payType: 1,
  50. orderInfo: {},
  51. orderId: '',
  52. out_trade_no: '',
  53. payTypeList: {
  54. wxpay: false,
  55. alipay: false
  56. },
  57. total: 0.00
  58. };
  59. },
  60. onLoad(options) {
  61. this.total = options.total;
  62. this.orderId = options.order_id;
  63. this.out_trade_no = options.out_trade_no;
  64. this.getPayType();
  65. },
  66. methods: {
  67. // 获取支付方式
  68. async getPayType() {
  69. let type = await this.$api.request('/pay/getPayType');
  70. if (type) {
  71. this.payTypeList = type;
  72. }
  73. },
  74. //选择支付方式
  75. changePayType(type) {
  76. this.payType = type;
  77. switch (type) {
  78. case 1: // 微信支付
  79. break;
  80. case 2: // 支付宝支付
  81. break;
  82. case 3: // 货到付款
  83. break;
  84. }
  85. },
  86. //确认支付
  87. async confirm() {
  88. if (this.payType == 1) {
  89. // #ifdef H5 || APP-PLUS || MP-WEIXIN
  90. this.weixinPay();
  91. // #endif
  92. } else if (this.payType == 2) {
  93. // 支付宝支付
  94. this.alipay();
  95. } else if (this.payType == 3) {
  96. // 货到付款
  97. this.offlinePay();
  98. }
  99. },
  100. async alipay() {
  101. // #ifdef H5
  102. window.open(this.$unishow + '/pay/alipay?order_id='+this.orderId);
  103. setTimeout(function() {
  104. uni.showModal({
  105. title: '提示',
  106. content: '是否已支付?',
  107. cancelText: '否',
  108. confirmText: '是',
  109. success: function(res) {
  110. if (res.confirm) {
  111. uni.redirectTo({
  112. url: '/pages/order/order?state=0'
  113. });
  114. } else if (res.cancel) {
  115. //console.log('用户点击取消');
  116. }
  117. },
  118. fail: function(res) {
  119. //console.log(res)
  120. }
  121. });
  122. }, 3000);
  123. // #endif
  124. // #ifdef APP-PLUS
  125. let orderInfo = await this.$api.request('/pay/alipay', 'POST',{
  126. order_id : this.orderId
  127. });
  128. if (orderInfo) {
  129. //console.log(orderInfo);
  130. uni.requestPayment({
  131. provider: 'alipay',
  132. orderInfo: orderInfo,
  133. success: function (res) {
  134. console.log('success:' + JSON.stringify(res));
  135. uni.redirectTo({
  136. url: '/pages/money/paySuccess'
  137. })
  138. },
  139. fail: function (err) {
  140. console.log('fail:' + JSON.stringify(err));
  141. that.$api.msg('支付失败');
  142. }
  143. });
  144. }
  145. // #endif
  146. },
  147. async weixinPay() {
  148. let data = await this.$api.request('/pay/unify', 'GET', {
  149. order_id: this.orderId
  150. });
  151. let that = this;
  152. if (data) {
  153. if (data.trade_type == 'MWEB') {
  154. // #ifdef H5
  155. // 微信外的H5
  156. location.href = data.mweb_url;
  157. // #endif
  158. // #ifdef APP-PLUS
  159. // app 使用h5支付
  160. var wv; //计划创建的webview
  161. wv = plus.webview.create("", "custom-webview", {
  162. 'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突
  163. })
  164. wv.loadURL(data.mweb_url, {
  165. Referer: data.referer
  166. });
  167. setTimeout(function() {
  168. uni.showModal({
  169. title: '提示',
  170. content: '是否已支付?',
  171. cancelText: '否',
  172. confirmText: '是',
  173. success: function(res) {
  174. if (res.confirm) {
  175. uni.redirectTo({
  176. url: '/pages/order/order?state=0'
  177. });
  178. } else if (res.cancel) {
  179. //console.log('用户点击取消');
  180. }
  181. },
  182. fail: function(res) {
  183. //console.log(res)
  184. }
  185. });
  186. }, 3000);
  187. // #endif
  188. } else if (data.trade_type == 'JSAPI') {
  189. // #ifdef H5
  190. // 微信内的H5
  191. let config = await this.$api.request('/pay/jssdkBuildConfig');
  192. if (config) {
  193. jweixin.config(config);
  194. jweixin.ready(function() {
  195. jweixin.chooseWXPay({
  196. timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  197. nonceStr: data.nonce_str, // 支付签名随机串,不长于 32 位
  198. package: 'prepay_id=' + data.prepay_id, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  199. signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  200. paySign: data.paySign, // 支付签名
  201. success: function(res) {
  202. // 支付成功后的回调函数
  203. uni.redirectTo({
  204. url: '/pages/money/paySuccess'
  205. })
  206. },
  207. fail: function(err) {
  208. //console.log('fail:' + JSON.stringify(err));
  209. //that.$api.msg('fail:' + JSON.stringify(err))
  210. that.$api.msg('支付失败');
  211. }
  212. })
  213. });
  214. jweixin.error(function(res) {
  215. //that.$api.msg(JSON.stringify(res));
  216. that.$api.msg('支付失败');
  217. });
  218. } else {
  219. that.$api.msg('支付失败');
  220. }
  221. // #endif
  222. // #ifdef MP-WEIXIN
  223. uni.requestPayment({
  224. provider: 'wxpay',
  225. timeStamp: data.timeStamp,
  226. nonceStr: data.nonce_str,
  227. package: 'prepay_id=' + data.prepay_id,
  228. signType: 'MD5',
  229. paySign: data.paySign,
  230. success: function(res) {
  231. uni.redirectTo({
  232. url: '/pages/money/paySuccess'
  233. })
  234. },
  235. fail: function(err) {
  236. //console.log('fail:' + JSON.stringify(err));
  237. //that.$api.msg('fail:' + JSON.stringify(err))
  238. that.$api.msg('支付失败');
  239. }
  240. });
  241. // #endif
  242. }
  243. }
  244. },
  245. async offlinePay() {
  246. let data = await this.$api.request('/pay/offline', 'GET', {
  247. order_id: this.orderId
  248. });
  249. if (data) {
  250. uni.redirectTo({
  251. url: '/pages/money/paySuccess'
  252. });
  253. }
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang='scss'>
  259. .app {
  260. width: 100%;
  261. }
  262. .price-box {
  263. background-color: #fff;
  264. height: 265upx;
  265. display: flex;
  266. flex-direction: column;
  267. justify-content: center;
  268. align-items: center;
  269. font-size: 28upx;
  270. color: #909399;
  271. .price {
  272. font-size: 50upx;
  273. color: #303133;
  274. margin-top: 12upx;
  275. &:before {
  276. content: '¥';
  277. font-size: 40upx;
  278. }
  279. }
  280. }
  281. .pay-type-list {
  282. margin-top: 20upx;
  283. background-color: #fff;
  284. padding-left: 60upx;
  285. .type-item {
  286. height: 120upx;
  287. padding: 20upx 0;
  288. display: flex;
  289. justify-content: space-between;
  290. align-items: center;
  291. padding-right: 60upx;
  292. font-size: 30upx;
  293. position: relative;
  294. }
  295. .icon {
  296. width: 100upx;
  297. font-size: 52upx;
  298. }
  299. .icon-pay {
  300. color: #fe8e2e;
  301. }
  302. .icon-wxpay {
  303. color: #36cb59;
  304. }
  305. .icon-alipay {
  306. color: #01aaef;
  307. }
  308. .tit {
  309. font-size: $font-lg;
  310. color: $font-color-dark;
  311. margin-bottom: 4upx;
  312. }
  313. .con {
  314. flex: 1;
  315. display: flex;
  316. flex-direction: column;
  317. font-size: $font-sm;
  318. color: $font-color-light;
  319. }
  320. }
  321. .mix-btn {
  322. display: flex;
  323. align-items: center;
  324. justify-content: center;
  325. width: 630upx;
  326. height: 80upx;
  327. margin: 80upx auto 30upx;
  328. font-size: $font-lg;
  329. color: #fff;
  330. background-color: $base-color;
  331. border-radius: 10upx;
  332. box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
  333. }
  334. </style>