123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package controllers
- import (
- "cc-officialweb/models"
- "cc-officialweb/service"
- "cc-officialweb/unity"
- "cc-officialweb/utils"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/go-playground/validator/v10"
- "strconv"
- )
- type ProductController struct {
- beego.Controller
- }
- // Get 产品详情页
- func (s *ProductController) Get() {
- var success []string
- ptype := s.GetString("ptype")
- product := service.GetProduct(ptype)
- //获取成功案例轮播图
- exams := service.GetResource("example")
- for _, v := range exams {
- success = append(success, v.Url)
- }
- //数据大屏展示
- data, err := service.GetData(false)
- if err == nil {
- s.Data["Datas"] = data
- } else {
- s.Data["Datas"] = "未获得数据"
- }
- productType := service.GetProductType()
- if len(productType) == 0 {
- s.Data["productFirst"] = "暂无产品分类"
- } else {
- s.Data["productFirst"] = productType[0].Name
- }
- s.Data["productType"] = productType
- s.Data["Products"] = product
- s.Data["example"] = success
- s.TplName = "product.html"
- }
- // AddProduct 添加产品
- func (s *ProductController) AddProduct() {
- var product models.ProductDto
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &product)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- if service.AddProduct(product) {
- s.Data["json"] = &JSON{Code: 200, Msg: "添加成功"}
- s.ServeJSON()
- } else {
- s.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
- s.ServeJSON()
- return
- }
- }
- // AddProductType 添加产品分类
- func (s *ProductController) AddProductType() {
- var productType models.ProductType
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &productType)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- if service.AddProductType(productType) {
- s.Data["json"] = &JSON{Code: 200, Msg: "添加成功"}
- s.ServeJSON()
- } else {
- s.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
- s.ServeJSON()
- return
- }
- }
- // GetProductType 获取产品类型
- func (s *ProductController) GetProductType() {
- productType := service.GetProductType()
- s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: productType}
- s.ServeJSON()
- }
- // DeleteProductTypeById 根据id删除产品类型
- func (s *ProductController) DeleteProductTypeById() {
- id := s.GetString("id")
- atoi, _ := strconv.Atoi(id)
- validate := validator.New()
- err2 := validate.Var(atoi, "required")
- if err2 != nil {
- s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
- s.ServeJSON()
- return
- }
- _, err := unity.DeleteById(atoi, models.ProductType{})
- if err != nil {
- s.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
- s.ServeJSON()
- }
- }
- // GetAllProductType 分页获取产品类型
- func (s *ProductController) GetAllProductType() {
- var page unity.PageParams
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &page)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
- s.ServeJSON()
- return
- }
- result, total, current, err := unity.Paginate(page, models.ProductType{})
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
- Current: current,
- Size: total,
- Data: result,
- }}
- s.ServeJSON()
- return
- }
- }
- // UpdateProductType 根据id修改产品类型
- func (s *ProductController) UpdateProductType() {
- var productType models.ProductType
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &productType)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- _, err = unity.UpdateById(productType.ID, productType)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "修改失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
- s.ServeJSON()
- return
- }
- }
- // DeleteProductById 根据id删除产品
- func (s *ProductController) DeleteProductById() {
- id := s.GetString("id")
- atoi, _ := strconv.Atoi(id)
- validate := validator.New()
- err2 := validate.Var(atoi, "required")
- if err2 != nil {
- s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
- s.ServeJSON()
- return
- }
- _, err := unity.DeleteById(atoi, models.Products{})
- if err != nil {
- s.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
- s.ServeJSON()
- }
- }
- // GetAllProduct 获取所有产品信息
- func (s *ProductController) GetAllProduct() {
- var page unity.PageParams
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &page)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
- s.ServeJSON()
- return
- }
- result, total, current, err := unity.Paginate(page, models.Products{})
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
- Current: current,
- Size: total,
- Data: result,
- }}
- s.ServeJSON()
- return
- }
- }
- // UpdateProductById 根据id进行修改产品
- func (s *ProductController) UpdateProductById() {
- var productDto models.ProductDto
- var product models.Products
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &productDto)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- product.Title = &productDto.Title //标题
- product.Synopsis = &productDto.Synopsis //简介
- product.Detail = &productDto.Detail //详情
- product.Type = &productDto.Type //类型 1.产品 2.服务
- product.Ptype = &productDto.Ptype //产品类型 软件 硬件
- product.IsIndex = &productDto.IsIndex //是否首页显示
- product.Url = &productDto.Url //图片链接
- product.ProductIntroduction = &productDto.ProductIntroduction //产品介绍
- product.TechnicalParameters = &productDto.TechnicalParameters //技术参数
- product.Instructions = &productDto.Instructions //使用说明
- product.SupportingSoftware = &productDto.SupportingSoftware //配套软件
- product.OptionalAccessories = &productDto.OptionalAccessories //可选配件
- product.IsActive = &productDto.IsActive
- _, err = unity.UpdateById(productDto.ID, product)
- if err != nil {
- s.Data["json"] = &JSON{Code: 102, Msg: err.Error()}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
- s.ServeJSON()
- }
- }
- // UpdateIsIndex 根据id修改状态
- func (s *ProductController) UpdateIsIndex() {
- var productDto models.ProductDto
- var product models.Products
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &productDto)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- tx := utils.DB.Model(&product).Where("id = ?", productDto.ID).Update("is_index", productDto.IsIndex)
- if tx.RowsAffected > 0 {
- s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 102, Msg: "修改失败"}
- s.ServeJSON()
- }
- }
- // UpdateIsSort 根据id修改排序
- func (s *ProductController) UpdateIsSort() {
- var productDto models.ProductDto
- var product models.Products
- err := json.Unmarshal(s.Ctx.Input.RequestBody, &productDto)
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- s.ServeJSON()
- return
- }
- tx := utils.DB.Model(&product).Where("id = ?", productDto.ID).Update("sort", productDto.Sort)
- if tx.RowsAffected > 0 {
- s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 102, Msg: "修改失败"}
- s.ServeJSON()
- }
- }
- // GetProduct 根据id获取产品信息
- func (s *ProductController) GetProduct() {
- getString := s.GetString("id")
- id, _ := strconv.Atoi(getString)
- validate := validator.New()
- err2 := validate.Var(id, "required")
- if err2 != nil {
- s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
- s.ServeJSON()
- return
- }
- result, err := unity.GetById(id, &models.Products{})
- if err != nil {
- s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
- s.ServeJSON()
- return
- } else {
- s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
- s.ServeJSON()
- return
- }
- }
|