pay.vue 11 KB

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