第194天学习打卡(项目 谷粒商城 36 新增商品 获取分类关联的平台)
Posted doudoutj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第194天学习打卡(项目 谷粒商城 36 新增商品 获取分类关联的平台)相关的知识,希望对你有一定的参考价值。
新增商品 获取分类关联的品牌
查看页面遇到的错误
TypeError: Cannot read property 'publish' of undefined
解决办法
关于pubsub、publish报错,无法发送查询品牌信息的请求:
1、npm install --save pubsub-js
2、在src下的main.js中引用:
import PubSub from 'pubsub-js'
Vue.prototype.PubSub = PubSub
DONE Compiled successfully in 31175ms 10:15:19 AM
I Your application is running here: http://localhost:8001
终止批处理操作吗(Y/N)? y
PS C:\\Users\\HP\\Desktop\\renren-fast-vue> npm install --save pubsub-js
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
+ pubsub-js@1.9.3
added 1 package from 1 contributor in 19.808s
13 packages are looking for funding
run `npm fund` for details
main.js
import Vue from 'vue'
import App from '@/App'
import router from '@/router' // api: https://github.com/vuejs/vue-router
import store from '@/store' // api: https://github.com/vuejs/vuex
import VueCookie from 'vue-cookie' // api: https://github.com/alfhen/vue-cookie
import '@/element-ui' // api: https://github.com/ElemeFE/element
import '@/icons' // api: http://www.iconfont.cn/
import '@/element-ui-theme'
import '@/assets/scss/index.scss'
import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
import { isAuth } from '@/utils'
import cloneDeep from 'lodash/cloneDeep'
import PubSub from 'pubsub-js'
Vue.prototype.PubSub = PubSub
Vue.use(VueCookie)
Vue.config.productionTip = false
// 非生产环境, 适配mockjs模拟数据 // api: https://github.com/nuysoft/Mock
if (process.env.NODE_ENV !== 'production') {
require('@/mock')
}
// 挂载全局
Vue.prototype.$http = httpRequest // ajax请求方法
Vue.prototype.isAuth = isAuth // 权限方法
// 保存整站vuex本地储存初始状态
window.SITE_CONFIG['storeState'] = cloneDeep(store.state)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
gulimall.product.vo
BrandVo.java
package com.doudou.gulimall.product.vo;
import lombok.Data;
@Data
public class BrandVo {
/**
* "brandId":0
* "brandName": "string"
*/
private Long brandId;
private String brandName;
}
gulimall.product.controller
CategoryBrandRelationController.java
package com.doudou.gulimall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudou.gulimall.product.entity.BrandEntity;
import com.doudou.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.doudou.gulimall.product.entity.CategoryBrandRelationEntity;
import com.doudou.gulimall.product.service.CategoryBrandRelationService;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.R;
/**
* 品牌分类关联
*
* @author doudoutj111
*
* @date 2021-06-19 19:40:52
*/
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
/**
* 获取当前品牌关联的所有分类列表
*/
//@RequestMapping(value = "/catelog/list",method = RequestMethod.GET)
@GetMapping("/catelog/list")
//@RequiresPermissions("product:categorybrandrelation:list")
public R cateloglist(@RequestParam("brandId")Long brandId){
List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
return R.ok().put("data", data);
}
/**
* /product/categorybrandrelation/brands/list
* 关联的所有品牌
* 1.Controller:处理请求,接受和校验数据
* 2.Service接受controller传来的数据,进行业务处理
* 3.Controller接受Service处理完的数据,封装页面指定的vo
*/
@GetMapping("/brands/list")
public R relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){
List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
List<BrandVo> collect = vos.stream().map(item -> {
BrandVo brandVo = new BrandVo();
brandVo.setBrandId(item.getBrandId());
brandVo.setBrandName(item.getName());
return brandVo;
}).collect(Collectors.toList());
return R.ok().put("data",collect);
}
/**
* 列表
*/
@RequestMapping("/list")
//@RequiresPermissions("product:categorybrandrelation:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = categoryBrandRelationService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
// @RequiresPermissions("product:categorybrandrelation:info")
public R info(@PathVariable("id") Long id){
CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
return R.ok().put("categoryBrandRelation", categoryBrandRelation);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("product:categorybrandrelation:save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("product:categorybrandrelation:update")
public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.updateById(categoryBrandRelation);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("product:categorybrandrelation:delete")
public R delete(@RequestBody Long[] ids){
categoryBrandRelationService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}
package com.doudou.gulimall.product.service.impl;
CategoryBrandRelationServiceImpl.java
package com.doudou.gulimall.product.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doudou.gulimall.product.dao.BrandDao;
import com.doudou.gulimall.product.dao.CategoryDao;
import com.doudou.gulimall.product.entity.BrandEntity;
import com.doudou.gulimall.product.entity.CategoryEntity;
import com.doudou.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.CategoryBrandRelationDao;
import com.doudou.gulimall.product.entity.CategoryBrandRelationEntity;
import com.doudou.gulimall.product.service.CategoryBrandRelationService;
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Autowired
BrandDao brandDao;
@Autowired
CategoryDao categoryDao;
@Autowired
CategoryBrandRelationDao relationDao;
@Autowired
BrandService brandService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryBrandRelationEntity> page = this.page(
new Query<CategoryBrandRelationEntity>().getPage(params),
new QueryWrapper<CategoryBrandRelationEntity>()
);
return new PageUtils(page);
}
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
Long brandId = categoryBrandRelation.getBrandId();
Long catelogId = categoryBrandRelation.getCatelogId();
//1.查询详细名字
BrandEntity brandEntity = brandDao.selectById(brandId);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
categoryBrandRelation.setBrandName(brandEntity.getName());
categoryBrandRelation.setCatelogName(categoryEntity.getName());
this.save(categoryBrandRelation);
}
@Override
public void updateBrand(Long brandId, String name) {
CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
relationEntity.setBrandId(brandId);
relationEntity.setBrandName(name);
this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
}
@Override
public void updateCategory(Long catId, String name) {
this.baseMapper.updateCategory(catId,name);
}
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
.eq("catelog_id", catId));
List<BrandEntity> collect = catelogId.stream().map(item -> {
Long brandId = item.getBrandId();
BrandEntity byId = brandService.getById(brandId);
return byId;
}).collect(Collectors.toList());
return collect ;
}
}
这里要进行关联,不然后面商品发布那里显示不了数据
错误信息
vue.esm.js?efeb:591 [Vue warn]: Duplicate keys detected: '21'. This may cause an update error.
以上是关于第194天学习打卡(项目 谷粒商城 36 新增商品 获取分类关联的平台)的主要内容,如果未能解决你的问题,请参考以下文章
第195天学习打卡(项目 谷粒商城 37新增商品 获取分类下所有属性及分组)
第176天学习打卡(项目 谷粒商城 18 API三级分类 删除效果细化 新增效果)
第192天学习打卡(项目 谷粒商城 34 新增分组和属性关联)
第188天学习打卡(项目 谷粒商城30 规格参数新增与VO 规格参数列表)