pay.vue 12 KB

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