pay.vue 12 KB

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