index.vue 7.9 KB

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