feat: init data in prod env
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crm/response"
|
||||||
|
"crm/service"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 初始化数据
|
||||||
|
func InitData(c *gin.Context) {
|
||||||
|
errCode := service.InitData()
|
||||||
|
response.Result(errCode, nil, c)
|
||||||
|
}
|
||||||
@@ -25,10 +25,12 @@ func Router() {
|
|||||||
route.POST("/user/login", api.NewUserApi().Login)
|
route.POST("/user/login", api.NewUserApi().Login)
|
||||||
route.POST("/user/register", api.NewUserApi().Register)
|
route.POST("/user/register", api.NewUserApi().Register)
|
||||||
route.POST("/user/pass", api.NewUserApi().ForgotPass)
|
route.POST("/user/pass", api.NewUserApi().ForgotPass)
|
||||||
route.DELETE("/user/logout", api.NewUserApi().Logout)
|
|
||||||
route.DELETE("/user/delete", api.NewUserApi().Delete)
|
route.DELETE("/user/delete", api.NewUserApi().Delete)
|
||||||
route.GET("/subscribe/callback", api.NewSubscribeApi().Callback)
|
route.GET("/subscribe/callback", api.NewSubscribeApi().Callback)
|
||||||
|
|
||||||
|
// 初始化数据
|
||||||
|
route.POST("/init/data", api.InitData)
|
||||||
|
|
||||||
// Jwt中间件
|
// Jwt中间件
|
||||||
route.Use(middleware.JwtAuth())
|
route.Use(middleware.JwtAuth())
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ const (
|
|||||||
ErrCodeNoLogin = 3 // 未登录或非法访问
|
ErrCodeNoLogin = 3 // 未登录或非法访问
|
||||||
ErrCodeTokenExpire = 4 // Token过期
|
ErrCodeTokenExpire = 4 // Token过期
|
||||||
|
|
||||||
|
ErrCodeInitDataSuccess = 10 // 初始化数据成功
|
||||||
|
ErrCodeInitDataFailed = 11 // 初始化数据失败
|
||||||
|
|
||||||
ErrCodeUserHasExist = 10001 // 用户已经存在
|
ErrCodeUserHasExist = 10001 // 用户已经存在
|
||||||
ErrCodeUserNotExist = 10002 // 用户不存在
|
ErrCodeUserNotExist = 10002 // 用户不存在
|
||||||
ErrCOdeUserEmailOrPass = 10003 // 用户邮箱或密码错误
|
ErrCOdeUserEmailOrPass = 10003 // 用户邮箱或密码错误
|
||||||
@@ -26,6 +29,8 @@ 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: "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",
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crm/global"
|
"crm/global"
|
||||||
"crm/models"
|
"crm/models"
|
||||||
|
"crm/response"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -21,6 +25,11 @@ const (
|
|||||||
StringNull = ""
|
StringNull = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Dev = "dev"
|
||||||
|
Prod = "prod"
|
||||||
|
)
|
||||||
|
|
||||||
var ctx = context.Background()
|
var ctx = context.Background()
|
||||||
|
|
||||||
// RestPage 分页查询
|
// RestPage 分页查询
|
||||||
@@ -37,3 +46,29 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 初始化数据库数据
|
||||||
|
func InitData() int {
|
||||||
|
env := global.Config.Server.Runenv
|
||||||
|
if env == Prod {
|
||||||
|
fn := "/home/ubuntu/crmapi/crm.sql"
|
||||||
|
sql, err := ioutil.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 == "" {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import request from '../axios/index'
|
||||||
|
|
||||||
|
// 初始化数据
|
||||||
|
export function initData(param) {
|
||||||
|
return request({
|
||||||
|
url: '/init/data',
|
||||||
|
method: 'post',
|
||||||
|
data: param,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -36,6 +36,7 @@ import { reactive } 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 { initData } from '../api/common';
|
||||||
import { message } from 'ant-design-vue';
|
import { message } from 'ant-design-vue';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -63,6 +64,7 @@ export default {
|
|||||||
localStorage.setItem('ver', res.data.data.ver)
|
localStorage.setItem('ver', res.data.data.ver)
|
||||||
localStorage.setItem('token', res.data.data.token)
|
localStorage.setItem('token', res.data.data.token)
|
||||||
router.push("/home")
|
router.push("/home")
|
||||||
|
initSysData()
|
||||||
}
|
}
|
||||||
if (res.data.code == 10002) {
|
if (res.data.code == 10002) {
|
||||||
message.error('用户不存在');
|
message.error('用户不存在');
|
||||||
@@ -86,6 +88,18 @@ export default {
|
|||||||
router.push("/register")
|
router.push("/register")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 初始化数据(只会在生产环境中初始化)
|
||||||
|
const initSysData = () => {
|
||||||
|
initData().then((res) => {
|
||||||
|
if (res.data.code == 10) {
|
||||||
|
message.success('初始化数据成功!')
|
||||||
|
}
|
||||||
|
if (res.data.code == 11) {
|
||||||
|
message.success('初始化数据失败!')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
formData,
|
formData,
|
||||||
onLogin,
|
onLogin,
|
||||||
|
|||||||
Reference in New Issue
Block a user