index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索商品">
  5. </view>
  6. <view class="two">
  7. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  8. <view class="list-scroll-view">
  9. <view class="two_1">
  10. <view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
  11. <image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''"
  12. mode="aspectFill">
  13. </image>
  14. <view class="name textOver">{{item.name||'暂无'}}</view>
  15. <view class="other">
  16. <view class="left">¥{{item.money||'暂无'}}</view>
  17. <view class="right">销量:{{item.sell_num||'0'}}</view>
  18. </view>
  19. </view>
  20. <view class="is_bottom" v-if="is_bottom">
  21. <text>{{config.bottom_title}}</text>
  22. </view>
  23. </view>
  24. </view>
  25. </scroll-view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. // 系统设置
  34. config: {},
  35. user: {},
  36. type: '',
  37. searchInfo: {},
  38. list: [],
  39. total: 0,
  40. skip: 0,
  41. limit: 6,
  42. page: 0,
  43. // 数据是否触底
  44. is_bottom: false,
  45. scrollTop: 0,
  46. }
  47. },
  48. onLoad: function(e) {
  49. const that = this;
  50. that.$set(that, `type`, e.type || '');
  51. that.searchToken();
  52. that.searchConfig();
  53. },
  54. onPullDownRefresh: async function() {
  55. const that = this;
  56. that.clearPage();
  57. await that.search();
  58. uni.stopPullDownRefresh();
  59. },
  60. methods: {
  61. searchConfig() {
  62. const that = this;
  63. try {
  64. const res = uni.getStorageSync('config');
  65. if (res) that.$set(that, `config`, res);
  66. } catch (e) {
  67. uni.showToast({
  68. title: err.errmsg,
  69. icon: 'error',
  70. duration: 2000
  71. });
  72. }
  73. },
  74. searchToken() {
  75. const that = this;
  76. try {
  77. const res = uni.getStorageSync('token');
  78. if (res) {
  79. that.$set(that, `user`, res);
  80. that.clearPage();
  81. that.search();
  82. }
  83. } catch (e) {
  84. uni.showToast({
  85. title: err.errmsg,
  86. icon: 'error',
  87. duration: 2000
  88. });
  89. }
  90. },
  91. async search() {
  92. const that = this;
  93. let user = that.user;
  94. let info = {
  95. skip: that.skip,
  96. limit: that.limit,
  97. type: that.type,
  98. is_use: '0'
  99. };
  100. let res;
  101. res = await that.$api(`/Good`, 'GET', {
  102. ...info,
  103. ...that.searchInfo
  104. });
  105. if (res.errcode == '0') {
  106. let list = [...that.list, ...res.data];
  107. for (let val of list) {
  108. const specs = await that.$api(`/Specs`, 'GET', {
  109. goods: val._id
  110. });
  111. if (specs.errcode == 0) {
  112. const arr = specs.data.sort((a, b) => {
  113. return a.money - b.money
  114. })
  115. val.money = arr[0].money
  116. }
  117. }
  118. that.$set(that, `list`, list);
  119. that.$set(that, `total`, res.total)
  120. } else {
  121. uni.showToast({
  122. title: res.errmsg,
  123. icon: 'none'
  124. })
  125. }
  126. },
  127. // 购买
  128. toBuy(e) {
  129. const that = this;
  130. uni.navigateTo({
  131. url: `/pagesGoods/index/index?id=${e.id||e._id}`
  132. })
  133. },
  134. // 分页
  135. toPage(e) {
  136. const that = this;
  137. let list = that.list;
  138. let limit = that.limit;
  139. if (that.total > list.length) {
  140. uni.showLoading({
  141. title: '加载中',
  142. mask: true
  143. })
  144. let page = that.page + 1;
  145. that.$set(that, `page`, page)
  146. let skip = page * limit;
  147. that.$set(that, `skip`, skip)
  148. that.search();
  149. uni.hideLoading();
  150. } else that.$set(that, `is_bottom`, true)
  151. },
  152. // 触底
  153. toScroll(e) {
  154. const that = this;
  155. let up = that.scrollTop;
  156. that.$set(that, `scrollTop`, e.detail.scrollTop);
  157. let num = Math.sign(up - e.detail.scrollTop);
  158. if (num == 1) that.$set(that, `is_bottom`, false);
  159. },
  160. // 输入框
  161. toInput(e) {
  162. const that = this;
  163. if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
  164. else that.$set(that, `searchInfo`, {})
  165. that.clearPage();
  166. that.search();
  167. },
  168. // 清空列表
  169. clearPage() {
  170. const that = this;
  171. that.$set(that, `list`, [])
  172. that.$set(that, `skip`, 0)
  173. that.$set(that, `limit`, 6)
  174. that.$set(that, `page`, 0)
  175. }
  176. }
  177. }
  178. </script>
  179. <style lang="scss">
  180. .content {
  181. display: flex;
  182. flex-direction: column;
  183. width: 100vw;
  184. height: 100vh;
  185. .one {
  186. padding: 2vw;
  187. input {
  188. padding: 2vw;
  189. background-color: var(--f1Color);
  190. font-size: var(--font14Size);
  191. border-radius: 5px;
  192. }
  193. }
  194. .two {
  195. position: relative;
  196. flex-grow: 1;
  197. background-color: var(--f9Color);
  198. .two_1 {
  199. display: flex;
  200. justify-content: space-between;
  201. flex-wrap: wrap;
  202. padding: 2vw;
  203. .list {
  204. display: flex;
  205. flex-direction: column;
  206. justify-content: inherit;
  207. width: 43vw;
  208. padding: 2vw;
  209. margin: 0 0 2vw 0;
  210. border-radius: 10px;
  211. background-color: var(--mainColor);
  212. .image {
  213. width: 100%;
  214. height: 40vw;
  215. border-top-right-radius: 10px;
  216. border-top-left-radius: 10px;
  217. }
  218. .name {
  219. font-size: var(--font14Size);
  220. }
  221. .other {
  222. padding: 1vw 0 0 0;
  223. display: flex;
  224. justify-content: space-between;
  225. .left {
  226. font-size: var(--font14Size);
  227. color: var(--fF0Color);
  228. }
  229. .right {
  230. font-size: var(--font12Size);
  231. color: var(--f85Color);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. .scroll-view {
  239. position: absolute;
  240. top: 0;
  241. left: 0;
  242. right: 0;
  243. bottom: 0;
  244. .list-scroll-view {
  245. display: flex;
  246. flex-direction: column;
  247. }
  248. }
  249. .is_bottom {
  250. width: 100%;
  251. text-align: center;
  252. text {
  253. padding: 2vw 0;
  254. display: inline-block;
  255. color: var(--f85Color);
  256. font-size: var(--font14Size);
  257. }
  258. }
  259. </style>