offlinePayment.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="offlinePayment">
  3. <view class="shop-info">
  4. <view class="shop-name">
  5. {{shopInfo.shopName}}
  6. </view>
  7. <view class="address">
  8. {{shopInfo.address}}
  9. </view>
  10. </view>
  11. <view class="pay-info">
  12. <view class="title">
  13. 请输入付款金额
  14. </view>
  15. <view class="input-box">
  16. <view class="icon">
  17. ¥
  18. </view>
  19. <input class="money-input" v-model.number="shopInfo.price" type="digit" />
  20. </view>
  21. <view class="coupon">
  22. <view class="label">
  23. 优惠券
  24. </view>
  25. <view class="choose-coupon" @click="choose">
  26. {{payInfo.goodsCouponName}}-¥{{payInfo.offset}}
  27. <image class="icon" src="../static/jiantou-icon.png" mode=""></image>
  28. </view>
  29. </view>
  30. <view class="explain">
  31. <view class="label">
  32. 优惠说明
  33. </view>
  34. <view class="right">
  35. 已优惠: <view class="red">¥{{payInfo.offset}} </view> 合计: <view class="red fs28">¥{{payInfo.price}}
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="btn-box">
  41. <view class="total-price">
  42. <view class="label">
  43. 合计:
  44. </view>
  45. <view class="price">
  46. ¥ {{payInfo.price}}
  47. </view>
  48. </view>
  49. <button class="btn" type="default" :loading="loading" @click="creat">提交订单</button>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import {
  55. calculateByInput,
  56. creatScanOrder
  57. } from '@/api/order.js';
  58. import {
  59. creatPayOrder,
  60. queryPayOrder,
  61. close
  62. } from '@/api/payment.js'
  63. import {shopDetail} from '@/api/shop.js'
  64. import guid from '@/utils/guid.js'
  65. import {debounce} from '@/utils/tool.js'
  66. export default {
  67. data() {
  68. return {
  69. loading: false,
  70. shopInfo: {
  71. shopName:'',
  72. address:'',
  73. price:0,
  74. },
  75. info: {},
  76. realPay: {
  77. goodsCouponName: '', //优惠券名
  78. couponGoodsLogId: '', //优惠券id
  79. goodsId: '', //商品id
  80. price: '', //支付价格
  81. offset: '', //实际抵扣值
  82. discount: '', //优惠券值
  83. condition: '', //优惠条件
  84. originalPrice: '', //原价
  85. },
  86. query: {
  87. "msgType": "wx.unifiedOrder",
  88. "orderDesc": "线下扫码支付",
  89. "orderNo": "",
  90. "subOpenId": "",
  91. "userId": ""
  92. },
  93. // 支付信息
  94. payData: {
  95. }
  96. }
  97. },
  98. computed: {
  99. // 正式支付时的信息
  100. payInfo() {
  101. return {
  102. goodsCouponName: this.realPay.goodsCouponName || '', //优惠券名
  103. couponGoodsLogId: this.realPay.couponGoodsLogId || '', //优惠券id
  104. price: this.realPay.price, //支付价格
  105. offset: this.realPay.offset, //实际抵扣值
  106. discount: this.realPay.discount || 0, //优惠券值
  107. condition: this.realPay.condition || '', //优惠条件
  108. originalPrice: this.realPay.originalPrice, //原价
  109. }
  110. }
  111. },
  112. watch: {
  113. 'shopInfo.price':debounce(function(val) {
  114. if(!val){
  115. this.shopInfo.price = 0
  116. }
  117. this.calculateByInput(this.shopInfo.shopId)
  118. })
  119. },
  120. methods: {
  121. calculateByInput(shopId) {
  122. return new Promise((resolve, reject) => {
  123. calculateByInput({
  124. couponId: this.info.couponId,
  125. "price": this.shopInfo.price,
  126. "shopId": shopId,
  127. "userId": JSON.parse(uni.getStorageSync('userInfo')).userId
  128. }).then(res => {
  129. if (res.state == 'Success') {
  130. this.realPay = res.content
  131. }
  132. })
  133. })
  134. },
  135. choose() {
  136. let that = this
  137. uni.navigateTo({
  138. url: `./coupon?couponId=${this.realPay.couponGoodsLogId}&type=offlinePayment`,
  139. success: function(res) {
  140. // 通过eventChannel向被打开页面传送数据
  141. res.eventChannel.emit('pay', that.shopInfo)
  142. }
  143. })
  144. },
  145. //创建订单
  146. creat() {
  147. if (this.loading) return
  148. this.loading = true
  149. if (!this.payInfo.price) { //价格为0
  150. uni.hideLoading()
  151. this.loading = false
  152. return uni.showToast({
  153. title:'请输入付款金额',
  154. icon:'none'
  155. })
  156. }
  157. uni.showLoading({
  158. title: '支付中'
  159. })
  160. let that = this
  161. creatScanOrder({
  162. discountId: this.payInfo.couponGoodsLogId ? [this.payInfo.couponGoodsLogId] : [],
  163. extend: "",
  164. price: this.shopInfo.price,
  165. shopId: this.shopInfo.shopId
  166. }).then(res => {
  167. this.loading = false
  168. if (res.state == 'Success') {
  169. this.query.orderNo = res.content.orderNo
  170. this.query.subOpenId = JSON.parse(uni.getStorageSync('userInfo')).openId
  171. creatPayOrder(this.query).then(data => {
  172. that.payData = JSON.parse(data.content.miniPayRequest)
  173. if(data.content.miniPayRequest == null) return uni.hideLoading()
  174. uni.requestPayment({
  175. "provider": "wxpay",
  176. "orderInfo": that.payData,
  177. "appid": that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  178. "paySign": that.payData.paySign,
  179. "nonceStr": that.payData.nonceStr, // 随机字符串
  180. "package": that.payData.package, // 固定值
  181. // "prepayid": that.payData.package, // 统一下单订单号
  182. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  183. "signType": that.payData.signType, //签名算法
  184. success(msg) {
  185. console.log('msg', msg,res);
  186. queryPayOrder(that.query.orderNo).then(res1 => {
  187. if (res1.state == 'Success') {
  188. uni.hideLoading()
  189. uni.reLaunch({
  190. url: '/my/order/index'
  191. })
  192. }
  193. })
  194. },
  195. fail(e) {
  196. console.log('err', e);
  197. // 取消支付后,直接关闭订单
  198. close(that.query.orderNo).then(res=>{
  199. if (res.state == 'Success') {
  200. that.loading = false
  201. uni.hideLoading()
  202. uni.showToast({
  203. title: '取消支付',
  204. icon: 'fail'
  205. })
  206. }
  207. })
  208. }
  209. })
  210. })
  211. }
  212. })
  213. },
  214. shopDetail(shopId){
  215. shopDetail({shopId}).then(res=>{
  216. if (res.state == 'Success') {
  217. this.shopInfo.shopName = res.content.shopName
  218. this.shopInfo.address = res.content.address
  219. }
  220. })
  221. }
  222. },
  223. onLoad(options) {
  224. let that = this
  225. this.shopInfo.shopId = decodeURIComponent(options.scene)
  226. this.shopDetail(this.shopInfo.shopId)
  227. if(!uni.getStorageSync('token')){
  228. return uni.showModal({
  229. title:'请登录',
  230. confirmText:'去登录',
  231. success(res){
  232. console.log(res);
  233. if(res.confirm){
  234. uni.navigateTo({
  235. url:`/login/login/login?redirect=/pay/offlinePayment&scene=${that.shopInfo.shopId}`
  236. })
  237. }
  238. }
  239. })
  240. }
  241. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  242. this.query.userId = userInfo.userId
  243. this.calculateByInput(this.shopInfo.shopId)
  244. const eventChannel = this.getOpenerEventChannel();
  245. if(JSON.stringify(eventChannel) !=='{}'){
  246. eventChannel.on('pay', function(data) {
  247. that.shopInfo = data
  248. })
  249. }
  250. }
  251. }
  252. </script>
  253. <style lang="scss" scoped>
  254. .offlinePayment {
  255. background: #F9F9F9;
  256. min-height: 100vh;
  257. padding-top: 20rpx;
  258. .shop-info {
  259. margin: 0 30rpx 20rpx;
  260. width: 690rpx;
  261. padding: 24rpx;
  262. box-sizing: border-box;
  263. background: #FFFFFF;
  264. border-radius: 16rpx 16rpx 16rpx 16rpx;
  265. .shop-name {
  266. color: #181818;
  267. font-size: 32rpx;
  268. }
  269. .address {
  270. color: #999999;
  271. font-size: 24rpx;
  272. margin-top: 12rpx;
  273. }
  274. }
  275. .pay-info {
  276. margin: 0 30rpx;
  277. background: #FFFFFF;
  278. border-radius: 16rpx 16rpx 16rpx 16rpx;
  279. padding: 28rpx 24rpx;
  280. .title {
  281. font-size: 28rpx;
  282. color: #222222;
  283. }
  284. .input-box {
  285. position: relative;
  286. line-height: 98rpx;
  287. font-size: 36rpx;
  288. border-bottom: 2rpx solid #f3f3f3;
  289. .icon {
  290. position: absolute;
  291. left: 0%;
  292. top: 50%;
  293. transform: translateY(-50%);
  294. }
  295. .money-input {
  296. padding-left: 30rpx;
  297. width: 100%;
  298. height: 100rpx;
  299. line-height: 100rpx;
  300. box-sizing: border-box;
  301. }
  302. }
  303. .coupon {
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. border-bottom: 2rpx dashed #f3f3f3;
  308. padding: 28rpx 0;
  309. .label {
  310. color: #222222;
  311. font-size: 28rpx;
  312. }
  313. .choose-coupon {
  314. color: #FF4848;
  315. font-size: 28rpx;
  316. display: flex;
  317. align-items: center;
  318. .icon {
  319. width: 28rpx;
  320. height: 28rpx;
  321. display: block;
  322. margin-left: 10rpx;
  323. }
  324. }
  325. }
  326. .explain {
  327. display: flex;
  328. align-items: center;
  329. justify-content: space-between;
  330. padding-top: 28rpx;
  331. .label {
  332. color: #AAAAAA;
  333. font-size: 28rpx;
  334. }
  335. .right {
  336. display: flex;
  337. align-items: center;
  338. font-size: 24rpx;
  339. .red {
  340. font-weight: 500;
  341. color: #FF4848;
  342. margin: 0 10rpx;
  343. }
  344. .fs28 {
  345. font-size: 28rpx !important;
  346. }
  347. }
  348. }
  349. }
  350. .btn-box {
  351. position: fixed;
  352. bottom: 0%;
  353. left: 0%;
  354. width: 100%;
  355. height: 98rpx;
  356. display: flex;
  357. align-items: center;
  358. justify-content: space-between;
  359. box-sizing: border-box;
  360. padding: 0 30rpx;
  361. border-top: 1rpx solid #EEEEEE;
  362. .total-price {
  363. display: flex;
  364. align-items: center;
  365. .label {
  366. color: #181818;
  367. font-size: 28rpx;
  368. }
  369. .price {
  370. font-size: 36rpx;
  371. font-weight: 800;
  372. color: #FF4848;
  373. margin-left: 10rpx;
  374. }
  375. }
  376. .btn {
  377. width: 240rpx;
  378. height: 80rpx;
  379. line-height: 80rpx;
  380. text-align: center;
  381. background: linear-gradient(265deg, #FF4A39 0%, #FC8B45 100%);
  382. box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
  383. border-radius: 46rpx 46rpx 46rpx 46rpx;
  384. font-weight: 800;
  385. color: #FFFFFF;
  386. font-size: 28rpx;
  387. margin: 0;
  388. }
  389. }
  390. }
  391. </style>