增加一个安装界面

pull/23/head
taoshihan1991 4 years ago
parent f14bdd646e
commit 164ca98b44

@ -1,7 +1,6 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
@ -21,11 +20,15 @@ var installCmd = &cobra.Command{
}
func install() {
if ok, _ := tools.IsFileNotExist("./install.lock"); !ok {
log.Println("请先删除./install.lock")
os.Exit(1)
}
sqlFile := common.Dir + "go-fly.sql"
isExit, _ := tools.IsFileExist(common.MysqlConf)
dataExit, _ := tools.IsFileExist(sqlFile)
if !isExit || !dataExit {
fmt.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在")
log.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在")
os.Exit(1)
}
sqls, _ := ioutil.ReadFile(sqlFile)
@ -42,4 +45,6 @@ func install() {
os.Exit(1)
}
}
installFile, _ := os.OpenFile("./install.lock", os.O_RDWR|os.O_CREATE, os.ModePerm)
installFile.WriteString("gofly live chat")
}

@ -1,5 +1,5 @@
{
"Server":"127.0.0.1",
"Server":"localhost",
"Port":"3306",
"Database":"go-fly",
"Username":"go-fly",

@ -1,11 +1,100 @@
package controller
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
"io/ioutil"
"log"
"os"
"strings"
)
func PostInstall(c *gin.Context) {
notExist, _ := tools.IsFileNotExist("./install.lock")
if !notExist {
c.JSON(200, gin.H{
"code": 400,
"msg": "系统已经安装过了",
})
return
}
server := c.PostForm("server")
port := c.PostForm("port")
database := c.PostForm("database")
username := c.PostForm("username")
password := c.PostForm("password")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, server, port, database)
_, err := gorm.Open("mysql", dsn)
if err != nil {
log.Println(err)
tools.Logger().Println(err)
c.JSON(200, gin.H{
"code": 400,
"msg": "数据库连接失败:" + err.Error(),
})
return
}
isExist, _ := tools.IsFileExist(common.Dir)
if !isExist {
os.Mkdir(common.Dir, os.ModePerm)
}
fileConfig := common.MysqlConf
file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm)
format := `{
"Server":"%s",
"Port":"%s",
"Database":"%s",
"Username":"%s",
"Password":"%s"
}
`
data := fmt.Sprintf(format, server, port, database, username, password)
file.WriteString(data)
models.Connect()
installFile, _ := os.OpenFile("./install.lock", os.O_RDWR|os.O_CREATE, os.ModePerm)
installFile.WriteString("gofly live chat")
ok, err := install()
if !ok {
c.JSON(200, gin.H{
"code": 200,
"msg": err.Error(),
})
return
}
c.JSON(200, gin.H{
"code": 200,
"msg": "安装成功",
})
}
func install() (bool, error) {
sqlFile := common.Dir + "go-fly.sql"
isExit, _ := tools.IsFileExist(common.MysqlConf)
dataExit, _ := tools.IsFileExist(sqlFile)
if !isExit || !dataExit {
return false, errors.New("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在")
}
sqls, _ := ioutil.ReadFile(sqlFile)
sqlArr := strings.Split(string(sqls), "|")
for _, sql := range sqlArr {
if sql == "" {
continue
}
err := models.Execute(sql)
if err == nil {
log.Println(sql, "\t success!")
} else {
log.Println(sql, err, "\t failed!")
}
}
return true, nil
}
func MainCheckAuth(c *gin.Context) {
id, _ := c.Get("kefu_id")
userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id)

@ -0,0 +1 @@
gofly live chat

