Forráskód Böngészése

fix 修复不能清除搜索历史和记录搜索关键词

tumobi 8 éve
szülő
commit
ac1724b082
3 módosított fájl, 119 hozzáadás és 92 törlés
  1. 71 30
      nideshop.sql
  2. 32 24
      src/api/controller/goods.js
  3. 16 38
      src/api/controller/search.js

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 71 - 30
nideshop.sql


+ 32 - 24
src/api/controller/goods.js

@@ -40,13 +40,13 @@ export default class extends Base {
     let goodsId = this.get('id');
     let model = this.model('goods');
 
-    let info = await model.where({'id': goodsId}).find();
-    let gallery = await this.model('goods_gallery').where({goods_id: goodsId}).limit(4).select();
-    let attribute = await this.model('goods_attribute').field('nideshop_goods_attribute.value, nideshop_attribute.name').join('nideshop_attribute ON nideshop_goods_attribute.attribute_id=nideshop_attribute.id').order({'nideshop_goods_attribute.id': 'asc'}).where({'nideshop_goods_attribute.goods_id': goodsId}).select();
+    let info = await model.where({ 'id': goodsId }).find();
+    let gallery = await this.model('goods_gallery').where({ goods_id: goodsId }).limit(4).select();
+    let attribute = await this.model('goods_attribute').field('nideshop_goods_attribute.value, nideshop_attribute.name').join('nideshop_attribute ON nideshop_goods_attribute.attribute_id=nideshop_attribute.id').order({ 'nideshop_goods_attribute.id': 'asc' }).where({ 'nideshop_goods_attribute.goods_id': goodsId }).select();
     let issue = await this.model('goods_issue').select();
-    let brand = await this.model('brand').where({id: info.brand_id}).find();
-    let commentCount = await this.model('comment').where({value_id: goodsId, type_id: 0}).count();
-    let hotComment = await this.model('comment').where({value_id: goodsId, type_id: 0}).find();
+    let brand = await this.model('brand').where({ id: info.brand_id }).find();
+    let commentCount = await this.model('comment').where({ value_id: goodsId, type_id: 0 }).count();
+    let hotComment = await this.model('comment').where({ value_id: goodsId, type_id: 0 }).find();
     let commentInfo = {};
     if (!think.isEmpty(hotComment)) {
       let commentContent = new Buffer(hotComment.content, 'base64').toString();
@@ -57,7 +57,7 @@ export default class extends Base {
         username: '明××天',
         avatar: 'http://yanxuan.nosdn.127.net/8bbc224d0f062596b8f64037a88d527e',
         level: 'V3',
-        pic_list: await this.model('comment_picture').where({comment_id: hotComment.id}).select()
+        pic_list: await this.model('comment_picture').where({ comment_id: hotComment.id }).select()
 
       }
     }
@@ -94,10 +94,10 @@ export default class extends Base {
   async categoryAction() {
 
     const model = this.model('category');
-    let currentCategory = await model.where({id: this.get('id')}).find();
-    let parentCategory = await model.where({id: currentCategory.parent_id}).find();
-    let brotherCategory = await model.where({parent_id: currentCategory.parent_id}).select();
-    let categoryGoods = await this.model('goods').field(['id', 'name', 'list_pic_url', 'retail_price']).where({category_id: currentCategory.id}).select();
+    let currentCategory = await model.where({ id: this.get('id') }).find();
+    let parentCategory = await model.where({ id: currentCategory.parent_id }).find();
+    let brotherCategory = await model.where({ parent_id: currentCategory.parent_id }).select();
+    let categoryGoods = await this.model('goods').field(['id', 'name', 'list_pic_url', 'retail_price']).where({ category_id: currentCategory.id }).select();
 
     // return this.success(ftlData);
     return this.success({
@@ -136,6 +136,14 @@ export default class extends Base {
 
     if (!think.isEmpty(keyword)) {
       whereMap.name = ['like', `%${keyword}%`];
+
+      //添加到搜索历史
+      let keywords = await this.model('search_history').add({
+        keyword: keyword,
+        user_id: 1,
+        add_time: parseInt(new Date().getTime() / 1000)
+      });
+
     }
 
     if (!think.isEmpty(brandId)) {
@@ -166,9 +174,9 @@ export default class extends Base {
     let categoryIds = await goodsQuery.where(whereMap).getField('category_id', 10000);
     if (!think.isEmpty(categoryIds)) {
       //查找二级分类的parent_id
-      let parentIds = await this.model('category').where({id: {'in': categoryIds}}).getField('parent_id', 10000);
+      let parentIds = await this.model('category').where({ id: { 'in': categoryIds } }).getField('parent_id', 10000);
       //一级分类
-      let parentCategory = await this.model('category').field(['id', 'name']).order({'sort_order': 'asc'}).where({'id': {'in': parentIds}}).select();
+      let parentCategory = await this.model('category').field(['id', 'name']).order({ 'sort_order': 'asc' }).where({ 'id': { 'in': parentIds } }).select();
 
       if (!think.isEmpty(parentCategory)) {
         filterCategory = filterCategory.concat(parentCategory);
@@ -209,19 +217,19 @@ export default class extends Base {
     let goodsQuery = this.model('goods');
 
     if (!think.isEmpty(categoryId)) {
-      goodsQuery.where({category_id: {'in': await this.model('category').getChildCategoryId(categoryId)}});
+      goodsQuery.where({ category_id: { 'in': await this.model('category').getChildCategoryId(categoryId) } });
     }
 
     if (!think.isEmpty(isNew)) {
-      goodsQuery.where({is_new: isNew});
+      goodsQuery.where({ is_new: isNew });
     }
 
     if (!think.isEmpty(isHot)) {
-      goodsQuery.where({is_hot: isHot});
+      goodsQuery.where({ is_hot: isHot });
     }
 
     if (!think.isEmpty(keyword)) {
-      goodsQuery.where({name: {'like': `%${keyword}%`}});
+      goodsQuery.where({ name: { 'like': `%${keyword}%` } });
     }
 
     let filterCategory = [{
@@ -233,9 +241,9 @@ export default class extends Base {
     let categoryIds = await goodsQuery.getField('category_id', 10000);
     if (!think.isEmpty(categoryIds)) {
       //查找二级分类的parent_id
-      let parentIds = await this.model('category').where({id: {'in': categoryIds}}).getField('parent_id', 10000);
+      let parentIds = await this.model('category').where({ id: { 'in': categoryIds } }).getField('parent_id', 10000);
       //一级分类
-      let parentCategory = await this.model('category').field(['id', 'name']).order({'sort_order': 'asc'}).where({'id': {'in': parentIds}}).select();
+      let parentCategory = await this.model('category').field(['id', 'name']).order({ 'sort_order': 'asc' }).where({ 'id': { 'in': parentIds } }).select();
 
       if (!think.isEmpty(parentCategory)) {
         filterCategory = filterCategory.concat(parentCategory);
@@ -284,14 +292,14 @@ export default class extends Base {
 
     const model = this.model('goods');
     const goodsId = this.get('id');
-    let relatedGoodsIds = await this.model('related_goods').where({goods_id: goodsId}).getField('related_goods_id');
+    let relatedGoodsIds = await this.model('related_goods').where({ goods_id: goodsId }).getField('related_goods_id');
     let relatedGoods = null;
     if (think.isEmpty(relatedGoodsIds)) {
       //查找同分类下的商品
-      let goodsCategory = await model.where({id: goodsId}).find();
-      relatedGoods = await model.where({category_id: goodsCategory.category_id}).field(['id', 'name', 'list_pic_url', 'retail_price']).limit(8).select();
+      let goodsCategory = await model.where({ id: goodsId }).find();
+      relatedGoods = await model.where({ category_id: goodsCategory.category_id }).field(['id', 'name', 'list_pic_url', 'retail_price']).limit(8).select();
     } else {
-      relatedGoods = await model.where({id: ['IN', relatedGoodsIds]}).field(['id', 'name', 'list_pic_url', 'retail_price']).select();
+      relatedGoods = await model.where({ id: ['IN', relatedGoodsIds] }).field(['id', 'name', 'list_pic_url', 'retail_price']).select();
     }
 
     return this.success({
@@ -305,7 +313,7 @@ export default class extends Base {
    */
   async countAction() {
 
-    let goodsCount = await this.model('goods').where({is_delete: 0, is_on_sale: 1}).count('id');
+    let goodsCount = await this.model('goods').where({ is_delete: 0, is_on_sale: 1 }).count('id');
 
     return this.success({
       goodsCount: goodsCount,

+ 16 - 38
src/api/controller/search.js

@@ -7,52 +7,30 @@ export default class extends Base {
    * index action
    * @return {Promise} []
    */
-  async indexAction(){
-
-    let defaultKeyword = await this.model('keywords').where({is_default: 1}).limit(1).find();
-    let historyKeywordList = await this.model('keywords').distinct('keyword').where({user_id: 1}).limit(10).getField('keyword');
+  async indexAction() {
+    //取出输入框默认的关键词
+    let defaultKeyword = await this.model('keywords').where({ is_default: 1 }).limit(1).find();
+    //取出热闹关键词
     let hotKeywordList = await this.model('keywords').distinct('keyword').field(['keyword', 'is_hot']).limit(10).select();
+
+    let historyKeywordList = await this.model('search_history').distinct('keyword').where({ user_id: 1 }).limit(10).getField('keyword');
+
     return this.success({
-        defaultKeyword: defaultKeyword,
-        historyKeywordList: historyKeywordList,
-        hotKeywordList: hotKeywordList
+      defaultKeyword: defaultKeyword,
+      historyKeywordList: historyKeywordList,
+      hotKeywordList: hotKeywordList
     });
+
   }
 
-  async helperAction(){
+  async helperAction() {
     let keyword = this.get('keyword');
-    let keywords = await this.model('keywords').distinct('keyword').where({keyword: ['like', keyword + '%']}).getField('keyword', 10);
+    let keywords = await this.model('keywords').distinct('keyword').where({ keyword: ['like', keyword + '%'] }).getField('keyword', 10);
     return this.success(keywords);
   }
 
-  async resultAction(){
-    let keyword = this.get('keyword');
-    let page = this.get('page') | 1;
-    let size = this.get('size') | 50;
-    let sort = this.get('sort') | 'id';  //排序的字段 综合 价格 分类
-    let order = this.get('order') | 'desc';
-    let categoryId = this.get('categoryId');
-
-    if (!think.isEmpty(keyword)) {
-        let keywords = await this.model('keywords').add({
-            keyword: keyword,
-            user_id: 1,
-            search_time: parseInt(new Date().getTime() / 1000)
-        });
-    }
-
-
-    let goodsList = await this.model('goods').field(['id', 'name', 'category_id', 'retail_price', 'list_pic_url']).where({name: ['like', `%${keyword}%`]}).select();
-    let categoryIds = await this.model('goods').where({name: ['like', `%${keyword}%`]}).getField('category_id', 1000);
-    let parentCategoryIds = await this.model('category').where({id: ['in', categoryIds]}).getField('parent_id');
-    let parentCategory = await this.model('category').where({parent_id: 0, id: ['in', parentCategoryIds]}).select();
-
-    return this.success({
-        categoryList: parentCategory,
-        goodsList: goodsList,
-        sort: sort,
-        order: order,
-        categoryId: categoryId
-    });
+  async clearhistoryAction() {
+    let historyKeywordList = await this.model('search_history').where({ user_id: 1 }).delete();
+    return this.success();
   }
 }

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott