pay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. "userId": JSON.parse(uni.getStorageSync('userInfo')).userId
  147. }).then(res => {
  148. uni.hideLoading()
  149. if (res.state == 'Success') {
  150. this.realPay = res.content
  151. this.info.couponId = res.content.couponId
  152. }
  153. })
  154. })
  155. },
  156. choose() {
  157. let that = this
  158. uni.navigateTo({
  159. url: './coupon?couponId=' + this.realPay.couponLogId,
  160. success: function(res) {
  161. // 通过eventChannel向被打开页面传送数据
  162. res.eventChannel.emit('pay', that.info)
  163. }
  164. })
  165. },
  166. //创建订单
  167. creat() {
  168. if (this.loading) return
  169. this.loading = true
  170. uni.showLoading({
  171. title: '支付中'
  172. })
  173. let that = this
  174. if (!this.payData.timeStamp) {
  175. // 处理扩展字段 暂时只有研学商品和视频会员 视频会员是字符串的JSON 研学是字符串
  176. let extend
  177. try{
  178. if(JSON.parse(this.info.extend)){
  179. extend = this.info.extend
  180. }
  181. }catch(e){
  182. extend = this.info.reservationTime || ''
  183. }
  184. creat({
  185. discountId: (this.payInfo.couponGoodsLogId || this.payInfo.couponGoodsLogId == -1) ? [this
  186. .payInfo.couponGoodsLogId
  187. ] : [],
  188. extend,
  189. channel:'ZhongShu',
  190. goodsList: this.info.goodsId ? [this.info.goodsId] : [],
  191. idempotent: guid(),
  192. shopId: this.info.shopId
  193. }).then(res => {
  194. this.loading = false
  195. if (res.state == 'Success') {
  196. if (!this.payInfo.price) { //价格为0
  197. uni.hideLoading()
  198. uni.reLaunch({
  199. url: '/my/order/index'
  200. })
  201. } else {
  202. this.query.orderNo = res.content.orderNo
  203. this.query.subOpenId = JSON.parse(uni.getStorageSync('userInfo')).openId
  204. this.query.orderDesc = this.info.goodsName
  205. creatPayOrder(this.query).then(data => {
  206. that.payData = JSON.parse(data.content.miniPayRequest)
  207. if (data.content.miniPayRequest == null) return uni.hideLoading()
  208. uni.requestPayment({
  209. "provider": "wxpay",
  210. "orderInfo": that.payData,
  211. "appid": that.payData
  212. .appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  213. "paySign": that.payData.paySign,
  214. "nonceStr": that.payData.nonceStr, // 随机字符串
  215. "package": that.payData.package, // 固定值
  216. // "prepayid": that.payData.package, // 统一下单订单号
  217. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  218. "signType": that.payData.signType, //签名算法
  219. success(msg) {
  220. console.log('msg', that.query.orderNo);
  221. queryPayOrder(that.query.orderNo).then(res1 => {
  222. if (res1.state == 'Success') {
  223. uni.hideLoading()
  224. uni.reLaunch({
  225. url: '/my/order/index'
  226. })
  227. }
  228. })
  229. },
  230. fail(e) {
  231. console.log('err', e);
  232. that.loading = false
  233. uni.hideLoading()
  234. uni.showToast({
  235. title: '取消支付',
  236. icon: 'fail'
  237. })
  238. // 取消支付后,获取支付信息以备再次支付
  239. payDetails(that.query.orderNo).then(r => {
  240. if (r.state == 'Success') {
  241. that.payData = JSON.parse(r.content.miniPayRequest)
  242. }
  243. })
  244. }
  245. })
  246. })
  247. }
  248. }
  249. })
  250. } else { // 取消支付后再次支付
  251. uni.requestPayment({
  252. "provider": "wxpay",
  253. "orderInfo": that.payData,
  254. "appid": that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  255. "paySign": that.payData.paySign,
  256. "nonceStr": that.payData.nonceStr, // 随机字符串
  257. "package": that.payData.package, // 固定值
  258. // "prepayid": that.payData.package, // 统一下单订单号
  259. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  260. "signType": that.payData.signType, //签名算法
  261. success(msg) {
  262. console.log('msg', msg);
  263. queryPayOrder(that.query.orderNo).then(res1 => {
  264. if (res1.state == 'Success') {
  265. uni.hideLoading()
  266. uni.reLaunch({
  267. url: '/my/order/index'
  268. })
  269. }
  270. })
  271. },
  272. fail(e) {
  273. that.loading = false
  274. uni.hideLoading()
  275. uni.showToast({
  276. title: '取消支付',
  277. icon: 'fail'
  278. })
  279. // 取消支付后,获取支付信息以备再次支付
  280. payDetails(that.query.orderNo).then(r => {
  281. if (r.state == 'Success') {
  282. that.payData = JSON.parse(r.content.miniPayRequest)
  283. }
  284. })
  285. console.log('err', e);
  286. }
  287. })
  288. }
  289. }
  290. },
  291. onReady() {
  292. },
  293. onLoad() {
  294. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  295. this.query.userId = userInfo.userId
  296. let that = this
  297. const eventChannel = this.getOpenerEventChannel();
  298. eventChannel.on('pay', function(data) {
  299. that.info = data
  300. try{
  301. let extend = JSON.parse(that.info.extend)
  302. if(extend.account){
  303. that.isVisual = true
  304. that.form.account = extend.account
  305. }
  306. }catch(e){
  307. //TODO handle the exception
  308. }
  309. console.log('data', data);
  310. that.calculate(that.info.goodsId, that.info.shopId)
  311. })
  312. }
  313. }
  314. </script>
  315. <style lang="scss">
  316. .pay {
  317. background: #F9F9F9;
  318. min-height: 100vh;
  319. padding-top: 20rpx;
  320. .shop-info {
  321. margin: 0 30rpx 20rpx;
  322. width: 690rpx;
  323. padding: 24rpx;
  324. box-sizing: border-box;
  325. background: #FFFFFF;
  326. border-radius: 16rpx 16rpx 16rpx 16rpx;
  327. .shop-name {
  328. color: #181818;
  329. font-size: 32rpx;
  330. }
  331. .address {
  332. color: #999999;
  333. font-size: 24rpx;
  334. margin-top: 12rpx;
  335. }
  336. }
  337. .pay-info {
  338. margin: 0 30rpx;
  339. background: #FFFFFF;
  340. border-radius: 16rpx 16rpx 16rpx 16rpx;
  341. padding: 28rpx 24rpx;
  342. .goods-info {
  343. display: flex;
  344. margin-bottom: 30rpx;
  345. .goods-img {
  346. width: 164rpx;
  347. height: 164rpx;
  348. border-radius: 16rpx;
  349. }
  350. .info {
  351. margin-left: 28rpx;
  352. flex: 1;
  353. .goods-name {
  354. font-weight: bold;
  355. color: #181818;
  356. font-size: 32rpx;
  357. width: 100%;
  358. word-break: break-all;
  359. text-overflow: ellipsis;
  360. overflow: hidden;
  361. display: -webkit-box;
  362. -webkit-box-orient: vertical;
  363. -webkit-line-clamp: 2;
  364. /* 这里是超出几行省略 */
  365. }
  366. .num {
  367. color: #999999;
  368. font-size: 24rpx;
  369. margin-top: 16rpx;
  370. }
  371. .price {
  372. font-weight: bold;
  373. color: #181818;
  374. font-size: 32rpx;
  375. margin-top: 26rpx;
  376. }
  377. }
  378. }
  379. .coupon {
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-between;
  383. border-bottom: 2rpx dashed #f3f3f3;
  384. padding: 28rpx 0;
  385. .label {
  386. color: #222222;
  387. font-size: 28rpx;
  388. }
  389. .choose-coupon.none{
  390. color: #AAAAAA;
  391. }
  392. .choose-coupon {
  393. color: #FF4848;
  394. font-size: 28rpx;
  395. display: flex;
  396. align-items: center;
  397. .icon {
  398. width: 28rpx;
  399. height: 28rpx;
  400. display: block;
  401. margin-left: 10rpx;
  402. }
  403. }
  404. }
  405. .explain {
  406. display: flex;
  407. align-items: center;
  408. justify-content: space-between;
  409. padding-top: 28rpx;
  410. .label {
  411. color: #AAAAAA;
  412. font-size: 28rpx;
  413. }
  414. .right {
  415. display: flex;
  416. align-items: center;
  417. font-size: 24rpx;
  418. .red {
  419. font-weight: 500;
  420. color: #FF4848;
  421. margin: 0 10rpx;
  422. }
  423. .fs28 {
  424. font-size: 28rpx !important;
  425. }
  426. }
  427. }
  428. }
  429. .input-box{
  430. margin: 20rpx 30rpx;
  431. padding: 28rpx 24rpx;
  432. background: #FFFFFF;
  433. border-radius: 16rpx 16rpx 16rpx 16rpx;
  434. display: flex;
  435. align-items: center;
  436. justify-content: space-between;
  437. .label {
  438. color: #222;
  439. font-size: 28rpx;
  440. }
  441. .value {
  442. font-size: 28rpx;
  443. color: #AAAAAA;
  444. }
  445. }
  446. .btn-box {
  447. position: fixed;
  448. bottom: 0%;
  449. left: 0%;
  450. width: 100%;
  451. display: flex;
  452. align-items: center;
  453. justify-content: space-between;
  454. box-sizing: border-box;
  455. padding: 10rpx 30rpx env(safe-area-inset-bottom);
  456. border-top: 1rpx solid #EEEEEE;
  457. .total-price {
  458. display: flex;
  459. align-items: center;
  460. .label {
  461. color: #181818;
  462. font-size: 28rpx;
  463. }
  464. .price {
  465. font-size: 36rpx;
  466. font-weight: 800;
  467. color: #FF4848;
  468. margin-left: 10rpx;
  469. }
  470. }
  471. .btn {
  472. width: 280rpx;
  473. height: 80rpx;
  474. line-height: 80rpx;
  475. text-align: center;
  476. background: $uni-color-primary;
  477. box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
  478. border-radius: 46rpx 46rpx 46rpx 46rpx;
  479. font-weight: 800;
  480. color: #FFFFFF;
  481. font-size: 28rpx;
  482. margin: 0;
  483. }
  484. }
  485. }
  486. </style>