refactor: database init module
This commit is contained in:
+14
-4
@@ -7,8 +7,18 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 初始化数据
|
type CommonApi struct {
|
||||||
func InitData(c *gin.Context) {
|
commonService *service.CommonService
|
||||||
errCode := service.InitData()
|
}
|
||||||
response.Result(errCode, nil, c)
|
|
||||||
|
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"
|
"context"
|
||||||
"crm/global"
|
"crm/global"
|
||||||
"crm/models"
|
"crm/models"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,6 +23,10 @@ const (
|
|||||||
// 空值
|
// 空值
|
||||||
NumberNull = 0
|
NumberNull = 0
|
||||||
StringNull = ""
|
StringNull = ""
|
||||||
|
|
||||||
|
// 运行环境
|
||||||
|
Dev = "dev"
|
||||||
|
Prod = "prod"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ctx = context.Background()
|
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)
|
res := global.Db.Table(name).Where(query).Find(bind)
|
||||||
return res.RowsAffected, res.Error
|
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.DELETE("/user/delete", api.NewUserApi().Delete)
|
||||||
route.POST("/subscribe/payback", api.NewSubscribeApi().PayBack)
|
route.POST("/subscribe/payback", api.NewSubscribeApi().PayBack)
|
||||||
|
|
||||||
// 初始化数据
|
// 通用接口
|
||||||
route.POST("/init/data", api.InitData)
|
route.POST("/common/database/init", api.NewCommonApi().InitDatabase)
|
||||||
|
|
||||||
// Jwt中间件
|
// Jwt中间件
|
||||||
route.Use(middleware.JwtAuth())
|
route.Use(middleware.JwtAuth())
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ const (
|
|||||||
ErrCodeNoLogin = 3 // 未登录或非法访问
|
ErrCodeNoLogin = 3 // 未登录或非法访问
|
||||||
ErrCodeTokenExpire = 4 // Token过期
|
ErrCodeTokenExpire = 4 // Token过期
|
||||||
|
|
||||||
ErrCodeInitDataSuccess = 10 // 初始化数据成功
|
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
||||||
ErrCodeInitDataFailed = 11 // 初始化数据失败
|
|
||||||
|
|
||||||
ErrCodeUserHasExist = 10001 // 用户已经存在
|
ErrCodeUserHasExist = 10001 // 用户已经存在
|
||||||
ErrCodeUserNotExist = 10002 // 用户不存在
|
ErrCodeUserNotExist = 10002 // 用户不存在
|
||||||
@@ -40,8 +39,7 @@ var msg = map[int]string{
|
|||||||
ErrCodeParamInvalid: "param invalid",
|
ErrCodeParamInvalid: "param invalid",
|
||||||
ErrCodeNoLogin: "no login",
|
ErrCodeNoLogin: "no login",
|
||||||
ErrCodeTokenExpire: "token expire",
|
ErrCodeTokenExpire: "token expire",
|
||||||
ErrCodeInitDataSuccess: "init data success",
|
ErrCodeInitDataFailed: "data init failed",
|
||||||
ErrCodeInitDataFailed: "init data failed",
|
|
||||||
ErrCodeUserHasExist: "user has exist",
|
ErrCodeUserHasExist: "user has exist",
|
||||||
ErrCodeUserNotExist: "user not exist",
|
ErrCodeUserNotExist: "user not exist",
|
||||||
ErrCOdeUserEmailOrPass: "user email or password error",
|
ErrCOdeUserEmailOrPass: "user email or password error",
|
||||||
|
|||||||
+14
-26
@@ -1,12 +1,8 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crm/global"
|
"crm/dao"
|
||||||
"crm/response"
|
"crm/response"
|
||||||
"os"
|
|
||||||
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -37,28 +33,20 @@ const (
|
|||||||
SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版"
|
SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 初始化数据库数据
|
type CommonService struct {
|
||||||
func InitData() int {
|
commonDao *dao.CommonDao
|
||||||
env := global.Config.Server.Runenv
|
}
|
||||||
if env == Prod {
|
|
||||||
fn := "/home/ubuntu/crmapi/crm.sql"
|
func NewCommonService() *CommonService {
|
||||||
sql, err := os.ReadFile(fn)
|
return &CommonService{
|
||||||
if err != nil {
|
commonDao: dao.NewCommonDao(),
|
||||||
log.Printf("[ERROR] read file %s error: %s", fn, err)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化数据库
|
||||||
|
func (c *CommonService) InitDatabase() int {
|
||||||
|
if err := c.commonDao.InitDatabase(); err != nil {
|
||||||
return response.ErrCodeInitDataFailed
|
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
|
return response.ErrCodeSuccess
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import request from '../axios/index'
|
import request from '../axios/index'
|
||||||
|
|
||||||
// 初始化数据
|
// 初始化数据库
|
||||||
export function initData(param) {
|
export function initDatabase(param) {
|
||||||
return request({
|
return request({
|
||||||
url: '/init/data',
|
url: '/common/database/init',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
data: param,
|
data: param,
|
||||||
})
|
})
|
||||||
|
|||||||
+22
-21
@@ -35,30 +35,43 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { reactive } from 'vue';
|
import { reactive, onMounted } from 'vue';
|
||||||
import { UserOutlined, LockOutlined } from '@ant-design/icons-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 { userLogin } from '../api/user';
|
||||||
import { message } from 'ant-design-vue';
|
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 router = useRouter()
|
||||||
|
|
||||||
// 用户登录
|
|
||||||
const formData = reactive({
|
const formData = reactive({
|
||||||
email: '1655064994@qq.com',
|
email: '1655064994@qq.com',
|
||||||
password: '1655064994',
|
password: '1655064994',
|
||||||
remember: true,
|
remember: true,
|
||||||
});
|
})
|
||||||
|
|
||||||
|
// 用户登录
|
||||||
const onLogin = () => {
|
const onLogin = () => {
|
||||||
let param = {
|
let param = {
|
||||||
email: formData.email,
|
email: formData.email,
|
||||||
password: formData.password
|
password: formData.password
|
||||||
}
|
}
|
||||||
// 初始化数据
|
|
||||||
if (formData.email == '1655064994@qq.com') {
|
|
||||||
initSysData()
|
|
||||||
}
|
|
||||||
userLogin(param).then((res) => {
|
userLogin(param).then((res) => {
|
||||||
if (res.data.code == 0) {
|
if (res.data.code == 0) {
|
||||||
localStorage.setItem('uid', res.data.data.uid)
|
localStorage.setItem('uid', res.data.data.uid)
|
||||||
@@ -86,18 +99,6 @@ const forgotPass = () => {
|
|||||||
const toRegister = () => {
|
const toRegister = () => {
|
||||||
router.push("/register")
|
router.push("/register")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化数据(只会在生产环境中初始化)
|
|
||||||
const initSysData = () => {
|
|
||||||
initData().then((res) => {
|
|
||||||
if (res.data.code == 10) {
|
|
||||||
message.success('初始化数据成功!')
|
|
||||||
}
|
|
||||||
if (res.data.code == 11) {
|
|
||||||
message.error('初始化数据失败!')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
Reference in New Issue
Block a user