category.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <view class="content">
  3. <view
  4. class="dddd"
  5. style="height: 10rpx"></view>
  6. <view
  7. class="flex cate-wrap"
  8. :style="{ height: height }">
  9. <scroll-view
  10. scroll-y="true"
  11. class="left-wrap"
  12. :style="{ height: height }">
  13. <view
  14. class="left-item"
  15. v-for="(leftitem, leftindex) in flist"
  16. :class="{ 'action-item': currentIndex == leftindex }"
  17. @click="leftClick(leftindex)">
  18. {{ leftitem.cate_name }}
  19. </view>
  20. </scroll-view>
  21. <scroll-view
  22. scroll-y="true"
  23. class="right-wrap"
  24. :style="{ height: height }"
  25. @scrolltolower="getProducts()">
  26. <view
  27. class="main"
  28. v-for="gooditem in list"
  29. @click="navto('/pages/index/product?id=' + gooditem.id)">
  30. <view class="img">
  31. <image
  32. :src="gooditem.image"
  33. mode=""></image>
  34. </view>
  35. <view class="main1">
  36. <view
  37. class="wenzi"
  38. @click="navToList(item.id, titem.id)">
  39. <text style="margin-bottom: 5rpx">{{
  40. gooditem.store_name
  41. }}</text>
  42. </view>
  43. <view class="wenzi2">
  44. <view class="">
  45. <text>¥{{ gooditem.price }}</text>
  46. </view>
  47. <!-- <view class="image">
  48. <image
  49. src="../../static/icon/cart (2).png"
  50. style="width: 44rpx; height: 44rpx"
  51. mode=""></image>
  52. </view> -->
  53. </view>
  54. </view>
  55. </view>
  56. </scroll-view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import { getProducts } from "@/api/product.js";
  62. import { getList } from "@/api/category.js";
  63. export default {
  64. data() {
  65. return {
  66. sizeCalcState: false,
  67. tabScrollTop: 0,
  68. currentId: 9,
  69. flist: [],
  70. height: "",
  71. currentIndex: 0,
  72. list: [],
  73. page: 1,
  74. limit: 20,
  75. loadingType: "more",
  76. loaded: false,
  77. };
  78. },
  79. onLoad() {
  80. this.loadData();
  81. },
  82. // 监听导航栏输入框点击事件
  83. onNavigationBarSearchInputClicked(e) {
  84. uni.navigateTo({
  85. url: "/pages/product/search",
  86. });
  87. },
  88. onReady(res) {
  89. var _this = this;
  90. uni.getSystemInfo({
  91. success: resu => {
  92. const query = uni.createSelectorQuery();
  93. query.select(".cate-wrap").boundingClientRect();
  94. query.exec(function (res) {
  95. _this.height = resu.windowHeight - res[0].top + "px";
  96. console.log("打印页面的剩余高度", _this.height);
  97. });
  98. },
  99. fail: res => {},
  100. });
  101. },
  102. methods: {
  103. leftClick(index) {
  104. this.currentIndex = index;
  105. this.getProducts("reload");
  106. },
  107. getProducts(type) {
  108. let index = this.flist[this.currentIndex].id;
  109. if (type == "reload") {
  110. this.list = [];
  111. this.page = 1;
  112. this.loadingType = "more";
  113. this.loaded = false;
  114. }
  115. if (
  116. this.loadingType == "loading" ||
  117. this.loadingType == "noMore"
  118. ) {
  119. return;
  120. }
  121. this.loadingType = "loading";
  122. getProducts({
  123. cid: index,
  124. }).then(res => {
  125. this.list = this.list.concat(res.data);
  126. if (this.limit == res.data.length) {
  127. this.loadingType = "more";
  128. } else {
  129. this.loadingType = "noMore";
  130. }
  131. this.loaded = true;
  132. });
  133. },
  134. // 载入数据
  135. async loadData() {
  136. let obj = this;
  137. getList({})
  138. .then(({ data }) => {
  139. obj.flist = data.map(function (s) {
  140. return s;
  141. });
  142. obj.getProducts();
  143. })
  144. .catch(err => {
  145. console.log(err);
  146. });
  147. },
  148. //一级分类点击
  149. tabtap(item) {
  150. console.log(item);
  151. // 判断有没有初始化页面高度对象数据
  152. if (!this.sizeCalcState) {
  153. this.calcSize();
  154. }
  155. // 获取当前点击的id
  156. this.currentId = item.id;
  157. console.log(item.top);
  158. this.tabScrollTop = item.top;
  159. console.log(this.tabScrollTop);
  160. },
  161. //右侧栏滚动
  162. asideScroll(e) {
  163. // 判断有没有初始化页面高度对象数据
  164. if (!this.sizeCalcState) {
  165. this.calcSize();
  166. }
  167. let scrollTop = e.detail.scrollTop;
  168. let box = 0; //列表包裹框高度初始化
  169. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  170. // 查询当前页面对象
  171. let view = uni.createSelectorQuery().select(".content");
  172. view.fields(
  173. {
  174. id: true,
  175. dataset: true,
  176. rect: true,
  177. size: true,
  178. scrollOffset: true,
  179. },
  180. function (e) {
  181. // 保存包裹框高度
  182. box = e.height;
  183. }
  184. ).exec();
  185. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  186. let tabs = this.flist
  187. .filter(item => item.top - 10 <= scrollTop)
  188. .reverse();
  189. if (tabs.length > 0) {
  190. // 判断是否已经到达滚轮底部
  191. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  192. this.currentId = this.flist[this.flist.length - 1].id;
  193. } else {
  194. this.currentId = tabs[0].id;
  195. }
  196. }
  197. },
  198. //计算右侧栏每个tab的高度等信息
  199. calcSize() {
  200. let h = 0;
  201. this.flist.forEach(item => {
  202. let view = uni
  203. .createSelectorQuery()
  204. .select("#main-" + item.id);
  205. view.fields(
  206. {
  207. size: true,
  208. },
  209. data => {
  210. item.top = h;
  211. h += data.height;
  212. item.bottom = h;
  213. }
  214. ).exec();
  215. });
  216. this.sizeCalcState = true;
  217. },
  218. navToList(sid, tid) {
  219. // 点击导航跳转到详细页面
  220. uni.navigateTo({
  221. url:
  222. "/pages/product/list?fid=" +
  223. this.currentId +
  224. "&sid=" +
  225. sid +
  226. "&tid=" +
  227. tid,
  228. });
  229. },
  230. navto(url) {
  231. uni.navigateTo({
  232. url,
  233. });
  234. },
  235. },
  236. };
  237. </script>
  238. <style lang="scss">
  239. .cate-wrap {
  240. width: 750rpx;
  241. .left-wrap {
  242. width: 180rpx;
  243. flex-shrink: 0;
  244. background-color: #f2f2f2;
  245. .left-item {
  246. width: 180rpx;
  247. height: 100rpx;
  248. line-height: 100rpx;
  249. font-size: 28rpx;
  250. font-weight: 500;
  251. color: #666666;
  252. text-align: center;
  253. }
  254. .action-item {
  255. background-color: #fff;
  256. font-weight: bold;
  257. position: relative;
  258. &::before {
  259. content: "";
  260. position: absolute;
  261. top: 0;
  262. left: 0;
  263. width: 3rpx;
  264. height: 100rpx;
  265. background: #ff7144;
  266. }
  267. }
  268. }
  269. .right-wrap {
  270. flex-wrap: 1;
  271. background-color: #fff;
  272. .right-item {
  273. width: 520rpx;
  274. height: 240rpx;
  275. padding: 30rpx 0;
  276. margin: 0 auto;
  277. border-bottom: 1px solid #eee;
  278. display: flex;
  279. justify-content: flex-start;
  280. align-items: center;
  281. .good-img {
  282. width: 180rpx;
  283. height: 180rpx;
  284. border-radius: 20rpx;
  285. }
  286. .good-info {
  287. flex-direction: column;
  288. justify-content: space-between;
  289. align-items: flex-start;
  290. height: 100%;
  291. padding-left: 25rpx;
  292. .good-name {
  293. font-size: 30rpx;
  294. font-weight: bold;
  295. color: #333333;
  296. }
  297. .good-price {
  298. font-size: 34rpx;
  299. font-weight: bold;
  300. color: #ff4c4c;
  301. }
  302. }
  303. }
  304. .right {
  305. display: flex;
  306. justify-content: flex-end;
  307. align-items: center;
  308. margin-left: 25rpx;
  309. }
  310. .image {
  311. margin-left: 30rpx;
  312. }
  313. }
  314. // 右侧商品样式重构
  315. .main {
  316. display: flex;
  317. // align-items: center;
  318. width: 750rpx;
  319. // height: 100vh;
  320. overflow: hidden;
  321. background-color: #fff;
  322. }
  323. .img image {
  324. // margin-right: 10rpx;
  325. margin-top: 15rpx;
  326. margin-left: 20rpx;
  327. width: 180rpx;
  328. height: 180rpx;
  329. object-fit: cover;
  330. }
  331. .main1 {
  332. display: flex;
  333. width: 320rpx;
  334. padding-left: 20rpx;
  335. box-sizing: border-box;
  336. flex-direction: column;
  337. justify-content: space-around;
  338. background-color: #fff;
  339. .wenzi {
  340. width: 269rpx;
  341. height: 29rpx;
  342. font-size: 30rpx;
  343. font-family: PingFang SC;
  344. font-weight: bold;
  345. color: #333333;
  346. display: inline;
  347. line-height: 35rpx;
  348. .wenzi1 {
  349. width: 181rpx;
  350. height: 25rpx;
  351. font-size: 26rpx;
  352. font-family: PingFang SC;
  353. font-weight: 500;
  354. color: #666;
  355. display: inline;
  356. }
  357. }
  358. .wenzi2 {
  359. width: 100%;
  360. height: 26rpx;
  361. font-size: 34rpx;
  362. font-family: PingFang SC;
  363. font-weight: bold;
  364. color: #ff4c4c;
  365. line-height: 35rpx;
  366. display: flex;
  367. justify-content: space-between;
  368. }
  369. }
  370. .main1 .wenzi text {
  371. margin-bottom: 20rpx;
  372. }
  373. }
  374. </style>