pay.vue 12 KB

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