You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pay.vue 8.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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">Confirm Pay</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. console.log(window.location.href)
  65. this.getPayType();
  66. },
  67. methods: {
  68. // 获取支付方式
  69. async getPayType() {
  70. let type = await this.$api.request('/pay/getPayType');
  71. if (type) {
  72. this.payTypeList = type;
  73. }
  74. },
  75. //选择支付方式
  76. changePayType(type) {
  77. this.payType = type;
  78. switch (type) {
  79. case 1: // 微信支付
  80. break;
  81. case 2: // 支付宝支付
  82. break;
  83. // case 3: // 货到付款
  84. // break;
  85. }
  86. },
  87. //确认支付
  88. async confirm() {
  89. // if (this.payType == 1) {
  90. // // #ifdef H5 || APP-PLUS || MP-WEIXIN
  91. // this.weixinPay();
  92. // // #endif
  93. // } else if (this.payType == 2) {
  94. // // 支付宝支付
  95. // this.alipay();
  96. // }
  97. // else if (this.payType == 3) {
  98. // // 货到付款
  99. this.offlinePay();
  100. // }
  101. },
  102. async alipay() {
  103. // #ifdef H5
  104. window.open(this.$unishow + '/pay/alipay?order_id=' + this.orderId);
  105. setTimeout(function() {
  106. uni.showModal({
  107. title: 'Tip',
  108. content: 'Whether paid?',
  109. cancelText: 'No',
  110. confirmText: 'Yes',
  111. success: function(res) {
  112. if (res.confirm) {
  113. uni.redirectTo({
  114. url: '/pages/order/order?state=0'
  115. });
  116. } else if (res.cancel) {
  117. //console.log('用户点击取消');
  118. }
  119. },
  120. fail: function(res) {
  121. //console.log(res)
  122. }
  123. });
  124. }, 3000);
  125. // #endif
  126. // #ifdef APP-PLUS
  127. let orderInfo = await this.$api.request('/pay/alipay', 'POST', {
  128. order_id: this.orderId
  129. });
  130. if (orderInfo) {
  131. //console.log(orderInfo);
  132. uni.requestPayment({
  133. provider: 'alipay',
  134. orderInfo: orderInfo,
  135. success: function(res) {
  136. console.log('success:' + JSON.stringify(res));
  137. uni.redirectTo({
  138. url: '/pages/money/paySuccess'
  139. })
  140. },
  141. fail: function(err) {
  142. console.log('fail:' + JSON.stringify(err));
  143. that.$api.msg('支付失败');
  144. }
  145. });
  146. }
  147. // #endif
  148. },
  149. async weixinPay() {
  150. let data = await this.$api.request('/pay/unify', 'GET', {
  151. order_id: this.orderId
  152. });
  153. let that = this;
  154. if (data) {
  155. if (data.trade_type == 'MWEB') {
  156. // #ifdef H5
  157. // 微信外的H5
  158. location.href = data.mweb_url;
  159. // #endif
  160. // #ifdef APP-PLUS
  161. // app 使用h5支付
  162. var wv; //计划创建的webview
  163. wv = plus.webview.create("", "custom-webview", {
  164. 'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突
  165. })
  166. wv.loadURL(data.mweb_url, {
  167. Referer: data.referer
  168. });
  169. setTimeout(function() {
  170. uni.showModal({
  171. title: 'Tip',
  172. content: 'Whether paid?',
  173. cancelText: 'NO',
  174. confirmText: 'Yes',
  175. success: function(res) {
  176. if (res.confirm) {
  177. uni.redirectTo({
  178. url: '/pages/order/order?state=0'
  179. });
  180. } else if (res.cancel) {
  181. //console.log('用户点击取消');
  182. }
  183. },
  184. fail: function(res) {
  185. //console.log(res)
  186. }
  187. });
  188. }, 3000);
  189. // #endif
  190. } else if (data.trade_type == 'JSAPI') {
  191. // #ifdef H5
  192. // 微信内的H5
  193. let config = await this.$api.request('/pay/jssdkBuildConfig');
  194. if (config) {
  195. jweixin.config(config);
  196. jweixin.ready(function() {
  197. jweixin.chooseWXPay({
  198. timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  199. nonceStr: data.nonce_str, // 支付签名随机串,不长于 32 位
  200. package: 'prepay_id=' + data.prepay_id, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  201. signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  202. paySign: data.paySign, // 支付签名
  203. success: function(res) {
  204. // 支付成功后的回调函数
  205. uni.redirectTo({
  206. url: '/pages/money/paySuccess'
  207. })
  208. },
  209. fail: function(err) {
  210. //console.log('fail:' + JSON.stringify(err));
  211. //that.$api.msg('fail:' + JSON.stringify(err))
  212. that.$api.msg('支付失败');
  213. }
  214. })
  215. });
  216. jweixin.error(function(res) {
  217. //that.$api.msg(JSON.stringify(res));
  218. that.$api.msg('支付失败');
  219. });
  220. } else {
  221. that.$api.msg('支付失败');
  222. }
  223. // #endif
  224. // #ifdef MP-WEIXIN
  225. uni.requestPayment({
  226. provider: 'wxpay',
  227. timeStamp: data.timeStamp,
  228. nonceStr: data.nonce_str,
  229. package: 'prepay_id=' + data.prepay_id,
  230. signType: 'MD5',
  231. paySign: data.paySign,
  232. success: function(res) {
  233. uni.redirectTo({
  234. url: '/pages/money/paySuccess'
  235. })
  236. },
  237. fail: function(err) {
  238. //console.log('fail:' + JSON.stringify(err));
  239. //that.$api.msg('fail:' + JSON.stringify(err))
  240. that.$api.msg('支付失败');
  241. }
  242. });
  243. // #endif
  244. }
  245. }
  246. },
  247. async offlinePay() {
  248. let data = await this.$api.request('/order/addQRCode?url=' + encodeURIComponent(window.location.href), 'GET', );
  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>