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.
 
 
 
 
 
 

382 lines
8.8 KiB

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