diff --git a/server/api/common.go b/server/api/common.go index 1dd3719..95df107 100644 --- a/server/api/common.go +++ b/server/api/common.go @@ -7,8 +7,18 @@ import ( "github.com/gin-gonic/gin" ) -// 初始化数据 -func InitData(c *gin.Context) { - errCode := service.InitData() - response.Result(errCode, nil, c) +type CommonApi struct { + commonService *service.CommonService +} + +func NewCommonApi() *CommonApi { + return &CommonApi{ + commonService: service.NewCommonService(), + } +} + +// 初始化数据库 +func (c *CommonApi) InitDatabase(context *gin.Context) { + errCode := c.commonService.InitDatabase() + response.Result(errCode, nil, context) } \ No newline at end of file diff --git a/server/dao/common.go b/server/dao/common.go index 8ad134a..80eb6c4 100644 --- a/server/dao/common.go +++ b/server/dao/common.go @@ -4,6 +4,9 @@ import ( "context" "crm/global" "crm/models" + "log" + "os" + "strings" ) const ( @@ -20,6 +23,10 @@ const ( // 空值 NumberNull = 0 StringNull = "" + + // 运行环境 + Dev = "dev" + Prod = "prod" ) var ctx = context.Background() @@ -38,3 +45,37 @@ func restPage(page models.Page, name string, query interface{}, dest interface{} res := global.Db.Table(name).Where(query).Find(bind) return res.RowsAffected, res.Error } + +type CommonDao struct { +} + +func NewCommonDao() *CommonDao { + return &CommonDao{} +} + +func (c *CommonDao) InitDatabase() error { + env := global.Config.Server.Runenv + if env == Prod { + // fn := "/home/ubuntu/crmapi/crm.sql" + // fn := "/Users/xuzxc/Downloads/crm.sql" + dbFile := global.Config.Mysql.DbFile + sql, err := os.ReadFile(dbFile) + if err != nil { + log.Printf("Common.InitDatabase.Error: read file %s error: %s", dbFile, err) + return err + } + sqls := strings.Split(string(sql), ";") + for _, sql := range sqls { + s := strings.TrimSpace(sql) + if s == StringNull { + continue + } + if err := global.Db.Exec(s).Error; err != nil { + log.Printf("Common.InitDatabase.Error: %s", err) + return err + } + } + return nil + } + return nil +} diff --git a/server/initialize/router.go b/server/initialize/router.go index d34893c..65e6df9 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -27,8 +27,8 @@ func Router() { route.DELETE("/user/delete", api.NewUserApi().Delete) route.POST("/subscribe/payback", api.NewSubscribeApi().PayBack) - // 初始化数据 - route.POST("/init/data", api.InitData) + // 通用接口 + route.POST("/common/database/init", api.NewCommonApi().InitDatabase) // Jwt中间件 route.Use(middleware.JwtAuth()) diff --git a/server/response/errcode.go b/server/response/errcode.go index 0e8209b..c11e214 100644 --- a/server/response/errcode.go +++ b/server/response/errcode.go @@ -7,8 +7,7 @@ const ( ErrCodeNoLogin = 3 // 未登录或非法访问 ErrCodeTokenExpire = 4 // Token过期 - ErrCodeInitDataSuccess = 10 // 初始化数据成功 - ErrCodeInitDataFailed = 11 // 初始化数据失败 + ErrCodeInitDataFailed = 10 // 初始化数据失败 ErrCodeUserHasExist = 10001 // 用户已经存在 ErrCodeUserNotExist = 10002 // 用户不存在 @@ -40,8 +39,7 @@ var msg = map[int]string{ ErrCodeParamInvalid: "param invalid", ErrCodeNoLogin: "no login", ErrCodeTokenExpire: "token expire", - ErrCodeInitDataSuccess: "init data success", - ErrCodeInitDataFailed: "init data failed", + ErrCodeInitDataFailed: "data init failed", ErrCodeUserHasExist: "user has exist", ErrCodeUserNotExist: "user not exist", ErrCOdeUserEmailOrPass: "user email or password error", diff --git a/server/service/common.go b/server/service/common.go index 7451ede..0046d3c 100644 --- a/server/service/common.go +++ b/server/service/common.go @@ -1,12 +1,8 @@ package service import ( - "crm/global" + "crm/dao" "crm/response" - "os" - - "log" - "strings" ) const ( @@ -37,28 +33,20 @@ const ( SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版" ) -// 初始化数据库数据 -func InitData() int { - env := global.Config.Server.Runenv - if env == Prod { - fn := "/home/ubuntu/crmapi/crm.sql" - sql, err := os.ReadFile(fn) - if err != nil { - log.Printf("[ERROR] read file %s error: %s", fn, err) - return response.ErrCodeInitDataFailed - } - sqls := strings.Split(string(sql), ";") - for _, sql := range sqls { - s := strings.TrimSpace(sql) - if s == StringNull { - continue - } - if err := global.Db.Exec(s).Error; err != nil { - log.Printf("[ERROR] datebase flie import error: %s", err) - return response.ErrCodeInitDataFailed - } - } - return response.ErrCodeInitDataSuccess +type CommonService struct { + commonDao *dao.CommonDao +} + +func NewCommonService() *CommonService { + return &CommonService{ + commonDao: dao.NewCommonDao(), + } +} + +// 初始化数据库 +func (c *CommonService) InitDatabase() int { + if err := c.commonDao.InitDatabase(); err != nil { + return response.ErrCodeInitDataFailed } return response.ErrCodeSuccess } diff --git a/web/src/api/common.js b/web/src/api/common.js index 56118a2..aa0d0fe 100644 --- a/web/src/api/common.js +++ b/web/src/api/common.js @@ -1,9 +1,9 @@ import request from '../axios/index' -// 初始化数据 -export function initData(param) { +// 初始化数据库 +export function initDatabase(param) { return request({ - url: '/init/data', + url: '/common/database/init', method: 'post', data: param, }) diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index 9701969..8b1a023 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -35,30 +35,43 @@