index.vue 18 KB

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