index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <template>
  2. <view>
  3. <form class="form" @submit="checkForm" :style="colorStyle">
  4. <view class="input-section">
  5. <view class="section-hd">{{ $t(`支付金额`) }}</view>
  6. <view class="section-bd">
  7. <view class="input-group">
  8. {{ $t(`¥`) }}
  9. <input v-model.number="money" class="input" name="money" type="digit" @input="inputChange" placeholder="0.00" />
  10. </view>
  11. <view v-if="payPrice && show" class="discount">{{ $t(`会员优惠价`) }}:{{ $t(`¥`) }}{{ payPrice || 0 }}</view>
  12. </view>
  13. </view>
  14. <view class="radio-section">
  15. <view class="section-hd">{{ $t(`支付方式`) }}</view>
  16. <radio-group class="section-bd" name="method">
  17. <label class="item" v-if="yuePay">
  18. <text class="iconfont icon-yue"></text>
  19. <view class="name">
  20. <text>{{ $t(`余额支付`) }}</text>
  21. <text class="money">{{ $t(`可用余额`) }}:{{ $t(`¥`) }}{{ now_money || 0 }}</text>
  22. </view>
  23. <radio value="yue" :checked="payType === 'yue'" />
  24. </label>
  25. <label v-if="wxpay" class="item">
  26. <text class="iconfont icon-weixinzhifu"></text>
  27. <text class="name">{{ $t(`微信支付`) }}</text>
  28. <radio value="weixin" :checked="payType === 'weixin'" />
  29. </label>
  30. </radio-group>
  31. </view>
  32. <button class="button" form-type="submit">{{ $t(`确认`) }}</button>
  33. <view class="alipay" v-html="alipayHtml"></view>
  34. </form>
  35. </view>
  36. </template>
  37. <script>
  38. import { offlineCheckPrice, offlineCreate, orderOfflinePayType } from '@/api/order.js';
  39. import { toLogin } from '@/libs/login.js';
  40. import { mapGetters } from 'vuex';
  41. import colors from '@/mixins/color';
  42. const app = getApp();
  43. export default {
  44. mixins: [colors],
  45. data() {
  46. return {
  47. money: '',
  48. payPrice: '',
  49. payType: 'weixin',
  50. alipayHtml: '',
  51. alipay: false,
  52. wxpay: false,
  53. yuePay: false,
  54. paying: false,
  55. now_money: 0,
  56. isWeixin: false,
  57. site_name: '',
  58. isCommitted: false,
  59. show: false
  60. };
  61. },
  62. computed: mapGetters(['isLogin']),
  63. onLoad(options) {
  64. if (!this.isLogin) {
  65. toLogin();
  66. }
  67. // #ifdef H5
  68. if (options.code) {
  69. let spread = app.globalData.spid ? app.globalData.spid : '';
  70. wechatAuthV2(options.code, spread).then((res) => {
  71. location.href = decodeURIComponent(decodeURIComponent(options.back_url));
  72. });
  73. }
  74. // #endif
  75. },
  76. onShow() {
  77. if (this.isLogin) {
  78. this.getPayType();
  79. }
  80. //#ifdef H5
  81. this.isWeixin = this.$wechat.isWeixin();
  82. //#endif
  83. },
  84. methods: {
  85. inputChange(e) {
  86. var that = this;
  87. e.target.value = e.target.value.match(/^\d*(.?\d{0,2})/g)[0] || '';
  88. this.$nextTick(() => {
  89. this.money = e.target.value;
  90. this.checkPrice();
  91. });
  92. },
  93. getPayType() {
  94. orderOfflinePayType()
  95. .then((res) => {
  96. const { ali_pay_status, pay_weixin_open, yue_pay_status, offline_pay_status, site_name, now_money } = res.data;
  97. this.alipay = ali_pay_status;
  98. this.wxpay = pay_weixin_open;
  99. this.yuePay = yue_pay_status;
  100. this.now_money = now_money;
  101. this.site_name = site_name;
  102. if (!offline_pay_status) {
  103. uni.showModal({
  104. title: this.$t(`支付提醒`),
  105. content: this.$t(`线下支付已关闭,请点击确认按钮返回主页`),
  106. showCancel: false,
  107. success() {
  108. uni.switchTab({
  109. url: '/pages/index/index'
  110. });
  111. }
  112. });
  113. }
  114. if (site_name) {
  115. uni.setNavigationBarTitle({
  116. title: site_name
  117. });
  118. }
  119. })
  120. .catch((err) => {
  121. uni.showToast({
  122. title: err,
  123. icon: 'none'
  124. });
  125. });
  126. },
  127. checkForm(e) {
  128. const { money, method } = e.detail.value;
  129. if (money) {
  130. this.combData(method);
  131. } else {
  132. uni.showToast({
  133. title: this.$t(`请输入支付金额`),
  134. icon: 'none'
  135. });
  136. }
  137. },
  138. // 优惠价
  139. checkPrice() {
  140. offlineCheckPrice({
  141. pay_price: this.money
  142. })
  143. .then((res) => {
  144. this.payPrice = res.data.pay_price;
  145. this.show = res.data.show;
  146. })
  147. .catch((err) => {
  148. uni.showToast({
  149. title: err,
  150. icon: 'none'
  151. });
  152. });
  153. },
  154. // 组合数据
  155. combData(payType) {
  156. let data = {
  157. type: 3,
  158. pay_type: payType,
  159. // #ifdef H5
  160. from: this.isWeixin ? 'weixin' : 'weixinh5',
  161. // #endif
  162. // #ifdef MP
  163. from: 'routine',
  164. // #endif
  165. // #ifdef APP-PLUS
  166. from: 'weixin',
  167. // #endif
  168. price: this.payPrice || this.money,
  169. money: this.money
  170. };
  171. // #ifdef H5
  172. if (this.isWeixin) {
  173. data.from = 'weixin';
  174. }
  175. // #endif
  176. // #ifdef MP
  177. data.from = 'routine';
  178. // #endif
  179. if (this.paying) {
  180. return;
  181. }
  182. this.paying = true;
  183. uni.showLoading({
  184. title: this.$t(`正在确认`)
  185. });
  186. offlineCreate(data)
  187. .then((res) => {
  188. uni.hideLoading();
  189. this.callPay(res);
  190. })
  191. .catch((err) => {
  192. this.paying = false;
  193. uni.showToast({
  194. title: err,
  195. icon: 'none'
  196. });
  197. });
  198. },
  199. formpost(url, postData) {
  200. let tempform = document.createElement('form');
  201. tempform.action = url;
  202. tempform.method = 'post';
  203. tempform.target = '_self';
  204. tempform.style.display = 'none';
  205. for (let x in postData) {
  206. let opt = document.createElement('input');
  207. opt.name = x;
  208. opt.value = postData[x];
  209. tempform.appendChild(opt);
  210. }
  211. document.body.appendChild(tempform);
  212. this.$nextTick((e) => {
  213. tempform.submit();
  214. });
  215. },
  216. // 调用支付
  217. callPay(res) {
  218. const { status, result } = res.data,
  219. { orderId, jsConfig } = result,
  220. goPages = '/pages/annex/offline_result/index?site_name=' + this.site_name;
  221. switch (status) {
  222. case 'ORDER_EXIST':
  223. case 'EXTEND_ORDER':
  224. case 'ALLINPAY_PAY':
  225. uni.hideLoading();
  226. // #ifdef MP
  227. this.initIn = true;
  228. wx.openEmbeddedMiniProgram({
  229. appId: 'wxef277996acc166c3',
  230. extraData: {
  231. cusid: jsConfig.cusid,
  232. appid: jsConfig.appid,
  233. version: jsConfig.version,
  234. trxamt: jsConfig.trxamt,
  235. reqsn: jsConfig.reqsn,
  236. notify_url: jsConfig.notify_url,
  237. body: jsConfig.body,
  238. remark: jsConfig.remark,
  239. validtime: jsConfig.validtime,
  240. randomstr: jsConfig.randomstr,
  241. paytype: jsConfig.paytype,
  242. sign: jsConfig.sign,
  243. signtype: jsConfig.signtype
  244. }
  245. });
  246. this.jumpData = {
  247. orderId: data.data.result.orderId,
  248. msg: data.msg
  249. };
  250. // #endif
  251. // #ifdef APP-PLUS
  252. plus.runtime.openURL(jsConfig.payinfo);
  253. setTimeout((e) => {
  254. uni.reLaunch({
  255. url: '/pages/annex/offline_pay/index'
  256. });
  257. }, 1000);
  258. // #endif
  259. // #ifdef H5
  260. this.formpost(res.data.result.pay_url, jsConfig);
  261. // #endif
  262. break;
  263. case 'PAY_ERROR':
  264. this.paying = false;
  265. this.$util.Tips(
  266. {
  267. title: res.msg
  268. },
  269. {
  270. tab: 5,
  271. url: goPages
  272. }
  273. );
  274. break;
  275. case 'SUCCESS':
  276. this.paying = false;
  277. this.money = '';
  278. this.$util.Tips(
  279. {
  280. title: res.msg,
  281. icon: 'success'
  282. },
  283. {
  284. tab: 5,
  285. url: goPages
  286. }
  287. );
  288. break;
  289. case 'WECHAT_PAY':
  290. // #ifdef MP
  291. let that = this;
  292. let mp_pay_name = '';
  293. if (uni.requestOrderPayment) {
  294. mp_pay_name = 'requestOrderPayment';
  295. } else {
  296. mp_pay_name = 'requestPayment';
  297. }
  298. uni[mp_pay_name]({
  299. timeStamp: jsConfig.timestamp,
  300. nonceStr: jsConfig.nonceStr,
  301. package: jsConfig.package,
  302. signType: jsConfig.signType,
  303. paySign: jsConfig.paySign,
  304. success: function (res) {
  305. that.$util.Tips(
  306. {
  307. title: that.$t(`支付成功`),
  308. icon: 'success'
  309. },
  310. {
  311. tab: 5,
  312. url: '/pages/annex/offline_result/index'
  313. }
  314. );
  315. },
  316. fail: function () {
  317. uni.showToast({
  318. title: that.$t(`取消支付`),
  319. icon: 'none',
  320. success: function () {
  321. that.paying = false;
  322. }
  323. });
  324. },
  325. complete: function () {
  326. that.paying = false;
  327. uni.hideLoading();
  328. }
  329. });
  330. // #endif
  331. // #ifndef MP
  332. this.$wechat
  333. .pay(result.jsConfig)
  334. .then((res) => {
  335. this.paying = false;
  336. this.$util.Tips(
  337. {
  338. title: this.$t(`支付成功`),
  339. icon: 'success'
  340. },
  341. {
  342. tab: 5,
  343. url: '/pages/annex/offline_result/index'
  344. }
  345. );
  346. })
  347. .catch((err) => {
  348. this.paying = false;
  349. if (err.errMsg == 'chooseWXPay:cancel') {
  350. uni.showToast({
  351. title: this.$t(`取消支付`),
  352. icon: 'none'
  353. });
  354. }
  355. });
  356. // #endif
  357. break;
  358. case 'PAY_DEFICIENCY':
  359. this.paying = false;
  360. this.$util.Tips({
  361. title: res.msg
  362. });
  363. break;
  364. case 'WECHAT_H5_PAY':
  365. this.paying = false;
  366. uni.showToast({
  367. title: res.msg,
  368. success() {
  369. location.href = jsConfig.h5_url;
  370. }
  371. });
  372. break;
  373. case 'ALIPAY_PAY':
  374. this.paying = false;
  375. // #ifdef H5
  376. if (this.$wechat.isWeixin()) {
  377. uni.navigateTo({
  378. url: `/pages/users/alipay_invoke/index?id=${orderId}&link=${jsConfig.qrCode}`
  379. });
  380. } else {
  381. this.alipayHtml = jsConfig;
  382. this.$nextTick(() => {
  383. document.getElementById('alipaysubmit').submit();
  384. });
  385. }
  386. // #endif
  387. // #ifdef MP
  388. uni.navigateTo({
  389. url: `/pages/users/alipay_invoke/index?id=${orderId}&link=${jsConfig.qrCode}`
  390. });
  391. // #endif
  392. break;
  393. }
  394. }
  395. }
  396. };
  397. </script>
  398. <style>
  399. page {
  400. background-color: #ffffff;
  401. }
  402. </style>
  403. <style lang="scss" scoped>
  404. /deep/uni-radio .uni-radio-input.uni-radio-input-checked {
  405. border: 1px solid #fdc383 !important;
  406. background-color: #fdc383 !important;
  407. }
  408. .input-section {
  409. .section-hd {
  410. padding: 30rpx;
  411. font-size: 28rpx;
  412. color: #666666;
  413. }
  414. .section-bd {
  415. padding-right: 30rpx;
  416. padding-left: 30rpx;
  417. }
  418. .input-group {
  419. display: flex;
  420. align-items: flex-end;
  421. padding: 45rpx 20rpx 47rpx;
  422. font-size: 80rpx;
  423. color: #999999;
  424. }
  425. .input {
  426. flex: 1;
  427. height: 110rpx;
  428. margin-left: 15rpx;
  429. font-size: 100rpx;
  430. color: #282828;
  431. }
  432. .discount {
  433. padding: 27rpx 20rpx;
  434. border-top: 1rpx solid #eeeeee;
  435. font-size: 28rpx;
  436. color: #e93323;
  437. }
  438. }
  439. .radio-section {
  440. border-top: 20rpx solid #f5f5f5;
  441. .section-hd {
  442. padding: 30rpx;
  443. font-size: 28rpx;
  444. color: #666666;
  445. }
  446. .section-bd {
  447. padding-left: 50rpx;
  448. }
  449. .item {
  450. display: flex;
  451. align-items: center;
  452. padding-top: 30rpx;
  453. padding-right: 30rpx;
  454. padding-bottom: 30rpx;
  455. border-bottom: 1rpx solid #f5f5f5;
  456. .name {
  457. display: flex;
  458. align-items: center;
  459. justify-content: space-between;
  460. }
  461. }
  462. .iconfont {
  463. font-size: 44rpx;
  464. }
  465. .icon-yue {
  466. color: #fe960f;
  467. }
  468. .icon-weixinzhifu {
  469. color: #41b035;
  470. }
  471. .icon-zhifubao {
  472. color: #099bdf;
  473. }
  474. .name {
  475. flex: 1;
  476. margin-left: 30rpx;
  477. font-size: 30rpx;
  478. color: #333333;
  479. }
  480. .money {
  481. float: right;
  482. padding-right: 20rpx;
  483. font-size: 20rpx;
  484. }
  485. }
  486. .button {
  487. height: 86rpx;
  488. border-radius: 43rpx;
  489. margin: 114rpx 30rpx 30rpx;
  490. background: linear-gradient(90deg, #fee2b7 0%, #fdc383 100%);
  491. font-size: 30rpx;
  492. line-height: 86rpx;
  493. color: #5d3324;
  494. }
  495. .alipay {
  496. display: none;
  497. }
  498. </style>