pay.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. payDetails(that.query.orderNo).then(r => {
  177. if (r.state == 'Success') {
  178. that.payData = JSON.parse(r.content.miniPayRequest)
  179. }
  180. })
  181. }
  182. })
  183. })
  184. }
  185. }
  186. })
  187. } else { // 取消支付后再次支付
  188. uni.requestPayment({
  189. "provider": "wxpay",
  190. "orderInfo": that.payData,
  191. "appid": that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  192. "paySign": that.payData.paySign,
  193. "nonceStr": that.payData.nonceStr, // 随机字符串
  194. "package": that.payData.package, // 固定值
  195. // "prepayid": that.payData.package, // 统一下单订单号
  196. "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  197. "signType": that.payData.signType, //签名算法
  198. success(msg) {
  199. console.log('msg', msg);
  200. queryPayOrder(that.query.orderNo).then(res1 => {
  201. if (res1.state == 'Success') {
  202. uni.hideLoading()
  203. uni.reLaunch({
  204. url: '/my/order/index'
  205. })
  206. }
  207. })
  208. },
  209. fail(e) {
  210. that.loading = false
  211. uni.hideLoading()
  212. uni.showToast({
  213. title: '取消支付',
  214. icon: 'fail'
  215. })
  216. // 取消支付后,获取支付信息以备再次支付
  217. payDetails(that.query.orderNo).then(r => {
  218. if (r.state == 'Success') {
  219. that.payData = JSON.parse(r.content.miniPayRequest)
  220. }
  221. })
  222. console.log('err', e);
  223. }
  224. })
  225. }
  226. }
  227. },
  228. onReady() {
  229. },
  230. onLoad() {
  231. let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  232. this.query.userId = userInfo.userId
  233. let that = this
  234. const eventChannel = this.getOpenerEventChannel();
  235. eventChannel.on('pay', function(data) {
  236. that.info = data
  237. try{
  238. let extend = JSON.parse(that.info.extend)
  239. if(extend.account){
  240. that.isVisual = true
  241. that.form.account = extend.account
  242. }
  243. }catch(e){
  244. //TODO handle the exception
  245. }
  246. console.log('data', data);
  247. })
  248. }
  249. }
  250. </script>
  251. <style lang="scss">
  252. .pay {
  253. background: #F9F9F9;
  254. min-height: 100vh;
  255. padding-top: 20rpx;
  256. .shop-info {
  257. margin: 0 30rpx 20rpx;
  258. width: 690rpx;
  259. padding: 24rpx;
  260. box-sizing: border-box;
  261. background: #FFFFFF;
  262. border-radius: 16rpx 16rpx 16rpx 16rpx;
  263. .shop-name {
  264. color: #181818;
  265. font-size: 32rpx;
  266. }
  267. .address {
  268. color: #999999;
  269. font-size: 24rpx;
  270. margin-top: 12rpx;
  271. }
  272. }
  273. .pay-info {
  274. margin: 0 30rpx;
  275. background: #FFFFFF;
  276. border-radius: 16rpx 16rpx 16rpx 16rpx;
  277. padding: 28rpx 24rpx;
  278. .goods-info {
  279. display: flex;
  280. margin-bottom: 30rpx;
  281. .goods-img {
  282. width: 164rpx;
  283. height: 164rpx;
  284. border-radius: 16rpx;
  285. }
  286. .info {
  287. margin-left: 28rpx;
  288. flex: 1;
  289. .goods-name {
  290. font-weight: bold;
  291. color: #181818;
  292. font-size: 32rpx;
  293. width: 100%;
  294. word-break: break-all;
  295. text-overflow: ellipsis;
  296. overflow: hidden;
  297. display: -webkit-box;
  298. -webkit-box-orient: vertical;
  299. -webkit-line-clamp: 2;
  300. /* 这里是超出几行省略 */
  301. }
  302. .num {
  303. color: #999999;
  304. font-size: 24rpx;
  305. margin-top: 16rpx;
  306. }
  307. .price {
  308. font-weight: bold;
  309. color: #181818;
  310. font-size: 32rpx;
  311. margin-top: 26rpx;
  312. }
  313. }
  314. }
  315. .coupon {
  316. display: flex;
  317. align-items: center;
  318. justify-content: space-between;
  319. border-bottom: 2rpx dashed #f3f3f3;
  320. padding: 28rpx 0;
  321. .label {
  322. color: #222222;
  323. font-size: 28rpx;
  324. }
  325. .choose-coupon.none{
  326. color: #AAAAAA;
  327. }
  328. .choose-coupon {
  329. color: #FF4848;
  330. font-size: 28rpx;
  331. display: flex;
  332. align-items: center;
  333. .icon {
  334. width: 28rpx;
  335. height: 28rpx;
  336. display: block;
  337. margin-left: 10rpx;
  338. }
  339. }
  340. }
  341. .explain {
  342. display: flex;
  343. align-items: center;
  344. justify-content: space-between;
  345. padding-top: 28rpx;
  346. .label {
  347. color: #AAAAAA;
  348. font-size: 28rpx;
  349. }
  350. .right {
  351. display: flex;
  352. align-items: center;
  353. font-size: 24rpx;
  354. .red {
  355. font-weight: 500;
  356. color: #FF4848;
  357. margin: 0 10rpx;
  358. }
  359. .fs28 {
  360. font-size: 28rpx !important;
  361. }
  362. }
  363. }
  364. }
  365. .input-box{
  366. margin: 20rpx 30rpx;
  367. padding: 28rpx 24rpx;
  368. background: #FFFFFF;
  369. border-radius: 16rpx 16rpx 16rpx 16rpx;
  370. display: flex;
  371. align-items: center;
  372. justify-content: space-between;
  373. .label {
  374. color: #222;
  375. font-size: 28rpx;
  376. }
  377. .value {
  378. font-size: 28rpx;
  379. color: #AAAAAA;
  380. }
  381. }
  382. .btn-box {
  383. position: fixed;
  384. bottom: 0%;
  385. left: 0%;
  386. width: 100%;
  387. height: 98rpx;
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. box-sizing: border-box;
  392. padding: 0 30rpx;
  393. border-top: 1rpx solid #EEEEEE;
  394. .total-price {
  395. display: flex;
  396. align-items: center;
  397. .label {
  398. color: #181818;
  399. font-size: 28rpx;
  400. }
  401. .price {
  402. font-size: 36rpx;
  403. font-weight: 800;
  404. color: #FF4848;
  405. margin-left: 10rpx;
  406. }
  407. }
  408. .btn {
  409. width: 280rpx;
  410. height: 80rpx;
  411. line-height: 80rpx;
  412. text-align: center;
  413. background: $uni-color-primary;
  414. box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
  415. border-radius: 46rpx 46rpx 46rpx 46rpx;
  416. font-weight: 800;
  417. color: #FFFFFF;
  418. font-size: 28rpx;
  419. margin: 0;
  420. }
  421. }
  422. }
  423. </style>