| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 | package controllersimport (	"encoding/json"	"fmt"	"net/http"	"net/url"	"os"	"path"	"strconv"	"strings"	"time"	"asrs3/models/drawing"	"asrs3/models/smsMgr"	"asrs3/models/userMgr"	"github.com/beego/beego/v2/server/web")type MainController struct {	web.Controller}func (c *MainController) Get() {	_, ok := c.GetSessionUser()	if !ok {		c.TplName = "login.tpl"		return	}	c.TplName = "index.tpl"}// Post is Login methodfunc (c *MainController) Post() {	v, err := c.Input()	if err != nil {		return	}	// 登录	if v.Has("login") {		b, ok := userMgr.Get(v.Get("phone"), v.Get("password"))		if !ok {			c.SendResult("failed", "wrong_password")			return		}		c.SetSessionUser(b.Phone)		c.SendResult("success", nil)		return	}	// 注册	if v.Has("register") {		c.register(v)	}}func (c *MainController) Logout() {	_ = c.DestroySession()	c.Redirect("/", 302)}func (c *MainController) ResetPwd() {	password := c.GetString("password")	if password == "" {		c.SendResult("failed", "password_error")		return	}	phone := c.GetString("phone")	if password == "" {		c.SendResult("failed", "phone_error")		return	}	code, err := c.GetInt64("code")	if err != nil {		c.SendResult("failed", "code_error")		return	}	vCode, ok := smsMgr.GetCode(phone)	if !ok {		c.SendResult("failed", "send_code_first")		return	}	if code != vCode {		c.SendResult("failed", "code_error")		return	}	if err = userMgr.ResetPwd(phone, password); err != nil {		c.SendResult("failed", err.Error())		return	}	c.SetSessionUser(phone)	c.SendResult("success", nil)}func (c *MainController) GetUserInfo() {	b, ok := c.GetSessionUser()	if !ok {		c.SendJson(make(map[string]interface{}))		return	}	c.SendJson(&b)}func (c *MainController) Tutorial0() {	c.empty()}func (c *MainController) DownloadPDF() {	c.empty()}// 检查文档是否有重名的,包含当前文件名func (c *MainController) DocumentNameOverlapCheck() {	u, ok := c.GetSessionUser()	if !ok {		return	}	list, err := drawing.GetList(u.Phone)	if err != nil {		c.Ctx.WriteString(`[]`)		return	}	if len(list) == 0 {		c.Ctx.WriteString(`[]`)		return	}	body, err := json.Marshal(list)	if err != nil {		return	}	c.Ctx.WriteString(string(body))}// GetProjectList 加载已存在的设计列表func (c *MainController) GetProjectList() {	c.DocumentNameOverlapCheck()}// Load 加载已存在的设计func (c *MainController) Load() {	u, ok := c.GetSessionUser()	if !ok {		return	}	name := c.GetString("document_name")	if d, err := drawing.GetMapFormName(u.Phone, name); err == nil {		// 网页端的 content-type:text/html; charset=UTF-8 因此需要发送字符串而非 json		c.Ctx.WriteString(d)		return	}	c.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound)}func (c *MainController) Delete() {	u, ok := c.GetSessionUser()	if !ok {		return	}	name := c.GetString("document_name")	ret := map[string]string{"status": "success"}	if err := drawing.Delete(u.Phone, name); err != nil {		ret = map[string]string{"status": "failed"}	}	c.SendJson(ret)}func (c *MainController) Save() {	c.ParseSaveBody()	c.SendJson(map[string]string{"status": "success"})}func (c *MainController) SaveBehavior() {	c.SendJson(map[string]string{"status": "success"})}func (c *MainController) GetSimulationList() {	c.empty()}func (c *MainController) GetPriceFromExcel() {	jsonStr := `{"racking":{"qty":216,"val":36710},"xtrack":{"qty":36,"val":28070},"lift":{"qty":3,"val":248220},"carrier":{"qty":4,"val":286400},"wifi":{"qty":2,"val":2540},"data_control":{"qty":1,"val":32110},"software_implementation":{"qty":1,"val":37200},"central_panel":{"qty":1,"val":13070},"total_excluding":{"qty":-1,"val":684320}}`	var jsonMap map[string]interface{}	if err := json.Unmarshal([]byte(jsonStr), &jsonMap); err == nil {		c.Data["json"] = jsonMap	}	_ = c.ServeJSON()}func (c *MainController) empty() {	c.Ctx.WriteString("")	return}func (c *MainController) ParseSaveBody() {	u, ok := c.GetSessionUser()	if !ok {		return	}		var data Data		data.DocumentName = c.GetString("document_name")	if data.DocumentName == "" {		return	}		extraPrice := c.GetString("extraPrice")	if err := json.Unmarshal([]byte(extraPrice), &data.ExtraPrice); err != nil {		fmt.Println(err)		return	}		icubeData := c.GetString("icubeData")	if err := json.Unmarshal([]byte(icubeData), &data.IcubeData); err != nil {		fmt.Println(err)		return	}		itemMData := c.GetString("itemMData")	if err := json.Unmarshal([]byte(itemMData), &data.ItemMData); err != nil {		fmt.Println(err)		return	}		layoutMap := c.GetString("layoutMap")	if err := json.Unmarshal([]byte(layoutMap), &data.LayoutMap); err != nil {		fmt.Println(err)		return	}		measurements := c.GetString("measurements")	if err := json.Unmarshal([]byte(measurements), &data.Measurements); err != nil {		fmt.Println(err)		return	}		unit, err := c.GetInt64("unit_measurement")	if err != nil {		fmt.Println(err)		return	}	data.UnitMeasurement = unit		warehouseDimensions := c.GetString("warehouse_dimensions")	if err := json.Unmarshal([]byte(warehouseDimensions), &data.WarehouseDimensions); err != nil {		fmt.Println(err)		return	}		body, err := json.Marshal(data)	if err != nil {		fmt.Println(err)		return	}	if err := drawing.SaveMap(u.Phone, data.DocumentName, body); err != nil {		fmt.Println(err)		return	}	if err := drawing.UpdateList(u.Phone, data.DocumentName); err != nil {		fmt.Println(err)		return	}}func (c *MainController) UploadItem() {	u, ok := c.GetSessionUser()	if !ok {		return	}		f, h, e := c.GetFile("fileUpload")		if e != nil {		c.Ctx.WriteString("{error}")		return	}	f.Close()	saveDir := fmt.Sprintf("data/maps/")	err := os.MkdirAll(saveDir, 0777)	if err != nil {		c.Ctx.WriteString("upload_error_create_dir")		return	}	c.SaveToFile("fileUpload", saveDir+h.Filename)	// 存入list.json中	var fileSuffix string	fileSuffix = path.Ext(h.Filename)	var filename = strings.TrimSuffix(h.Filename, fileSuffix)	if err := drawing.UpdateList(u.Phone, filename); err != nil {		fmt.Println(err)		return	}	// 加载	/*if d, err := drawing.GetMapFormName(filename); err == nil {		c.Ctx.WriteString(d)		return	}*/	c.Redirect("/", 302)	c.Ctx.ResponseWriter.WriteHeader(http.StatusOK)	}func (c *MainController) register(val url.Values) {	f := []string{"name", "email", "password", "phone", "code"}	for i := 0; i < len(f); i++ {		if v := val.Get(f[i]); v == "" {			c.SendResult("failed", f[i]+"_error")			return		}	}	name := strings.TrimSpace(val.Get("name"))	company := val.Get("company")	email := val.Get("email")	password := val.Get("password")	phone := val.Get("phone")	code, err := strconv.ParseInt(val.Get("code"), 10, 64)	if err != nil {		c.SendResult("failed", "code_error")		return	}	vCode, ok := smsMgr.GetCode(phone)	if !ok {		c.SendResult("failed", "send_code_first")		return	}	if code != vCode {		c.SendResult("failed", "code_error")		return	}	b, err := userMgr.New(name, email, password, phone, company)	if err != nil {		c.SendResult("failed", err.Error())		return	}	c.SetSessionUser(b.Phone)	c.SendResult("success", nil)}func (c *MainController) SendResult(ret string, result interface{}) {	c.Data["json"] = map[string]interface{}{"ret": ret, "result": result}	_ = c.ServeJSON()}func (c *MainController) SendJson(j interface{}) {	c.Data["json"] = j	_ = c.ServeJSON()}func (c *MainController) GetSessionUser() (*userMgr.Body, bool) {	phone, ok := c.GetSession("identity").(string)	if !ok {		return nil, false	}	return userMgr.Is(phone)}func (c *MainController) SetSessionUser(phone string) {	_ = c.SetSession("identity", phone)	_ = c.SetSession("remember_code", time.Now().String())}func (c *MainController) SendCode() {	phone := strings.TrimSpace(c.GetString("phone"))	err := smsMgr.SendCode(phone)	if err != nil {		c.SendResult("failed", err)		return	}	c.SendResult("success", nil)}func (c *MainController) CreateBehavior() {	type behavior struct {		DocumentName string `json:"document_name"`		SavedTime    string `json:"saved_time"`		DocumentData string `json:"documentData"`		IcubeData    string `json:"icubeData"`	}	s := `[{"document_name":"0","saved_time":"%s","documentData":"{\"warehouse_dimensions\":\"[15,15,10]\",\"itemMData\":\"[]\",\"unit_measurement\":\"0\",\"layoutMap\":\"{\\\"url\\\":\\\"\\\",\\\"scale\\\":1,\\\"uOffset\\\":0,\\\"vOffset\\\":0}\",\"extraInfo\":\"\\\"{}\\\"\",\"extraPrice\":\"[]\",\"measurements\":\"[]\",\"custom_values\":\"[]\"}","icubeData":"null"}]`	s = fmt.Sprintf(s, time.Now().Format("2006-01-02 15:01:05"))	var bs []behavior	if err := json.Unmarshal([]byte(s), &bs); err != nil {		c.SendJson([]interface{}{})		return	}	c.SendJson(bs)}func (c *MainController) GetRevisions() {	c.empty()}
 |