index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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" @input="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. // 点击查看商品名称列表
  153. async focusHandler(e) {
  154. const that = this;
  155. if (e.detail.value) {
  156. let res;
  157. let info = {
  158. name: e.detail.value,
  159. is_use: '0'
  160. }
  161. res = await that.$api(`/Good/search`, 'GET', {
  162. ...info,
  163. })
  164. if (res.errcode == '0') that.$set(that, `goodsList`, res.data)
  165. that.$set(that, `focus`, true)
  166. }
  167. },
  168. // 取消搜索
  169. cancelHandler(e) {
  170. const that = this;
  171. that.$set(that, `searchInfo`, {})
  172. that.$set(that, `focus`, false)
  173. that.clearPage();
  174. that.search();
  175. },
  176. async change(e) {
  177. uni.navigateTo({
  178. url: `${e.route}?type=${e.type}`
  179. })
  180. },
  181. // 购买
  182. toBuy(e) {
  183. const that = this;
  184. uni.navigateTo({
  185. url: `/pagesGoods/index/index?id=${e.id||e._id}`
  186. })
  187. },
  188. // 分页
  189. toPage(e) {
  190. const that = this;
  191. let list = that.list;
  192. let limit = that.limit;
  193. if (that.total > list.length) {
  194. uni.showLoading({
  195. title: '加载中',
  196. mask: true
  197. })
  198. let page = that.page + 1;
  199. that.$set(that, `page`, page)
  200. let skip = page * limit;
  201. that.$set(that, `skip`, skip)
  202. that.search();
  203. uni.hideLoading();
  204. } else that.$set(that, `is_bottom`, true)
  205. },
  206. toScroll(e) {
  207. const that = this;
  208. let up = that.scrollTop;
  209. that.$set(that, `scrollTop`, e.detail.scrollTop);
  210. let num = Math.sign(up - e.detail.scrollTop);
  211. if (num == 1) that.$set(that, `is_bottom`, false);
  212. },
  213. // 清空列表
  214. clearPage() {
  215. const that = this;
  216. that.$set(that, `list`, [])
  217. that.$set(that, `skip`, 0)
  218. that.$set(that, `limit`, 6)
  219. that.$set(that, `page`, 0)
  220. },
  221. }
  222. }
  223. </script>
  224. <style lang="scss">
  225. .content {
  226. display: flex;
  227. flex-direction: column;
  228. width: 100vw;
  229. height: 100vh;
  230. .one {
  231. padding: 2vw;
  232. .one_1 {
  233. input {
  234. padding: 2vw;
  235. background-color: var(--f1Color);
  236. font-size: var(--font14Size);
  237. border-radius: 5px;
  238. }
  239. }
  240. .one_2 {
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. .left {
  245. padding: 0 2vw;
  246. width: 75vw;
  247. input {
  248. padding: 2vw;
  249. background-color: var(--f1Color);
  250. font-size: var(--font14Size);
  251. border-radius: 5px;
  252. }
  253. }
  254. .button {
  255. background-color: var(--f3CColor);
  256. color: var(--mainColor);
  257. }
  258. }
  259. .one_3 {
  260. height: 20vh;
  261. .list {
  262. padding: 2vw;
  263. border-bottom: 1px solid var(--f5Color);
  264. .name {
  265. display: inline-block;
  266. white-space: nowrap;
  267. width: 100%;
  268. overflow: hidden;
  269. text-overflow: ellipsis;
  270. }
  271. }
  272. }
  273. }
  274. .two {
  275. padding: 1vw 2vw;
  276. background-color: var(--f9Color);
  277. .grid {
  278. display: flex;
  279. flex-direction: column;
  280. align-items: center;
  281. padding: 5px 0 0 0;
  282. margin: 5px;
  283. background-color: var(--mainColor);
  284. border-radius: 10px;
  285. .image {
  286. width: 25px;
  287. height: 25px;
  288. }
  289. .text {
  290. font-size: var(--font14Size);
  291. margin-top: 5px;
  292. }
  293. }
  294. }
  295. .thr {
  296. position: relative;
  297. flex-grow: 1;
  298. background-color: var(--f9Color);
  299. .thr_1 {
  300. display: flex;
  301. justify-content: space-between;
  302. flex-wrap: wrap;
  303. padding: 2vw;
  304. .list {
  305. display: flex;
  306. flex-direction: column;
  307. justify-content: inherit;
  308. width: 43vw;
  309. padding: 2vw;
  310. margin: 0 0 2vw 0;
  311. border-radius: 10px;
  312. background-color: var(--mainColor);
  313. .image {
  314. width: 100%;
  315. height: 40vw;
  316. border-top-right-radius: 10px;
  317. border-top-left-radius: 10px;
  318. }
  319. .name {
  320. font-size: var(--font14Size);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. .scroll-view {
  327. position: absolute;
  328. top: 0;
  329. left: 0;
  330. right: 0;
  331. bottom: 0;
  332. .list-scroll-view {
  333. display: flex;
  334. flex-direction: column;
  335. }
  336. }
  337. .is_bottom {
  338. width: 100%;
  339. text-align: center;
  340. text {
  341. padding: 2vw 0;
  342. display: inline-block;
  343. color: var(--f85Color);
  344. font-size: var(--font14Size);
  345. }
  346. }
  347. </style>