index.vue 8.1 KB

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