index.vue 18 KB

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