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.3 KiB

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