confirm_pay.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <template>
  2. <view>
  3. <nav-bar title="订单支付"></nav-bar>
  4. <view>
  5. <view>
  6. <view class="box2">
  7. <view class="title2">支付信息</view>
  8. <!-- <view style="padding:10rpx 30rpx;" v-for="(item,index) in shoppingCartList" :key="index">
  9. <view class="list_title">
  10. <view class="txt_left">商品名称</view>
  11. <view class="txt_right">{{item.goodsName}}</view>
  12. </view>
  13. <view class="list_item">
  14. <view class="txt_left">应付金额</view>
  15. <view class="txt_right_price">¥ {{item.standPrice}}</view>
  16. </view>
  17. </view> -->
  18. <view
  19. v-for="(item, index) in shoppingCartList"
  20. :key="index"
  21. style="
  22. display: flex;
  23. justify-content: space-between;
  24. margin-bottom: 32rpx;
  25. "
  26. >
  27. <image
  28. :src="$method.splitImgHost(item.coverUrl)"
  29. style="height: 120rpx; width: 204rpx; border-radius: 16rpx"
  30. ></image>
  31. <view class="right_con" style="margin-left: 20rpx">
  32. <view style="color: #333333; font-size: 30rpx; font-weight: bold">
  33. {{ item.goodsName }}
  34. </view>
  35. <view class="priceTag"> ¥ {{ item.standPrice }} </view>
  36. <Class-time-tip
  37. v-if="item.gradObj && item.gradObj.gradeId"
  38. :classInfo="item.gradObj"
  39. ></Class-time-tip>
  40. </view>
  41. </view>
  42. <view class="info_right">
  43. <!-- <u-line color="#D6D6DB" /> -->
  44. <view class="list_item">
  45. <view class="txt_left">应付总金额</view>
  46. <view class="txt_right_price">¥ {{ totalPrice }}</view>
  47. </view>
  48. </view>
  49. </view>
  50. <view class="box3">
  51. <view class="title2">支付方式</view>
  52. <view>
  53. <u-radio-group v-model="value" @change="radioGroupChange">
  54. <view class="list_item">
  55. <view class="txt_left_pay"
  56. ><image src="/static/wepay.png" class="pay_icon"></image
  57. >微信支付</view
  58. >
  59. <view
  60. ><u-radio @change="radioChange" name="wepay"></u-radio
  61. ></view>
  62. </view>
  63. <view class="list_item" v-if="false">
  64. <view class="txt_left_pay"
  65. ><image src="/static/unipay.png" class="pay_icon"></image
  66. >云闪付</view
  67. >
  68. <view
  69. ><u-radio @change="radioChange" name="unipay"></u-radio
  70. ></view>
  71. </view>
  72. </u-radio-group>
  73. </view>
  74. <u-line color="#D6D6DB" />
  75. </view>
  76. </view>
  77. </view>
  78. <view class="bottomBox safeArea">
  79. <view class="sums">
  80. <text class="all_sum">总金额:</text>
  81. <text class="priceTag">¥ {{ totalPrice }}</text>
  82. </view>
  83. <view style="display: flex; color: #ffffff; align-items: center">
  84. <button class="btn2" @click="pay()" :disabled="btnNo">确认支付</button>
  85. </view>
  86. </view>
  87. <u-modal
  88. v-model="showModal"
  89. :confirm-text="confirmText"
  90. cancel-text="知道了"
  91. cancel-color="#666666"
  92. confirm-color="rgba(0, 122, 255, 1);"
  93. :show-confirm-button="showConfirmButton"
  94. :show-cancel-button="true"
  95. :content="modalMsg"
  96. @cancel="modalCancel()"
  97. @confirm="modalConfirm()"
  98. ref="uModal"
  99. ></u-modal>
  100. </view>
  101. </template>
  102. <script>
  103. import { mapGetters, mapActions } from "vuex";
  104. import { getQueryString } from "../../common/navTo";
  105. import ClassTimeTip from "../../components/common/ClassTimeTip.vue";
  106. export default {
  107. data() {
  108. return {
  109. showModal: false,
  110. modalMsg: "",
  111. list: [
  112. {
  113. name: "网课",
  114. },
  115. {
  116. name: "题库通",
  117. },
  118. ],
  119. array: [
  120. "全部",
  121. "建设工程施工管理",
  122. "机电全科",
  123. "机电工程管理与实",
  124. "机电全科",
  125. "全科",
  126. ],
  127. current: 0,
  128. menuIndex: 0,
  129. value: "wepay",
  130. btnNo: false,
  131. isBK: "",
  132. fromCart: "",
  133. hasPaying: false,
  134. showConfirmButton: false,
  135. confirmText: "",
  136. options: {},
  137. };
  138. },
  139. onPullDownRefresh() {},
  140. onLoad(option) {
  141. this.fromCart = option.fromCart;
  142. this.isBK = option.isBK;
  143. this.options = option;
  144. },
  145. onShow() {
  146. // #ifdef H5
  147. this.authorize();
  148. // #endif
  149. },
  150. methods: {
  151. ...mapActions(["getUserInfo"]),
  152. authorize() {
  153. if (location.search.includes("code")) {
  154. const code = getQueryString("code");
  155. this.$store.commit("setShoppingCartList", {
  156. shoppingCartList: uni.getStorageSync("shopList"),
  157. });
  158. uni.setStorageSync("h5_code", code);
  159. this.OfficialLogin(code);
  160. } else {
  161. // 没有code,就重定向到地址https://www.xyyxt.net?ask_type=https://api.xyyxt.net/pages2/order/confirm_pay 去获取code,授权后就会把code带上然后访问域名
  162. // ?fromCart=&code=061F5a1w3aolh03SLe1w3sMsCF4F5a16&state=STATE
  163. this.$api.checkBindGzh().then((res) => {
  164. if (!res.data.data && process.env.NODE_ENV !== "development") {
  165. uni.setStorageSync("shopList", this.shoppingCartList);
  166. const url = window.location.host + "/pages2/order/confirm_pay";
  167. // 跳自己授权
  168. if (this.config.gzhSelfLicense) {
  169. this.$api.getWxConfig().then((res) => {
  170. location.replace(
  171. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
  172. res.data.data.gzhAppId
  173. }&redirect_uri=${encodeURIComponent(
  174. "https://" + url
  175. )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  176. );
  177. });
  178. } else {
  179. location.replace("https://www.xyyxt.net/?ask_type=" + url);
  180. }
  181. }
  182. });
  183. }
  184. },
  185. OfficialLogin(code) {
  186. this.$api
  187. .OfficialLogin({
  188. code,
  189. })
  190. .then((res) => {
  191. if (res.data.code !== 200) {
  192. this.$u.toast(res.data.msg);
  193. }
  194. });
  195. },
  196. // 二建 && 七大员 需要先完善信息
  197. async checkNeedInfo() {
  198. let keys = [];
  199. this.shoppingCartList.forEach((item) => {
  200. if (item.erJianErZao) {
  201. keys.push("realname", "idCard");
  202. }
  203. if (item.jjShiGongYuan) {
  204. keys.push("realname", "idCard", "sex", "companyName", "eduLevel");
  205. }
  206. });
  207. if (keys.length) {
  208. if (!this.userInfo) await this.getUserInfo();
  209. if (keys.some((key) => !this.userInfo[key])) {
  210. uni.showModal({
  211. title: "提示",
  212. content: "请先完善个人信息!",
  213. success: (res) => {
  214. if (res.confirm) {
  215. this.$navTo.togo("/pages2/verify/info_fill", {
  216. keys: Array.from(new Set(keys)).toString(),
  217. });
  218. }
  219. },
  220. });
  221. return false;
  222. }
  223. }
  224. return true;
  225. },
  226. async postOrder() {
  227. let list = this.shoppingCartList;
  228. for (let i = 0; i < list.length; i++) {
  229. let item = list[i];
  230. if (item.goodsType == 1) {
  231. if (item.templateType == "class") {
  232. delete item.gradObj.goodsList;
  233. let goodsInputData = {
  234. type: "class",
  235. gradeId: item.gradObj.gradeId,
  236. gradeJson: JSON.stringify(item.gradObj),
  237. };
  238. item.goodsInputData = goodsInputData;
  239. }
  240. if (item.templateType == "apply") {
  241. let goodsInputData = {
  242. type: "apply",
  243. applyAreasJson: JSON.stringify(item.applyAreas),
  244. examDateJson: JSON.stringify(item.examDate),
  245. };
  246. item.goodsInputData = goodsInputData;
  247. }
  248. }
  249. if (this.options.distributionCode) {
  250. item.distributionLinkCode = this.options.linkCode;
  251. item.distributionCode = this.options.distributionCode;
  252. }
  253. }
  254. let data = { goodsList: list };
  255. // 邀请码
  256. if (this.sac) {
  257. data["shareActivityCode"] = this.sac;
  258. }
  259. // #ifdef MP-WEIXIN
  260. this.$api.placeSmallOrder(data).then((res) => {
  261. this.orderResult(res);
  262. });
  263. // #endif
  264. // #ifdef H5
  265. data["url"] = location.href;
  266. this.$api.placeGzhOrder(data).then((res) => {
  267. console.log(res, "h5支付res");
  268. this.orderResult(res);
  269. });
  270. // #endif
  271. },
  272. orderResult(res) {
  273. if (res.data.code == 200) {
  274. uni.setStorageSync("updateCart", 1); //提醒刷新购物车
  275. if (this.totalPrice == 0) {
  276. //免费商品
  277. uni.redirectTo({
  278. url: `/pages2/order/confirm_success?sn=${res.data.data.orderSn}&isBk=${this.isBK}`,
  279. });
  280. } else {
  281. let data = res.data.data;
  282. uni.showLoading({
  283. title: "支付中",
  284. mask: true,
  285. });
  286. // #ifdef MP-WEIXIN
  287. uni.requestPayment({
  288. provider: data.provider,
  289. nonceStr: data.nonceStr,
  290. package: data.package,
  291. signType: data.signType,
  292. paySign: data.sign,
  293. timeStamp: String(data.timeStamp),
  294. success: (res) => {
  295. uni.hideLoading();
  296. this.btnNo = false;
  297. uni.redirectTo({
  298. url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${this.isBK}`,
  299. });
  300. console.log("success:" + JSON.stringify(res));
  301. },
  302. fail: (err) => {
  303. uni.hideLoading();
  304. this.btnNo = false;
  305. console.log("fail:" + JSON.stringify(err));
  306. },
  307. });
  308. // #endif
  309. // #ifdef H5
  310. this.h5_wxpay(data);
  311. // #endif
  312. }
  313. } else if (res.data.code == 510) {
  314. //有未支付订单
  315. this.hasPaying = true;
  316. this.btnNo = false;
  317. this.modalMsg = res.data.msg;
  318. this.showConfirmButton = true;
  319. this.confirmText = "跳转到【我的订单】\n查看未支付订单";
  320. this.showModal = true;
  321. } else if (res.data.code == 511) {
  322. //511 重复购买
  323. this.hasPaying = false;
  324. this.btnNo = false;
  325. this.modalMsg = res.data.msg;
  326. this.showConfirmButton = true;
  327. this.showModal = true;
  328. if (this.fromCart) {
  329. console.log(this.fromCart);
  330. this.confirmText = "返回购物车";
  331. } else {
  332. let type = "";
  333. if (this.shoppingCartList.length == 1) {
  334. type = this.shoppingCartList[0].goodsType;
  335. } else {
  336. type = this.shoppingCartList[0].goodsType;
  337. if (this.shoppingCartList.find((item) => item.goodsType != type)) {
  338. type = 3;
  339. }
  340. }
  341. this.confirmText =
  342. type == 1 || type == 6
  343. ? "继续选课"
  344. : type == 2
  345. ? "继续选题"
  346. : type == 3 || type == 8
  347. ? "继续选购"
  348. : "";
  349. }
  350. } else {
  351. this.hasPaying = false;
  352. this.btnNo = false;
  353. this.showConfirmButton = false;
  354. this.modalMsg = res.data.msg;
  355. this.showModal = true;
  356. }
  357. },
  358. h5_wxpay(data) {
  359. console.log("====调起支付状态", data);
  360. let self = this;
  361. function onBridgeReady() {
  362. // 加载框
  363. WeixinJSBridge.invoke(
  364. "getBrandWCPayRequest",
  365. {
  366. appId: data.appId, // 公众号名称,由商户传入
  367. timeStamp: data.timeStamp, // 时间戳,自1970年以来的秒数
  368. nonceStr: data.nonceStr, // 随机串
  369. package: data.package,
  370. signType: data.signType, // 微信签名方式:
  371. paySign: data.paySign, // 微信签名
  372. },
  373. function (res) {
  374. // 判断支付状态
  375. console.log("支付状态", res, "this:", this, self);
  376. if (res.err_msg === "get_brand_wcpay_request:ok") {
  377. self.$u.toast("支付成功");
  378. uni.hideLoading();
  379. self.btnNo = false;
  380. uni.redirectTo({
  381. url: `/pages2/order/confirm_success?sn=${data.orderSn}&isBk=${self.isBK}`,
  382. });
  383. } else if (res.err_msg === "get_brand_wcpay_request:cancel") {
  384. self.$u.toast("取消支付");
  385. } else {
  386. uni.hideLoading();
  387. self.btnNo = false;
  388. self.$u.toast("支付失败");
  389. }
  390. }
  391. );
  392. }
  393. if (typeof WeixinJSBridge === "undefined") {
  394. if (document.addEventListener) {
  395. document.addEventListener(
  396. "WeixinJSBridgeReady",
  397. onBridgeReady,
  398. false
  399. );
  400. } else if (document.attachEvent) {
  401. document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
  402. document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
  403. }
  404. } else {
  405. onBridgeReady();
  406. }
  407. },
  408. modalCancel() {
  409. this.showModal = false;
  410. },
  411. modalConfirm() {
  412. if (this.hasPaying) {
  413. uni.navigateTo({
  414. url: "/pages2/order/index?current=0",
  415. });
  416. } else {
  417. if (this.fromCart) {
  418. uni.navigateTo({
  419. url: "/pages4/shopping/shoppingCart",
  420. });
  421. } else {
  422. if (this.shoppingCartList[0].goodsType == 8) {
  423. uni.reLaunch({
  424. url: "/pages/information/index",
  425. });
  426. return;
  427. }
  428. uni.switchTab({
  429. url: "/pages/course/index",
  430. });
  431. }
  432. }
  433. },
  434. getOpenid(code) {
  435. let self = this;
  436. this.$api.wxOpenid({ code: code }).then((res) => {
  437. if (res.data.code == 200) {
  438. self.postOrder();
  439. }
  440. });
  441. },
  442. async pay() {
  443. if (!(await this.checkNeedInfo())) {
  444. return;
  445. }
  446. let self = this;
  447. this.btnNo = true;
  448. // #ifdef MP-WEIXIN
  449. uni.login({
  450. provider: "weixin",
  451. success: function (loginRes) {
  452. self.getOpenid(loginRes.code);
  453. },
  454. });
  455. // #endif
  456. // #ifdef H5
  457. // 在首页已经请求过接口/gzh_login 提交code了
  458. this.postOrder();
  459. // #endif
  460. },
  461. radioChange(e) {
  462. // console.log(e);
  463. },
  464. // 选中任一radio时,由radio-group触发
  465. radioGroupChange(e) {
  466. // console.log(e);
  467. },
  468. cMenu(index) {
  469. this.menuIndex = index;
  470. },
  471. change(index) {
  472. this.current = index;
  473. },
  474. },
  475. onReachBottom() {},
  476. computed: {
  477. ...mapGetters(["userInfo", "shoppingCartList", "sac", "config"]),
  478. totalPrice() {
  479. return this.shoppingCartList.reduce(
  480. (a, b) => a + Number(b.standPrice),
  481. 0
  482. );
  483. },
  484. },
  485. components: { ClassTimeTip },
  486. };
  487. </script>
  488. <style>
  489. ::-webkit-scrollbar {
  490. width: 0;
  491. height: 0;
  492. color: transparent;
  493. }
  494. page {
  495. background-color: #eaeef1;
  496. }
  497. </style>
  498. <style lang="scss" scoped>
  499. .txt_left_pay {
  500. display: flex;
  501. align-items: center;
  502. height: 64rpx;
  503. font-size: 24rpx;
  504. color: #666666;
  505. }
  506. .pay_icon {
  507. width: 64rpx;
  508. height: 64rpx;
  509. margin-right: 10rpx;
  510. }
  511. .box3 {
  512. width: 100%;
  513. height: 886rpx;
  514. background: #ffffff;
  515. // border-radius: 16rpx;
  516. margin-top: 16rpx;
  517. padding: 32rpx 32rpx 0rpx 32rpx;
  518. }
  519. .tip {
  520. font-size: 24rpx;
  521. color: #999999;
  522. height: 40rpx;
  523. line-height: 40rpx;
  524. }
  525. .txt_right_sn {
  526. font-size: 30rpx;
  527. font-weight: bold;
  528. color: #666666;
  529. }
  530. .txt_right_price {
  531. font-size: 32rpx;
  532. font-weight: bold;
  533. color: #ff2d55;
  534. }
  535. .txt_left {
  536. font-size: 28rpx;
  537. color: #666666;
  538. }
  539. .txt_right {
  540. text-align: right;
  541. color: #333333;
  542. font-weight: bold;
  543. width: 75%;
  544. }
  545. .list_title {
  546. display: flex;
  547. justify-content: space-between;
  548. }
  549. .list_item {
  550. display: flex;
  551. justify-content: space-between;
  552. height: 110rpx;
  553. align-items: center;
  554. }
  555. .title2 {
  556. font-size: 32rpx;
  557. font-weight: bold;
  558. color: #333333;
  559. // height: 80rpx;
  560. // line-height: 80rpx;
  561. // margin-left: 30rpx;
  562. margin-bottom: 32rpx;
  563. }
  564. .box2 {
  565. width: 100%;
  566. background: #ffffff;
  567. // border-radius: 16rpx;
  568. padding: 32rpx 32rpx 0rpx 32rpx;
  569. .right_con {
  570. width: 450rpx;
  571. }
  572. .priceTag {
  573. font-size: 28rpx;
  574. font-family: PingFang SC;
  575. font-weight: bold;
  576. color: #fc3f3f;
  577. margin-top: 6rpx;
  578. margin-bottom: 10rpx;
  579. }
  580. .info_right {
  581. height: 110rpx;
  582. line-height: 110rpx;
  583. border-top: 1rpx solid #f2f2f2;
  584. }
  585. }
  586. .box1_t2 {
  587. font-size: 24rpx;
  588. color: #999999;
  589. text-align: center;
  590. }
  591. .box1_t1 {
  592. font-size: 30rpx;
  593. font-weight: bold;
  594. color: #333333;
  595. height: 70rpx;
  596. display: flex;
  597. align-items: center;
  598. justify-content: center;
  599. }
  600. .box1 {
  601. width: 100%;
  602. height: 120rpx;
  603. background: #ffffff;
  604. border-radius: 16rpx;
  605. }
  606. .bottomBtn {
  607. position: fixed;
  608. bottom: 0;
  609. width: 100%;
  610. height: 98rpx;
  611. background: linear-gradient(0deg, #015eea, #00c0fa);
  612. color: #ffffff;
  613. text-align: center;
  614. line-height: 98rpx;
  615. font-weight: bold;
  616. font-size: 30rpx;
  617. border-radius: 0;
  618. }
  619. .bottomBox {
  620. position: fixed;
  621. bottom: 0;
  622. width: 100%;
  623. left: 0;
  624. height: 132rpx;
  625. background-color: #ffffff;
  626. display: flex;
  627. justify-content: space-between;
  628. align-items: center;
  629. box-sizing: unset;
  630. box-shadow: 0px -2px 6px 0px rgba(0, 0, 0, 0.1);
  631. > view {
  632. margin: 0 30rpx;
  633. }
  634. .sums {
  635. display: flex;
  636. }
  637. .all_sum {
  638. font-size: 28rpx;
  639. font-weight: 500;
  640. color: #303030;
  641. }
  642. .priceTag {
  643. font-size: 32rpx;
  644. font-weight: 800;
  645. color: #fc3f3f;
  646. }
  647. .btn2 {
  648. width: 232rpx;
  649. height: 92rpx;
  650. background: #fc3f3f;
  651. border-radius: 120rpx;
  652. text-align: center;
  653. line-height: 92rpx;
  654. color: #fff;
  655. }
  656. }
  657. </style>