Пример #1
0
//申请服务器列表
func (this *BusiData) ApplyServList() {
	beego.Info("ApplyServList ==> begin")
	worldSession := models.WorldOrm.NewSession()
	defer worldSession.Close()
	err := worldSession.Begin()
	if err != nil {
		beego.Error("开启事务失败:=>", err)
	}
	defer worldSession.Commit()
	var servList []models.ServList
	err = worldSession.Where("status='0' and serv_type='online'").Find(&servList)
	if err != nil {
		beego.Error("获取服务器列表失败=>", err)
		this.Response.Set("retCode", ERR_QUERY)
		return
	}
	busiInfo := gjson.New()
	var srvList []*gjson.Json
	for _, v := range servList {
		tempNode := gjson.New()
		tempNode.Set("servAddr", v.ServAddr)
		tempNode.Set("servPort", v.ServPort)
		tempNode.Set("servIndex", v.ServIndex)
		tempNode.Set("servDesc", v.ServDesc)
		srvList = append(srvList, tempNode)
	}
	busiInfo.Set("servList", srvList)
	this.Response.Set("busiInfo", busiInfo)
	this.Response.Set("retCode", RET_SUCC)
	beego.Info("ApplyServList ==> end")
	return
}
Пример #2
0
func (this *MainController) Post() {
	beego.Info("new business start  ...")
	var (
		transData    string
		w4Data       *libtool.ParamData
		req          *gjson.Json
		err          error
		bizApidCode  string
		bizMethod    string
		retMsg       string
		retCode      string
		newTransData string
	)
	//处理业务
	busiData := new(libtool.BusiData)
	busiData.Session = models.Orm.NewSession()
	defer busiData.Session.Close()
	//开启事务
	err = busiData.Session.Begin()
	if !libtool.CheckError(err, "开启事务失败") {
		beego.Error("开启事务失败: ", err)
		this.retData.Set("retCode", libtool.ERR_TRANS_BEGIN)
		goto BUSI_END
	}
	busiData.Response = gjson.New()
	//初始化返回数据节点
	this.retData = gjson.New()
	//获取交易数据
	transData = this.GetString("transData")
	if "" == transData {
		beego.Info("retCode: ", libtool.ERR_NO_DATA)
		this.retData.Set("retCode", libtool.ERR_NO_DATA)
		goto BUSI_END
	}
	beego.Info("transData: ", transData)

	//临时修改 将空格转换成加号
	newTransData = strings.Replace(transData, " ", "+", -1)
	w4Data = libtool.ParamTransData(newTransData)

	//检查数据报文长度
	if !libtool.CheckDataLen(w4Data) {
		beego.Error("报文长度不一致")
		this.retData.Set("retCode", libtool.ERR_DATA_LEN)
		goto BUSI_END
	}
	//数据JSON化
	req, err = gjson.NewJson([]byte(w4Data.TransData))
	if err != nil {
		beego.Error("解析报文错误: ", err)
		this.retData.Set("retCode", libtool.ERR_TRANS_DATA)
		goto BUSI_END
	}
	//设置业务请求数据
	busiData.Request = req
	beego.Info("busiREQ: ", libtool.InnerJsonToString(busiData.Request))
	bizApidCode, _ = busiData.Request.Get("bizApiCode").String()
	beego.Info("bizApiCode: ", bizApidCode)
	bizMethod = beego.AppConfig.String(bizApidCode)
	reflect.ValueOf(busiData).MethodByName(bizMethod).Call([]reflect.Value{})
	retMsg = libtool.InnerJsonToString(busiData.Response)
	busiData.Response, _ = gjson.NewJson([]byte(retMsg))
	//组织部分返回数据
	this.retData = busiData.Response
	//写返回报文头
	err = this.writeHeaderData(req)
	if err != nil {
		beego.Error("组织报文头错误: ", err)
		goto BUSI_END
	}
	retCode, err = busiData.Response.Get("retCode").String()
	if err != nil {
		beego.Error("biz err: 无返回码", err)
		this.retData.Set("retCode", libtool.ERR_NO_RETCODE)
		goto BUSI_END
	}
BUSI_END:
	retCode, _ = this.retData.Get("retCode").String()
	errorCode := new(models.ErrorCode)
	errorCode.ErrorId = retCode
	busiData.Session.Get(errorCode)
	this.retData.Set("retMsg", errorCode.ErrorDes)

	//提交事务&关闭数据库连接
	if retCode != libtool.RET_SUCC {
		busiData.Session.Rollback()
	} else {
		busiData.Session.Commit()
	}
	//数据写回
	this.writeBackData()
	beego.Info("new business end  ...")

	return
}
Пример #3
0
func (this *BusiData) SystemUpdate() {
	beego.Info("SystemUpdate ==> begin")
	curVer, _ := this.Request.Get("curVersion").String()
	if curVer == "" {
		beego.Error("[curVersion]版本号不能为空:=>", curVer)
		this.Response.Set("retCode", ERR_GET_FIELD)
		return
	}
	ncurVer, _ := strconv.ParseInt(curVer, 10, 64)
	//保存需要更新的版本目录
	var getDirList []string
	var allFileSize int64 = 0
	//切换目录
	os.Chdir("./updatePack")
	rdList, err := os.OpenFile("listVersion", os.O_RDONLY, os.ModePerm)
	if err != nil {
		beego.Error("file Name=>", "listVersion")
		beego.Error("open file failed=>", err)
	}
	defer rdList.Close()
	//读取需要升级的目录
	fsTmp := bufio.NewScanner(rdList)
	for fsTmp.Scan() {
		verTmp := fsTmp.Text()
		nverTmp, _ := strconv.ParseInt(verTmp, 10, 64)
		if ncurVer < nverTmp {
			getDirList = append(getDirList, verTmp)
		}
	}
	beego.Info("getDirList=>", getDirList)
	//如果getDirList为空则表示不需要升级
	gdLen := len(getDirList)
	if gdLen == 0 {
		this.Response.Set("retCode", RET_SUCC)
		return
	}
	var fileList []string
	busiInfo := gjson.New()
	busiInfo.Set("verInfo", getDirList[gdLen-1])
	//获取需要更新的文件
	for _, v := range getDirList {
		fp := "./" + v + "/fileList"
		md5Handle := md5.New()
		fl, err := os.OpenFile(fp, os.O_RDONLY, os.ModePerm)
		if err != nil {
			beego.Error("File[", fp, "]", "打开文件失败=>", err)
			continue
		}
		flTmp := bufio.NewScanner(fl)
		for flTmp.Scan() {
			fileList = append(fileList, flTmp.Text())
			f := "./" + flTmp.Text()
			r, _ := os.OpenFile(f, os.O_RDONLY, os.ModePerm)
			fi, _ := os.Stat(f)
			allFileSize += fi.Size()
			sbuf, _ := ioutil.ReadAll(r)
			md5Handle.Write(sbuf)
			md5Code := md5Handle.Sum(nil)
			smd5 := fmt.Sprintf("%x", md5Code)
			fileList = append(fileList, smd5)
			r.Close()
		}
		fl.Close()
	}
	//计算前端数据库文件
	frontFile := beego.AppConfig.String("frontDbPath")
	ff, err := os.OpenFile(frontFile, os.O_RDONLY, os.ModePerm)
	if err != nil {
		beego.Error("File[", frontFile, "]", "打开文件失败=>", err)
		this.Response.Set("retCode", RET_SUCC)
		return
	}
	defer ff.Close()
	md5Handle := md5.New()
	sbuf, _ := ioutil.ReadAll(ff)
	md5Handle.Write(sbuf)
	md5Code := md5Handle.Sum(nil)
	smd5 := fmt.Sprintf("%x", md5Code)
	fileList = append(fileList, frontFile)
	fileList = append(fileList, smd5)

	busiInfo.Set("downloads", fileList)
	busiInfo.Set("size", allFileSize)
	this.Response.Set("busiInfo", busiInfo)
	this.Response.Set("retCode", RET_SUCC)
	beego.Info("SystemUpdate ==> end")
}