123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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"] = "未获得数据"
- }
- 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
- }
- }
- // 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()
- }
- }
- // 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
- }
- }
|