goods.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. let keywords = await this.model('search_history').add({
  116. keyword: keyword,
  117. user_id: 1,
  118. add_time: parseInt(new Date().getTime() / 1000)
  119. });
  120. }
  121. if (!think.isEmpty(brandId)) {
  122. whereMap.brand_id = brandId;
  123. }
  124. //排序
  125. let orderMap = {};
  126. if (sort === 'price') {
  127. //按价格
  128. orderMap = {
  129. retail_price: order
  130. };
  131. } else {
  132. //按商品添加时间
  133. orderMap = {
  134. id: 'desc'
  135. };
  136. }
  137. //筛选的分类
  138. let filterCategory = [{
  139. 'id': 0,
  140. 'name': '全部',
  141. 'checked': false
  142. }];
  143. let categoryIds = await goodsQuery.where(whereMap).getField('category_id', 10000);
  144. if (!think.isEmpty(categoryIds)) {
  145. //查找二级分类的parent_id
  146. let parentIds = await this.model('category').where({ id: { 'in': categoryIds } }).getField('parent_id', 10000);
  147. //一级分类
  148. let parentCategory = await this.model('category').field(['id', 'name']).order({ 'sort_order': 'asc' }).where({ 'id': { 'in': parentIds } }).select();
  149. if (!think.isEmpty(parentCategory)) {
  150. filterCategory = filterCategory.concat(parentCategory);
  151. }
  152. }
  153. //加入分类条件
  154. if (!think.isEmpty(categoryId) && parseInt(categoryId) > 0) {
  155. whereMap.category_id = ['in', await this.model('category').getCategoryWhereIn(categoryId)];
  156. }
  157. console.log(whereMap);
  158. //搜索到的商品
  159. let goodsData = await goodsQuery.where(whereMap).field(['id', 'name', 'list_pic_url', 'retail_price']).order(orderMap).page(page, size).countSelect();
  160. goodsData.filterCategory = filterCategory.map(function (v) {
  161. if ((think.isEmpty(categoryId) && v.id === 0) || v.id === parseInt(categoryId)) {
  162. v.checked = true;
  163. } else {
  164. v.checked = false;
  165. }
  166. return v;
  167. });
  168. goodsData.goodsList = goodsData.data;
  169. return this.success(goodsData);
  170. }
  171. /**
  172. * 商品列表筛选的分类列表
  173. * @returns {Promise.<Promise|void|PreventPromise>}
  174. */
  175. async filterAction() {
  176. let categoryId = this.get('categoryId');
  177. let keyword = this.get('keyword');
  178. let isNew = this.get('isNew');
  179. let isHot = this.get('isHot');
  180. let goodsQuery = this.model('goods');
  181. if (!think.isEmpty(categoryId)) {
  182. goodsQuery.where({ category_id: { 'in': await this.model('category').getChildCategoryId(categoryId) } });
  183. }
  184. if (!think.isEmpty(isNew)) {
  185. goodsQuery.where({ is_new: isNew });
  186. }
  187. if (!think.isEmpty(isHot)) {
  188. goodsQuery.where({ is_hot: isHot });
  189. }
  190. if (!think.isEmpty(keyword)) {
  191. goodsQuery.where({ name: { 'like': `%${keyword}%` } });
  192. }
  193. let filterCategory = [{
  194. 'id': 0,
  195. 'name': '全部'
  196. }];
  197. //二级分类id
  198. let categoryIds = await goodsQuery.getField('category_id', 10000);
  199. if (!think.isEmpty(categoryIds)) {
  200. //查找二级分类的parent_id
  201. let parentIds = await this.model('category').where({ id: { 'in': categoryIds } }).getField('parent_id', 10000);
  202. //一级分类
  203. let parentCategory = await this.model('category').field(['id', 'name']).order({ 'sort_order': 'asc' }).where({ 'id': { 'in': parentIds } }).select();
  204. if (!think.isEmpty(parentCategory)) {
  205. filterCategory = filterCategory.concat(parentCategory);
  206. }
  207. }
  208. return this.success(filterCategory);
  209. }
  210. /**
  211. * 新品首发
  212. * @returns {Promise.<Promise|void|PreventPromise>}
  213. */
  214. async newAction() {
  215. return this.success({
  216. bannerInfo: {
  217. url: '',
  218. name: '坚持初心,为你寻觅世间好物',
  219. img_url: 'http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png',
  220. }
  221. });
  222. }
  223. /**
  224. * 人气推荐
  225. * @returns {Promise.<Promise|void|PreventPromise>}
  226. */
  227. async hotAction() {
  228. return this.success({
  229. bannerInfo: {
  230. url: '',
  231. name: '大家都在买的严选好物',
  232. img_url: 'http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png',
  233. }
  234. });
  235. }
  236. /**
  237. * 商品详情页的大家都在看的商品
  238. * @returns {Promise.<Promise|PreventPromise|void>}
  239. */
  240. async relatedAction() {
  241. //大家都在看商品,取出关联表的商品,如果没有则随机取同分类下的商品
  242. const model = this.model('goods');
  243. const goodsId = this.get('id');
  244. let relatedGoodsIds = await this.model('related_goods').where({ goods_id: goodsId }).getField('related_goods_id');
  245. let relatedGoods = null;
  246. if (think.isEmpty(relatedGoodsIds)) {
  247. //查找同分类下的商品
  248. let goodsCategory = await model.where({ id: goodsId }).find();
  249. relatedGoods = await model.where({ category_id: goodsCategory.category_id }).field(['id', 'name', 'list_pic_url', 'retail_price']).limit(8).select();
  250. } else {
  251. relatedGoods = await model.where({ id: ['IN', relatedGoodsIds] }).field(['id', 'name', 'list_pic_url', 'retail_price']).select();
  252. }
  253. return this.success({
  254. goodsList: relatedGoods,
  255. });
  256. }
  257. /**
  258. * 在售的商品总数
  259. * @returns {Promise.<Promise|PreventPromise|void>}
  260. */
  261. async countAction() {
  262. let goodsCount = await this.model('goods').where({ is_delete: 0, is_on_sale: 1 }).count('id');
  263. return this.success({
  264. goodsCount: goodsCount,
  265. });
  266. }
  267. }