goods.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. 'use strict';
  2. import Base from './base.js';
  3. export default class extends Base {
  4. /**
  5. * index action
  6. * @return {Promise} []
  7. */
  8. async indexAction() {
  9. let model = this.model('goods');
  10. let goodsList = await model.select();
  11. return this.success(goodsList);
  12. }
  13. /**
  14. * 获取sku信息,用于购物车编辑时选择规格
  15. * @returns {Promise.<Promise|PreventPromise|void>}
  16. */
  17. async skuAction() {
  18. let goodsId = this.get('id');
  19. let model = this.model('goods');
  20. return this.success({
  21. specificationList: await model.getSpecificationList(goodsId),
  22. productList: await model.getProductList(goodsId)
  23. });
  24. }
  25. /**
  26. * 商品详情页数据
  27. * @returns {Promise.<Promise|PreventPromise|void>}
  28. */
  29. async detailAction() {
  30. let goodsId = this.get('id');
  31. let model = this.model('goods');
  32. let info = await model.where({'id': goodsId}).find();
  33. let gallery = await this.model('goods_gallery').where({goods_id: goodsId}).limit(4).select();
  34. 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();
  35. let issue = await this.model('goods_issue').select();
  36. let brand = await this.model('brand').where({id: info.brand_id}).find();
  37. let commentCount = await this.model('comment').where({value_id: goodsId, type_id: 0}).count();
  38. let hotComment = await this.model('comment').where({value_id: goodsId, type_id: 0}).find();
  39. let commentInfo = {};
  40. if (!think.isEmpty(hotComment)) {
  41. let commentContent = new Buffer(hotComment.content, 'base64').toString();
  42. let addTime = new Date(hotComment.add_time);
  43. commentInfo = {
  44. content: new Buffer(hotComment.content, 'base64').toString(),
  45. add_time: addTime.getFullYear() + '-' + addTime.getMonth() + '-' + addTime.getDay() + ' ' + addTime.getHours() + ':' + addTime.getMinutes() + ':' + addTime.getSeconds(),
  46. username: '明××天',
  47. avatar: 'http://yanxuan.nosdn.127.net/8bbc224d0f062596b8f64037a88d527e',
  48. level: 'V3',
  49. pic_list: await this.model('comment_picture').where({comment_id: hotComment.id}).select()
  50. }
  51. }
  52. let comment = {
  53. count: commentCount,
  54. data: commentInfo
  55. };
  56. //当前用户是否收藏
  57. let userHasCollect = await this.model('collect').isUserHasCollect(1, 0, goodsId);
  58. //记录用户的足迹 TODO
  59. await await this.model('footprint').addFootprint(goodsId);
  60. // return this.json(jsonData);
  61. return this.success({
  62. info: info,
  63. gallery: gallery,
  64. attribute: attribute,
  65. userHasCollect: userHasCollect,
  66. issue: issue,
  67. comment: comment,
  68. brand: brand,
  69. specificationList: await model.getSpecificationList(goodsId),
  70. productList: await model.getProductList(goodsId)
  71. });
  72. }
  73. /**
  74. * 获取分类下的商品
  75. * @returns {Promise.<Promise|PreventPromise|void>}
  76. */
  77. async categoryAction() {
  78. const model = this.model('category');
  79. let currentCategory = await model.where({id: this.get('id')}).find();
  80. let parentCategory = await model.where({id: currentCategory.parent_id}).find();
  81. let brotherCategory = await model.where({parent_id: currentCategory.parent_id}).select();
  82. let categoryGoods = await this.model('goods').field(['id', 'name', 'list_pic_url', 'retail_price']).where({category_id: currentCategory.id}).select();
  83. // return this.success(ftlData);
  84. return this.success({
  85. currentCategory: currentCategory,
  86. parentCategory: parentCategory,
  87. brotherCategory: brotherCategory
  88. });
  89. }
  90. /**
  91. * 获取商品列表
  92. * @returns {Promise.<Promise|PreventPromise|void>}
  93. */
  94. async listAction() {
  95. let categoryId = this.get('categoryId');
  96. let brandId = this.get('brandId');
  97. let keyword = this.get('keyword');
  98. let isNew = this.get('isNew');
  99. let isHot = this.get('isHot');
  100. let page = this.get('page');
  101. let size = this.get('size');
  102. let sort = this.get('sort');
  103. let order = this.get('order');
  104. let goodsQuery = this.model('goods');
  105. let whereMap = {};
  106. if (!think.isEmpty(isNew)) {
  107. whereMap.is_new = isNew;
  108. }
  109. if (!think.isEmpty(isHot)) {
  110. whereMap.is_hot = isHot;
  111. }
  112. if (!think.isEmpty(keyword)) {
  113. whereMap.name = ['like', `%${keyword}%`];
  114. }
  115. if (!think.isEmpty(brandId)) {
  116. whereMap.brand_id = brandId;
  117. }
  118. //排序
  119. let orderMap = {};
  120. if (sort === 'price') {
  121. //按价格
  122. orderMap = {
  123. retail_price: order
  124. };
  125. } else {
  126. //按商品添加时间
  127. orderMap = {
  128. id: 'desc'
  129. };
  130. }
  131. //筛选的分类
  132. let filterCategory = [{
  133. 'id': 0,
  134. 'name': '全部',
  135. 'checked': false
  136. }];
  137. let categoryIds = await goodsQuery.where(whereMap).getField('category_id', 10000);
  138. if (!think.isEmpty(categoryIds)) {
  139. //查找二级分类的parent_id
  140. let parentIds = await this.model('category').where({id: {'in': categoryIds}}).getField('parent_id', 10000);
  141. //一级分类
  142. let parentCategory = await this.model('category').field(['id', 'name']).order({'sort_order': 'asc'}).where({'id': {'in': parentIds}}).select();
  143. if (!think.isEmpty(parentCategory)) {
  144. filterCategory = filterCategory.concat(parentCategory);
  145. }
  146. }
  147. //加入分类条件
  148. if (!think.isEmpty(categoryId) && parseInt(categoryId) > 0) {
  149. whereMap.category_id = ['in', await this.model('category').getCategoryWhereIn(categoryId)];
  150. }
  151. console.log(whereMap);
  152. //搜索到的商品
  153. let goodsData = await goodsQuery.where(whereMap).field(['id', 'name', 'list_pic_url', 'retail_price']).order(orderMap).page(page, size).countSelect();
  154. goodsData.filterCategory = filterCategory.map(function (v) {
  155. if ((think.isEmpty(categoryId) && v.id === 0) || v.id === parseInt(categoryId)) {
  156. v.checked = true;
  157. } else {
  158. v.checked = false;
  159. }
  160. return v;
  161. });
  162. goodsData.goodsList = goodsData.data;
  163. return this.success(goodsData);
  164. }
  165. /**
  166. * 商品列表筛选的分类列表
  167. * @returns {Promise.<Promise|void|PreventPromise>}
  168. */
  169. async filterAction() {
  170. let categoryId = this.get('categoryId');
  171. let keyword = this.get('keyword');
  172. let isNew = this.get('isNew');
  173. let isHot = this.get('isHot');
  174. let goodsQuery = this.model('goods');
  175. if (!think.isEmpty(categoryId)) {
  176. goodsQuery.where({category_id: {'in': await this.model('category').getChildCategoryId(categoryId)}});
  177. }
  178. if (!think.isEmpty(isNew)) {
  179. goodsQuery.where({is_new: isNew});
  180. }
  181. if (!think.isEmpty(isHot)) {
  182. goodsQuery.where({is_hot: isHot});
  183. }
  184. if (!think.isEmpty(keyword)) {
  185. goodsQuery.where({name: {'like': `%${keyword}%`}});
  186. }
  187. let filterCategory = [{
  188. 'id': 0,
  189. 'name': '全部'
  190. }];
  191. //二级分类id
  192. let categoryIds = await goodsQuery.getField('category_id', 10000);
  193. if (!think.isEmpty(categoryIds)) {
  194. //查找二级分类的parent_id
  195. let parentIds = await this.model('category').where({id: {'in': categoryIds}}).getField('parent_id', 10000);
  196. //一级分类
  197. let parentCategory = await this.model('category').field(['id', 'name']).order({'sort_order': 'asc'}).where({'id': {'in': parentIds}}).select();
  198. if (!think.isEmpty(parentCategory)) {
  199. filterCategory = filterCategory.concat(parentCategory);
  200. }
  201. }
  202. return this.success(filterCategory);
  203. }
  204. /**
  205. * 新品首发
  206. * @returns {Promise.<Promise|void|PreventPromise>}
  207. */
  208. async newAction() {
  209. return this.success({
  210. bannerInfo: {
  211. url: '',
  212. name: '坚持初心,为你寻觅世间好物',
  213. img_url: 'http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png',
  214. }
  215. });
  216. }
  217. /**
  218. * 人气推荐
  219. * @returns {Promise.<Promise|void|PreventPromise>}
  220. */
  221. async hotAction() {
  222. return this.success({
  223. bannerInfo: {
  224. url: '',
  225. name: '大家都在买的严选好物',
  226. img_url: 'http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png',
  227. }
  228. });
  229. }
  230. /**
  231. * 商品详情页的大家都在看的商品
  232. * @returns {Promise.<Promise|PreventPromise|void>}
  233. */
  234. async relatedAction() {
  235. //大家都在看商品,取出关联表的商品,如果没有则随机取同分类下的商品
  236. const model = this.model('goods');
  237. const goodsId = this.get('id');
  238. let relatedGoodsIds = await this.model('related_goods').where({goods_id: goodsId}).getField('related_goods_id');
  239. let relatedGoods = null;
  240. if (think.isEmpty(relatedGoodsIds)) {
  241. //查找同分类下的商品
  242. let goodsCategory = await model.where({id: goodsId}).find();
  243. relatedGoods = await model.where({category_id: goodsCategory.category_id}).field(['id', 'name', 'list_pic_url', 'retail_price']).limit(8).select();
  244. } else {
  245. relatedGoods = await model.where({id: ['IN', relatedGoodsIds]}).field(['id', 'name', 'list_pic_url', 'retail_price']).select();
  246. }
  247. return this.success({
  248. goodsList: relatedGoods,
  249. });
  250. }
  251. /**
  252. * 在售的商品总数
  253. * @returns {Promise.<Promise|PreventPromise|void>}
  254. */
  255. async countAction() {
  256. let goodsCount = await this.model('goods').where({is_delete: 0, is_on_sale: 1}).count('id');
  257. return this.success({
  258. goodsCount: goodsCount,
  259. });
  260. }
  261. }