refactor: database init module
This commit is contained in:
+14
-4
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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",
|
||||
|
||||
+14
-26
@@ -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)
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
+22
-21
@@ -35,30 +35,43 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { reactive } from 'vue';
|
||||
import { reactive, onMounted } from 'vue';
|
||||
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useRouter } from 'vue-router';
|
||||
import { userLogin } from '../api/user';
|
||||
import { message } from 'ant-design-vue';
|
||||
import { initData } from '../api/common';
|
||||
import { initDatabase } from '../api/common';
|
||||
|
||||
// 初始化数据库(只会在生产环境中初始化)
|
||||
onMounted(() => {
|
||||
if (formData.email == '1655064994@qq.com') {
|
||||
message.loading('正在初始化数据...', 2.5).then(() => {
|
||||
initDatabase().then((res) => {
|
||||
if (res.data.code == 0) {
|
||||
message.success('初始化成功!', 2)
|
||||
}
|
||||
if (res.data.code == 10) {
|
||||
message.error('初始化失败!')
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
// 用户登录
|
||||
const formData = reactive({
|
||||
email: '1655064994@qq.com',
|
||||
password: '1655064994',
|
||||
remember: true,
|
||||
});
|
||||
})
|
||||
|
||||
// 用户登录
|
||||
const onLogin = () => {
|
||||
let param = {
|
||||
email: formData.email,
|
||||
password: formData.password
|
||||
}
|
||||
// 初始化数据
|
||||
if (formData.email == '1655064994@qq.com') {
|
||||
initSysData()
|
||||
}
|
||||
userLogin(param).then((res) => {
|
||||
if (res.data.code == 0) {
|
||||
localStorage.setItem('uid', res.data.data.uid)
|
||||
@@ -86,18 +99,6 @@ const forgotPass = () => {
|
||||
const toRegister = () => {
|
||||
router.push("/register")
|
||||
}
|
||||
|
||||
// 初始化数据(只会在生产环境中初始化)
|
||||
const initSysData = () => {
|
||||
initData().then((res) => {
|
||||
if (res.data.code == 10) {
|
||||
message.success('初始化数据成功!')
|
||||
}
|
||||
if (res.data.code == 11) {
|
||||
message.error('初始化数据失败!')
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user