Пример #1
0
//后台首页
func (this Controller) Index(w http.ResponseWriter, r *http.Request) {
	sess, _ := globalSessions.SessionStart(w, r)
	defer sess.SessionRelease(w)
	if r.Method == "GET" {
		if sess.Get("adminName") == nil || sess.Get("adminId") == nil {
			http.Redirect(w, r, "/admin/login", http.StatusFound)
		}

		stdinfo := db.NewModel("info")

		tmp, err := stdinfo.FindAll()
		if err != nil {
			unitl.CheckErr(err)
		}
		//fmt.Println(stdinfos)

		//var stdInfos []interface{}

		/*for _, v := range tmp {
			stdInfos = append(stdInfos, v)
		}*/
		//fmt.Println(stdInfos)
		//data := make(map[string]interface{})

		//stdinfo2 := make(map[string]interface{})
		//stdinfo2["info"] = stdinfos
		//data["info"] = stdInfos

		unitl.ReadHtml(w, "admin/index", tmp)
	}
}
Пример #2
0
//NewModel工厂模式实例化一个数据库连接
func NewModel(tbname string) *model {

	if len(tbname) < 0 {
		err := errors.New("Sorry,you must select a table")
		unitl.CheckErr(err)
	}
	sql := "select * from " + strings.TrimSpace(tbname)
	rows, err := globalDB.Query(sql)
	if err != nil {
		unitl.CheckErr(err)
	}
	defer rows.Close()
	fields, err := rows.Columns()
	if err != nil {
		unitl.CheckErr(err)
	}
	return &model{tbname: tbname, fields: fields}
}
Пример #3
0
func init() {
	if globalDB != nil {
		return
	}
	var err error
	globalDB, err = sql.Open("mysql", "root:@/web_frame?charset=utf8")
	if err != nil {
		unitl.CheckErr(err)
	}
}
Пример #4
0
//插入数据
func (m *model) Add(d data) error {
	var sql string           //sql语句
	var slices []interface{} //存入数据库中的数据(格式化)

	//拼接sql语句和数据装载
	for i := 1; i < len(m.fields); i++ {
		slices = append(slices, d[m.fields[i]])
		sql += m.fields[i] + "=?, "
	}
	sql = "INSERT " + m.tbname + " SET " + sql
	sql = sql[:len(sql)-2]

	stmt, err := globalDB.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		unitl.CheckErr(err)
	}
	//fmt.Println(slices)
	_, err = stmt.Exec(slices...) //插入数据
	return err
}
Пример #5
0
//Index 首页
func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {

	//展示首页
	if r.Method == "GET" {
		//获取客户端ip与天气信息
		var data = make(map[string]interface{}) //数据存储变量(天气,学校列表)
		s := r.RemoteAddr
		ip := s[0 : len(s)-6]
		if ip == "127.0.0.1" {
			data["err"] = "获取天气失败"
		} else {
			city, _ := unitl.GetCity(ip)
			data, _ = unitl.GetWeather(city)
			data["and"] = "~"
		}

		//获取学校
		var schoolInfo []interface{}
		school := db.NewModel("school")
		defer school.Close()

		tmp, err := school.FindAll()
		if err != nil {
			unitl.CheckErr(err)
		}
		for _, v := range tmp {
			schoolInfo = append(schoolInfo, v)
		}
		data["school"] = schoolInfo

		//加载首页魔板
		if err := unitl.ReadHtml(w, "index", data); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	} else {
		unitl.ReadHtml(w, "404", nil)
	}

}