-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (97 loc) · 2.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"OrderMeal/model"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"time"
)
// 路由
func main() {
http.HandleFunc("/", showIndex)
http.HandleFunc("/nelab", showNelab)
http.HandleFunc("/save", saveOrder)
http.HandleFunc("/upd", cronUpdate)
http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("./templates"))))
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
//首页展示
func showIndex(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/index.html")
type data struct {
UserName map[int]string
MenuName map[int]string
ListOrder []model.OrderData
TotalMenus [5]int
ThreeOrderList []model.OrderData
GroupName string
}
orderModel := model.Order{}
userModel := model.User{}
list := orderModel.GetList("services")
GroupName := "services"
ShowData := data{userModel.GetUser(), userModel.GetMenu(), list, orderModel.GetSumTotal(list), orderModel.GetPreThre("services"), GroupName}
t.Execute(w, ShowData)
}
// showNelab 游戏点餐
func showNelab(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/index.html")
type data struct {
UserName map[int]string
MenuName map[int]string
ListOrder []model.OrderData
TotalMenus [5]int
ThreeOrderList []model.OrderData
GroupName string
}
orderModel := model.Order{}
userModel := model.User{}
list := orderModel.GetList("nelab")
GroupName := "nelab"
ShowData := data{userModel.GetNelabUser(), userModel.GetMenu(), list, orderModel.GetSumTotal(list), orderModel.GetPreThre("nelab"), GroupName}
t.Execute(w, ShowData)
}
//保存订单
func saveOrder(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
groupName := r.Form.Get("groupName")
orderModel := model.Order{}
userModel := model.User{}
var userList map[int]string
if groupName == "services" {
userList = userModel.GetUser()
} else {
userList = userModel.GetNelabUser()
}
menuList := userModel.GetMenu()
userId, err := strconv.Atoi(r.Form.Get("name"))
orderModel.CheckErr(err)
if userId == 0 {
fmt.Fprint(w, 3)
return
}
menuId, err := strconv.Atoi(r.Form.Get("menu"))
orderModel.CheckErr(err)
create_time := time.Now().Format("2006-01-02 15:04:05")
info := orderModel.GetInfoByUid(userId, groupName)
if info.Id > 0 {
orderModel.Update(info.Id, menuList[menuId], userId, menuId)
fmt.Fprint(w, 1)
return
} else {
orderModel.Insert(userList[userId], menuList[menuId], create_time, userId, menuId, groupName)
fmt.Fprint(w, 2)
return
}
}
//更新状态
func cronUpdate(w http.ResponseWriter, r *http.Request) {
orderModel := model.Order{}
affectNum := orderModel.UpdateStatus()
fmt.Fprint(w, affectNum)
}