pay.vue 10 KB

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