pay.vue 17 KB

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