pay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{ money }}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <!-- v-if="ispg == 'android'" -->
  9. <!-- #ifndef APP-PLUS -->
  10. <view class="type-item b-b" @click="changePayType(1)">
  11. <text class="icon iconfont iconweixin"></text>
  12. <view class="con">
  13. <text class="tit">微信支付</text>
  14. <text>推荐使用微信支付</text>
  15. </view>
  16. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
  17. </view>
  18. <!-- #endif -->
  19. <!-- #ifdef APP-PLUS -->
  20. <view class="type-item b-b" @click="changePayType(2)">
  21. <text class="icon iconfont iconzhifubao"></text>
  22. <view class="con"><text class="tit">支付宝支付</text></view>
  23. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
  24. </view>
  25. <!-- #endif -->
  26. <view class="type-item" @click="changePayType(3)">
  27. <text class="icon iconfont iconyue"></text>
  28. <view class="con">
  29. <text class="tit">余额支付</text>
  30. <text>可用余额 ¥{{ now_money }}</text>
  31. </view>
  32. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
  33. </view>
  34. <view class="type-item" @click="changePayType(4)">
  35. <view class="type-icon"><image class="type-icon-bg" src="../../static/icon/inPay.png" mode=""></image></view>
  36. <view class="con">
  37. <text class="tit">积分支付</text>
  38. <text>可用积分 ¥{{ brokerage_price }}</text>
  39. </view>
  40. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 4"></radio></label>
  41. </view>
  42. </view>
  43. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  44. </view>
  45. </template>
  46. <script>
  47. import { balance } from '@/api/wallet.js';
  48. import { createOrderkey, computedOrderkey, orderPay } from '@/api/order.js';
  49. import { getUserInfo } from '@/api/user.js';
  50. import { mapState } from 'vuex';
  51. import { isIOS } from '@/utils/platform.js';
  52. export default {
  53. data() {
  54. return {
  55. payType: 1, //支付类型
  56. payName: 'weixin',
  57. orderInfo: {},
  58. money: 0.0, //订单金额
  59. now_money: 0.0, //余额
  60. brokerage_price: 0.0, //零钱
  61. orderKey: '',
  62. orderId: '', //保存订单id
  63. payLoding: false, //判断是否支付中
  64. type: '', //判断是否从订单中进入
  65. // #ifdef H5
  66. froms: '', //保存h5中数据来源对象
  67. // #endif
  68. pinkid: '', //保存拼团商品id
  69. ispg: '' //是否是苹果
  70. };
  71. },
  72. computed: {
  73. // #ifdef H5
  74. ...mapState(['weichatObj'])
  75. // #endif
  76. },
  77. onLoad(options) {
  78. console.log(options, 'options');
  79. this.ispg = uni.getSystemInfoSync().platform;
  80. console.log(this.ispg);
  81. if (options.type == 1) {
  82. this.type = 1;
  83. this.orderId = options.ordid;
  84. this.money = options.money;
  85. } else {
  86. this.orderKey = options.key;
  87. let prepage = this.$api.prePage();
  88. computedOrderkey({
  89. orderkey: this.orderKey,
  90. couponId: prepage.couponChecked.id, //优惠券编号
  91. addressId: prepage.addressData.id, //地址编号
  92. useIntegral: prepage.checkedPoints ? 1 : 0
  93. }).then(({ data }) => {
  94. // 获取支付金额
  95. this.money = data.result.pay_price;
  96. });
  97. }
  98. // 保存pinkid
  99. if (options.pinkid) {
  100. this.pinkid = options.pinkid;
  101. }
  102. // 载入余额
  103. getUserInfo({}).then(({ data }) => {
  104. // 获取余额
  105. console.log(data, '123456');
  106. this.now_money = data.now_money;
  107. this.brokerage_price = data.commissionCount;
  108. });
  109. },
  110. methods: {
  111. //选择支付方式
  112. changePayType(type) {
  113. this.payType = type;
  114. if (this.payType == 1) {
  115. this.payName = 'weixin';
  116. }
  117. if (this.payType == 2) {
  118. this.payName = 'ali';
  119. }
  120. if (this.payType == 3) {
  121. this.payName = 'yue';
  122. }
  123. if (this.payType == 4) {
  124. this.payName = 'integral';
  125. }
  126. },
  127. // 支付金额
  128. orderMoneyPay() {
  129. let obj = this;
  130. console.log(obj.orderId, '编号');
  131. orderPay({
  132. uni: obj.orderId,
  133. // #ifdef H5
  134. from: obj.froms ? 'weixin' : 'H5', //来源
  135. // #endif
  136. // #ifdef MP-WEIXIN
  137. from: 'routine', //来源
  138. // #endif
  139. // #ifdef APP-PLUS
  140. from: 'app', //来源
  141. // #endif
  142. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  143. })
  144. .then(e => {
  145. // 判断是否为余额支付
  146. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  147. if (e.status == 200) {
  148. obj.paySuccessTo();
  149. } else {
  150. obj.$api.msg(msg);
  151. }
  152. }
  153. if (obj.payName == 'brokerage' && e.data.status == 'SUCCESS') {
  154. if (e.status == 200) {
  155. obj.paySuccessTo();
  156. } else {
  157. obj.$api.msg(msg);
  158. }
  159. }
  160. //#ifndef APP-PLUS
  161. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  162. let da = e.data.result.jsConfig;
  163. console.log(da, '支付打印');
  164. let data = {
  165. // #ifdef H5
  166. timestamp: da.timestamp,
  167. // #endif
  168. // #ifdef MP
  169. timeStamp: da.timestamp,
  170. // #endif
  171. nonceStr: da.nonceStr,
  172. package: da.package,
  173. signType: da.signType,
  174. paySign: da.paySign,
  175. success: function(res) {
  176. obj.paySuccessTo();
  177. },
  178. fail: e => {
  179. console.log(e, '失败');
  180. // uni.navigateTo({
  181. // url: '/pages/order/order?state=0'
  182. // });
  183. }
  184. };
  185. // #ifdef H5
  186. if (obj.payName == 'weixin') {
  187. obj.weichatObj.chooseWXPay(data);
  188. }
  189. // #endif
  190. // #ifdef MP-WEIXIN
  191. if (obj.payName == 'routine') {
  192. wx.requestPayment(data);
  193. }
  194. // #endif
  195. }
  196. // #endif
  197. //#ifdef APP-PLUS
  198. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  199. let da = e.data.result.jsConfig;
  200. console.log('--da--', da);
  201. let data = {
  202. appid: da.appid,
  203. noncestr: da.noncestr,
  204. package: da.package,
  205. partnerid: da.partnerid,
  206. prepayid: da.prepayid,
  207. timestamp: da.timestamp,
  208. sign: da.sign
  209. };
  210. console.log('--data--', data);
  211. uni.requestPayment({
  212. provider: 'wxpay',
  213. orderInfo: data,
  214. success(res) {
  215. console.log(res);
  216. uni.showToast({
  217. title: '支付成功',
  218. duration: 2000
  219. });
  220. obj.paySuccessTo();
  221. },
  222. fail(res) {
  223. console.log(res, '失败');
  224. }
  225. });
  226. }
  227. if (obj.payName == 'ali') {
  228. const url = e.data.result.jsConfig;
  229. console.log(url, 'url');
  230. uni.requestPayment({
  231. provider: 'alipay',
  232. orderInfo: url,
  233. success: res => {
  234. console.log(res);
  235. uni.showToast({
  236. title: '支付成功',
  237. duration: 2000
  238. });
  239. obj.paySuccessTo();
  240. },
  241. fail: e => {
  242. console.log(e);
  243. },
  244. complete: () => {}
  245. });
  246. obj.payLoding = false;
  247. }
  248. //#endif
  249. uni.hideLoading();
  250. obj.payLoding = false;
  251. })
  252. .catch(e => {
  253. // 支付完成
  254. uni.hideLoading();
  255. obj.payLoding = false;
  256. console.log(e);
  257. });
  258. },
  259. // 支付成功跳转
  260. paySuccessTo() {
  261. uni.hideLoading();
  262. uni.redirectTo({
  263. url: '/pages/money/paySuccess?orderid=' + this.orderId
  264. });
  265. },
  266. //确认支付
  267. confirm: async function() {
  268. let obj = this;
  269. // 判断是否余额不足
  270. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  271. uni.showModal({
  272. title: '提示',
  273. content: '账户余额不足!',
  274. showCancel: false,
  275. success: res => {},
  276. fail: () => {},
  277. complete: () => {}
  278. });
  279. return;
  280. }
  281. // 判断是否余额不足
  282. if (obj.payName == 'brokerage' && +obj.brokerage_price < obj.money) {
  283. uni.showModal({
  284. title: '提示',
  285. content: '账户零钱不足!',
  286. showCancel: false,
  287. success: res => {},
  288. fail: () => {},
  289. complete: () => {}
  290. });
  291. return;
  292. }
  293. uni.showLoading({
  294. title: '支付中',
  295. mask: true
  296. });
  297. // 支付中
  298. obj.payLoding = true;
  299. // #ifdef H5
  300. // 获取当前是否为微信浏览器
  301. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  302. // #endif
  303. // 判断是否为未支付订单中跳转进入
  304. if (obj.type != 1) {
  305. // 初次生成订单
  306. obj.firstCreateOrder();
  307. } else {
  308. // 已经生成订单未支付
  309. obj.orderMoneyPay();
  310. }
  311. },
  312. // 初次订单创建
  313. firstCreateOrder() {
  314. let obj = this;
  315. // 获取下单页面数据
  316. let prepage = obj.$api.prePage();
  317. let data = {
  318. real_name: prepage.addressData.real_name, //联系人名称
  319. phone: prepage.addressData.phone, //联系人号码
  320. couponId: prepage.couponChecked.id, //优惠券编号
  321. addressId: prepage.addressData.id, //支付地址id
  322. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  323. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  324. mark: prepage.desc, //备注
  325. // #ifdef H5
  326. from: obj.froms ? 'weixin' : 'H5', //来源
  327. // #endif
  328. // #ifdef MP-WEIXIN
  329. from: 'routine', //来源
  330. // #endif
  331. // #ifdef APP-PLUS
  332. from: 'app', //来源
  333. // #endif
  334. shipping_type: 1 //提货方式 1 快递 2自提
  335. };
  336. // 判断是否拼团商品
  337. if (obj.pinkid) {
  338. data.pinkId = obj.pinkid;
  339. }
  340. console.log(data, '订单参数', obj.orderKey, '订单key');
  341. // 生成订单
  342. createOrderkey(data, obj.orderKey)
  343. .then(({ data, status, msg }) => {
  344. console.log(data, '生成订单');
  345. // 判断是否支付失败
  346. if (data.status == 'ORDER_EXIST') {
  347. uni.showModal({
  348. title: '提示',
  349. content: msg,
  350. showCancel: false
  351. });
  352. uni.hideLoading();
  353. obj.payLoding = false;
  354. return;
  355. }
  356. // 保存订单号
  357. obj.orderId = data.result.orderId;
  358. // 判断是否为余额支付
  359. if (obj.payName == 'yue') {
  360. if (status == 200) {
  361. obj.paySuccessTo();
  362. } else {
  363. obj.$api.msg(msg);
  364. }
  365. } else if (obj.payName == 'brokerage') {
  366. if (status == 200) {
  367. obj.paySuccessTo();
  368. } else {
  369. obj.$api.msg(msg);
  370. }
  371. } else {
  372. // 立即支付
  373. obj.orderMoneyPay();
  374. }
  375. })
  376. .catch(e => {
  377. uni.hideLoading();
  378. obj.payLoding = false;
  379. console.log(e, '123456789');
  380. });
  381. }
  382. }
  383. };
  384. </script>
  385. <style lang="scss">
  386. .app {
  387. width: 100%;
  388. }
  389. .price-box {
  390. background-color: #fff;
  391. height: 265upx;
  392. display: flex;
  393. flex-direction: column;
  394. justify-content: center;
  395. align-items: center;
  396. font-size: 28upx;
  397. color: #909399;
  398. .price {
  399. font-size: 50upx;
  400. color: #303133;
  401. margin-top: 12upx;
  402. &:before {
  403. content: '¥';
  404. font-size: 40upx;
  405. }
  406. }
  407. }
  408. .pay-type-list {
  409. margin-top: 20upx;
  410. background-color: #fff;
  411. padding-left: 60upx;
  412. .type-item {
  413. .type-icon {
  414. width: 104rpx;
  415. height: 50rpx;
  416. image {
  417. width: 50rpx;
  418. height: 50rpx;
  419. }
  420. }
  421. height: 120upx;
  422. padding: 20upx 0;
  423. display: flex;
  424. justify-content: space-between;
  425. align-items: center;
  426. padding-right: 60upx;
  427. font-size: 30upx;
  428. position: relative;
  429. }
  430. .icon {
  431. width: 100upx;
  432. font-size: 52upx;
  433. }
  434. .iconweixin1 {
  435. color: #ff0000;
  436. }
  437. .iconyue {
  438. color: #fe8e2e;
  439. }
  440. .iconweixin {
  441. color: #36cb59;
  442. }
  443. .iconzhifubao {
  444. color: #01aaef;
  445. }
  446. .tit {
  447. font-size: $font-lg;
  448. color: $font-color-dark;
  449. margin-bottom: 4upx;
  450. }
  451. .con {
  452. flex: 1;
  453. display: flex;
  454. flex-direction: column;
  455. font-size: $font-sm;
  456. color: $font-color-light;
  457. }
  458. }
  459. .mix-btn {
  460. display: flex;
  461. align-items: center;
  462. justify-content: center;
  463. width: 630upx;
  464. height: 80upx;
  465. margin: 80upx auto 30upx;
  466. font-size: $font-lg;
  467. color: #fff;
  468. background-color: $base-color;
  469. border-radius: 10upx;
  470. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  471. }
  472. .clickbg {
  473. background-color: $color-gray !important;
  474. }
  475. </style>