index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <view class="one_1" v-if="!focus">
  5. <input type='text' placeholder='搜索商品' @focus="focusHandler"></input>
  6. </view>
  7. <view class="one_2" v-if="focus">
  8. <view class="left">
  9. <input type="text" v-model="searchInfo.name" @input="focusHandler" placeholder="搜索商品">
  10. </view>
  11. <view class="right">
  12. <button size="mini" class="button" type="default" @click="cancelHandler">取消</button>
  13. </view>
  14. </view>
  15. <!-- 搜索结果显示框 -->
  16. <scroll-view v-if="focus" class="one_3" scroll-y="true">
  17. <view class="list" v-for="(item, index) in goodsList" :index="index" :key="index"
  18. @click="toInput(item)">
  19. <text class="name"> {{item.name}}</text>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. <view class="two">
  24. <uni-grid :column="4" :show-border="false" :square="false">
  25. <uni-grid-item v-for="(item, index) in moduleList" :index="index" :key="index" @tap="change(item)">
  26. <view class="grid">
  27. <image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode="aspectFill">
  28. </image>
  29. <text class="text">{{ item.name }}</text>
  30. </view>
  31. </uni-grid-item>
  32. </uni-grid>
  33. </view>
  34. <view class="thr">
  35. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  36. <view class="list-scroll-view">
  37. <view class="thr_1">
  38. <view class="list" v-for="(item,index) in list" :key="index" @tap="toBuy(item)">
  39. <image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''"
  40. mode="aspectFill">
  41. </image>
  42. <view class="name textOver">{{item.name||'暂无'}}</view>
  43. </view>
  44. </view>
  45. <view class="is_bottom" v-if="is_bottom">
  46. <text>{{config.bottom_title}}</text>
  47. </view>
  48. </view>
  49. </scroll-view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. focus: false, //控制是否显示带取消按钮的搜索框
  58. config: {},
  59. user: {},
  60. searchInfo: {},
  61. moduleList: [],
  62. goodsList: [],
  63. list: [],
  64. total: 0,
  65. skip: 0,
  66. limit: 6,
  67. page: 0,
  68. // 数据是否触底
  69. is_bottom: false,
  70. scrollTop: 0,
  71. }
  72. },
  73. async onLoad() {
  74. const that = this;
  75. that.searchToken();
  76. that.searchConfig();
  77. },
  78. async onShow() {
  79. const that = this;
  80. that.clearPage();
  81. await that.search();
  82. },
  83. onPullDownRefresh: async function() {
  84. const that = this;
  85. that.clearPage();
  86. await that.search();
  87. uni.stopPullDownRefresh();
  88. },
  89. methods: {
  90. searchToken() {
  91. const that = this;
  92. try {
  93. const res = uni.getStorageSync('token');
  94. if (res) that.$set(that, `user`, res);
  95. } catch (e) {
  96. uni.showToast({
  97. title: err.errmsg,
  98. icon: 'error',
  99. duration: 2000
  100. });
  101. }
  102. },
  103. searchConfig() {
  104. const that = this;
  105. try {
  106. const res = uni.getStorageSync('config');
  107. if (res) that.$set(that, `config`, res);
  108. } catch (e) {
  109. uni.showToast({
  110. title: err.errmsg,
  111. icon: 'error',
  112. duration: 2000
  113. });
  114. }
  115. },
  116. async search() {
  117. const that = this;
  118. let res;
  119. res = await that.$api(`/Module`, 'GET', {
  120. is_use: '0'
  121. });
  122. if (res.errcode == '0') that.$set(that, `moduleList`, res.data);
  123. let info = {
  124. skip: that.skip,
  125. limit: that.limit,
  126. is_use: '0'
  127. }
  128. res = await that.$api(`/Good`, 'GET', {
  129. ...info,
  130. ...that.searchInfo
  131. })
  132. if (res.errcode == '0') {
  133. let list = [...that.list, ...res.data];
  134. that.$set(that, `list`, list)
  135. that.$set(that, `total`, res.total)
  136. } else {
  137. uni.showToast({
  138. title: res.errmsg,
  139. });
  140. }
  141. },
  142. // 输入框
  143. toInput(e) {
  144. const that = this;
  145. if (e.name) that.$set(that.searchInfo, `name`, e.name)
  146. else that.$set(that, `searchInfo`, {})
  147. that.clearPage();
  148. that.search();
  149. that.$set(that, `focus`, false)
  150. },
  151. async focusHandler(e) {
  152. const that = this;
  153. let res;
  154. let info = {
  155. name: e.detail.value,
  156. is_use: '0'
  157. }
  158. res = await that.$api(`/Good/search`, 'GET', {
  159. ...info,
  160. })
  161. if (res.errcode == '0') that.$set(that, `goodsList`, res.data)
  162. that.$set(that, `focus`, true)
  163. },
  164. cancelHandler(e) {
  165. const that = this;
  166. that.$set(that, `searchInfo`, {})
  167. that.$set(that, `focus`, false)
  168. that.clearPage();
  169. that.search();
  170. },
  171. async change(e) {
  172. uni.navigateTo({
  173. url: `${e.route}?type=${e.type}`
  174. })
  175. },
  176. // 购买
  177. toBuy(e) {
  178. const that = this;
  179. uni.navigateTo({
  180. url: `/pagesGoods/index/index?id=${e.id||e._id}`
  181. })
  182. },
  183. // 分页
  184. toPage(e) {
  185. const that = this;
  186. let list = that.list;
  187. let limit = that.limit;
  188. if (that.total > list.length) {
  189. uni.showLoading({
  190. title: '加载中',
  191. mask: true
  192. })
  193. let page = that.page + 1;
  194. that.$set(that, `page`, page)
  195. let skip = page * limit;
  196. that.$set(that, `skip`, skip)
  197. that.search();
  198. uni.hideLoading();
  199. } else that.$set(that, `is_bottom`, true)
  200. },
  201. toScroll(e) {
  202. const that = this;
  203. let up = that.scrollTop;
  204. that.$set(that, `scrollTop`, e.detail.scrollTop);
  205. let num = Math.sign(up - e.detail.scrollTop);
  206. if (num == 1) that.$set(that, `is_bottom`, false);
  207. },
  208. // 清空列表
  209. clearPage() {
  210. const that = this;
  211. that.$set(that, `list`, [])
  212. that.$set(that, `skip`, 0)
  213. that.$set(that, `limit`, 6)
  214. that.$set(that, `page`, 0)
  215. },
  216. }
  217. }
  218. </script>
  219. <style lang="scss">
  220. .content {
  221. display: flex;
  222. flex-direction: column;
  223. width: 100vw;
  224. height: 100vh;
  225. .one {
  226. padding: 2vw;
  227. .one_1 {
  228. input {
  229. padding: 2vw;
  230. background-color: var(--f1Color);
  231. font-size: var(--font14Size);
  232. border-radius: 5px;
  233. }
  234. }
  235. .one_2 {
  236. display: flex;
  237. justify-content: center;
  238. align-items: center;
  239. .left {
  240. padding: 0 2vw;
  241. width: 75vw;
  242. input {
  243. padding: 2vw;
  244. background-color: var(--f1Color);
  245. font-size: var(--font14Size);
  246. border-radius: 5px;
  247. }
  248. }
  249. .button {
  250. background-color: var(--f3CColor);
  251. color: var(--mainColor);
  252. }
  253. }
  254. .one_3 {
  255. height: 20vh;
  256. .list {
  257. padding: 2vw;
  258. border-bottom: 1px solid var(--f5Color);
  259. .name {
  260. display: inline-block;
  261. white-space: nowrap;
  262. width: 100%;
  263. overflow: hidden;
  264. text-overflow: ellipsis;
  265. }
  266. }
  267. }
  268. }
  269. .two {
  270. padding: 1vw 2vw;
  271. background-color: var(--f9Color);
  272. .grid {
  273. display: flex;
  274. flex-direction: column;
  275. align-items: center;
  276. padding: 5px 0 0 0;
  277. margin: 5px;
  278. background-color: var(--mainColor);
  279. border-radius: 10px;
  280. .image {
  281. width: 25px;
  282. height: 25px;
  283. }
  284. .text {
  285. font-size: var(--font14Size);
  286. margin-top: 5px;
  287. }
  288. }
  289. }
  290. .thr {
  291. position: relative;
  292. flex-grow: 1;
  293. background-color: var(--f9Color);
  294. .thr_1 {
  295. display: flex;
  296. justify-content: space-between;
  297. flex-wrap: wrap;
  298. padding: 2vw;
  299. .list {
  300. display: flex;
  301. flex-direction: column;
  302. justify-content: inherit;
  303. width: 43vw;
  304. padding: 2vw;
  305. margin: 0 0 2vw 0;
  306. border-radius: 10px;
  307. background-color: var(--mainColor);
  308. .image {
  309. width: 100%;
  310. height: 40vw;
  311. border-top-right-radius: 10px;
  312. border-top-left-radius: 10px;
  313. }
  314. .name {
  315. font-size: var(--font14Size);
  316. }
  317. }
  318. }
  319. }
  320. }
  321. .scroll-view {
  322. position: absolute;
  323. top: 0;
  324. left: 0;
  325. right: 0;
  326. bottom: 0;
  327. .list-scroll-view {
  328. display: flex;
  329. flex-direction: column;
  330. }
  331. }
  332. .is_bottom {
  333. width: 100%;
  334. text-align: center;
  335. text {
  336. padding: 2vw 0;
  337. display: inline-block;
  338. color: var(--f85Color);
  339. font-size: var(--font14Size);
  340. }
  341. }
  342. </style>