index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <template>
  2. <view
  3. class="container"
  4. :style="{ background: containerBg }"
  5. :class="bookList.length ? 'book-list' : 'no-list'"
  6. >
  7. <u-navbar
  8. :is-back="false"
  9. :border-bottom="false"
  10. :background="{ background: navbarBackground }"
  11. >
  12. <text class="nav-title">卖书给书嗨</text>
  13. </u-navbar>
  14. <not-scanned v-if="!bookList.length"></not-scanned>
  15. <scan-book-list
  16. v-else
  17. @updateBooks="updateBooksList"
  18. @deleted="getLastOrder"
  19. :bookList="bookList"
  20. ref="scanBookList"
  21. @upsell="handleUpsell"
  22. ></scan-book-list>
  23. <!-- 底部固定按钮 -->
  24. <view class="bottom-fixed">
  25. <view class="btn-wrap mb-20">
  26. <button class="scan-btn flex-1" @click="handleScan">
  27. <u-icon name="scan" color="#FFFFFF" size="40"></u-icon>
  28. <text>扫码卖书</text>
  29. </button>
  30. <button class="isbn-btn flex-1" @click="goToInputISBN">
  31. <u-icon name="edit-pen" color="#4CD964" size="40"></u-icon>
  32. <text>输入ISBN</text>
  33. </button>
  34. </view>
  35. <view
  36. class="flex-a flex-j-b pad-20"
  37. style="padding-top: 0"
  38. v-if="bookList.length"
  39. >
  40. <view class="left-info" style="min-width: 194px">
  41. <view class="flex-a common-text">
  42. 共<text class="color-red">{{ totalBooks }}</text
  43. >件 预估回收价 <text class="color-red">¥{{ totalPrice || 0 }}</text>
  44. </view>
  45. <text class="common-text tip"
  46. >*旧书预估价格满 {{ orderInfo.minOrderMoney }} 元起收</text
  47. >
  48. </view>
  49. <button
  50. class="scan-btn next-btn"
  51. @click="onNext"
  52. :disabled="isDisabled"
  53. >
  54. 下一步
  55. </button>
  56. </view>
  57. <service-info
  58. v-if="bookList.length"
  59. :serviceList="serviceList"
  60. :firstOrder="orderInfo.firstOrder"
  61. ></service-info>
  62. </view>
  63. <InputIsbn ref="isbnPopup" @submit="checkBookISBN" />
  64. <!-- 套装书说明弹窗 -->
  65. <CommonDialog
  66. ref="setBookDialog"
  67. title="套装书说明"
  68. :showCancel="false"
  69. @confirm="handleSetBookConfirm"
  70. >
  71. <text
  72. >套装书(ISBN码相同的系列书箱)只需扫描其中一册,扫码价即套装价。打包时请把所有单册统在一起或放在一个袋子里寄出。</text
  73. >
  74. </CommonDialog>
  75. <!-- 暂不回收弹窗 -->
  76. <CommonDialog ref="notAcceptDialog" title="暂不回收" :showCancel="false">
  77. <text>这本书暂时不回收,请您过段时间再来试试~</text>
  78. </CommonDialog>
  79. <!-- 暂无信息弹窗 -->
  80. <CommonDialog ref="noInfoDialog" title="暂无信息" :showCancel="false">
  81. <text
  82. >抱歉,没有该书的信息,书嗨会定期补充图书信息,请您过段时间再来试试~</text
  83. >
  84. </CommonDialog>
  85. <!-- 扫累了弹窗 -->
  86. <CommonDialog ref="tiredDialog" title="温馨提示" :showCancel="false">
  87. <text>扫累了,休息休息吧~</text>
  88. </CommonDialog>
  89. <!-- 该书超出最大回收本数 maxAcceptDialog-->
  90. <CommonDialog ref="maxAcceptDialog" title="温馨提示" :showCancel="false">
  91. <text>该书超出最大回收本数</text>
  92. </CommonDialog>
  93. <!-- 单个订单最多40本书 orderMaxNumDialog-->
  94. <CommonDialog ref="orderMaxNumDialog" title="温馨提示" :showCancel="false">
  95. <text>单个订单最多40本书</text>
  96. </CommonDialog>
  97. <!-- 温馨提示弹窗 -->
  98. <KindReminder
  99. ref="kindReminder"
  100. @start="handleStartSelling"
  101. @viewRules="handleViewRules"
  102. />
  103. <view
  104. class="customer-service"
  105. :style="{
  106. left: servicePosition.left + 'px',
  107. right: servicePosition.right + 'px',
  108. bottom: servicePosition.bottom + 'px',
  109. }"
  110. @touchstart="touchStart"
  111. @touchmove="touchMove"
  112. @touchend="touchEnd"
  113. >
  114. <button class="service-btn" open-type="contact">
  115. <image
  116. src="/static/img/kf.png"
  117. mode="widthFix"
  118. style="width: 126rpx; height: 140rpx"
  119. ></image>
  120. </button>
  121. </view>
  122. <ConfirmBooks ref="confirmBooks" @incomplete="handleIncomplete" />
  123. <!-- 首单免费弹窗 -->
  124. <FirstOrderFreePopup ref="firstOrderFreePopup" />
  125. <!-- 图书加价弹窗 -->
  126. <UpsellBook ref="upsellRef" />
  127. <!-- 加价规则弹窗 -->
  128. <UpsellRules ref="upsellRulesRef" />
  129. <!-- 加价分享弹窗 -->
  130. <UpsellShare ref="shareRef" @viewRules="handleViewSellRules" />
  131. </view>
  132. </template>
  133. <script>
  134. import notScanned from "./components/notScanned.vue";
  135. import InputIsbn from "./components/InputIsbn.vue";
  136. import ScanBookList from "./components/ScanBookList.vue";
  137. import CommonDialog from "@/components/common-dialog.vue";
  138. import KindReminder from "./components/KindReminder.vue";
  139. import ServiceInfo from "./components/ServiceInfo.vue";
  140. import ConfirmBooks from "./components/ConfirmBooks.vue";
  141. import FirstOrderFreePopup from "./components/FirstOrderFreePopup.vue";
  142. import UpsellBook from "./components/upsell-book.vue";
  143. import UpsellRules from "./components/upsell-rules.vue";
  144. import UpsellShare from "./components/upsell-share.vue";
  145. export default {
  146. components: {
  147. notScanned,
  148. InputIsbn,
  149. ScanBookList,
  150. CommonDialog,
  151. KindReminder,
  152. ServiceInfo,
  153. ConfirmBooks,
  154. FirstOrderFreePopup,
  155. UpsellBook,
  156. UpsellRules,
  157. UpsellShare,
  158. },
  159. data() {
  160. return {
  161. orderInfo: {},
  162. collapseState: {
  163. step1: false,
  164. step3: false,
  165. },
  166. scrollTop: 0,
  167. bookList: [],
  168. serviceList: [],
  169. currentBook: {},
  170. // 客服按钮位置
  171. servicePosition: {
  172. left: "auto",
  173. right: 0,
  174. bottom: "20%",
  175. },
  176. // 触摸开始位置
  177. startX: 0,
  178. startY: 0,
  179. // 屏幕宽度和高度
  180. screenWidth: 0,
  181. screenHeight: 0,
  182. // 初始位置记录,用于计算拖动
  183. initialLeft: 0,
  184. initialBottom: 0,
  185. // 是否正在更新位置,用于防止频繁更新
  186. isUpdatingPosition: false,
  187. };
  188. },
  189. computed: {
  190. navbarBackground() {
  191. if (this.scrollTop > 0) {
  192. return "linear-gradient(180deg, #4CD964 0%, #5ff178 100%)";
  193. }
  194. return "transparent";
  195. },
  196. containerBg() {
  197. return this.bookList.length > 0
  198. ? "linear-gradient(180deg, #4CD964 0%, #F8F8F8 25%)"
  199. : "linear-gradient(180deg, #4CD964 0%, #ffffff 25%)";
  200. },
  201. containerPb() {
  202. return this.bookList.length > 0 ? "300rpx" : "110rpx";
  203. },
  204. totalBooks() {
  205. return this.bookList.reduce((sum, book) => sum + (book.num || 1), 0);
  206. },
  207. totalPrice() {
  208. return this.bookList
  209. .reduce((sum, book) => sum + book.recyclePrice * (book.num || 1), 0)
  210. .toFixed(2);
  211. },
  212. isDisabled() {
  213. return this.totalPrice < this.orderInfo.minOrderMoney;
  214. },
  215. },
  216. onPageScroll(e) {
  217. this.$nextTick(() => {
  218. this.scrollTop = e.scrollTop;
  219. });
  220. },
  221. // 分享配置
  222. onShareAppMessage() {
  223. let userInfo = uni.getStorageSync("userInfo");
  224. let userId = userInfo.userId;
  225. return {
  226. title: "书嗨",
  227. path: "/pages/home/index?inviteCode=" + userId,
  228. imageUrl: "/static/img/activity/share.jpg",
  229. };
  230. },
  231. // 分享到朋友圈
  232. onShareTimeline() {
  233. let userInfo = uni.getStorageSync("userInfo");
  234. let userId = userInfo.userId;
  235. return {
  236. title: "书嗨",
  237. path: "/pages/home/index?inviteCode=" + userId,
  238. imageUrl: "/static/img/activity/share.jpg",
  239. };
  240. },
  241. onReady() {
  242. // 获取屏幕宽度和高度
  243. uni.getSystemInfo({
  244. success: (res) => {
  245. this.screenWidth = res.windowWidth;
  246. this.screenHeight = res.windowHeight;
  247. },
  248. });
  249. this.$refs.shareRef.open();
  250. },
  251. methods: {
  252. slientLogin() {
  253. let inviteCode = wx.getStorageSync("inviteCode") || "";
  254. let token = wx.getStorageSync("token");
  255. if (token) return;
  256. uni.login({
  257. success(res) {
  258. uni.$u.http
  259. .post("/user/wxLogin", {
  260. code: res.code,
  261. inviteCode,
  262. })
  263. .then((response) => {
  264. if (response.code == 200) {
  265. uni.setStorageSync("token", response.data.token);
  266. }
  267. });
  268. },
  269. fail: (err) => {
  270. console.log(err, "wx.login登录失败");
  271. },
  272. });
  273. },
  274. //查看活动规则
  275. handleViewSellRules() {
  276. this.$refs.upsellRulesRef.open();
  277. },
  278. // 触摸开始
  279. touchStart(e) {
  280. const touch = e.touches[0];
  281. this.startX = touch.clientX;
  282. this.startY = touch.clientY;
  283. // 记录初始位置,用于计算移动距离
  284. if (this.servicePosition.right !== "auto") {
  285. // 如果是靠右定位,记录当前位置但不立即改变显示位置
  286. // 只在内部计算中使用,避免视觉上的位置跳动
  287. this.initialLeft = this.screenWidth - 126;
  288. } else {
  289. this.initialLeft = parseFloat(this.servicePosition.left);
  290. }
  291. // 如果bottom是百分比,转换为具体像素值,但不改变显示位置
  292. if (
  293. typeof this.servicePosition.bottom === "string" &&
  294. this.servicePosition.bottom.includes("%")
  295. ) {
  296. const percentage = parseFloat(this.servicePosition.bottom) / 100;
  297. this.initialBottom = this.screenHeight * percentage;
  298. } else {
  299. this.initialBottom = parseFloat(this.servicePosition.bottom);
  300. }
  301. },
  302. // 触摸移动
  303. touchMove(e) {
  304. // 阻止默认行为,防止页面滚动
  305. e.preventDefault && e.preventDefault();
  306. e.stopPropagation && e.stopPropagation();
  307. const touch = e.touches[0];
  308. // 计算移动距离
  309. const deltaX = touch.clientX - this.startX;
  310. const deltaY = touch.clientY - this.startY;
  311. // 使用初始位置计算新位置,避免累积误差
  312. let newLeft = this.initialLeft + deltaX;
  313. let newBottom = this.initialBottom - deltaY; // 注意:y轴方向是相反的
  314. // 确保按钮不超出屏幕边界
  315. if (newLeft < 0) {
  316. newLeft = 0;
  317. } else if (newLeft > this.screenWidth - 126) {
  318. newLeft = this.screenWidth - 126;
  319. }
  320. // 确保按钮不超出屏幕垂直边界
  321. if (newBottom < 20) {
  322. newBottom = 20;
  323. } else if (newBottom > this.screenHeight - 160) {
  324. newBottom = this.screenHeight - 160;
  325. }
  326. // 使用节流方式更新位置,避免过于频繁的更新
  327. if (!this.isUpdatingPosition) {
  328. this.isUpdatingPosition = true;
  329. // 更新位置 - 第一次移动时才真正改变right为auto
  330. this.servicePosition = {
  331. left: newLeft,
  332. right: "auto",
  333. bottom: newBottom,
  334. };
  335. // 使用setTimeout代替requestAnimationFrame,在微信小程序中更兼容
  336. setTimeout(() => {
  337. this.isUpdatingPosition = false;
  338. }, 16); // 约等于60fps的刷新率
  339. }
  340. // 不更新起始点,保持相对于初始触摸点的位移计算
  341. // 这样可以避免累积误差,使拖动更精确
  342. },
  343. // 触摸结束,实现吸附效果
  344. touchEnd() {
  345. // 确保不再有待处理的更新
  346. this.isUpdatingPosition = false;
  347. const buttonCenter = this.servicePosition.left + 63; // 按钮中心位置
  348. const halfScreen = this.screenWidth / 2;
  349. // 判断是吸附到左边还是右边
  350. if (buttonCenter < halfScreen) {
  351. // 吸附到左边
  352. this.servicePosition = {
  353. left: 0,
  354. right: "auto",
  355. bottom: this.servicePosition.bottom,
  356. };
  357. } else {
  358. // 吸附到右边
  359. this.servicePosition = {
  360. left: "auto",
  361. right: 0,
  362. bottom: this.servicePosition.bottom,
  363. };
  364. }
  365. },
  366. handleStart() {
  367. this.showPopup = true;
  368. },
  369. //套装书确认
  370. handleSetBookConfirm() {
  371. this.$refs.confirmBooks.openPopup(this.currentBook);
  372. },
  373. //书册补全
  374. handleIncomplete() {
  375. this.$refs.scanBookList.handleDeleteBook(this.currentBook);
  376. },
  377. //提交
  378. onNext() {
  379. let orderId = this.orderInfo.orderId;
  380. //预提交
  381. uni.$u.http
  382. .get("/token/order/preSubmit?orderId=" + orderId)
  383. .then((res) => {
  384. if (res.code == 200) {
  385. if (res.data.code == 1 || res.data.code == 2) {
  386. uni.navigateTo({
  387. url: "/pages-home/pages/book-order",
  388. });
  389. uni.setStorageSync("orderId", orderId);
  390. } else {
  391. uni.showToast({
  392. icon: "none",
  393. title: res.msg,
  394. });
  395. }
  396. } else {
  397. uni.showToast({
  398. icon: "none",
  399. title: res.msg,
  400. });
  401. }
  402. });
  403. },
  404. // 加价
  405. handleUpsell(book) {
  406. this.$refs.upsellRef.open(book);
  407. },
  408. updateBooksList(data) {
  409. this.bookList = data;
  410. },
  411. toggleCollapse(step) {
  412. this.$set(this.collapseState, step, !this.collapseState[step]);
  413. },
  414. handleScan() {
  415. uni.scanCode({
  416. scanType: ["barCode"],
  417. success: (res) => {
  418. this.checkBookISBN(res.result);
  419. },
  420. fail: () => {
  421. uni.showToast({
  422. title: "扫码失败",
  423. icon: "none",
  424. });
  425. },
  426. });
  427. },
  428. checkBookISBN(isbn) {
  429. uni.$u.http.get("/token/order/scanIsbn?isbn=" + isbn).then((res) => {
  430. if (res.code == 200) {
  431. let code = res.data.code;
  432. if (code == 1) {
  433. res.data.num = 1;
  434. res.data.status = 1;
  435. res.data.recyclePrice = res.data.recycleMoney;
  436. this.currentBook = res.data;
  437. this.bookList.unshift(res.data);
  438. if (res.data.suit == 1) {
  439. this.$refs.setBookDialog.openPopup();
  440. }
  441. if (res.data.canInvite == 1) {
  442. this.$refs.upsellRef.open(res.data);
  443. }
  444. } else if (code == 2) {
  445. let item = this.bookList.find((v) => v.isbn === isbn);
  446. item.num = item.num + 1;
  447. } else {
  448. this.handleBookCode(res.data.code);
  449. }
  450. } else {
  451. uni.showToast({
  452. title: res.msg,
  453. icon: "none",
  454. });
  455. }
  456. });
  457. },
  458. //处理扫码之后不同的状态 0-扫码频繁 1-成功 2-本单已有该书,数量+1 3-没有该书 4-本书暂不回收 5-超过每单最大可卖数量 6-单个订单最多40本书
  459. handleBookCode(code) {
  460. if (code == 1) {
  461. this.bookList.push();
  462. }
  463. let tempKeys = [
  464. "tiredDialog",
  465. "",
  466. "",
  467. "noInfoDialog",
  468. "notAcceptDialog",
  469. "maxAcceptDialog",
  470. "orderMaxNumDialog",
  471. ];
  472. let key = tempKeys[code];
  473. if (key) {
  474. this.$refs[key].openPopup();
  475. }
  476. },
  477. //获取当前用户未提交订单 /api/token/order/lastOrder
  478. getLastOrder() {
  479. uni.$u.http.get("/token/order/lastOrder").then((res) => {
  480. if (res.code == 200) {
  481. this.orderInfo = res.data;
  482. if (res.data.showDialog == 1) {
  483. this.$refs.firstOrderFreePopup.openPopup();
  484. } else if (res.data.showDialog == 2) {
  485. this.$refs.kindReminder.openPopup();
  486. }
  487. this.bookList = res.data?.orderDetailList
  488. ? res.data.orderDetailList.map((v) => {
  489. v.orderId = res.data.orderId;
  490. return v;
  491. })
  492. : [];
  493. this.serviceList = res.data.serviceList || [];
  494. }
  495. });
  496. },
  497. goToScannedBooks() {
  498. uni.navigateTo({
  499. url: "/pages-home/pages/scaned-book",
  500. });
  501. },
  502. goToInputISBN() {
  503. this.$refs.isbnPopup.openPopup();
  504. },
  505. handleStartSelling() {
  506. // 标记已显示过温馨提示
  507. uni.setStorageSync("kindReminderShown", true);
  508. },
  509. },
  510. onShow() {
  511. // 获取上一个订单
  512. setTimeout(() => {
  513. this.getLastOrder();
  514. setTimeout(() => {
  515. this.slientLogin();
  516. }, 10);
  517. }, 300);
  518. },
  519. };
  520. </script>
  521. <style lang="scss" scoped>
  522. .customer-service {
  523. position: fixed;
  524. width: 126rpx;
  525. height: 140rpx;
  526. bottom: 20%;
  527. z-index: 999;
  528. transition: all 0.3s ease;
  529. /* 添加过渡效果使吸附更平滑 */
  530. button {
  531. height: max-content;
  532. background-color: transparent;
  533. padding: 0;
  534. }
  535. }
  536. .container {
  537. height: 100%;
  538. position: relative;
  539. overflow: auto;
  540. z-index: 1;
  541. /* #ifdef MP-WEIXIN */
  542. min-height: 100vh;
  543. /* #endif */
  544. /* #ifndef MP-WEIXIN */
  545. min-height: calc(100vh - 120rpx);
  546. /* #endif */
  547. &.book-list {
  548. padding-bottom: 300rpx;
  549. }
  550. padding-bottom: 130rpx;
  551. .nav-title {
  552. font-family: PingFang SC;
  553. font-weight: bold;
  554. font-size: 34rpx;
  555. color: #ffffff;
  556. padding-left: 40rpx;
  557. }
  558. }
  559. .common-text {
  560. font-family: PingFang SC;
  561. font-weight: 500;
  562. font-size: 28rpx;
  563. color: #999999;
  564. &.tip {
  565. color: #ff8a4b;
  566. }
  567. }
  568. .color-red {
  569. color: #ff0000;
  570. margin: 0 10rpx;
  571. }
  572. .bottom-fixed {
  573. position: fixed;
  574. left: 0;
  575. right: 0;
  576. bottom: 0;
  577. z-index: 9;
  578. background-color: #ffffff;
  579. /* #ifdef H5 */
  580. padding-bottom: 120rpx;
  581. /* #endif */
  582. .btn-wrap {
  583. display: flex;
  584. gap: 20rpx;
  585. padding: 20rpx;
  586. padding-bottom: 0;
  587. button {
  588. flex: 1;
  589. height: 88rpx;
  590. border-radius: 10rpx;
  591. display: flex;
  592. align-items: center;
  593. justify-content: center;
  594. gap: 10rpx;
  595. border: none;
  596. text {
  597. font-size: 32rpx;
  598. }
  599. &::after {
  600. border: none;
  601. }
  602. }
  603. .isbn-btn {
  604. background-color: #ffffff;
  605. color: #4cd964;
  606. border: 3rpx solid #4cd964;
  607. }
  608. }
  609. .scan-btn {
  610. background-color: #4cd964;
  611. color: #ffffff;
  612. }
  613. .next-btn {
  614. margin: 0;
  615. margin-left: 20rpx;
  616. &[aria-disabled="true"] {
  617. background-color: #cccccc;
  618. color: #ffffff;
  619. opacity: 0.7;
  620. cursor: not-allowed;
  621. }
  622. }
  623. }
  624. </style>