pay.vue 10 KB

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