pay.vue 8.5 KB

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