@@ -1,5 +1,6 @@
|
||||
{
|
||||
"api_ok":0,
|
||||
"api_err":-1,
|
||||
"user_name_dup":1,
|
||||
"user_name_nofind":2,
|
||||
"user_password_err":3,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
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) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
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 {
|
||||
//fmt.Println("找到用户:", user.ID)
|
||||
err_msg = "user_name_dup"
|
||||
err_code = Error_code[err_msg]
|
||||
Return_json(ctx, "user_name_dup", nil)
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
models.DB.Create(&newUser) // 传入指针
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
Return_json(ctx, "api_ok", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
Return_json(ctx, "json_error", nil)
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
//Return_json(ctx, "api_err", nil)
|
||||
|
||||
})
|
||||
|
||||
r.POST("/login", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//转换传进来的数据
|
||||
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{
|
||||
Name: jsonData.Username,
|
||||
@@ -84,8 +75,6 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
|
||||
if user.Pass == newUser.Pass {
|
||||
//成功登录
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//发送cookie
|
||||
//cookie时间
|
||||
var cookie_time = 0
|
||||
@@ -113,28 +102,37 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
|
||||
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 {
|
||||
//密码错误
|
||||
err_msg = "user_password_err"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
Return_json(ctx, "user_password_err", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
err_msg = "user_name_nofind"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
Return_json(ctx, "user_name_nofind", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
Return_json(ctx, "json_error", nil)
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
//Return_json(ctx, "api_err", nil)
|
||||
|
||||
})
|
||||
|
||||
|
||||
@@ -8,13 +8,32 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
func Def_router(r *gin.RouterGroup) {
|
||||
r.Use(func(ctx *gin.Context) {
|
||||
ctx.Set("is_login", false)
|
||||
|
||||
cookie_vel := ""
|
||||
//读取用户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)
|
||||
if cookie_vel != "" {
|
||||
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:{
|
||||
switchTheme(theme){
|
||||
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>
|
||||
|
||||
+11
-1
@@ -1,9 +1,19 @@
|
||||
import { myfunc } from "./myfunc";
|
||||
|
||||
export const my_network_func = {
|
||||
host: "",
|
||||
port: 0,
|
||||
head_path: "/api/v1",
|
||||
|
||||
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 = {}
|
||||
uni.request({
|
||||
header: {
|
||||
@@ -11,7 +21,7 @@ export const my_network_func = {
|
||||
},
|
||||
url: this.head_path + path,
|
||||
method: 'POST',
|
||||
data: json,
|
||||
data: data,
|
||||
timeout: 10000,
|
||||
success(res) {
|
||||
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@
|
||||
right: 0;
|
||||
left: 0;
|
||||
|
||||
height: 100rpx;
|
||||
height: 85rpx;
|
||||
margin-left: 20rpx;
|
||||
margin-right: 20rpx;
|
||||
margin-top: 20rpx;
|
||||
|
||||
+26
-2
@@ -2,15 +2,39 @@ export const myfunc = {
|
||||
|
||||
themeStorageKey:"tablerTheme",
|
||||
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() {
|
||||
var storedTheme = localStorage.getItem(this.themeStorageKey);
|
||||
var storedTheme = this.load(this.themeStorageKey);
|
||||
return storedTheme ? storedTheme : this.defaultTheme;
|
||||
},
|
||||
|
||||
setTheme(selectedTheme,save) {
|
||||
if(save){
|
||||
localStorage.setItem(this.themeStorageKey, selectedTheme); // 保存到本地存储
|
||||
this.save(this.themeStorageKey, selectedTheme); // 保存到本地存储
|
||||
}
|
||||
if (selectedTheme === 'dark') {
|
||||
document.body.setAttribute("data-bs-theme", selectedTheme); // 暗色模式
|
||||
|
||||
+43
-13
@@ -35,14 +35,14 @@
|
||||
maxlength="100" v-model="post_data.password">
|
||||
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<!-- <div class="mb-2">
|
||||
<checkbox-group @change="chack_box_change">
|
||||
<label class="d-flex">
|
||||
<checkbox value="keep_login_in" />
|
||||
<span class="form-check-label">保持登录</span>
|
||||
</label>
|
||||
</checkbox-group>
|
||||
</div>
|
||||
</div> -->
|
||||
<div class="form-footer">
|
||||
<button class="btn btn-primary w-100" @click="submit_data">登录</button>
|
||||
</div>
|
||||
@@ -72,7 +72,7 @@
|
||||
<tabler-footer ref="footer"></tabler-footer>
|
||||
</view>
|
||||
|
||||
|
||||
|
||||
|
||||
</template>
|
||||
|
||||
@@ -80,24 +80,35 @@
|
||||
import {
|
||||
my_network_func
|
||||
} from '../my_network_func'
|
||||
import {
|
||||
myfunc
|
||||
} from '../myfunc'
|
||||
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
ph_username: "输入你的用户名",
|
||||
ph_password: "输入你的密码",
|
||||
ph_username: "",
|
||||
ph_password: "",
|
||||
is_username_err: false,
|
||||
is_password_err: false,
|
||||
post_data: {
|
||||
is_keep_login: false,
|
||||
is_keep_login: true,
|
||||
username: "",
|
||||
password: "",
|
||||
}
|
||||
}
|
||||
},
|
||||
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) {
|
||||
//console.log(val.detail.value[0])
|
||||
@@ -110,8 +121,8 @@
|
||||
submit_data() {
|
||||
//提交登录数据,
|
||||
//先验证数据合法性
|
||||
this.$refs.footer.alert('success',"123")
|
||||
|
||||
|
||||
|
||||
if (this.post_data.username == "") {
|
||||
this.is_username_err = true
|
||||
this.ph_username = "用户名不能为空"
|
||||
@@ -132,14 +143,35 @@
|
||||
console.log(this.post_data)
|
||||
my_network_func.post_json("/user/login", this.post_data, (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 {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.initdata()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -157,6 +189,4 @@
|
||||
font-size: 24px;
|
||||
|
||||
}
|
||||
|
||||
|
||||
</style>
|
||||
Reference in New Issue
Block a user