index.vue 8.1 KB

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