search.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <view>
  3. <!--pages/search/search.wxml-->
  4. <view class="ssbox bgfff">
  5. <view class="search dflex">
  6. <view class="searchs dflex flex">
  7. <image src="/static/pages/images/ssico.png" class="ssico"></image>
  8. <input name="searchTitle" placeholder="搜索你想要的商品" placeholder-class="placeholder" class="flex" :value="searchTitle" @input="search" @confirm="confirm" confirm-type="search" auto-focus focus="true"></input>
  9. <image src="/static/pages/images/clear.png" class="clear" @tap="clearInput" v-if="isCuo"></image>
  10. </view>
  11. <view class="cancel" @tap="cancelSearch" v-if="isCancel">取消</view>
  12. <view class="sbtn" @tap="confirmSearch" v-else>搜索</view>
  13. </view>
  14. </view>
  15. <view class="lishi">
  16. <view class="tit">历史搜索</view>
  17. <view class="list dflex">
  18. <view v-for="(item, index) in history" :key="index" class="li" @tap="toTapSearch" :data-value="item">{{item}}</view>
  19. </view>
  20. <view class="delete dflex" @tap="clearHistory" v-if="history.length > 0"><image src="/static/pages/images/delete.png"></image>清空历史记录</view>
  21. </view>
  22. <view class="cai" v-if="likeList.length>0">
  23. <view class="cai-tit dflex"><image src="/static/pages/images/cai_bg1.png"></image>猜您喜欢<image src="/static/pages/images/cai_bg2.png"></image></view>
  24. <view class="tabcon pdlr20 dflex">
  25. <block v-for="(item, index) in likeList" :key="index">
  26. <view class="li">
  27. <view class="jsdimgs" @tap="toDetail" :data-id="item.id">
  28. <image :src="item.pic + '?x-oss-process=style/w375'" mode="aspectFit" class="jsdpro"></image>
  29. <image src="/static/pages/images/jishida.png" v-if="item.isJs == 1" class="jsdimg"></image>
  30. </view>
  31. <view class="jsdinfo">
  32. <view class="proname" @tap="toDetail" :data-id="item.id">{{item.title}}</view>
  33. <view class="operate dflex">
  34. <view class="price proprice">¥<text>{{show_membership_price==1&&item.actualPrice < item.salePrice?item.actualPrice:item.salePrice}}</text></view>
  35. <view class="add-cart">
  36. <view class="nums dflex" v-if="item.count > 0">
  37. <image src="/static/pages/images/jian.png"></image>
  38. <view class="num">{{item.count}}</view>
  39. <image src="/static/pages/images/jia1.png"></image>
  40. </view>
  41. <image src="/static/pages/images/jia.png" @tap="addShopCart" :data-id="item.id" :data-skuid="item.skuId" :data-index="index" :data-state="item.state" v-else></image>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </block>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. // pages/search/search.js
  53. const app = getApp();
  54. const req = require("../../utils/request.js");
  55. export default {
  56. data() {
  57. return {
  58. searchTitle: '',
  59. history: [],
  60. page: 1,
  61. likeList: [],
  62. isLikeLoad: true,
  63. merchant: {},
  64. isCancel: true,
  65. isCuo: false,
  66. merchantId: "",
  67. show_membership_price: 0
  68. };
  69. },
  70. components: {},
  71. props: {},
  72. onLoad(options) {
  73. this.query = options;
  74. if (options.searchTitle) {
  75. this.setData({
  76. searchTitle: options.searchTitle
  77. });
  78. }
  79. if (options.merchantId) {
  80. this.setData({
  81. merchantId: options.merchantId
  82. });
  83. } else {// this.setData({merchantId:req.getStorage("defaultMerchant").id})
  84. }
  85. // console.log(options);
  86. },
  87. onShow(options) {
  88. this.getConfig();
  89. let history = req.getStorage('HISTORYS'); // let merchant = req.getStorage("MERCHANT");
  90. this.setData({
  91. history: history // merchant:merchant
  92. });
  93. this.getLikeList();
  94. },
  95. methods: {
  96. getConfig() {
  97. var _this = this;
  98. return new Promise((resolve, reject) => {
  99. req.getRequest('/api/config', {}, function (res) {
  100. if (res && res.show_membership_price == 1) {
  101. _this.setData({
  102. show_membership_price: 1
  103. });
  104. }
  105. resolve();
  106. });
  107. });
  108. },
  109. search(e) {
  110. this.searchTitle = e.detail.value;
  111. this.setData({
  112. searchTitle: e.detail.value
  113. });
  114. if (this.searchTitle.length > 0) {
  115. this.setData({
  116. isCancel: false,
  117. isCuo: true
  118. });
  119. } else {
  120. this.setData({
  121. isCancel: true,
  122. isCuo: false
  123. });
  124. }
  125. return e.detail.value.replace(/\s+/g, '').replace(/[^\u4e00-\u9fa5a-zA-Z0-9\w]/g, '');
  126. },
  127. cancelSearch() {
  128. uni.navigateBack();
  129. },
  130. clearInput() {
  131. this.setData({
  132. searchTitle: '',
  133. isCancel: true,
  134. isCuo: false
  135. });
  136. },
  137. addShopCart(e) {
  138. let state = e.currentTarget.dataset.state;
  139. let skuId = e.currentTarget.dataset.skuid;
  140. let id = e.currentTarget.dataset.id;
  141. let params = {
  142. skuId: skuId,
  143. quantity: 1,
  144. productId: id
  145. }; // if(state!=1){
  146. // return req.msg('您来晚了,商品已经下架')
  147. // }
  148. if (this.merchantId) {
  149. params.merchantId = this.merchantId;
  150. }
  151. req.postRequest('/api/purchase/cart', params, res => {
  152. req.msg('加入购物车成功');
  153. });
  154. },
  155. confirm(e) {
  156. let value = e.detail.value;
  157. if (!value) return req.msg('请输入商品名称');
  158. this.toSearch(value, true);
  159. },
  160. confirmSearch() {
  161. let value = this.searchTitle;
  162. if (!value) return req.msg('请输入商品名称');
  163. this.toSearch(value, true);
  164. },
  165. toTapSearch(e) {
  166. let value = e.currentTarget.dataset.value;
  167. this.toSearch(value, false);
  168. },
  169. toSearch(value, isSave) {
  170. let supplier = '';
  171. if (this.query.supplier) {
  172. supplier = '&supplier=' + this.query.supplier;
  173. }
  174. if (this.merchantId) {
  175. if (this.query.shopId) {
  176. app.globalData.redirectTab('product/list/list?searchTitle=' + value+'&merchantId='+this.query.merchantId + '&shopId=' + this.query.shopId + supplier);
  177. } else {
  178. app.globalData.redirectTab('product/list/list?searchTitle=' + value + '&merchantId=' + this.merchantId + supplier);
  179. }
  180. } else {
  181. app.globalData.redirectTab('product/list/list?searchTitle=' + value + supplier);
  182. }
  183. if (isSave) {
  184. let historys = this.history;
  185. if (historys.length < 1) {
  186. historys = [];
  187. historys.unshift(value);
  188. } else {
  189. historys.unshift(value);
  190. }
  191. req.setStorage('HISTORYS', historys);
  192. }
  193. },
  194. clearHistory() {
  195. req.removeStorage('HISTORYS');
  196. this.setData({
  197. history: []
  198. });
  199. },
  200. getLikeList() {
  201. //Recommend
  202. if (!this.isLikeLoad) return false;
  203. this.isLikeLoad = false;
  204. let that = this;
  205. let form = {
  206. page: that.page,
  207. limit: 10,
  208. attr: 'sell_well'
  209. };
  210. if (this.merchantId) {
  211. form.merchantId = this.merchantId;
  212. }
  213. req.getRequest('/api/product/list', form, res => {
  214. if (res && res.length >= 10) that.isLikeLoad = true;
  215. if (that.page > 1) res = that.likeList.concat(res);
  216. res = res.map(it => {
  217. if (it.deliverWay && JSON.parse(it.deliverWay).indexOf(3) != -1) it.isJs = true;
  218. return it;
  219. });
  220. that.setData({
  221. likeList: res
  222. });
  223. });
  224. },
  225. toDetail(e) {
  226. let id = e.currentTarget.dataset.id;
  227. // console.log(id);
  228. if (this.merchantId) {
  229. app.globalData.openPage('product/detail/detail?id=' + id + "&merchantId=" + this.merchantId);
  230. } else {
  231. app.globalData.openPage('product/detail/detail?id=' + id);
  232. }
  233. }
  234. }
  235. };
  236. </script>
  237. <style>
  238. @import "./search.css";
  239. </style>