金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
6.9 KiB

  1. <template>
  2. <view id="store-search">
  3. <view class="search-box">
  4. <view class="input-box">
  5. <i class="iconfont icon-sousuo " type="search" size="20"></i>
  6. <input type="text" :value="text" @input="search" confirm-type="search" bindconfirm="searchKeywords"></input>
  7. <!-- #ifdef MP-WEIXIN -->
  8. <icon type="clear" size="20" class="clear" :hidden="clear" @tap="clearF"></icon>
  9. <!-- #endif -->
  10. </view>
  11. <view class="text" @tap="searchKeywords">
  12. 搜索
  13. </view>
  14. </view>
  15. <view class="history-box" v-if="searches.length && show">
  16. <view class="keywords mx-1px-bottom" v-for="(item, index) in searches" :key="index">
  17. <view class="name" :data-index="index" @tap="searchHistory">{{item}}</view>
  18. <view class="clear" :data-index="index" @tap="removeSearchHistory">
  19. <i class="iconfont icon-close" type="clear" size="20"></i>
  20. </view>
  21. </view>
  22. <view class="clear-all" @tap="clearSearchHistory">
  23. 清空历史记录
  24. </view>
  25. </view>
  26. <view class="goods-list" v-if="!show">
  27. <view v-for="(item, idx) in storeList" :key="idx">
  28. <view class="commodity-out" :data-id="item.id" v-for="(item, index) in storeList[idx]" :key="index" @tap="jump">
  29. <view class="commodity-box">
  30. <view class="commodity-img">
  31. <image mode="widthFix" :src="item.img"></image>
  32. </view>
  33. <view class="commodity-name">
  34. {{item.name}}
  35. </view>
  36. <view class="commodity-money">
  37. {{item.sell_price}}
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import {pageLogin, getUrl,config} from '@/common/js/utils.js';
  47. export default {
  48. data() {
  49. return {
  50. storeList: [],
  51. text: '',
  52. clear: true,
  53. searches: [],
  54. show: true,
  55. meta: ''
  56. };
  57. },
  58. onReachBottom() {
  59. var hasMore = this.meta.pagination.total_pages > this.meta.pagination.current_page;
  60. if (hasMore) {
  61. var query = {
  62. keyword: this.text
  63. };
  64. var page = this.meta.pagination.current_page + 1;
  65. this.querySearchList(query, page);
  66. } else {
  67. wx.showToast({
  68. image: '../../../static/error.png',
  69. title: '再拉也没有啦'
  70. });
  71. }
  72. },
  73. onShow() {// let app =getApp();
  74. // app.isBirthday().then(()=>{
  75. // if(this.$cookieStorage.get("birthday_gift")){
  76. // var giftData=this.$cookieStorage.get("birthday_gift").data;
  77. // new app.ToastPannel().__page.showText(giftData);
  78. // }
  79. // });
  80. },
  81. onReady() {
  82. var searches = this.$cookieStorage.get('goods_search_history');
  83. if (searches && searches.length) {
  84. this.setData({
  85. searches: searches
  86. });
  87. }
  88. },
  89. components: {},
  90. props: {},
  91. methods: {
  92. jump(e) {
  93. var id = e.currentTarget.dataset.id;
  94. wx.navigateTo({
  95. url: '/pages/store/detail/detail?id=' + id
  96. });
  97. },
  98. search(e) {
  99. this.setData({
  100. text: e.detail.value,
  101. clear: e.detail.value <= 0
  102. });
  103. },
  104. clearF() {
  105. this.setData({
  106. text: '',
  107. clear: true
  108. });
  109. },
  110. // 单击搜索
  111. searchKeywords() {
  112. var keyword = this.text;
  113. if (!keyword || !keyword.length) return;
  114. var searches = JSON.parse(JSON.stringify(this.searches));
  115. for (let i = 0, len = searches.length; i < len; i++) {
  116. let search = searches[i];
  117. if (search === keyword) {
  118. searches.splice(i, 1);
  119. break;
  120. }
  121. }
  122. searches.unshift(keyword);
  123. this.$cookieStorage.set('goods_search_history', searches);
  124. wx.setNavigationBarTitle({
  125. title: '搜索:' + "'" + keyword + "'"
  126. });
  127. this.querySearchList({
  128. keyword: keyword
  129. });
  130. this.setData({
  131. show: false,
  132. searches: searches
  133. });
  134. },
  135. // 点击单个搜索记录搜索
  136. searchHistory(e) {
  137. var searches = JSON.parse(JSON.stringify(this.searches));
  138. var keyword = searches[e.currentTarget.dataset.index];
  139. searches.splice(e.currentTarget.dataset.index, 1);
  140. searches.unshift(keyword);
  141. this.$cookieStorage.set('goods_search_history', searches);
  142. wx.setNavigationBarTitle({
  143. title: '搜索:' + "'" + keyword + "'"
  144. });
  145. this.querySearchList({
  146. keyword: keyword
  147. });
  148. this.setData({
  149. show: false,
  150. searches: searches,
  151. text: keyword
  152. });
  153. },
  154. // 删除单个搜索记录
  155. removeSearchHistory(e) {
  156. var searches = JSON.parse(JSON.stringify(this.searches));
  157. searches.splice(e.currentTarget.dataset.index, 1);
  158. this.$cookieStorage.set('goods_search_history', searches);
  159. this.setData({
  160. searches: searches
  161. });
  162. },
  163. // 清空搜索记录
  164. clearSearchHistory() {
  165. uni.removeStorageSync('goods_search_history');
  166. this.setData({
  167. show: false,
  168. searches: []
  169. });
  170. },
  171. // 搜索商品
  172. querySearchList(query = {}, page = 1) {
  173. var params = Object.assign({}, query, {
  174. page
  175. });
  176. wx.showLoading({
  177. title: '加载中',
  178. mask: true
  179. });
  180. this.$http.get({
  181. api: 'api/store/list',
  182. data: params
  183. }).then(res => {
  184. res = res.data;
  185. if (res.status) {
  186. // var keyReg = new RegExp('(' + this.text + ')(?!</i>)', 'g')
  187. // list.forEach(item => {
  188. // item.name = item.name.replace(keyReg, '<i>$1</i>');
  189. // })
  190. if (!res.data || !res.data.length) {
  191. wx.showModal({
  192. content: '暂无该商品',
  193. showCancel: false
  194. });
  195. }
  196. this.setData({
  197. [`storeList.${page - 1}`]: res.data,
  198. meta: res.meta
  199. });
  200. } else {
  201. wx.showModal({
  202. title: '',
  203. content: res.message
  204. });
  205. }
  206. wx.hideLoading();
  207. }).catch(rej => {
  208. wx.showModal({
  209. title: '',
  210. content: '请求失败,请稍后重试'
  211. });
  212. wx.hideLoading();
  213. });
  214. },
  215. setData: function (obj) {
  216. let that = this;
  217. let keys = [];
  218. let val, data;
  219. Object.keys(obj).forEach(function (key) {
  220. keys = key.split('.');
  221. val = obj[key];
  222. data = that.$data;
  223. keys.forEach(function (key2, index) {
  224. if (index + 1 == keys.length) {
  225. that.$set(data, key2, val);
  226. } else {
  227. if (!data[key2]) {
  228. that.$set(data, key2, {});
  229. }
  230. }
  231. data = data[key2];
  232. });
  233. });
  234. }
  235. },
  236. computed: {},
  237. watch: {}
  238. };
  239. </script>
  240. <style rel="stylesheet/less" lang="less">
  241. @import "search";
  242. </style>