index.vue 18 KB

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