pay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <template>
  2. <view class="pay">
  3. <view class="shop-info" v-if="!isVisual">
  4. <view class="shop-name">
  5. {{info.shopName}}
  6. </view>
  7. <view class="address">
  8. {{info.address}}
  9. </view>
  10. </view>
  11. <view class="pay-info">
  12. <view class="goods-info">
  13. <image class="goods-img" :src="info.goodsImg" mode=""></image>
  14. <view class="info">
  15. <view class="goods-name">
  16. {{info.goodsName}}
  17. </view>
  18. <view class="num">
  19. 商品数量:1
  20. </view>
  21. <view class="price">
  22. <!-- ¥{{info.buyPrice}} -->
  23. ¥{{payInfo.originalPrice}}
  24. </view>
  25. </view>
  26. </view>
  27. <view class="coupon">
  28. <view class="label">
  29. 优惠券
  30. </view>
  31. <view class="choose-coupon" @click="choose">
  32. {{payInfo.goodsCouponName}}-¥{{payInfo.offset}}
  33. <image class="icon" src="../static/jiantou-icon.png" mode=""></image>
  34. </view>
  35. </view>
  36. <view class="explain">
  37. <view class="label">
  38. 优惠说明
  39. </view>
  40. <view class="right">
  41. 已优惠: <view class="red">¥{{payInfo.offset}} </view> 合计: <view class="red fs28">¥{{payInfo.price}}
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="input-box" v-if="isVisual">
  47. <!-- <u--form labelPosition="left" errorType="toast" :model="form" :rules="rules" ref="uForm">
  48. <u-form-item required label="充值账号" prop="account" labelWidth="200rpx" ref="item1">
  49. <u--input disabled v-model="form.account" placeholder="请输入充值账号" border="none"></u--input>
  50. </u-form-item>
  51. </u--form> -->
  52. <view class="label">
  53. 充值账号
  54. </view>
  55. <view class="value">
  56. {{form.account}}
  57. </view>
  58. </view>
  59. <view class="btn-box">
  60. <view class="total-price">
  61. <view class="label">
  62. 合计:
  63. </view>
  64. <view class="price">
  65. ¥ {{payInfo.price}}
  66. </view>
  67. </view>
  68. <button class="btn" type="default" :loading="loading" @click="creat">提交订单</button>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. calculate
  75. } from '@/api/order.js';
  76. import {
  77. creat
  78. } from '@/api/goods.js'
  79. import {
  80. creatPayOrder,
  81. queryPayOrder,
  82. payDetails
  83. } from '@/api/payment.js'
  84. import guid from '@/utils/guid.js'
  85. export default {
  86. data() {
  87. return {
  88. isVisual:false,//是否是虚拟商品
  89. loading: false,
  90. info: {},
  91. realPay: {
  92. goodsCouponName: '', //优惠券名
  93. couponGoodsLogId: '', //优惠券id
  94. goodsId: '', //商品id
  95. price: '', //支付价格
  96. offset: '', //实际抵扣值
  97. discount: '', //优惠券值
  98. condition: '', //优惠条件
  99. originalPrice: '', //原价
  100. },
  101. form:{
  102. account:''
  103. },
  104. rules: {
  105. 'account': {
  106. type: 'string',
  107. required: true,
  108. message: '请输入充值账号',
  109. trigger: ['blur', 'change']
  110. },
  111. },
  112. query: {
  113. "msgType": "wx.unifiedOrder",
  114. "orderDesc": "测试",
  115. "orderNo": "",
  116. "subOpenId": "",
  117. "userId": ""
  118. },
  119. // 支付信息
  120. payData: {
  121. }
  122. }
  123. },
  124. computed: {
  125. // 正式支付时的信息
  126. payInfo() {
  127. return {
  128. goodsCouponName: this.realPay.goodsCouponName || '', //优惠券名
  129. couponGoodsLogId: this.info.couponId, //优惠券id
  130. goodsId: this.realPay.goodsId || this.info.goodsId, //商品id
  131. price: this.realPay.price, //支付价格
  132. offset: this.realPay.offset, //实际抵扣值
  133. discount: this.realPay.discount || 0, //优惠券值
  134. condition: this.realPay.condition || '', //优惠条件
  135. originalPrice: this.realPay.originalPrice, //原价
  136. }
  137. }
  138. },
  139. methods: {
  140. calculate(goodsId, shopId) {
  141. return new Promise((resolve, reject) => {
  142. calculate({
  143. couponId: this.info.couponId,
  144. "goodsId": goodsId,
  145. "shopId": shopId,
  146. "userId": JSON.parse(uni.getStorageSync('userInfo')).userId
  147. }).then(res => {
  148. if (res.state == 'Success') {
  149. this.realPay = res.content
  150. }
  151. })
  152. })
  153. },
  154. choose() {
  155. let that = this
  156. uni.navigateTo({
  157. url: './coupon?couponId=' + this.realPay.couponGoodsLogId,
  158. success: function(res) {
  159. // 通过eventChannel向被打开页面传送数据
  160. res.eventChannel.emit('pay', that.info)
  161. }
  162. })
  163. },
  164. //创建订单
  165. creat() {
  166. if (this.loading) return
  167. this.loading = true
  168. uni.showLoading({
  169. title: '支付中'
  170. })
  171. let that = this
  172. if (!this.payData.timeStamp) {
  173. // 处理扩展字段 暂时只有研学商品和视频会员 视频会员是字符串的JSON 研学是字符串
  174. let extend
  175. try{
  176. if(JSON.parse(this.info.extend)){
  177. extend = this.info.extend
  178. }
  179. }catch(e){
  180. extend = this.info.reservationTime || ''
  181. }
  182. creat({
  183. discountId: (this.payInfo.couponGoodsLogId || this.payInfo.couponGoodsLogId == -1) ? [this
  184. .payInfo.couponGoodsLogId
  185. ] : [],
  186. extend,
  187. goodsList: this.info.goodsId ? [this.info.goodsId] : [],
  188. idempotent: guid(),
  189. shopId: this.info.shopId
  190. }).then(res => {
  191. this.loading = false
  192. if (res.state == 'Success') {
  193. if (!this.payInfo.price) { //价格为0
  194. uni.hideLoading()
  195. uni.reLaunch({
  196. url: '/my/order/index'
  197. })
  198. } else {
  199. this.query.orderNo = res.content.orderNo
  200. this.query.subOpenId = JSON.parse(uni.getStorageSync('userInfo')).openId
  201. this.query.orderDesc = this.info.goodsName
  202. creatPayOrder(this.query).then(data => {
  203. that.payData = JSON.parse(data.content.miniPayRequest)
  204. if (data.content.miniPayRequest == null) return uni.hideLoading()
  205. uni.requestPayment({
  206. "provider": "wxpay",
  207. "orderInfo": that.payData,
  208. "appid": that.payData
  209. .appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  210. "paySign": that.payData.paySign,
  211. "nonceStr": that.payData.nonceStr, // 随机字符串
  212. "package": that.payData.package, // 固定值
  213. // "prepayid": that.payData.package, // 统一下单订单号
  214. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  215. "signType": that.payData.signType, //签名算法
  216. success(msg) {
  217. console.log('msg', msg, res);
  218. queryPayOrder(that.query.orderNo).then(res1 => {
  219. if (res1.state == 'Success') {
  220. uni.hideLoading()
  221. uni.reLaunch({
  222. url: '/my/order/index'
  223. })
  224. }
  225. })
  226. },
  227. fail(e) {
  228. console.log('err', e);
  229. that.loading = false
  230. uni.hideLoading()
  231. uni.showToast({
  232. title: '取消支付',
  233. icon: 'fail'
  234. })
  235. // 取消支付后,获取支付信息以备再次支付
  236. payDetails(that.query.orderNo).then(r => {
  237. if (r.state == 'Success') {
  238. }
  239. })
  240. }
  241. })
  242. })
  243. }
  244. }
  245. })
  246. } else { // 取消支付后再次支付
  247. uni.requestPayment({
  248. "provider": "wxpay",
  249. "orderInfo": that.payData,
  250. "appid": that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  251. "paySign": that.payData.paySign,
  252. "nonceStr": that.payData.nonceStr, // 随机字符串
  253. "package": that.payData.package, // 固定值
  254. // "prepayid": that.payData.package, // 统一下单订单号
  255. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  256. "signType": that.payData.signType, //签名算法
  257. success(msg) {
  258. console.log('msg', msg);
  259. queryPayOrder(that.query.orderNo).then(res1 => {
  260. if (res1.state == 'Success') {
  261. uni.hideLoading()
  262. uni.reLaunch({
  263. url: '/my/order/index'
  264. })
  265. }
  266. })
  267. },
  268. fail(e) {
  269. that.loading = false
  270. uni.hideLoading()
  271. uni.showToast({
  272. title: '取消支付',
  273. icon: 'fail'
  274. })
  275. // 取消支付后,获取支付信息以备再次支付
  276. payDetails(that.query.orderNo).then(r => {
  277. if (r.state == 'Success') {
  278. that.payData = JSON.parse(r.content.miniPayRequest)
  279. }
  280. })
  281. console.log('err', e);
  282. }
  283. })
  284. }
  285. }
  286. },
  287. onReady() {
  288. },
  289. onLoad() {
  290. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  291. this.query.userId = userInfo.userId
  292. let that = this
  293. const eventChannel = this.getOpenerEventChannel();
  294. eventChannel.on('pay', function(data) {
  295. that.info = data
  296. try{
  297. let extend = JSON.parse(that.info.extend)
  298. if(extend.account){
  299. that.isVisual = true
  300. that.form.account = extend.account
  301. }
  302. }catch(e){
  303. //TODO handle the exception
  304. }
  305. console.log('data', data);
  306. that.calculate(that.info.goodsId, that.info.shopId)
  307. })
  308. }
  309. }
  310. </script>
  311. <style lang="scss">
  312. .pay {
  313. background: #F9F9F9;
  314. min-height: 100vh;
  315. padding-top: 20rpx;
  316. .shop-info {
  317. margin: 0 30rpx 20rpx;
  318. width: 690rpx;
  319. padding: 24rpx;
  320. box-sizing: border-box;
  321. background: #FFFFFF;
  322. border-radius: 16rpx 16rpx 16rpx 16rpx;
  323. .shop-name {
  324. color: #181818;
  325. font-size: 32rpx;
  326. }
  327. .address {
  328. color: #999999;
  329. font-size: 24rpx;
  330. margin-top: 12rpx;
  331. }
  332. }
  333. .pay-info {
  334. margin: 0 30rpx;
  335. background: #FFFFFF;
  336. border-radius: 16rpx 16rpx 16rpx 16rpx;
  337. padding: 28rpx 24rpx;
  338. .goods-info {
  339. display: flex;
  340. margin-bottom: 30rpx;
  341. .goods-img {
  342. width: 164rpx;
  343. height: 164rpx;
  344. border-radius: 16rpx;
  345. }
  346. .info {
  347. margin-left: 28rpx;
  348. flex: 1;
  349. .goods-name {
  350. font-weight: bold;
  351. color: #181818;
  352. font-size: 32rpx;
  353. width: 100%;
  354. word-break: break-all;
  355. text-overflow: ellipsis;
  356. overflow: hidden;
  357. display: -webkit-box;
  358. -webkit-box-orient: vertical;
  359. -webkit-line-clamp: 2;
  360. /* 这里是超出几行省略 */
  361. }
  362. .num {
  363. color: #999999;
  364. font-size: 24rpx;
  365. margin-top: 16rpx;
  366. }
  367. .price {
  368. font-weight: bold;
  369. color: #181818;
  370. font-size: 32rpx;
  371. margin-top: 26rpx;
  372. }
  373. }
  374. }
  375. .coupon {
  376. display: flex;
  377. align-items: center;
  378. justify-content: space-between;
  379. border-bottom: 2rpx dashed #f3f3f3;
  380. padding: 28rpx 0;
  381. .label {
  382. color: #222222;
  383. font-size: 28rpx;
  384. }
  385. .choose-coupon {
  386. color: #FF4848;
  387. font-size: 28rpx;
  388. display: flex;
  389. align-items: center;
  390. .icon {
  391. width: 28rpx;
  392. height: 28rpx;
  393. display: block;
  394. margin-left: 10rpx;
  395. }
  396. }
  397. }
  398. .explain {
  399. display: flex;
  400. align-items: center;
  401. justify-content: space-between;
  402. padding-top: 28rpx;
  403. .label {
  404. color: #AAAAAA;
  405. font-size: 28rpx;
  406. }
  407. .right {
  408. display: flex;
  409. align-items: center;
  410. font-size: 24rpx;
  411. .red {
  412. font-weight: 500;
  413. color: #FF4848;
  414. margin: 0 10rpx;
  415. }
  416. .fs28 {
  417. font-size: 28rpx !important;
  418. }
  419. }
  420. }
  421. }
  422. .input-box{
  423. margin: 20rpx 30rpx;
  424. padding: 28rpx 24rpx;
  425. background: #FFFFFF;
  426. border-radius: 16rpx 16rpx 16rpx 16rpx;
  427. display: flex;
  428. align-items: center;
  429. justify-content: space-between;
  430. .label {
  431. color: #222;
  432. font-size: 28rpx;
  433. }
  434. .value {
  435. font-size: 28rpx;
  436. color: #AAAAAA;
  437. }
  438. }
  439. .btn-box {
  440. position: fixed;
  441. bottom: 0%;
  442. left: 0%;
  443. width: 100%;
  444. height: 98rpx;
  445. display: flex;
  446. align-items: center;
  447. justify-content: space-between;
  448. box-sizing: border-box;
  449. padding: 0 30rpx;
  450. border-top: 1rpx solid #EEEEEE;
  451. .total-price {
  452. display: flex;
  453. align-items: center;
  454. .label {
  455. color: #181818;
  456. font-size: 28rpx;
  457. }
  458. .price {
  459. font-size: 36rpx;
  460. font-weight: 800;
  461. color: #FF4848;
  462. margin-left: 10rpx;
  463. }
  464. }
  465. .btn {
  466. width: 240rpx;
  467. height: 80rpx;
  468. line-height: 80rpx;
  469. text-align: center;
  470. background: $uni-color-primary;
  471. box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
  472. border-radius: 46rpx 46rpx 46rpx 46rpx;
  473. font-weight: 800;
  474. color: #FFFFFF;
  475. font-size: 28rpx;
  476. margin: 0;
  477. }
  478. }
  479. }
  480. </style>