pay.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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"> 优惠券 </view>
  29. <view
  30. class="choose-coupon"
  31. :class="[payInfo.offset ? '' : 'none']"
  32. @click="choose"
  33. >
  34. <!-- {{payInfo.goodsCouponName}}-¥{{payInfo.offset}} -->
  35. {{ payInfo.offset ? "-¥" + payInfo.offset : "暂无可用" }}
  36. <image class="icon" src="../static/jiantou-icon.png" mode=""></image>
  37. </view>
  38. </view>
  39. <!-- <view class="explain">
  40. <view class="label">
  41. 优惠说明
  42. </view>
  43. <view class="right">
  44. 已优惠: <view class="red">¥{{payInfo.offset}} </view> 合计: <view class="red fs28">¥{{payInfo.price}}
  45. </view>
  46. </view>
  47. </view> -->
  48. </view>
  49. <view class="input-box" v-if="isVisual">
  50. <view class="label"> 充值账号 </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"> 合计: </view>
  58. <view class="price"> ¥ {{ payInfo.price }} </view>
  59. </view>
  60. <button class="btn" type="default" :loading="loading" @click="creat">
  61. 提交订单
  62. </button>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import { calculate } from "@/api/order.js";
  68. import { creat } from "@/api/goods.js";
  69. import { creatPayOrder, queryPayOrder, payDetails } from "@/api/payment.js";
  70. import guid from "@/utils/guid.js";
  71. export default {
  72. data() {
  73. return {
  74. isVisual: false, //是否是虚拟商品
  75. loading: false,
  76. info: {},
  77. realPay: {
  78. goodsCouponName: "", //优惠券名
  79. couponGoodsLogId: "", //优惠券id
  80. goodsId: "", //商品id
  81. price: "", //支付价格
  82. offset: "", //实际抵扣值
  83. discount: "", //优惠券值
  84. condition: "", //优惠条件
  85. originalPrice: "", //原价
  86. },
  87. form: {
  88. account: "",
  89. },
  90. rules: {
  91. account: {
  92. type: "string",
  93. required: true,
  94. message: "请输入充值账号",
  95. trigger: ["blur", "change"],
  96. },
  97. },
  98. query: {
  99. msgType: "wx.unifiedOrder",
  100. orderDesc: "测试",
  101. orderNo: "",
  102. subOpenId: "",
  103. userId: "",
  104. },
  105. // 支付信息
  106. payData: {},
  107. };
  108. },
  109. computed: {
  110. // 正式支付时的信息
  111. payInfo() {
  112. return {
  113. goodsCouponName: this.realPay.goodsCouponName || "", //优惠券名
  114. couponGoodsLogId: this.info.couponId, //优惠券id
  115. goodsId: this.realPay.goodsId || this.info.goodsId, //商品id
  116. price: this.realPay.price, //支付价格
  117. offset: this.realPay.offset, //实际抵扣值
  118. discount: this.realPay.discount || 0, //优惠券值
  119. condition: this.realPay.condition || "", //优惠条件
  120. originalPrice: this.realPay.originalPrice, //原价
  121. };
  122. },
  123. },
  124. methods: {
  125. calculate(goodsId, shopId) {
  126. return new Promise((resolve, reject) => {
  127. // uni.request({
  128. // url:'http://192.168.110.226:9002/goodsInfo/calculate',
  129. // method:'post',
  130. // data:{
  131. // "goodsId": goodsId,
  132. // // "shopId": shopId,
  133. // "userId": JSON.parse(uni.getStorageSync('userInfo')).userId
  134. // },
  135. // success() {
  136. // }
  137. // })
  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. if (!this.info.extend) {
  172. this.info.extend = JSON.stringify({ channel: "GZU" });
  173. }
  174. console.log("this.info.extend", this.info.extend);
  175. try {
  176. if (JSON.parse(this.info.extend)) {
  177. extend = this.info.extend;
  178. }
  179. } catch (e) {
  180. console.log("e", e);
  181. extend = this.info.reservationTime || "";
  182. }
  183. creat({
  184. discountId:
  185. this.payInfo.couponGoodsLogId || this.payInfo.couponGoodsLogId == -1
  186. ? [this.payInfo.couponGoodsLogId]
  187. : [],
  188. extend,
  189. goodsList: this.info.goodsId ? [this.info.goodsId] : [],
  190. idempotent: guid(),
  191. shopId: this.info.shopId,
  192. }).then((res) => {
  193. this.loading = false;
  194. if (res.state == "Success") {
  195. if (!this.payInfo.price) {
  196. //价格为0
  197. uni.hideLoading();
  198. uni.reLaunch({
  199. url: "/my/order/index",
  200. });
  201. } else {
  202. // 创建订单完成。
  203. this.query.orderNo = res.content.orderNo;
  204. this.query.subOpenId = JSON.parse(
  205. uni.getStorageSync("userInfo")
  206. ).openId;
  207. this.query.orderDesc = this.info.goodsName;
  208. creatPayOrder({
  209. ...this.query,
  210. subAppId: "wx3be1d6d84d46cdf7",
  211. }).then((data) => {
  212. that.payData = JSON.parse(data.content.miniPayRequest);
  213. if (data.content.miniPayRequest == null)
  214. return uni.hideLoading();
  215. uni.redirectTo({
  216. url: "/my/order/detail?id=" + res.content.orderNo,
  217. success() {
  218. that.loading = false;
  219. uni.hideLoading();
  220. },
  221. });
  222. // uni.requestPayment({
  223. // "provider": "wxpay",
  224. // "orderInfo": that.payData,
  225. // "appid": "wxbc64403830bb13c5", // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  226. // "paySign": that.payData.paySign,
  227. // "nonceStr": that.payData.nonceStr, // 随机字符串
  228. // "package": that.payData.package, // 固定值
  229. // // "prepayid": that.payData.package, // 统一下单订单号
  230. // "timeStamp": that.payData.timeStamp, // 时间戳(单位:秒)
  231. // "signType": that.payData.signType, //签名算法
  232. // success(msg) {
  233. // console.log('msg', msg, res);
  234. // queryPayOrder(that.query.orderNo).then(res1 => {
  235. // if (res1.state == 'Success') {
  236. // uni.hideLoading()
  237. // uni.reLaunch({
  238. // url: '/my/order/index'
  239. // })
  240. // }
  241. // })
  242. // },
  243. // fail(e) {
  244. // console.log('err', e);
  245. // that.loading = false
  246. // uni.hideLoading()
  247. // uni.showToast({
  248. // title: '取消支付',
  249. // icon: 'fail'
  250. // })
  251. // // 取消支付后,获取支付信息以备再次支付
  252. // payDetails(that.query.orderNo).then(r => {
  253. // if (r.state == 'Success') {
  254. // that.payData = JSON.parse(r.content.miniPayRequest)
  255. // }
  256. // })
  257. // }
  258. // })
  259. });
  260. // 跳转到支付页面
  261. // /oreder/detail
  262. // uni.redirectTo({
  263. // url: "/my/order/detail?id=" + res.content.orderNo,
  264. // success() {
  265. // that.loading = false;
  266. // uni.hideLoading();
  267. // },
  268. // });
  269. }
  270. }
  271. });
  272. } else {
  273. // 取消支付后再次支付
  274. uni.requestPayment({
  275. provider: "wxpay",
  276. orderInfo: that.payData,
  277. appid: that.payData.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  278. paySign: that.payData.paySign,
  279. nonceStr: that.payData.nonceStr, // 随机字符串
  280. package: that.payData.package, // 固定值
  281. // "prepayid": that.payData.package, // 统一下单订单号
  282. timeStamp: that.payData.timeStamp, // 时间戳(单位:秒)
  283. signType: that.payData.signType, //签名算法
  284. success(msg) {
  285. console.log("msg", msg);
  286. queryPayOrder(that.query.orderNo).then((res1) => {
  287. if (res1.state == "Success") {
  288. uni.hideLoading();
  289. uni.reLaunch({
  290. url: "/my/order/index",
  291. });
  292. }
  293. });
  294. },
  295. fail(e) {
  296. that.loading = false;
  297. uni.hideLoading();
  298. uni.showToast({
  299. title: "取消支付",
  300. icon: "fail",
  301. });
  302. // 取消支付后,获取支付信息以备再次支付
  303. payDetails(that.query.orderNo).then((r) => {
  304. if (r.state == "Success") {
  305. that.payData = JSON.parse(r.content.miniPayRequest);
  306. }
  307. });
  308. console.log("err", e);
  309. },
  310. });
  311. }
  312. },
  313. },
  314. onReady() {},
  315. onLoad() {
  316. let userInfo = JSON.parse(uni.getStorageSync("userInfo"));
  317. this.query.userId = userInfo.userId;
  318. let that = this;
  319. const eventChannel = this.getOpenerEventChannel();
  320. eventChannel.on("pay", function (data) {
  321. that.info = data;
  322. try {
  323. let extend = JSON.parse(that.info.extend);
  324. if (extend.account) {
  325. that.isVisual = true;
  326. that.form.account = extend.account;
  327. }
  328. } catch (e) {
  329. //TODO handle the exception
  330. }
  331. console.log("data", data);
  332. that.calculate(that.info.goodsId, that.info.shopId);
  333. });
  334. },
  335. };
  336. </script>
  337. <style lang="scss">
  338. .pay {
  339. background: #f9f9f9;
  340. min-height: 100vh;
  341. padding-top: 20rpx;
  342. .shop-info {
  343. margin: 0 30rpx 20rpx;
  344. width: 690rpx;
  345. padding: 24rpx;
  346. box-sizing: border-box;
  347. background: #ffffff;
  348. border-radius: 16rpx 16rpx 16rpx 16rpx;
  349. .shop-name {
  350. color: #181818;
  351. font-size: 32rpx;
  352. }
  353. .address {
  354. color: #999999;
  355. font-size: 24rpx;
  356. margin-top: 12rpx;
  357. }
  358. }
  359. .pay-info {
  360. margin: 0 30rpx;
  361. background: #ffffff;
  362. border-radius: 16rpx 16rpx 16rpx 16rpx;
  363. padding: 28rpx 24rpx;
  364. .goods-info {
  365. display: flex;
  366. margin-bottom: 30rpx;
  367. .goods-img {
  368. width: 164rpx;
  369. height: 164rpx;
  370. border-radius: 16rpx;
  371. }
  372. .info {
  373. margin-left: 28rpx;
  374. flex: 1;
  375. .goods-name {
  376. font-weight: bold;
  377. color: #181818;
  378. font-size: 32rpx;
  379. width: 100%;
  380. word-break: break-all;
  381. text-overflow: ellipsis;
  382. overflow: hidden;
  383. display: -webkit-box;
  384. -webkit-box-orient: vertical;
  385. -webkit-line-clamp: 2;
  386. /* 这里是超出几行省略 */
  387. }
  388. .num {
  389. color: #999999;
  390. font-size: 24rpx;
  391. margin-top: 16rpx;
  392. }
  393. .price {
  394. font-weight: bold;
  395. color: #181818;
  396. font-size: 32rpx;
  397. margin-top: 26rpx;
  398. }
  399. }
  400. }
  401. .coupon {
  402. display: flex;
  403. align-items: center;
  404. justify-content: space-between;
  405. border-bottom: 2rpx dashed #f3f3f3;
  406. padding: 28rpx 0;
  407. .label {
  408. color: #222222;
  409. font-size: 28rpx;
  410. }
  411. .choose-coupon.none {
  412. color: #aaaaaa;
  413. }
  414. .choose-coupon {
  415. color: #ff4848;
  416. font-size: 28rpx;
  417. display: flex;
  418. align-items: center;
  419. .icon {
  420. width: 28rpx;
  421. height: 28rpx;
  422. display: block;
  423. margin-left: 10rpx;
  424. }
  425. }
  426. }
  427. .explain {
  428. display: flex;
  429. align-items: center;
  430. justify-content: space-between;
  431. padding-top: 28rpx;
  432. .label {
  433. color: #aaaaaa;
  434. font-size: 28rpx;
  435. }
  436. .right {
  437. display: flex;
  438. align-items: center;
  439. font-size: 24rpx;
  440. .red {
  441. font-weight: 500;
  442. color: #ff4848;
  443. margin: 0 10rpx;
  444. }
  445. .fs28 {
  446. font-size: 28rpx !important;
  447. }
  448. }
  449. }
  450. }
  451. .input-box {
  452. margin: 20rpx 30rpx;
  453. padding: 28rpx 24rpx;
  454. background: #ffffff;
  455. border-radius: 16rpx 16rpx 16rpx 16rpx;
  456. display: flex;
  457. align-items: center;
  458. justify-content: space-between;
  459. .label {
  460. color: #222;
  461. font-size: 28rpx;
  462. }
  463. .value {
  464. font-size: 28rpx;
  465. color: #aaaaaa;
  466. }
  467. }
  468. .btn-box {
  469. position: fixed;
  470. bottom: 0%;
  471. left: 0%;
  472. width: 100%;
  473. height: 98rpx;
  474. display: flex;
  475. align-items: center;
  476. justify-content: space-between;
  477. box-sizing: border-box;
  478. padding: 0 30rpx;
  479. border-top: 1rpx solid #eeeeee;
  480. .total-price {
  481. display: flex;
  482. align-items: center;
  483. .label {
  484. color: #181818;
  485. font-size: 28rpx;
  486. }
  487. .price {
  488. font-size: 36rpx;
  489. font-weight: 800;
  490. color: #ff4848;
  491. margin-left: 10rpx;
  492. }
  493. }
  494. .btn {
  495. width: 280rpx;
  496. height: 80rpx;
  497. line-height: 80rpx;
  498. text-align: center;
  499. background: $uni-color-primary;
  500. box-shadow: inset 0rpx 6rpx 12rpx 2rpx rgba(255, 255, 255, 0.16);
  501. border-radius: 46rpx 46rpx 46rpx 46rpx;
  502. font-weight: 800;
  503. color: #ffffff;
  504. font-size: 28rpx;
  505. margin: 0;
  506. }
  507. }
  508. }
  509. </style>