Golang实战项目-B2C电商平台项目

Posted 旧时星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang实战项目-B2C电商平台项目相关的知识,希望对你有一定的参考价值。

Golang实战项目-B2C电商平台项目(5)

实现商品上架

  • 本质是修改tb_item表中status=1
  • 之前在完成商品删除时已经在TbItemDao.go中编写了修改status值的函数,直接复用即可
  • 根据页面中内容,客户端给服务端发起请求后要求服务器端返回数据已经是EgoResult对应的json数据

在TbItemService中添加函数实现商品上架

//商品上架
func instockService(ids string) (e commons.EgoResult){
	count:=updStatusByIdsDao(strings.Split(ids,","),1)
	if count>0{
		e.Status=200
	}
	return
}

在TbItemController中添加函数,并修改Itemhandler()函数中内容

//商品上架
func instockController(w http.ResponseWriter, r *http.Request) {
	ids:=r.FormValue("ids")
	er:=instockService(ids)
	b,_:=json.Marshal(er)
	w.Header().Set("Content-Type","application/json;charset=utf-8")
	w.Write(b)
}
func ItemHandler() {
	commons.Router.HandleFunc("/showItem", showItemController)
	commons.Router.HandleFunc("/item/delete", delByIdsController)
	commons.Router.HandleFunc("/item/instock", instockController)
}

实现商品下架

  • 商品下架的本质是修改tb_item表中的status=2

在TbItemService.go中添加函数

//商品下架
func offStockService(ids string) (e commons.EgoResult){
	count:=updStatusByIdsDao(strings.Split(ids,","),2)
	if count>0{
		e.Status=200
	}
	return
}

在TbItemController.go中添加函数,并修改ItemController()函数内容

//商品下架
func offstockController(w http.ResponseWriter,r *http.Request){
	ids:=r.FormValue("ids")
	er:=offStockService(ids)
	b,_:=json.Marshal(er)
	w.Header().Set("Content-Type","application/json;charset=utf-8")
	w.Write(b)
}

新增商品之商品类目显示

新增商品是将需要选择该商品对应的类目即将该商品进行区分,类目区分由tree组件显示,即以树图的形式,每个节点代表一种商品类目

  • EasyUI Tree组件中每个节点属性如下,必须为这些名称,否则无法识别,所以需要在commons/EasyUI.go中新建一个结构体表示tree的节点
id:绑定节点的标识值。
text:显示的节点文本。
iconCls:显示的节点图标CSS类ID。
checked:该节点是否被选中。
state:节点状态,'open''closed'。
attributes:绑定该节点的自定义属性。
target:目标DOM对象。
  • Tree的state表示节点状态,如果节点是closed状态,节点将显示成文件夹的形式,可以打开这个节点,开始时EasyUI默认向服务器端发送当前节点的id.如果节点的状态是closed节点表示叶子节点,没有子节点
//tree
type EasyUITree struct {
	Id int `json:"id"`
	Text string `json:"text"`
	State string `json:"state"`
}
  • 在/item/cat/TbItemCatDao.go中添加新函数
/*
根据parent_id查询所有子类目
 */
func selByPid(pid int) (c []TbItemCat){
	rows,err:=commons.Dql("select * from tb_item_cat where parent_id=?",pid)
	if err!=nil{
		fmt.Println(err)
		return nil
	}
	c = make([]TbItemCat,0)
	for rows.Next()  {
		var t TbItemCat
		rows.Scan(&t.Id,&t.ParentId,&t.Name,&t.Status,&t.SortOrder,&t.IsParent,&t.Created,&t.Updated)
		c= append(c,t)
	}
	commons.CloseConn()
	return
}
  • 修改/item/cat/TbItemCat.go中结构体如下
//商品类目
type TbItemCat struct {
	Id        int
	ParentId  int
	Name      string
	Status    byte
	SortOrder int8
	IsParent  bool//此处由byte修改为bool
	Created   string
	Updated   string
}
  • 在/item/cat下新建TbItemCatController.go
import (
	"net/http"
	"strconv"
	"encoding/json"
	"commons"
)

func ItemCatHandler(){
	commons.Router.HandleFunc("/item/cat/show",showItemCatController)
}

func showItemCatController(w http.ResponseWriter,r *http.Request){
	id:=r.FormValue("id")
	if id==""{
		id="0"
	}
	idInt,_:=strconv.Atoi(id)
	t:=showCatByPidService(idInt)
	b,_:=json.Marshal(t)
	w.Header().Set("Content-type","application/json;charset=utf-8")
	w.Write(b)
}
  • 在main.go中添加ItemCatHandler()函数的引用
import (
	"net/http"
	"html/template"
	"user"
	"commons"
	"github.com/gorilla/mux"
	"item"
	"item/cat"
)

//显示登录页面
func welcome(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("view/login.html")
	t.Execute(w, nil)
}

//restful显示页面
func showPage(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	t, _ := template.ParseFiles("view/" + vars["page"] + ".html")
	t.Execute(w, nil)
}
func main() {
	commons.Router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	commons.Router.HandleFunc("/", welcome)
	//满足/page/{page}格式的处理
	commons.Router.HandleFunc("/page/{page}", showPage)
	//用户
	user.UserHandler()
	//商品
	item.ItemHandler()
	//商品类目
	cat.ItemCatHandler()
	http.ListenAndServe(":80", commons.Router)
}

以上是关于Golang实战项目-B2C电商平台项目的主要内容,如果未能解决你的问题,请参考以下文章

Golang实战项目-B2C电商平台

Golang实战项目-B2C电商平台项目

Golang实战项目-B2C电商平台项目

Golang实战项目-B2C电商平台项目

Golang实战项目-B2C电商平台项目

Golang实战项目-B2C电商平台