Commit 2a7e5bb0a07cebcd77b33854e2ee9259db63fbb0
1 parent
16f21b8b
Exists in
develop
feat:根据ip地址控制显示语言系统
Showing
5 changed files
with
133 additions
and
61 deletions
Show diff stats
@@ -0,0 +1,13 @@ | @@ -0,0 +1,13 @@ | ||
1 | +const { http } = uni.$u | ||
2 | +import config from "@/common/config.js" | ||
3 | + | ||
4 | +export const getIp = () => http.get(`https://ipinfo.io/ip`) | ||
5 | + | ||
6 | +/* 通过ip查询 */ | ||
7 | +// export const getIpToCountry = ip => http.get(`https://ipinfo.io/${ip}?token=${config.ipToken}`) | ||
8 | +// export const getIpToCountry = ip => http.get(`https://ipinfo.io/1.208.106.0?token=${config.ipToken}`) | ||
9 | + | ||
10 | +/* 直接访问查询 */ | ||
11 | +// export const getIpToCountry = ip => http.get(`https://ipinfo.io/widget`) | ||
12 | + | ||
13 | +export const getIpToCountry = ip => http.get(`https://ipinfo.io/${ip}?token=${config.ipToken}`) | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
common/config.js
@@ -16,6 +16,8 @@ module.exports = { | @@ -16,6 +16,8 @@ module.exports = { | ||
16 | prefixUrl:"https://qr.hdk-aero.com", //仅带前缀,用于拼接维护报修页图片预览地址 | 16 | prefixUrl:"https://qr.hdk-aero.com", //仅带前缀,用于拼接维护报修页图片预览地址 |
17 | fileUrl:"https://qr.hdk-aero.com/hdk", | 17 | fileUrl:"https://qr.hdk-aero.com/hdk", |
18 | baseUrl: 'https://qr.hdk-aero.com/admin-api', | 18 | baseUrl: 'https://qr.hdk-aero.com/admin-api', |
19 | + // 查询ip归属地 | ||
20 | + ipToken:'eb98f48bf288e4', | ||
19 | // 超时 | 21 | // 超时 |
20 | timeout: 30000, | 22 | timeout: 30000, |
21 | // 禁用 Cookie 等信息 | 23 | // 禁用 Cookie 等信息 |
main.js
@@ -5,8 +5,8 @@ import uView from '@/uni_modules/uview-ui' | @@ -5,8 +5,8 @@ import uView from '@/uni_modules/uview-ui' | ||
5 | import { deviceMixin } from '@/utils/utils'; | 5 | import { deviceMixin } from '@/utils/utils'; |
6 | 6 | ||
7 | let i18nConfig = { | 7 | let i18nConfig = { |
8 | - // locale: uni.getLocale(), | ||
9 | - locale: 'en', //默认英文 | 8 | + locale: uni.getLocale(), |
9 | + // locale: 'en', //默认英文 | ||
10 | messages | 10 | messages |
11 | } | 11 | } |
12 | 12 |
pages/component/topWindow.vue
@@ -21,9 +21,9 @@ | @@ -21,9 +21,9 @@ | ||
21 | </b-nav-item> | 21 | </b-nav-item> |
22 | <b-nav-item-dropdown :text="selectedLanguage" right> | 22 | <b-nav-item-dropdown :text="selectedLanguage" right> |
23 | <template #button-content> | 23 | <template #button-content> |
24 | - <img src="@/static/images/icon/language.png" width="17px" height="17px" | ||
25 | - alt="Kitten" style="margin:2px 2px 5px 2px;"> | ||
26 | - <text>{{selectedLanguage}}</text> | 24 | + <img src="@/static/images/icon/language.png" width="17px" height="17px" alt="Kitten" |
25 | + style="margin:2px 2px 5px 2px;"> | ||
26 | + <text>{{selectedLanguage}}</text> | ||
27 | </template> | 27 | </template> |
28 | <b-dropdown-item href="#" @click="changeLanguage('en')">{{$t('locale.en')}} | 28 | <b-dropdown-item href="#" @click="changeLanguage('en')">{{$t('locale.en')}} |
29 | </b-dropdown-item> | 29 | </b-dropdown-item> |
@@ -37,6 +37,10 @@ | @@ -37,6 +37,10 @@ | ||
37 | </template> | 37 | </template> |
38 | 38 | ||
39 | <script> | 39 | <script> |
40 | + import { | ||
41 | + getIp, | ||
42 | + getIpToCountry | ||
43 | + } from '@/api/ip.js' | ||
40 | export default { | 44 | export default { |
41 | data() { | 45 | data() { |
42 | return { | 46 | return { |
@@ -50,23 +54,71 @@ | @@ -50,23 +54,71 @@ | ||
50 | default: true | 54 | default: true |
51 | }, | 55 | }, |
52 | }, | 56 | }, |
53 | - mounted() { | 57 | + async created() { |
58 | + // try { | ||
59 | + // const response = await uni.request({ | ||
60 | + // url: 'https://ipinfo.io/ip', | ||
61 | + // method: 'GET' | ||
62 | + // }); | ||
63 | + // this.userIP = response[1].data.trim(); | ||
64 | + // console.log("ip地址",this.userIP); | ||
65 | + // // if(this.userIP){ | ||
66 | + // // let result = await getIpToCountry(this.userIP); | ||
67 | + // // console.log(result); | ||
68 | + // // } | ||
69 | + // } catch (error) { | ||
70 | + // console.error('Error getting IP:', error); | ||
71 | + // this.userIP = 'Error'; | ||
72 | + // } | ||
73 | + }, | ||
74 | + async mounted() { | ||
54 | var url = window.location.href; | 75 | var url = window.location.href; |
55 | var lang = this.getQueryString('lang'); | 76 | var lang = this.getQueryString('lang'); |
77 | + let that = this; | ||
56 | console.log("地址携带语种", lang); | 78 | console.log("地址携带语种", lang); |
57 | - if (lang === "zh") { | ||
58 | - this.$i18n.locale = 'zh-Hans'; | ||
59 | - uni.setLocale('zh-Hans'); | ||
60 | - this.selectedLanguage = `${this.$t('index.language')}:${this.$t('locale.zh-hans')}`; | ||
61 | - } else if (lang === "en") { | ||
62 | - this.$i18n.locale = 'en'; | ||
63 | - uni.setLocale('en'); | ||
64 | - this.selectedLanguage = `${this.$t('index.language')}:${this.$t('locale.en')}`; | 79 | + if (lang) { |
80 | + if (lang === "zh") { | ||
81 | + that.$i18n.locale = 'zh-Hans'; | ||
82 | + uni.setLocale('zh-Hans'); | ||
83 | + that.selectedLanguage = `${that.$t('index.language')}:${that.$t('locale.zh-hans')}`; | ||
84 | + } else if (lang === "en") { | ||
85 | + that.$i18n.locale = 'en'; | ||
86 | + uni.setLocale('en'); | ||
87 | + that.selectedLanguage = `${that.$t('index.language')}:${that.$t('locale.en')}`; | ||
88 | + } else { | ||
89 | + let Localelang = 'en'; | ||
90 | + console.log("本地语言", Localelang); | ||
91 | + that.selectedLanguage = Localelang == 'en' ? | ||
92 | + `${that.$t('index.language')}:${that.$t('locale.en')}` : | ||
93 | + `${that.$t('index.language')}:${that.$t('locale.zh-hans')}` | ||
94 | + } | ||
95 | + that.$emit('lanChange', uni.getLocale()) | ||
65 | } else { | 96 | } else { |
66 | - let Localelang = 'en'; | ||
67 | - console.log("本地语言", Localelang); | ||
68 | - this.selectedLanguage = Localelang == 'en' ? `${this.$t('index.language')}:${this.$t('locale.en')}` : | ||
69 | - `${this.$t('index.language')}:${this.$t('locale.zh-hans')}` | 97 | + // 地址未携带则 |
98 | + const Choice = sessionStorage.getItem('Choice'); | ||
99 | + console.log("是否已选择",Choice); | ||
100 | + if(!Choice){ | ||
101 | + const response = await uni.request({ | ||
102 | + url: 'https://ipinfo.io/ip', | ||
103 | + method: 'GET' | ||
104 | + }); | ||
105 | + this.userIP = response[1].data.trim(); | ||
106 | + let result = await getIpToCountry(that.userIP); //1.208.112.0 外国ip地址测试 | ||
107 | + if (result) { | ||
108 | + if (result && result.country === 'CN') { | ||
109 | + console.log("CN"); | ||
110 | + that.$i18n.locale = 'zh-Hans'; | ||
111 | + uni.setLocale('zh-Hans'); | ||
112 | + that.selectedLanguage = `${that.$t('index.language')}:${that.$t('locale.zh-hans')}`; | ||
113 | + } else { | ||
114 | + console.log("EN"); | ||
115 | + that.$i18n.locale = 'en'; | ||
116 | + uni.setLocale('en'); | ||
117 | + that.selectedLanguage = `${that.$t('index.language')}:${that.$t('locale.en')}`; | ||
118 | + } | ||
119 | + } | ||
120 | + that.$emit('lanChange', uni.getLocale()) | ||
121 | + } | ||
70 | } | 122 | } |
71 | }, | 123 | }, |
72 | methods: { | 124 | methods: { |
@@ -90,7 +142,8 @@ | @@ -90,7 +142,8 @@ | ||
90 | uni.setLocale('zh-Hans'); | 142 | uni.setLocale('zh-Hans'); |
91 | this.selectedLanguage = `${this.$t('index.language')}:${this.$t('locale.zh-hans')}`; | 143 | this.selectedLanguage = `${this.$t('index.language')}:${this.$t('locale.zh-hans')}`; |
92 | } | 144 | } |
93 | - this.$emit('lanChange',uni.getLocale()) | 145 | + sessionStorage.setItem('Choice', 'true'); |
146 | + this.$emit('lanChange', uni.getLocale()) | ||
94 | } | 147 | } |
95 | } | 148 | } |
96 | } | 149 | } |
@@ -158,4 +211,4 @@ | @@ -158,4 +211,4 @@ | ||
158 | .b-navCss { | 211 | .b-navCss { |
159 | color: #fff; | 212 | color: #fff; |
160 | } | 213 | } |
161 | -</style> | 214 | -</style> |
215 | +</style> | ||
162 | \ No newline at end of file | 216 | \ No newline at end of file |
pages/homePage/index.vue
@@ -28,6 +28,10 @@ | @@ -28,6 +28,10 @@ | ||
28 | import { | 28 | import { |
29 | getProductFind | 29 | getProductFind |
30 | } from '@/api/scan' | 30 | } from '@/api/scan' |
31 | + import { | ||
32 | + getIp, | ||
33 | + getIpToCountry | ||
34 | + } from '@/api/ip.js' | ||
31 | import topWindow from "@/pages/component/topWindow.vue" | 35 | import topWindow from "@/pages/component/topWindow.vue" |
32 | export default { | 36 | export default { |
33 | data() { | 37 | data() { |
@@ -42,6 +46,7 @@ | @@ -42,6 +46,7 @@ | ||
42 | lastCode: "", | 46 | lastCode: "", |
43 | nextTime: "", | 47 | nextTime: "", |
44 | nextCode: "", | 48 | nextCode: "", |
49 | + userIP:"" | ||
45 | } | 50 | } |
46 | }, | 51 | }, |
47 | components: { | 52 | components: { |
@@ -61,47 +66,46 @@ | @@ -61,47 +66,46 @@ | ||
61 | } | 66 | } |
62 | }, | 67 | }, |
63 | created() { | 68 | created() { |
64 | - | ||
65 | - window.document.onkeypress = (e) => { | ||
66 | - if (window.event) { | ||
67 | - // IE | ||
68 | - this.nextCode = e.keyCode; | ||
69 | - } else if (e.which) { | ||
70 | - // Netscape/Firefox/Opera | ||
71 | - this.nextCode = e.which; | ||
72 | - } | ||
73 | - if (e.which === 20) { | ||
74 | - // 键盘回车事件 | ||
75 | - if (this.code.length < 3) return; // 扫码枪的速度很快,手动输入的时间不会让code的长度大于2,所以这里不会对扫码枪有效 | ||
76 | - console.log("扫码结束。"); | ||
77 | - console.log("条形码:", this.code); | ||
78 | - this.handleScan(this.code); // 获取到扫码枪输入的内容,做别的操作 | ||
79 | - this.lastCode = ""; | ||
80 | - this.lastTime = ""; | ||
81 | - return; | ||
82 | - } | ||
83 | - this.nextTime = new Date().getTime(); | ||
84 | - if (!this.lastTime && !this.lastCode) { | ||
85 | - this.code = ""; // 清空上次的条形码 | ||
86 | - this.code += e.key; | ||
87 | - console.log("扫码开始---", this.code); | ||
88 | - } | ||
89 | - if ( | ||
90 | - this.lastCode && | ||
91 | - this.lastTime && | ||
92 | - this.nextTime - this.lastTime > 500 | ||
93 | - ) { | ||
94 | - // 当扫码前有keypress事件时,防止首字缺失 | ||
95 | - this.code = e.key; | ||
96 | - // console.log("防止首字缺失。。。", this.code); | ||
97 | - } else if (this.lastCode && this.lastTime) { | ||
98 | - this.code += e.key; | ||
99 | - // console.log("扫码中。。。", this.code); | ||
100 | - } | ||
101 | - this.lastCode = this.nextCode; | ||
102 | - this.lastTime = this.nextTime; | ||
103 | - }; | ||
104 | - }, | 69 | + window.document.onkeypress = (e) => { |
70 | + if (window.event) { | ||
71 | + // IE | ||
72 | + this.nextCode = e.keyCode; | ||
73 | + } else if (e.which) { | ||
74 | + // Netscape/Firefox/Opera | ||
75 | + this.nextCode = e.which; | ||
76 | + } | ||
77 | + if (e.which === 20) { | ||
78 | + // 键盘回车事件 | ||
79 | + if (this.code.length < 3) return; // 扫码枪的速度很快,手动输入的时间不会让code的长度大于2,所以这里不会对扫码枪有效 | ||
80 | + console.log("扫码结束。"); | ||
81 | + console.log("条形码:", this.code); | ||
82 | + this.handleScan(this.code); // 获取到扫码枪输入的内容,做别的操作 | ||
83 | + this.lastCode = ""; | ||
84 | + this.lastTime = ""; | ||
85 | + return; | ||
86 | + } | ||
87 | + this.nextTime = new Date().getTime(); | ||
88 | + if (!this.lastTime && !this.lastCode) { | ||
89 | + this.code = ""; // 清空上次的条形码 | ||
90 | + this.code += e.key; | ||
91 | + console.log("扫码开始---", this.code); | ||
92 | + } | ||
93 | + if ( | ||
94 | + this.lastCode && | ||
95 | + this.lastTime && | ||
96 | + this.nextTime - this.lastTime > 500 | ||
97 | + ) { | ||
98 | + // 当扫码前有keypress事件时,防止首字缺失 | ||
99 | + this.code = e.key; | ||
100 | + // console.log("防止首字缺失。。。", this.code); | ||
101 | + } else if (this.lastCode && this.lastTime) { | ||
102 | + this.code += e.key; | ||
103 | + // console.log("扫码中。。。", this.code); | ||
104 | + } | ||
105 | + this.lastCode = this.nextCode; | ||
106 | + this.lastTime = this.nextTime; | ||
107 | + }; | ||
108 | + }, | ||
105 | onLoad() { | 109 | onLoad() { |
106 | let systemInfo = uni.getSystemInfoSync(); | 110 | let systemInfo = uni.getSystemInfoSync(); |
107 | this.systemLocale = systemInfo.language; | 111 | this.systemLocale = systemInfo.language; |