From 63645d248c765244488cd34dbc1bb6528ca6b7c7 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期二, 05 九月 2023 09:58:13 +0800 Subject: [PATCH] 修复编译 --- version-control/controller/controller.go | 2084 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 1,042 insertions(+), 1,042 deletions(-) diff --git a/version-control/controller/controller.go b/version-control/controller/controller.go index c0707f8..3ee2c85 100644 --- a/version-control/controller/controller.go +++ b/version-control/controller/controller.go @@ -1,1042 +1,1042 @@ -package controller - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "os" - "os/exec" - "path" - "path/filepath" - "sort" - "strconv" - "strings" - "time" - "vamicro/extend/code" - "vamicro/extend/util" - "vamicro/version-control/models" - "vamicro/version-control/routers" - "vamicro/version-control/service" - - "basic.com/valib/bhomeclient.git" - "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" - "basic.com/valib/licence.git" - "basic.com/valib/logger.git" - "github.com/gin-gonic/gin" -) - -type ( - VersionRouter struct { - routers.Router - } -) - -const ( - basePath = "/version" - manifest = "dist/bin/manifest.ini" - zipDir = "dist/bin" -) - -var ( - CheckVersion *VersionRouter - Upgrade *VersionRouter - UploadDist *VersionRouter - SettingFetch *VersionRouter - SettingPush *VersionRouter - UpdateNotice *VersionRouter - DelayNotice *VersionRouter - PreUpLoad *VersionRouter - UpgradePreUpLoad *VersionRouter - RollBack *VersionRouter - Authorization *VersionRouter - GetSn *VersionRouter - Qrcode *VersionRouter - AuthorizationUpload *VersionRouter - CancelAuthorization *VersionRouter - CancelAuthorizationUpload *VersionRouter - - ShopMakeSmsCod *VersionRouter - ShopCenterLogin *VersionRouter - ShopCenterGetOrderList *VersionRouter - AuthActiveByOrder *VersionRouter - - AutoBakShow *VersionRouter - AutoBakConfSet *VersionRouter - AutoBakRightNow *VersionRouter - BackupListShow *VersionRouter - BackupRecov *VersionRouter - - Ms *bhomeclient.MicroNode -) - -func init() { - CheckVersion = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "check", - Handles: []gin.HandlerFunc{versionCheckHandle}, - }, - } - - UploadDist = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "upload", - Handles: []gin.HandlerFunc{uploadDistHandle}, - }, - } - - Upgrade = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "upgrade", - Handles: []gin.HandlerFunc{upgradeHandle}, - }, - } - - SettingFetch = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet}, - Path: "settings", - Handles: []gin.HandlerFunc{settingFetchHandle}, - }, - } - - SettingPush = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "setting/update", - Handles: []gin.HandlerFunc{settingPushHandle}, - }, - } - - UpdateNotice = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet}, - Path: "notice", - Handles: []gin.HandlerFunc{updateNoticeHandle}, - }, - } - - DelayNotice = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "notice/delay", - Handles: []gin.HandlerFunc{delayNoticeHandle}, - }, - } - PreUpLoad = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "upload/zip", - Handles: []gin.HandlerFunc{preUploadDistHandle}, - }, - } - UpgradePreUpLoad = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "upgrade/zip", - Handles: []gin.HandlerFunc{upgradePreUploadHandle}, - }, - } - RollBack = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "rollback", - Handles: []gin.HandlerFunc{RollBackHandle}, - }, - } - Authorization = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "authorization", - Handles: []gin.HandlerFunc{AuthorizationHandle}, - }, - } - - AuthorizationUpload = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "authorization/upload", - Handles: []gin.HandlerFunc{AuthorizationUploadHandle}, - }, - } - - GetSn = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "sn", - Handles: []gin.HandlerFunc{GetSnHandle}, - }, - } - Qrcode = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "offline/qrcode", - Handles: []gin.HandlerFunc{QrcodeHandle}, - }, - } - CancelAuthorization = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "cancelAuthorization", - Handles: []gin.HandlerFunc{CancelAuthorizationHandle}, - }, - } - CancelAuthorizationUpload = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet, http.MethodPost}, - Path: "cancelAuthorization/upload", - Handles: []gin.HandlerFunc{CancelAuthorizationUploadHandle}, - }, - } - - ShopMakeSmsCod = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet}, - Path: "auth/shop/smsCode", - Handles: []gin.HandlerFunc{shopMakSmsHandle}, - }, - } - - ShopCenterLogin = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "auth/shop/login", - Handles: []gin.HandlerFunc{shopCenterLoginHandle}, - }, - } - ShopCenterGetOrderList = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "auth/shop/getOrderList", - Handles: []gin.HandlerFunc{shopCenterGetOrderListHandle}, - }, - } - AuthActiveByOrder = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "auth/shop/activeByOrder", - Handles: []gin.HandlerFunc{authActiveByOrderHandle}, - }, - } - - AutoBakShow = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet}, - Path: "autoBak/config", - Handles: []gin.HandlerFunc{ShowAutoBakConf}, - }, - } - AutoBakConfSet = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "autoBak/saveConf", - Handles: []gin.HandlerFunc{SaveAutoBakConf}, - }, - } - AutoBakRightNow = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "autoBak/backupRN", - Handles: []gin.HandlerFunc{BackupRN}, - }, - } - BackupListShow = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodGet}, - Path: "autoBak/list", - Handles: []gin.HandlerFunc{BackupList}, - }, - } - BackupRecov = &VersionRouter{ - routers.Router{ - Methods: []string{http.MethodPost}, - Path: "autoBak/recover", - Handles: []gin.HandlerFunc{BackupRecover}, - }, - } -} - -func (r *VersionRouter) GetPath() string { - return path.Join(basePath, r.Path) -} - -//鍦ㄧ郴缁熼厤缃產pp涓紝缁欓〉闈㈣皟鐢紝鍒ゆ柇鎻愰啋鏈夋病鏈夌▼搴忔槸鍚﹂渶瑕佸崌绾� -func versionCheckHandle(c *gin.Context) { - _, version, intro := service.CheckVersion() - - curVersion := service.GetCurVersion() - //curEnv, _ := service.GetRunVersionEnv() - //hasNewVersion := len(programs) > 0 - hasNewVersion := version != curVersion - if version == "" { - hasNewVersion = false - version = curVersion - } - - c.JSON(http.StatusOK, gin.H{ - "code": 200, - "msg": "ok", - "data": map[string]interface{}{ - "hasNewVersion": hasNewVersion, - "newVersion": version, - "newVersionInfo": intro, - "curVersion": curVersion, - "curVersionPath": curVersion, - }, - }) -} - -func preUploadDistHandle(c *gin.Context) { - file, err := c.FormFile("archive") - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle upload field is not filled with a file, err:", err.Error()) - return - } - UpgradePath := service.GetPreUploadPath() - err = os.RemoveAll(UpgradePath) - if nil == err { - _ = os.MkdirAll(UpgradePath, 0777) - } else { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle remove pre upload path failed, err:", err.Error()) - return - } - fileMd5 := service.GetMd5(file.Filename) + ".tgz" - if err = c.SaveUploadedFile(file, path.Join(UpgradePath, fileMd5)); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle save file failed, err:", err.Error()) - return - } - logger.Info("uploadDistHandle saved to temp file ok:", fileMd5) - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": map[string]string{"id": fileMd5}, - }) -} - -func upgradePreUploadHandle(c *gin.Context) { - upid := c.Query("id") - if upid == "" { - util.ResponseFormat(c, code.RequestParamError, "id涓嶈兘涓虹┖") - return - } - UpgradePath := service.GetPreUploadPath() - upZip := path.Join(UpgradePath, upid) - if !util.FileExists(upZip) { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "鍗囩骇鍖呮湭鎵惧埌!", - "data": []*models.Program{}, - }) - logger.Error("upgradePreUploadHandle upload file not found:", upZip) - return - } - - logger.Info("寮�濮嬪崌绾э紒") - apps, err := service.UpgradeViaZip(upZip) - logger.Info("绯荤粺鍗囩骇瀹屾垚锛侊紒") - if nil != err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": err.Error(), - "data": apps, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": apps, - }) - killApp(apps) - } -} - -//鍦ㄧ绾跨殑鎯呭喌锛屼娇鐢╰gz鍖呯洿鎺ュ湪鍐呯綉涓婁紶绂荤嚎瀹夎鍖� -func uploadDistHandle(c *gin.Context) { - file, err := c.FormFile("archive") - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle upload field is not filled with a file, err:", err.Error()) - return - } - UpgradePath := service.GetPreUploadPath() - tmpFile, err := ioutil.TempFile(UpgradePath, "dist-*.tgz") - if nil != err { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle create temp file failed, err:", err.Error()) - return - } - defer func() { - tmpFile.Close() - os.Remove(tmpFile.Name()) - }() - - if err = c.SaveUploadedFile(file, tmpFile.Name()); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("uploadDistHandle save file failed, err:", err.Error()) - return - } - logger.Info("uploadDistHandle saved to temp file ok:", tmpFile.Name()) - - apps, err := service.UpgradeViaZip(tmpFile.Name()) - if nil != err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": err.Error(), - "data": apps, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": apps, - }) - } -} - -func upgradeHandle(c *gin.Context) { - apps, err := service.OnlineUpgrade() - if nil != err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": err.Error(), - "data": apps, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": apps, - }) - //_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT) - killApp(apps) - } -} - -func killApp(apps []*models.Program) { - //寰幆鏉�姝诲崌绾у簲鐢ㄨ繘绋� - versionControl := "" - for _, app := range apps { - if app.Name == "version-control" { - versionControl = app.Name - continue - } - if app.Type == "sys" || app.Type == "app" { - command := "pkill " + app.Name - result, err := execCommand(command) - logger.Info("鎵ц鍛戒护銆�", command, "銆�") - logger.Info("鎵ц缁撴灉銆�", result, "err:,", err, "銆�") - } - } - if versionControl != "" { - command := "pkill " + versionControl - result, err := execCommand(command) - logger.Info("鎵ц鍛戒护銆�", command, "銆�") - logger.Info("鎵ц缁撴灉銆�", result, "\n err:,", err, "銆�") - } -} - -//鎵ц鍛戒护 -func execCommand(cmd string) ([]byte, error) { - return exec.Command("/bin/bash", "-c", cmd).Output() -} - -//鑾峰彇閰嶇疆 -func settingFetchHandle(c *gin.Context) { - var model models.SysSetting - settings, err := model.GetAllSetting() - if nil != err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": err.Error(), - "data": settings, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": settings, - }) - } -} - -//鏇存柊閰嶇疆 -func settingPushHandle(c *gin.Context) { - name := c.PostForm("name") - value := c.PostForm("value") - setting := models.SysSetting{Name: name, Value: value} - err := setting.SaveSetting(true) - if nil != err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": err.Error(), - "data": []interface{}{}, - }) - } else { - data, _ := json.Marshal(setting) - var nodes []bhome_msg.BHAddress - nodes = append(nodes, bhome_msg.BHAddress{}) - go Ms.PublishNetTimeout(nodes, service.SysUpdateConfigTopic, data, 10) - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": []interface{}{}, - }) - } -} - -func updateNoticeHandle(c *gin.Context) { - notice := service.GetUpdateNotice() - uid, ok := c.Get("uid") - if ok { - LastNoticeTime, ok := notice.NoticeUser[uid.(string)] - if ok { - notice.LastNoticeTime = int64(LastNoticeTime) - } - } - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": notice, - }) -} - -func delayNoticeHandle(c *gin.Context) { - delaySecond := c.PostForm("delaySecond") - second, err := strconv.Atoi(delaySecond) - if nil == err { - uid, ok := c.Get("uid") - if ok { - notice := service.DelayNotice(uid.(string), second) - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": notice, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鏈壘鍒扮敤鎴�", - "data": []interface{}{}, - }) - } - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鍙傛暟閿欒", - "data": []interface{}{}, - }) - } -} - -func RollBackHandle(c *gin.Context) { - version := c.PostForm("version") - err := service.Rollback(version) - if nil == err { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": []interface{}{}, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鍥炴粴澶辫触锛�" + err.Error(), - "data": []interface{}{}, - }) - } -} - -func sn() map[string]interface{} { - data := make(map[string]interface{}, 0) - sn := util.GetSn() - data["sn"] = sn - auth := util.GetAuthorization() - data["expireTime"] = "" - - authInfo, err := util.GetAuthorizationInfo(auth) - //logger.Debug("鎺堟潈淇℃伅锛�", authInfo, err, licence.GetMachineCode()) - if err == nil && authInfo.MachineCode == licence.GetMachineCode() { - expireUt := time.Unix(authInfo.ExpirationTime, 0) - expireTimeStr := expireUt.Format("2006-01-02 15:04:05") - data["expireTime"] = expireTimeStr - data["expire"] = false - if authInfo.ActivateCode != "" { - data["sn"] = authInfo.ActivateCode - } - if authInfo.DevCount == -1 { //璇曠敤鐗� - data["sn"] = "" - } - if time.Now().Sub(expireUt) > 0 { - data["expire"] = true - data["sn"] = "" - } - if data["sn"] != "" { - data["authorization"] = auth - } - data["edition"] = authInfo.Edition - data["installTime"] = authInfo.InstallTime - if authInfo.Edition == "" || authInfo.InstallTime == "" { - if eii, err := util.GetEdtionSetFromIns(); err == nil { - if authInfo.Edition == "" { - data["edition"] = eii.Edition - } - if authInfo.InstallTime == "" { - data["installTime"] = eii.InstallTime - } - } - } - } else { - //娴嬭瘯鐗� - eii, err := util.GetEdtionSetFromIns() - if err == nil { - insT, _ := time.ParseInLocation("2006-01-02 15:04:05", eii.InstallTime, time.Local) - if eii.OrderAuth != nil && eii.OrderAuth.ProductId != "" && eii.OrderAuth.OrderId != "" { - data["sn"] = "" - expireT := insT.AddDate(eii.OrderAuth.ServeYear, 0, 0) - data["expireTime"] = expireT.Format("2006-01-02 15:04:05") - if time.Now().Sub(expireT) > 0 { - data["expire"] = true - } else { - data["expire"] = false - } - } else { - data["sn"] = "" - expireT := insT.AddDate(0, 0, eii.Setting.TrialDays) - data["expireTime"] = expireT.Format("2006-01-02 15:04:05") - if time.Now().Sub(expireT) > 0 { - data["expire"] = true - } else { - data["expire"] = false - } - } - data["edition"] = eii.Edition - data["installTime"] = eii.InstallTime - } else { - logger.Error("Install info missing") - data["sn"] = "" - data["authorization"] = "" - data["expire"] = true - data["edition"] = "trial" - data["installTime"] = "" - } - } - - data["q"] = service.GetQ() - return data -} - -func GetSnHandle(c *gin.Context) { - data := sn() - /* set := logc.RuleServerPushLog{} - set.TaskName = "Test" - set.Type = 0 - set.Info = "111222111" - uuid, _ := uuid.GenerateUUID() - set.ID = uuid - data2, _ := json.Marshal(set) - var nodes []bhome_msg.BHAddress - nodes = append(nodes, bhome_msg.BHAddress{}) - go Ms.PublishNetTimeout(nodes, "ruleServerLogSaveTopic", data2, 10)*/ - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": data, - }) -} - -func QrcodeHandle(c *gin.Context) { - png, url, err := service.GenQRCode() - if nil == err { - c.Header("content-disposition", `attachment; filename=qrcode.png`) - c.Header("qrcode-url", url) - c.Data(200, "image/png", png) - return - } - - logger.Error("QrcodeHandle err!!!") - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusBadRequest, - "msg": "鐢熸垚浜岀淮鐮佸け璐�", - "data": "", - }) -} - -func AuthorizationUploadHandle(c *gin.Context) { - codeF, err := c.FormFile("code") //灏辨槸鍟嗗煄涓敓鎴愮殑浜у搧瀵嗛挜璁㈠崟涓嬭浇鐨勬枃浠� - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - F, err := codeF.Open() - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - buffer := make([]byte, codeF.Size) - _, err = F.Read(buffer) - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - - code := string(buffer) - if "" == code { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "msg": "浜у搧瀵嗛挜涓嶈兘涓虹┖", - "data": []interface{}{}, - }) - return - } - - _, err = service.Authorization(code, true) - if nil == err || strings.Contains(err.Error(), "鎴愬姛") { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": string(code), - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "msg": "婵�娲诲け璐ワ紝" + err.Error(), - "data": []interface{}{}, - }) - } -} - -func AuthorizationHandle(c *gin.Context) { - code := c.PostForm("code") //浜у搧瀵嗛挜鐨勫唴瀹�,灏辨槸鍟嗗煄涓敓鎴愮殑浜у搧瀵嗛挜璁㈠崟涓嬭浇鐨勬枃浠跺唴瀹� - if "" == code { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "msg": "浜у搧瀵嗛挜涓嶈兘涓虹┖", - "data": []interface{}{}, - }) - return - } - _, err := service.Authorization(code, true) - if nil == err || strings.Contains(err.Error(), "鎴愬姛") { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": []interface{}{}, - }) - } else { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": "婵�娲诲け璐ワ紝" + err.Error(), - "data": []interface{}{}, - }) - } -} - -func CancelAuthorizationHandle(c *gin.Context) { - q := c.PostForm("q") - supperPasswd := c.PostForm("passwd") - down := c.PostForm("down") - - err, authCode := service.CancelAuthorization(supperPasswd, q) - if err != nil && !strings.Contains(err.Error(), "鎴愬姛") { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []interface{}{}, - }) - } else { - if "1" == down { - c.Header("content-disposition", `attachment; filename=authorization.txt`) - ///c.Header("qrcode-url", url) - c.Data(200, "text/plain", []byte(authCode)) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": authCode, - }) - } - } -} - -func CancelAuthorizationUploadHandle(c *gin.Context) { - q, err := c.FormFile("q") - supperPasswd := c.PostForm("passwd") - down := c.PostForm("down") - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - F, err := q.Open() - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - buffer := make([]byte, q.Size) - _, err = F.Read(buffer) - if nil != err { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []*models.Program{}, - }) - logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) - return - } - - err, authinfo := service.CancelAuthorization(supperPasswd, string(buffer)) - if nil == err { - if "1" == down { - c.Header("content-disposition", `attachment; filename=authorization.txt`) - ///c.Header("qrcode-url", url) - c.Data(200, "text/plain", []byte(authinfo)) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "ok", - "data": authinfo, - }) - } - } else { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []interface{}{}, - }) - } -} - -func ShowAutoBakConf(c *gin.Context) { - var ab models.AutoBackupConf - i, e := ab.Select() - if e == nil && i > 0 { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鏌ヨ鎴愬姛", - "data": ab, - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鏌ヨ鎴愬姛", - "data": models.AutoBackupConf{ - Dir: util.GetVamicroPath() + "/backup_auto", - }, - }) -} - -func SaveAutoBakConf(c *gin.Context) { - var reqBody models.AutoBackupConf - err := c.BindJSON(&reqBody) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": err.Error(), - "data": []models.AutoBackupConf{}, - }) - return - } - fmt.Println("go here 1 ", reqBody) - reqBody.Dir = util.GetVamicroPath() + "/backup_auto" - var ab models.AutoBackupConf - i, _ := ab.Select() - if i > 0 { - fmt.Println("go here 2 ", reqBody) - if reqBody.Update() { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鏇存柊鎴愬姛", - "data": reqBody, - }) - } else { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "鏇存柊澶辫触", - "data": nil, - }) - } - } else { - fmt.Println("go here 3 ") - if reqBody.Insert() { - fmt.Println("go here 4 ") - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "淇濆瓨鎴愬姛", - "data": reqBody, - }) - } else { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "淇濆瓨澶辫触", - "data": nil, - }) - } - } -} - -//绔嬪嵆澶囦唤鎿嶄綔 -func BackupRN(c *gin.Context) { - var ab models.AutoBackupConf - i, e := ab.Select() - if i == 0 || e != nil { - ab.Dir = util.GetVamicroPath() + "/backup_auto" - } - err := service.DoAutoBackup(&ab) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "绔嬪嵆澶囦唤澶辫触:" + err.Error(), - "data": nil, - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "绔嬪嵆澶囦唤鎴愬姛", - "data": nil, - }) -} - -//鏄剧ず澶囦唤鍒楄〃 -func BackupList(c *gin.Context) { - var ab models.AutoBackupConf - i, e := ab.Select() - if i == 0 || e != nil { - ab.Dir = util.GetVamicroPath() + "/backup_auto/" - } else { - if !strings.HasSuffix(ab.Dir, "/") { - ab.Dir = ab.Dir + "/" - } - } - backList := make([]string, 0) - err := filepath.Walk(ab.Dir, func(src string, f os.FileInfo, err error) error { - if src == ab.Dir { - return nil - } - if f.IsDir() { - depth := strings.Count(src, "/") - strings.Count(ab.Dir, "/") - if depth != 0 { - return filepath.SkipDir - } - _, e := time.ParseInLocation("2006-01-02-15-04-05", f.Name(), time.Local) - if e != nil { - return e - } - backList = append(backList, f.Name()) - } - return nil - }) - - if err != nil { - logger.Error("filepath.Walk ab.dir err:", err) - } - - sort.Sort(sort.Reverse(sort.StringSlice(backList))) //闄嶅簭鎺掑簭 - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "鏌ヨ鎴愬姛", - "data": backList, - }) -} - -//鏍规嵁鎸囧畾鐨勮繕鍘熻妭鐐硅繘琛岃繕鍘熸搷浣� -func BackupRecover(c *gin.Context) { - bakDir := c.PostForm("bakDir") - if bakDir == "" { - c.JSON(http.StatusBadRequest, gin.H{ - "code": http.StatusBadRequest, - "msg": "鍙傛暟鏈夎锛宐akDir涓嶈兘涓虹┖", - "data": nil, - }) - return - } - var ab models.AutoBackupConf - i, e := ab.Select() - if i == 0 || e != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "澶囦唤璁剧疆涓虹┖", - "data": nil, - }) - return - } - fd := ab.Dir + "/" + bakDir - if !util.DirExists(fd) { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "褰撳墠澶囦唤鏂囦欢澶瑰凡涓嶅瓨鍦�,璇锋鏌�", - "data": nil, - }) - return - } - //鎵ц杩樺師鎿嶄綔 - err := service.DoBackupRecover(fd) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "code": http.StatusInternalServerError, - "msg": "杩樺師澶辫触:" + err.Error(), - "data": nil, - }) - } else { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "msg": "杩樺師鎴愬姛", - "data": nil, - }) - } -} +package controller + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "os/exec" + "path" + "path/filepath" + "sort" + "strconv" + "strings" + "time" + "vamicro/extend/code" + "vamicro/extend/util" + "vamicro/version-control/models" + "vamicro/version-control/routers" + "vamicro/version-control/service" + + "basic.com/valib/bhomeclient.git" + "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" + "basic.com/valib/licence.git" + "basic.com/valib/logger.git" + "github.com/gin-gonic/gin" +) + +type ( + VersionRouter struct { + routers.Router + } +) + +const ( + basePath = "/version" + manifest = "dist/bin/manifest.ini" + zipDir = "dist/bin" +) + +var ( + CheckVersion *VersionRouter + Upgrade *VersionRouter + UploadDist *VersionRouter + SettingFetch *VersionRouter + SettingPush *VersionRouter + UpdateNotice *VersionRouter + DelayNotice *VersionRouter + PreUpLoad *VersionRouter + UpgradePreUpLoad *VersionRouter + RollBack *VersionRouter + Authorization *VersionRouter + GetSn *VersionRouter + Qrcode *VersionRouter + AuthorizationUpload *VersionRouter + CancelAuthorization *VersionRouter + CancelAuthorizationUpload *VersionRouter + + ShopMakeSmsCod *VersionRouter + ShopCenterLogin *VersionRouter + ShopCenterGetOrderList *VersionRouter + AuthActiveByOrder *VersionRouter + + AutoBakShow *VersionRouter + AutoBakConfSet *VersionRouter + AutoBakRightNow *VersionRouter + BackupListShow *VersionRouter + BackupRecov *VersionRouter + + Ms *bhomeclient.MicroNode +) + +func init() { + CheckVersion = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "check", + Handles: []gin.HandlerFunc{versionCheckHandle}, + }, + } + + UploadDist = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "upload", + Handles: []gin.HandlerFunc{uploadDistHandle}, + }, + } + + Upgrade = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "upgrade", + Handles: []gin.HandlerFunc{upgradeHandle}, + }, + } + + SettingFetch = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet}, + Path: "settings", + Handles: []gin.HandlerFunc{settingFetchHandle}, + }, + } + + SettingPush = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "setting/update", + Handles: []gin.HandlerFunc{settingPushHandle}, + }, + } + + UpdateNotice = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet}, + Path: "notice", + Handles: []gin.HandlerFunc{updateNoticeHandle}, + }, + } + + DelayNotice = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "notice/delay", + Handles: []gin.HandlerFunc{delayNoticeHandle}, + }, + } + PreUpLoad = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "upload/zip", + Handles: []gin.HandlerFunc{preUploadDistHandle}, + }, + } + UpgradePreUpLoad = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "upgrade/zip", + Handles: []gin.HandlerFunc{upgradePreUploadHandle}, + }, + } + RollBack = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "rollback", + Handles: []gin.HandlerFunc{RollBackHandle}, + }, + } + Authorization = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "authorization", + Handles: []gin.HandlerFunc{AuthorizationHandle}, + }, + } + + AuthorizationUpload = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "authorization/upload", + Handles: []gin.HandlerFunc{AuthorizationUploadHandle}, + }, + } + + GetSn = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "sn", + Handles: []gin.HandlerFunc{GetSnHandle}, + }, + } + Qrcode = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "offline/qrcode", + Handles: []gin.HandlerFunc{QrcodeHandle}, + }, + } + CancelAuthorization = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "cancelAuthorization", + Handles: []gin.HandlerFunc{CancelAuthorizationHandle}, + }, + } + CancelAuthorizationUpload = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet, http.MethodPost}, + Path: "cancelAuthorization/upload", + Handles: []gin.HandlerFunc{CancelAuthorizationUploadHandle}, + }, + } + + ShopMakeSmsCod = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet}, + Path: "auth/shop/smsCode", + Handles: []gin.HandlerFunc{shopMakSmsHandle}, + }, + } + + ShopCenterLogin = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "auth/shop/login", + Handles: []gin.HandlerFunc{shopCenterLoginHandle}, + }, + } + ShopCenterGetOrderList = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "auth/shop/getOrderList", + Handles: []gin.HandlerFunc{shopCenterGetOrderListHandle}, + }, + } + AuthActiveByOrder = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "auth/shop/activeByOrder", + Handles: []gin.HandlerFunc{authActiveByOrderHandle}, + }, + } + + AutoBakShow = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet}, + Path: "autoBak/config", + Handles: []gin.HandlerFunc{ShowAutoBakConf}, + }, + } + AutoBakConfSet = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "autoBak/saveConf", + Handles: []gin.HandlerFunc{SaveAutoBakConf}, + }, + } + AutoBakRightNow = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "autoBak/backupRN", + Handles: []gin.HandlerFunc{BackupRN}, + }, + } + BackupListShow = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodGet}, + Path: "autoBak/list", + Handles: []gin.HandlerFunc{BackupList}, + }, + } + BackupRecov = &VersionRouter{ + routers.Router{ + Methods: []string{http.MethodPost}, + Path: "autoBak/recover", + Handles: []gin.HandlerFunc{BackupRecover}, + }, + } +} + +func (r *VersionRouter) GetPath() string { + return path.Join(basePath, r.Path) +} + +//鍦ㄧ郴缁熼厤缃產pp涓紝缁欓〉闈㈣皟鐢紝鍒ゆ柇鎻愰啋鏈夋病鏈夌▼搴忔槸鍚﹂渶瑕佸崌绾� +func versionCheckHandle(c *gin.Context) { + _, version, intro := service.CheckVersion() + + curVersion := service.GetCurVersion() + //curEnv, _ := service.GetRunVersionEnv() + //hasNewVersion := len(programs) > 0 + hasNewVersion := version != curVersion + if version == "" { + hasNewVersion = false + version = curVersion + } + + c.JSON(http.StatusOK, gin.H{ + "code": 200, + "msg": "ok", + "data": map[string]interface{}{ + "hasNewVersion": hasNewVersion, + "newVersion": version, + "newVersionInfo": intro, + "curVersion": curVersion, + "curVersionPath": curVersion, + }, + }) +} + +func preUploadDistHandle(c *gin.Context) { + file, err := c.FormFile("archive") + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle upload field is not filled with a file, err:", err.Error()) + return + } + UpgradePath := service.GetPreUploadPath() + err = os.RemoveAll(UpgradePath) + if nil == err { + _ = os.MkdirAll(UpgradePath, 0777) + } else { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle remove pre upload path failed, err:", err.Error()) + return + } + fileMd5 := service.GetMd5(file.Filename) + ".tgz" + if err = c.SaveUploadedFile(file, path.Join(UpgradePath, fileMd5)); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle save file failed, err:", err.Error()) + return + } + logger.Info("uploadDistHandle saved to temp file ok:", fileMd5) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": map[string]string{"id": fileMd5}, + }) +} + +func upgradePreUploadHandle(c *gin.Context) { + upid := c.Query("id") + if upid == "" { + util.ResponseFormat(c, code.RequestParamError, "id涓嶈兘涓虹┖") + return + } + UpgradePath := service.GetPreUploadPath() + upZip := path.Join(UpgradePath, upid) + if !util.FileExists(upZip) { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "鍗囩骇鍖呮湭鎵惧埌!", + "data": []*models.Program{}, + }) + logger.Error("upgradePreUploadHandle upload file not found:", upZip) + return + } + + logger.Info("寮�濮嬪崌绾э紒") + apps, err := service.UpgradeViaZip(upZip) + logger.Info("绯荤粺鍗囩骇瀹屾垚锛侊紒") + if nil != err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": err.Error(), + "data": apps, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": apps, + }) + killApp(apps) + } +} + +//鍦ㄧ绾跨殑鎯呭喌锛屼娇鐢╰gz鍖呯洿鎺ュ湪鍐呯綉涓婁紶绂荤嚎瀹夎鍖� +func uploadDistHandle(c *gin.Context) { + file, err := c.FormFile("archive") + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle upload field is not filled with a file, err:", err.Error()) + return + } + UpgradePath := service.GetPreUploadPath() + tmpFile, err := ioutil.TempFile(UpgradePath, "dist-*.tgz") + if nil != err { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle create temp file failed, err:", err.Error()) + return + } + defer func() { + tmpFile.Close() + os.Remove(tmpFile.Name()) + }() + + if err = c.SaveUploadedFile(file, tmpFile.Name()); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("uploadDistHandle save file failed, err:", err.Error()) + return + } + logger.Info("uploadDistHandle saved to temp file ok:", tmpFile.Name()) + + apps, err := service.UpgradeViaZip(tmpFile.Name()) + if nil != err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": err.Error(), + "data": apps, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": apps, + }) + } +} + +func upgradeHandle(c *gin.Context) { + apps, err := service.OnlineUpgrade() + if nil != err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": err.Error(), + "data": apps, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": apps, + }) + //_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT) + killApp(apps) + } +} + +func killApp(apps []*models.Program) { + //寰幆鏉�姝诲崌绾у簲鐢ㄨ繘绋� + versionControl := "" + for _, app := range apps { + if app.Name == "version-control" { + versionControl = app.Name + continue + } + if app.Type == "sys" || app.Type == "app" { + command := "pkill " + app.Name + result, err := execCommand(command) + logger.Info("鎵ц鍛戒护銆�", command, "銆�") + logger.Info("鎵ц缁撴灉銆�", result, "err:,", err, "銆�") + } + } + if versionControl != "" { + command := "pkill " + versionControl + result, err := execCommand(command) + logger.Info("鎵ц鍛戒护銆�", command, "銆�") + logger.Info("鎵ц缁撴灉銆�", result, "\n err:,", err, "銆�") + } +} + +//鎵ц鍛戒护 +func execCommand(cmd string) ([]byte, error) { + return exec.Command("/bin/bash", "-c", cmd).Output() +} + +//鑾峰彇閰嶇疆 +func settingFetchHandle(c *gin.Context) { + var model models.SysSetting + settings, err := model.GetAllSetting() + if nil != err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": err.Error(), + "data": settings, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": settings, + }) + } +} + +//鏇存柊閰嶇疆 +func settingPushHandle(c *gin.Context) { + name := c.PostForm("name") + value := c.PostForm("value") + setting := models.SysSetting{Name: name, Value: value} + err := setting.SaveSetting(true) + if nil != err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": err.Error(), + "data": []interface{}{}, + }) + } else { + data, _ := json.Marshal(setting) + var nodes []bhome_msg.BHAddress + nodes = append(nodes, bhome_msg.BHAddress{}) + go Ms.PublishNetTimeout(nodes, service.SysUpdateConfigTopic, data, 10) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": []interface{}{}, + }) + } +} + +func updateNoticeHandle(c *gin.Context) { + notice := service.GetUpdateNotice() + uid, ok := c.Get("uid") + if ok { + LastNoticeTime, ok := notice.NoticeUser[uid.(string)] + if ok { + notice.LastNoticeTime = int64(LastNoticeTime) + } + } + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": notice, + }) +} + +func delayNoticeHandle(c *gin.Context) { + delaySecond := c.PostForm("delaySecond") + second, err := strconv.Atoi(delaySecond) + if nil == err { + uid, ok := c.Get("uid") + if ok { + notice := service.DelayNotice(uid.(string), second) + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": notice, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鏈壘鍒扮敤鎴�", + "data": []interface{}{}, + }) + } + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鍙傛暟閿欒", + "data": []interface{}{}, + }) + } +} + +func RollBackHandle(c *gin.Context) { + version := c.PostForm("version") + err := service.Rollback(version) + if nil == err { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": []interface{}{}, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鍥炴粴澶辫触锛�" + err.Error(), + "data": []interface{}{}, + }) + } +} + +func sn() map[string]interface{} { + data := make(map[string]interface{}, 0) + sn := util.GetSn() + data["sn"] = sn + auth := util.GetAuthorization() + data["expireTime"] = "" + + authInfo, err := util.GetAuthorizationInfo(auth) + //logger.Debug("鎺堟潈淇℃伅锛�", authInfo, err, licence.GetMachineCode()) + if err == nil && authInfo.MachineCode == licence.GetMachineCode() { + expireUt := time.Unix(authInfo.ExpirationTime, 0) + expireTimeStr := expireUt.Format("2006-01-02 15:04:05") + data["expireTime"] = expireTimeStr + data["expire"] = false + if authInfo.ActivateCode != "" { + data["sn"] = authInfo.ActivateCode + } + if authInfo.DevCount == -1 { //璇曠敤鐗� + data["sn"] = "" + } + if time.Now().Sub(expireUt) > 0 { + data["expire"] = true + data["sn"] = "" + } + if data["sn"] != "" { + data["authorization"] = auth + } + data["edition"] = authInfo.Edition + data["installTime"] = authInfo.InstallTime + if authInfo.Edition == "" || authInfo.InstallTime == "" { + if eii, err := util.GetEdtionSetFromIns(); err == nil { + if authInfo.Edition == "" { + data["edition"] = eii.Edition + } + if authInfo.InstallTime == "" { + data["installTime"] = eii.InstallTime + } + } + } + } else { + //娴嬭瘯鐗� + eii, err := util.GetEdtionSetFromIns() + if err == nil { + insT, _ := time.ParseInLocation("2006-01-02 15:04:05", eii.InstallTime, time.Local) + if eii.OrderAuth != nil && eii.OrderAuth.ProductId != "" && eii.OrderAuth.OrderId != "" { + data["sn"] = "" + expireT := insT.AddDate(eii.OrderAuth.ServeYear, 0, 0) + data["expireTime"] = expireT.Format("2006-01-02 15:04:05") + if time.Now().Sub(expireT) > 0 { + data["expire"] = true + } else { + data["expire"] = false + } + } else { + data["sn"] = "" + expireT := insT.AddDate(0, 0, eii.Setting.TrialDays) + data["expireTime"] = expireT.Format("2006-01-02 15:04:05") + if time.Now().Sub(expireT) > 0 { + data["expire"] = true + } else { + data["expire"] = false + } + } + data["edition"] = eii.Edition + data["installTime"] = eii.InstallTime + } else { + logger.Error("Install info missing") + data["sn"] = "" + data["authorization"] = "" + data["expire"] = true + data["edition"] = "trial" + data["installTime"] = "" + } + } + + data["q"] = service.GetQ() + return data +} + +func GetSnHandle(c *gin.Context) { + data := sn() + /* set := logc.RuleServerPushLog{} + set.TaskName = "Test" + set.Type = 0 + set.Info = "111222111" + uuid, _ := uuid.GenerateUUID() + set.ID = uuid + data2, _ := json.Marshal(set) + var nodes []bhome_msg.BHAddress + nodes = append(nodes, bhome_msg.BHAddress{}) + go Ms.PublishNetTimeout(nodes, "ruleServerLogSaveTopic", data2, 10)*/ + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": data, + }) +} + +func QrcodeHandle(c *gin.Context) { + png, url, err := service.GenQRCode() + if nil == err { + c.Header("content-disposition", `attachment; filename=qrcode.png`) + c.Header("qrcode-url", url) + c.Data(200, "image/png", png) + return + } + + logger.Error("QrcodeHandle err!!!") + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusBadRequest, + "msg": "鐢熸垚浜岀淮鐮佸け璐�", + "data": "", + }) +} + +func AuthorizationUploadHandle(c *gin.Context) { + codeF, err := c.FormFile("code") //灏辨槸鍟嗗煄涓敓鎴愮殑浜у搧瀵嗛挜璁㈠崟涓嬭浇鐨勬枃浠� + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + F, err := codeF.Open() + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + buffer := make([]byte, codeF.Size) + _, err = F.Read(buffer) + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + + code := string(buffer) + if "" == code { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusBadRequest, + "msg": "浜у搧瀵嗛挜涓嶈兘涓虹┖", + "data": []interface{}{}, + }) + return + } + + _, err = service.Authorization(code, true) + if nil == err || strings.Contains(err.Error(), "鎴愬姛") { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": string(code), + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusBadRequest, + "msg": "婵�娲诲け璐ワ紝" + err.Error(), + "data": []interface{}{}, + }) + } +} + +func AuthorizationHandle(c *gin.Context) { + code := c.PostForm("code") //浜у搧瀵嗛挜鐨勫唴瀹�,灏辨槸鍟嗗煄涓敓鎴愮殑浜у搧瀵嗛挜璁㈠崟涓嬭浇鐨勬枃浠跺唴瀹� + if "" == code { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusBadRequest, + "msg": "浜у搧瀵嗛挜涓嶈兘涓虹┖", + "data": []interface{}{}, + }) + return + } + _, err := service.Authorization(code, true) + if nil == err || strings.Contains(err.Error(), "鎴愬姛") { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": []interface{}{}, + }) + } else { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": "婵�娲诲け璐ワ紝" + err.Error(), + "data": []interface{}{}, + }) + } +} + +func CancelAuthorizationHandle(c *gin.Context) { + q := c.PostForm("q") + supperPasswd := c.PostForm("passwd") + down := c.PostForm("down") + + err, authCode := service.CancelAuthorization(supperPasswd, q) + if err != nil && !strings.Contains(err.Error(), "鎴愬姛") { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []interface{}{}, + }) + } else { + if "1" == down { + c.Header("content-disposition", `attachment; filename=authorization.txt`) + ///c.Header("qrcode-url", url) + c.Data(200, "text/plain", []byte(authCode)) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": authCode, + }) + } + } +} + +func CancelAuthorizationUploadHandle(c *gin.Context) { + q, err := c.FormFile("q") + supperPasswd := c.PostForm("passwd") + down := c.PostForm("down") + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + F, err := q.Open() + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + buffer := make([]byte, q.Size) + _, err = F.Read(buffer) + if nil != err { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []*models.Program{}, + }) + logger.Error("AuthorizationUploadHandle upload field is not filled with a file, err:", err.Error()) + return + } + + err, authinfo := service.CancelAuthorization(supperPasswd, string(buffer)) + if nil == err { + if "1" == down { + c.Header("content-disposition", `attachment; filename=authorization.txt`) + ///c.Header("qrcode-url", url) + c.Data(200, "text/plain", []byte(authinfo)) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "ok", + "data": authinfo, + }) + } + } else { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []interface{}{}, + }) + } +} + +func ShowAutoBakConf(c *gin.Context) { + var ab models.AutoBackupConf + i, e := ab.Select() + if e == nil && i > 0 { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鏌ヨ鎴愬姛", + "data": ab, + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鏌ヨ鎴愬姛", + "data": models.AutoBackupConf{ + Dir: util.GetVamicroPath() + "/backup_auto", + }, + }) +} + +func SaveAutoBakConf(c *gin.Context) { + var reqBody models.AutoBackupConf + err := c.BindJSON(&reqBody) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": err.Error(), + "data": []models.AutoBackupConf{}, + }) + return + } + fmt.Println("go here 1 ", reqBody) + reqBody.Dir = util.GetVamicroPath() + "/backup_auto" + var ab models.AutoBackupConf + i, _ := ab.Select() + if i > 0 { + fmt.Println("go here 2 ", reqBody) + if reqBody.Update() { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鏇存柊鎴愬姛", + "data": reqBody, + }) + } else { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "鏇存柊澶辫触", + "data": nil, + }) + } + } else { + fmt.Println("go here 3 ") + if reqBody.Insert() { + fmt.Println("go here 4 ") + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "淇濆瓨鎴愬姛", + "data": reqBody, + }) + } else { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "淇濆瓨澶辫触", + "data": nil, + }) + } + } +} + +//绔嬪嵆澶囦唤鎿嶄綔 +func BackupRN(c *gin.Context) { + var ab models.AutoBackupConf + i, e := ab.Select() + if i == 0 || e != nil { + ab.Dir = util.GetVamicroPath() + "/backup_auto" + } + err := service.DoAutoBackup(&ab) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "绔嬪嵆澶囦唤澶辫触:" + err.Error(), + "data": nil, + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "绔嬪嵆澶囦唤鎴愬姛", + "data": nil, + }) +} + +//鏄剧ず澶囦唤鍒楄〃 +func BackupList(c *gin.Context) { + var ab models.AutoBackupConf + i, e := ab.Select() + if i == 0 || e != nil { + ab.Dir = util.GetVamicroPath() + "/backup_auto/" + } else { + if !strings.HasSuffix(ab.Dir, "/") { + ab.Dir = ab.Dir + "/" + } + } + backList := make([]string, 0) + err := filepath.Walk(ab.Dir, func(src string, f os.FileInfo, err error) error { + if src == ab.Dir { + return nil + } + if f.IsDir() { + depth := strings.Count(src, "/") - strings.Count(ab.Dir, "/") + if depth != 0 { + return filepath.SkipDir + } + _, e := time.ParseInLocation("2006-01-02-15-04-05", f.Name(), time.Local) + if e != nil { + return e + } + backList = append(backList, f.Name()) + } + return nil + }) + + if err != nil { + logger.Error("filepath.Walk ab.dir err:", err) + } + + sort.Sort(sort.Reverse(sort.StringSlice(backList))) //闄嶅簭鎺掑簭 + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "鏌ヨ鎴愬姛", + "data": backList, + }) +} + +//鏍规嵁鎸囧畾鐨勮繕鍘熻妭鐐硅繘琛岃繕鍘熸搷浣� +func BackupRecover(c *gin.Context) { + bakDir := c.PostForm("bakDir") + if bakDir == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": "鍙傛暟鏈夎锛宐akDir涓嶈兘涓虹┖", + "data": nil, + }) + return + } + var ab models.AutoBackupConf + i, e := ab.Select() + if i == 0 || e != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "澶囦唤璁剧疆涓虹┖", + "data": nil, + }) + return + } + fd := ab.Dir + "/" + bakDir + if !util.DirExists(fd) { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "褰撳墠澶囦唤鏂囦欢澶瑰凡涓嶅瓨鍦�,璇锋鏌�", + "data": nil, + }) + return + } + //鎵ц杩樺師鎿嶄綔 + err := service.DoBackupRecover(fd) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "code": http.StatusInternalServerError, + "msg": "杩樺師澶辫触:" + err.Error(), + "data": nil, + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "杩樺師鎴愬姛", + "data": nil, + }) + } +} -- Gitblit v1.8.0