@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"api_ok":0,
|
"api_ok":0,
|
||||||
|
"api_err":-1,
|
||||||
"user_name_dup":1,
|
"user_name_dup":1,
|
||||||
"user_name_nofind":2,
|
"user_name_nofind":2,
|
||||||
"user_password_err":3,
|
"user_password_err":3,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
func V1_user_api(r *gin.RouterGroup) {
|
func V1_user_api(r *gin.RouterGroup) {
|
||||||
@@ -15,8 +16,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
r.POST("/add", func(ctx *gin.Context) {
|
r.POST("/add", func(ctx *gin.Context) {
|
||||||
//返回前端的数据
|
//返回前端的数据
|
||||||
err_msg = "user_api_error"
|
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
//转换传进来的数据
|
//转换传进来的数据
|
||||||
var jsonData map[string]interface{}
|
var jsonData map[string]interface{}
|
||||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||||
@@ -37,36 +37,27 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
if models.DB.Where(&user).First(&user).Error == nil {
|
if models.DB.Where(&user).First(&user).Error == nil {
|
||||||
//fmt.Println("找到用户:", user.ID)
|
//fmt.Println("找到用户:", user.ID)
|
||||||
err_msg = "user_name_dup"
|
Return_json(ctx, "user_name_dup", nil)
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("用户不存在")
|
//fmt.Println("用户不存在")
|
||||||
models.DB.Create(&newUser) // 传入指针
|
models.DB.Create(&newUser) // 传入指针
|
||||||
err_msg = "api_ok"
|
Return_json(ctx, "api_ok", nil)
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err_msg = "json_error"
|
Return_json(ctx, "json_error", nil)
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
}
|
}
|
||||||
|
//Return_json(ctx, "api_err", nil)
|
||||||
ctx.JSON(200, map[string]interface{}{
|
|
||||||
"api": "ok",
|
|
||||||
"err_code": err_code,
|
|
||||||
"err_msg": err_msg,
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.POST("/login", func(ctx *gin.Context) {
|
r.POST("/login", func(ctx *gin.Context) {
|
||||||
//返回前端的数据
|
//返回前端的数据
|
||||||
err_msg = "user_api_error"
|
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
|
|
||||||
//转换传进来的数据
|
//转换传进来的数据
|
||||||
var jsonData Login_from
|
var jsonData Login_from
|
||||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
data, _ := ctx.Get("data")
|
||||||
|
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||||
//转换字段
|
//转换字段
|
||||||
newUser := models.User{
|
newUser := models.User{
|
||||||
Name: jsonData.Username,
|
Name: jsonData.Username,
|
||||||
@@ -84,8 +75,6 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
if user.Pass == newUser.Pass {
|
if user.Pass == newUser.Pass {
|
||||||
//成功登录
|
//成功登录
|
||||||
err_msg = "api_ok"
|
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
//发送cookie
|
//发送cookie
|
||||||
//cookie时间
|
//cookie时间
|
||||||
var cookie_time = 0
|
var cookie_time = 0
|
||||||
@@ -113,28 +102,37 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
models.DB.Create(&new_cookie) // 传入指针
|
models.DB.Create(&new_cookie) // 传入指针
|
||||||
|
|
||||||
|
//获取用户info
|
||||||
|
user_info := models.User_info{
|
||||||
|
UserID: user.ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
models.DB.Where(&user_info).First(&user_info)
|
||||||
|
|
||||||
|
red := map[string]interface{}{
|
||||||
|
"cookie": new_cookie,
|
||||||
|
"user_info": user_info,
|
||||||
|
}
|
||||||
|
|
||||||
|
Return_json(ctx, "api_ok", red)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//密码错误
|
//密码错误
|
||||||
err_msg = "user_password_err"
|
|
||||||
err_code = Error_code[err_msg]
|
Return_json(ctx, "user_password_err", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("用户不存在")
|
//fmt.Println("用户不存在")
|
||||||
err_msg = "user_name_nofind"
|
|
||||||
err_code = Error_code[err_msg]
|
Return_json(ctx, "user_name_nofind", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err_msg = "json_error"
|
Return_json(ctx, "json_error", nil)
|
||||||
err_code = Error_code[err_msg]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(200, map[string]interface{}{
|
//Return_json(ctx, "api_err", nil)
|
||||||
"api": "ok",
|
|
||||||
"err_code": err_code,
|
|
||||||
"err_msg": err_msg,
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,32 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Def_router(r *gin.RouterGroup) {
|
func Def_router(r *gin.RouterGroup) {
|
||||||
r.Use(func(ctx *gin.Context) {
|
r.Use(func(ctx *gin.Context) {
|
||||||
ctx.Set("is_login", false)
|
ctx.Set("is_login", false)
|
||||||
|
|
||||||
|
cookie_vel := ""
|
||||||
//读取用户cookie,判断用户是否已登录
|
//读取用户cookie,判断用户是否已登录
|
||||||
cookie_vel, _ := ctx.Cookie("user")
|
cookie_s, _ := ctx.Cookie("user")
|
||||||
|
cookie_vel = cookie_s
|
||||||
|
//转换传进来的数据
|
||||||
|
var jsonData map[string]interface{}
|
||||||
|
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||||
|
//分离数据
|
||||||
|
var cookie_t models.Cookie
|
||||||
|
if err = mapstructure.Decode(jsonData["cookie"], &cookie_t); err == nil {
|
||||||
|
cookie_vel = cookie_t.Value
|
||||||
|
}
|
||||||
|
var data_t map[string]interface{}
|
||||||
|
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
||||||
|
ctx.Set("data", data_t)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//fmt.Println(cookie_vel)
|
//fmt.Println(cookie_vel)
|
||||||
if cookie_vel != "" {
|
if cookie_vel != "" {
|
||||||
var cookie models.Cookie
|
var cookie models.Cookie
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package routers
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func Return_json(ctx *gin.Context, err_msg string, data map[string]interface{}) {
|
||||||
|
var err_code = Error_code[err_msg]
|
||||||
|
ctx.JSON(200, map[string]interface{}{
|
||||||
|
"err_code": err_code,
|
||||||
|
"err_msg": err_msg,
|
||||||
|
"return": data,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
@@ -159,7 +159,23 @@
|
|||||||
methods:{
|
methods:{
|
||||||
switchTheme(theme){
|
switchTheme(theme){
|
||||||
myfunc.setTheme(theme,true);
|
myfunc.setTheme(theme,true);
|
||||||
}
|
},
|
||||||
|
logout(){
|
||||||
|
myfunc.dele("user_info")
|
||||||
|
myfunc.dele("cookie")
|
||||||
|
},
|
||||||
|
updata_user_info_to_heard(){
|
||||||
|
//h5先判断有没有cookie
|
||||||
|
var cookie=myfunc.load_json("cookie")
|
||||||
|
if (cookie){
|
||||||
|
//判断cookie有没有过期
|
||||||
|
}else{
|
||||||
|
this.logout()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this.updata_user_info_to_heard()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
+11
-1
@@ -1,9 +1,19 @@
|
|||||||
|
import { myfunc } from "./myfunc";
|
||||||
|
|
||||||
export const my_network_func = {
|
export const my_network_func = {
|
||||||
host: "",
|
host: "",
|
||||||
port: 0,
|
port: 0,
|
||||||
head_path: "/api/v1",
|
head_path: "/api/v1",
|
||||||
|
|
||||||
post_json(path, json,callback) {
|
post_json(path, json,callback) {
|
||||||
|
//把cookie插入json
|
||||||
|
var data={}
|
||||||
|
data['data']=json
|
||||||
|
var cookie =myfunc.load_json("cookie")
|
||||||
|
if(cookie)
|
||||||
|
{
|
||||||
|
data['cookie']=cookie
|
||||||
|
}
|
||||||
var re_data = {}
|
var re_data = {}
|
||||||
uni.request({
|
uni.request({
|
||||||
header: {
|
header: {
|
||||||
@@ -11,7 +21,7 @@ export const my_network_func = {
|
|||||||
},
|
},
|
||||||
url: this.head_path + path,
|
url: this.head_path + path,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: json,
|
data: data,
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
success(res) {
|
success(res) {
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -30,7 +30,7 @@
|
|||||||
right: 0;
|
right: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
|
|
||||||
height: 100rpx;
|
height: 85rpx;
|
||||||
margin-left: 20rpx;
|
margin-left: 20rpx;
|
||||||
margin-right: 20rpx;
|
margin-right: 20rpx;
|
||||||
margin-top: 20rpx;
|
margin-top: 20rpx;
|
||||||
|
|||||||
+26
-2
@@ -2,15 +2,39 @@ export const myfunc = {
|
|||||||
|
|
||||||
themeStorageKey:"tablerTheme",
|
themeStorageKey:"tablerTheme",
|
||||||
defaultTheme:"light",
|
defaultTheme:"light",
|
||||||
|
|
||||||
|
save(key,data){
|
||||||
|
uni.setStorageSync(key, data)
|
||||||
|
},
|
||||||
|
load(key){
|
||||||
|
return uni.getStorageSync(key)
|
||||||
|
},
|
||||||
|
dele(key){
|
||||||
|
uni.removeStorageSync(key)
|
||||||
|
},
|
||||||
|
|
||||||
|
save_json(key,data){
|
||||||
|
this.save(key,JSON.stringify(data))
|
||||||
|
},
|
||||||
|
|
||||||
|
load_json(key){
|
||||||
|
var js_data=this.load(key)
|
||||||
|
if(js_data){
|
||||||
|
return JSON.parse(js_data)
|
||||||
|
}else{
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
getThemefromStorge() {
|
getThemefromStorge() {
|
||||||
var storedTheme = localStorage.getItem(this.themeStorageKey);
|
var storedTheme = this.load(this.themeStorageKey);
|
||||||
return storedTheme ? storedTheme : this.defaultTheme;
|
return storedTheme ? storedTheme : this.defaultTheme;
|
||||||
},
|
},
|
||||||
|
|
||||||
setTheme(selectedTheme,save) {
|
setTheme(selectedTheme,save) {
|
||||||
if(save){
|
if(save){
|
||||||
localStorage.setItem(this.themeStorageKey, selectedTheme); // 保存到本地存储
|
this.save(this.themeStorageKey, selectedTheme); // 保存到本地存储
|
||||||
}
|
}
|
||||||
if (selectedTheme === 'dark') {
|
if (selectedTheme === 'dark') {
|
||||||
document.body.setAttribute("data-bs-theme", selectedTheme); // 暗色模式
|
document.body.setAttribute("data-bs-theme", selectedTheme); // 暗色模式
|
||||||
|
|||||||
+43
-13
@@ -35,14 +35,14 @@
|
|||||||
maxlength="100" v-model="post_data.password">
|
maxlength="100" v-model="post_data.password">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-2">
|
<!-- <div class="mb-2">
|
||||||
<checkbox-group @change="chack_box_change">
|
<checkbox-group @change="chack_box_change">
|
||||||
<label class="d-flex">
|
<label class="d-flex">
|
||||||
<checkbox value="keep_login_in" />
|
<checkbox value="keep_login_in" />
|
||||||
<span class="form-check-label">保持登录</span>
|
<span class="form-check-label">保持登录</span>
|
||||||
</label>
|
</label>
|
||||||
</checkbox-group>
|
</checkbox-group>
|
||||||
</div>
|
</div> -->
|
||||||
<div class="form-footer">
|
<div class="form-footer">
|
||||||
<button class="btn btn-primary w-100" @click="submit_data">登录</button>
|
<button class="btn btn-primary w-100" @click="submit_data">登录</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -72,7 +72,7 @@
|
|||||||
<tabler-footer ref="footer"></tabler-footer>
|
<tabler-footer ref="footer"></tabler-footer>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -80,24 +80,35 @@
|
|||||||
import {
|
import {
|
||||||
my_network_func
|
my_network_func
|
||||||
} from '../my_network_func'
|
} from '../my_network_func'
|
||||||
|
import {
|
||||||
|
myfunc
|
||||||
|
} from '../myfunc'
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
ph_username: "输入你的用户名",
|
ph_username: "",
|
||||||
ph_password: "输入你的密码",
|
ph_password: "",
|
||||||
is_username_err: false,
|
is_username_err: false,
|
||||||
is_password_err: false,
|
is_password_err: false,
|
||||||
post_data: {
|
post_data: {
|
||||||
is_keep_login: false,
|
is_keep_login: true,
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
initdata(){
|
||||||
|
this.ph_username="输入你的用户名"
|
||||||
|
this.ph_password="输入你的密码"
|
||||||
|
this.is_username_err=false
|
||||||
|
this.is_password_err=false
|
||||||
|
this.post_data.is_keep_login=true
|
||||||
|
this.post_data.username=""
|
||||||
|
this.post_data.password=""
|
||||||
|
},
|
||||||
|
|
||||||
chack_box_change(val) {
|
chack_box_change(val) {
|
||||||
//console.log(val.detail.value[0])
|
//console.log(val.detail.value[0])
|
||||||
@@ -110,8 +121,8 @@
|
|||||||
submit_data() {
|
submit_data() {
|
||||||
//提交登录数据,
|
//提交登录数据,
|
||||||
//先验证数据合法性
|
//先验证数据合法性
|
||||||
this.$refs.footer.alert('success',"123")
|
|
||||||
|
|
||||||
if (this.post_data.username == "") {
|
if (this.post_data.username == "") {
|
||||||
this.is_username_err = true
|
this.is_username_err = true
|
||||||
this.ph_username = "用户名不能为空"
|
this.ph_username = "用户名不能为空"
|
||||||
@@ -132,14 +143,35 @@
|
|||||||
console.log(this.post_data)
|
console.log(this.post_data)
|
||||||
my_network_func.post_json("/user/login", this.post_data, (c) => {
|
my_network_func.post_json("/user/login", this.post_data, (c) => {
|
||||||
console.log(c)
|
console.log(c)
|
||||||
|
if (c.statusCode == 200) {
|
||||||
|
if (c.data.err_code == 0) {
|
||||||
|
this.$refs.footer.alert('success', "登录成功")
|
||||||
|
myfunc.save_json("cookie", c.data.return.cookie)
|
||||||
|
myfunc.save_json("user_info", c.data.return.user_info)
|
||||||
|
this.initdata()
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.navigateTo({
|
||||||
|
url: '/'
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this.$refs.footer.alert('warning', "账号或密码不正确")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.$refs.footer.alert('danger', "网络连接错误:" + c.statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
this.initdata()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -157,6 +189,4 @@
|
|||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
Reference in New Issue
Block a user