Index.cshtml
7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
@using HHECS.Web.Resources
@using HHECS.WebCommon.Config
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IViewLocalizer L
@inject IStringLocalizer<SharedResource> SR
@{
Layout = null;
// 获取当前文化
//var currentCulture = System.Globalization.CultureInfo.CurrentCulture.Name.ToLower();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>@ConfigRead.GetInstance.GetAppsetConnection().LoginTitle</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="~/lib/layuimini/lib/layui-v2.6.3/css/layui.css" media="all" />
<link rel="stylesheet" href="~/css/login.css" media="all" />
</head>
<body class="main">
<div id="mainBody">
<div id="cloud1" class="cloud" style="background-position: 891.1px 100px;"></div>
<div id="cloud2" class="cloud" style="background-position: 401px 460px;"></div>
</div>
<div class="logintop">
<form class="layui-form" style="display:flex;justify-content: space-between;" lay-filter="languageFrom-filter">
<ul id="Img">
<li><img src="~/images/logo.png" style="height:38px;" /></li>
<li>
<img src="~/images/logo1.png" style="height: 8px; width: 62px;" />
</li>
</ul>
<div>
<div class="layui-input-inline" style="width:80px;padding-top:3px;">
<select id="cultureSelector" name="cultureSelector" lay-filter="languageSelect">
<option value="en-us">English</option>
<option value="zh-cn">中文</option>
</select>
</div>
<ul>
<li> <div style="color: aliceblue ;text-align:right;">v:V1.0</div></li>
</ul>
</div>
</form>
</div>
<div class="loginbody">
<span class="systemlogo"></span>
<div class="loginbox" style="position: absolute; left: 40%;top:80% ;">
@*<div class="loginbox" style="position: absolute; left: 40%;top:20% ;">*@
@*<lable class="ver">登录</lable>*@
<div class="layui-tab-brief" style="margin-top:30px;justify-content: center;display: flex;" lay-filter="loginWebcam">
@* <ul class="layui-tab-title">
<li class="layui-this" lay-id="1">默认</li>
</ul>*@
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<form class="layui-form">
<ul>
<li>
<input name="username" type="text" class="loginuser ipt" placeholder="@L["user"]" autocomplete="on" lay-verify="required" autofocus="autofocus">
</li>
<li>
<input name="password" type="password" autocomplete="new-password" class="loginpwd ipt" placeholder="@L["pwd"]" lay-verify="required">
</li>
<li>
<button class="layui-btn loginbtn" style=" border-radius: 12px; margin-left:-120px; width:180px;height:45px;" lay-submit lay-filter="login">@L["logIn"]</button>
</li>
</ul>
</form>
<ul>
<li>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="color:#F5F559;" class="loginTitle">@SR["company"]</div>
<div style="color:#FFFFFF;" class="loginbm">@ViewBag.copyright</div>
<script type="text/javascript" src="~/lib/layuimini/lib/layui-v2.6.3/layui.js"></script>
<script type="text/javascript" src="/js/login.js" asp-append-version="true"></script>
<script>
// 初始化 LayUI
layui.use(['form', 'layer'], function(){
var form = layui.form;
var layer = layui.layer;
// 从 Cookie 中获取当前设置的语言
var currentCultureValue =getCurrentCultureFromCookie();
// 2设置下拉框的当前选中项
form.val('languageFrom-filter', { cultureSelector:currentCultureValue });
form.on('select(languageSelect)', function(data){
var culture = data.value;
// 使用 AJAX 请求切换语言
$.ajax({
url: '/Login/SetCulture', // 正确的路径,避免重复
type: 'post',
data: {
culture: culture,
returnUrl: window.location.pathname
},
success: function(response) {
// 请求成功后刷新页面
layer.msg('语言已切换', {icon: 1});
setTimeout(function() {
window.location.reload();
}, 500);
},
error: function(xhr, status, error) {
layer.msg('切换语言失败,请重试', {icon: 2});
// 恢复下拉框到之前的状态
cultureSelector.value = currentCultureValue;
form.render('select');
}
});
});
});
// 全局资源也可以在 JS 中使用
var resources = {
user: '@L["user"]',
company: '@SR["company"]'
};
// 获取 Cookie 值的辅助函数
function getCookieValue(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length === 2) {
return parts.pop().split(";").shift();
}
return null;
}
// 从 Cookie 解析当前文化的函数
function getCurrentCultureFromCookie() {
var cookieValue = getCookieValue('.AspNetCore.Culture');
if (!cookieValue) return 'en-us'; // 默认英文
try {
// 解码 URL 编码
cookieValue = decodeURIComponent(cookieValue);
} catch (e) {
console.warn("解码 Cookie 失败,使用原始值:", cookieValue);
}
var culture = 'en-us';
// 方式1:正则表达式
var match = cookieValue.match(/c=([^|;]+)/i);
if (match && match[1]) {
culture = match[1].toLowerCase();
} else {
// 方式2:split 方法
var parts = cookieValue.split('|');
for (var i = 0; i < parts.length; i++) {
var part = parts[i].trim();
if (part.toLowerCase().startsWith('c=')) {
culture = part.substring(2).toLowerCase();
break;
}
}
}
return culture;
}
// 使用方式:
var currentCultureValue = getCurrentCultureFromCookie();
</script>
</body>
</html>