@ -19,21 +19,26 @@ type Model struct {
}
func init() {
Connect()
}
func Connect() error {
mysql := common.GetMysqlConf()
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database)
var err error
DB, err = gorm.Open("mysql", dsn)
if err != nil {
log.Println(err)
panic("数据库连接失败!")
tools.Logger().Println(err)
return err
//panic("数据库连接失败!")
}
DB.SingularTable(true)
DB.LogMode(true)
DB.SetLogger(tools.Logger())
DB.DB().SetMaxIdleConns(10)
DB.DB().SetMaxOpenConns(100)
InitConfig()
return nil
}
func Execute(sql string) error {
return DB.Exec(sql).Error

@ -25,6 +25,7 @@ func InitApiRouter(engine *gin.Engine) {
engine.POST("/check_auth", middleware.JwtApiMiddleware, controller.MainCheckAuth)
engine.GET("/userinfo", middleware.JwtApiMiddleware, controller.GetKefuInfoAll)
engine.POST("/register", middleware.Ipblack, controller.PostKefuRegister)
engine.POST("/install", controller.PostInstall)
//前后聊天
engine.GET("/ws_kefu", middleware.JwtApiMiddleware, ws.NewKefuServer)
engine.GET("/ws_visitor", middleware.Ipblack, ws.NewVisitorServer)

@ -8,6 +8,7 @@ import (
func InitViewRouter(engine *gin.Engine) {
engine.GET("/index_:lang", middleware.SetLanguage, tmpl.PageIndex)
engine.GET("/install", tmpl.PageInstall)
engine.GET("/detail_:page", middleware.SetLanguage, tmpl.PageDetail)
engine.GET("/login", tmpl.PageLogin)
engine.GET("/chat_page", middleware.SetLanguage, tmpl.PageChat)

@ -0,0 +1,162 @@
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="description" content="">
<meta name="author" content="陶士涵">
<title>GOFLY客服系统安装页面</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js"></script>
<style>
body {
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.clear{clear: both;}
.signin {
width: 500px;
padding: 20px;
margin:100px auto;
background: #fff;
-webkit-box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
}
.signin h1,.signin h2,.signin .copyright{
font-weight: normal;
color: #4d627b;
text-align: center;
}
.signin .loginTitle{
font-size: 24px;
}
.signin .loginDesc{
font-size: 14px;
margin-bottom: 15px;
}
.signin .loginDesc .regBtn{
float: left;
}
.signin .loginDesc a{
text-decoration: none;
cursor: pointer;
color: #409EFF;
}
.signin .copyright{
font-size: 12px;
}
@media (max-width: 768px) {
.signin{
width: 90%;
margin:40px auto;
background-color: #f5f5f5;
box-shadow:none;
}
}
</style>
</head>
<body>
<div id="app" class="signin">
<template>
<div class="loginHtml">
<h1 class="loginTitle">GOFLY客服系统安装</h1>
<el-form :model="mysql" :rules="rules" ref="mysql">
<el-form-item prop="server">
<el-input v-model="mysql.server" placeholder="数据库地址"></el-input>
</el-form-item>
<el-form-item prop="port">
<el-input v-model="mysql.port" placeholder="数据库端口"></el-input>
</el-form-item>
<el-form-item prop="database">
<el-input v-model="mysql.database" placeholder="数据库名"></el-input>
</el-form-item>
<el-form-item prop="username">
<el-input v-model="mysql.username" placeholder="数据库用户名"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input v-model="mysql.password" placeholder="数据库密码"></el-input>
</el-form-item>
<el-form-item>
<el-button :disabled="sendDisabled" style="width: 100%" :loading="loading" type="primary" @click="install()">安装</el-button>
</el-form-item>
</el-form>
<div class="clear"></div>
</div>
<p class="copyright">陶士涵版权所有 &copy; 2020-2021 </p>
</template>
</div>
</body>
<script>
new Vue({
el: '#app',
delimiters:["<{","}>"],
data: {
window:window,
mysql:{
server:'',
port:'',
database:'',
username:'',
password:'',
},
rules: {
server: [
{ required: true, message: '数据库地址不能为空', trigger: 'blur' },
],
port: [
{ required: true, message: '数据库端口不能为空', trigger: 'blur' },
],
database: [
{ required: true, message: '数据库名不能为空', trigger: 'blur' },
],
username: [
{ required: true, message: '数据库用户名不能为空', trigger: 'blur' },
],
password: [
{ required: true, message: '数据库密码不能为空', trigger: 'blur' },
],
},
sendDisabled:false,
},
methods: {
//注册
install(){
var _this=this;
this.$refs['mysql'].validate((valid) => {
if (!valid) {
return false;
} else {
var data = _this.mysql;
_this.loading=true;
_this.sendDisabled=true;
$.post("/install", data, function (data) {
if (data.code == 200) {
_this.$message({
message: data.msg,
type: 'success'
});
//window.location.href="/login";
} else {
_this.$message({
message: data.msg,
type: 'error'
});
}
_this.loading=false;
_this.sendDisabled=false;
});
}
});
}
},
created: function () {
}
})
</script>
</html>

@ -49,7 +49,9 @@ func PageIndex(c *gin.Context) {
if c.Request.RequestURI == "/favicon.ico" {
return
}
if noExist, _ := tools.IsFileNotExist("./install.lock"); noExist {
c.Redirect(302, "/install")
}
lang, _ := c.Get("lang")
language := config.CreateLanguage(lang.(string))
about := models.FindAboutByPageLanguage("index", lang.(string))
@ -90,3 +92,11 @@ func PageMain(c *gin.Context) {
func PageChatMain(c *gin.Context) {
c.HTML(http.StatusOK, "chat_main.html", nil)
}
//安装界面
func PageInstall(c *gin.Context) {
if noExist, _ := tools.IsFileNotExist("./install.lock"); !noExist {
c.Redirect(302, "/login")
}
c.HTML(http.StatusOK, "install.html", nil)
}

@ -2,10 +2,14 @@ package tmpl
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools"
"net/http"
)
//登陆界面
func PageLogin(c *gin.Context) {
if noExist, _ := tools.IsFileNotExist("./install.lock"); noExist {
c.Redirect(302, "/install")
}
c.HTML(http.StatusOK, "login.html", nil)
}

@ -2,7 +2,7 @@ package tools
import "os"
//判断文件文件夹是否存在
//判断文件文件夹是否存在(字节0也算不存在)
func IsFileExist(path string) (bool, error) {
fileInfo, err := os.Stat(path)
@ -18,3 +18,12 @@ func IsFileExist(path string) (bool, error) {
}
return false, err
}
//判断文件文件夹不存在
func IsFileNotExist(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return true, nil
}
return false, err
}

Loading…
Cancel
Save