index.vue 8.2 KB

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