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