index.vue 18 KB

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