pay.vue 10 KB

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