pay.vue 11 KB

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