소스 검색

处理浏览器语系、根据ip判断地区

liqk 4 년 전
부모
커밋
fe4b647586
3개의 변경된 파일111개의 추가작업 그리고 67개의 파일을 삭제
  1. 64 43
      TEAMModelOS/ClientApp/src/api/regist.js
  2. 38 18
      TEAMModelOS/ClientApp/src/components/public/countryCode/Index.vue
  3. 9 6
      TEAMModelOS/ClientApp/src/locale/index.js

+ 64 - 43
TEAMModelOS/ClientApp/src/api/regist.js

@@ -3,49 +3,70 @@ import config from '@/store/module/config'
 import jwtDecode from 'jwt-decode'
 import jwtDecode from 'jwt-decode'
 
 
 export default {
 export default {
-/**
- * 註冊帳號
- * @param {String} applyType - 寄信類型(email, phone)
- * @param {String} name - 姓名
- * @param {String} account - 驗證帳號
- * @param {String} pw - 密碼
- * @param {String} country - 手機區碼,無須+號(有phone為必填)
- * @param {String} pinCode - 驗證碼
- */
-crtAccount: function(item)   {
-    return new Promise((resolve) => {
-      let srvAdr = localStorage.getItem('srvAdr')
-      let url = config.state[srvAdr].coreAPIUrl
-      let nonceStr = 'habook'  // 檢查項目
-      let data = {
-        "grant_type": "create",
-        'client_id': config.state[srvAdr].clientID,
-        'nonce': nonceStr,
-        "name" : item.name,
-        "password": item.pw,
-        "pin_code": item.pinCode.toString()
-      }
+    /**
+     * 註冊帳號
+     * @param {String} applyType - 寄信類型(email, phone)
+     * @param {String} name - 姓名
+     * @param {String} account - 驗證帳號
+     * @param {String} pw - 密碼
+     * @param {String} country - 手機區碼,無須+號(有phone為必填)
+     * @param {String} pinCode - 驗證碼
+     */
+    crtAccount: function (item) {
+        return new Promise((resolve) => {
+            let srvAdr = localStorage.getItem('srvAdr')
+            let url = config.state[srvAdr].coreAPIUrl
+            let nonceStr = 'habook'  // 檢查項目
+            let data = {
+                "grant_type": "create",
+                'client_id': config.state[srvAdr].clientID,
+                'nonce': nonceStr,
+                "name": item.name,
+                "password": item.pw,
+                "pin_code": item.pinCode.toString()
+            }
 
 
-      if(item.applyType == 'phone'){
-          data.account = '+' + item.country + '-' + item.account
-      } else {
-        data.account = item.account
-      }
+            if (item.applyType == 'phone') {
+                data.account = '+' + item.country + '-' + item.account
+            } else {
+                data.account = item.account
+            }
 
 
-      corePost(url+'/oauth2/login', data).then( res => {
-        if(res.error){
-          resolve(res)
-        } else {
-          let t_data = jwtDecode(res.id_token)
-          if(nonceStr === t_data.nonce){
-            resolve(res)
-          } else {
-            resolve({error: 'nonce'})
-          }
-        }
-      },err => {
-        console.log(err)
-      })
-    })
-  },
+            corePost(url + '/oauth2/login', data).then(res => {
+                if (res.error) {
+                    resolve(res)
+                } else {
+                    let t_data = jwtDecode(res.id_token)
+                    if (nonceStr === t_data.nonce) {
+                        resolve(res)
+                    } else {
+                        resolve({ error: 'nonce' })
+                    }
+                }
+            }, err => {
+                console.log(err)
+            })
+        })
+    },
+    //通过IP判断地区(大陆、香港、台湾)
+    checkIp: function () {
+        return new Promise((r, j) => {
+            let srvAdr = localStorage.getItem('srvAdr')
+            let url = config.state[srvAdr].coreAPIUrl
+            corePost(url + '/area/lang').then(
+                res => {
+                    if (res.error) {
+                        j('error')
+                    } else {
+                        r(res)
+                    }
+                },
+                err => {
+                    j(err)
+                }
+            )
+        })
+    }
 }
 }
+
+

+ 38 - 18
TEAMModelOS/ClientApp/src/components/public/countryCode/Index.vue

@@ -26,36 +26,56 @@ export default {
         dataList(){
         dataList(){
             let data = []
             let data = []
             let LowerCaseLang = this.lang.toLowerCase()
             let LowerCaseLang = this.lang.toLowerCase()
-            let _this = this
+            let k = ''
+            switch (LowerCaseLang) {
+                case 'zh-tw':
+                    k = 'CountryTw'
+                    break;
+                case 'zh-cn':
+                    k = 'CountryCn'
+                    break;
+                case 'zh-hk':
+                    k = 'CountryTw'
+                    break;
+                default:
+                    k = 'CountryEn'
+                    break;
+            }
             Object.keys(codeData).forEach(function(key){
             Object.keys(codeData).forEach(function(key){
                 let OItem = codeData[key]
                 let OItem = codeData[key]
-                let lowerCaseCulture = OItem.Culture !='' ? OItem.Culture.toString().toLowerCase() : ''
+                //let lowerCaseCulture = OItem.Culture !='' ? OItem.Culture.toString().toLowerCase() : ''
                 let label;
                 let label;
-                switch (LowerCaseLang) {
-                    case 'zh-tw':
-                        label = '+' + OItem.Code + '(' + OItem.CountryTw + ')'
-                        break;
-                    case 'zh-cn':
-                        label = '+' + OItem.Code + '(' + OItem.CountryCn + ')'
-                        break;                
-                    default:
-                        label = '+' + OItem.Code + '(' + OItem.CountryEn + ')'
-                        break;
-                }
+                label = '+' + OItem.Code + '(' + OItem[k] + ')'
                 data.push({
                 data.push({
                     label: label,
                     label: label,
                     value: OItem.Code
                     value: OItem.Code
                 })
                 })
-
-                if(LowerCaseLang == lowerCaseCulture){
-                    _this.value1 = OItem.Code
-                }
+                //优先通过ip判断,如果ip判断结果为en-us则根据语系判断
+                //if(LowerCaseLang == lowerCaseCulture){
+                //    _this.value1 = OItem.Code
+                //}
             })
             })
             return data
             return data
         }
         }
     },
     },
     created:  function () {
     created:  function () {
-        // console.log(this.dataList)
+        this.$api.regist.checkIp().then(
+            res => {
+                let LowerCaseLang = this.lang.toLowerCase()
+                if (res !== 'en-us') {
+                    LowerCaseLang = res
+                }
+                let k = Object.keys(codeData).filter((key) => {
+                    return codeData[key].Culture.toString().toLowerCase() == LowerCaseLang
+                })
+                if (k) {
+                    this.value1 = codeData[k].Code
+                }
+            },
+            err => {
+                console.log(err)
+            }
+        )
     },
     },
     watch:{
     watch:{
         value1: function(newVal, oldVal){
         value1: function(newVal, oldVal){

+ 9 - 6
TEAMModelOS/ClientApp/src/locale/index.js

@@ -11,9 +11,10 @@ import twLocale from 'view-design/src/locale/lang/zh-TW'
 Vue.use(VueI18n)
 Vue.use(VueI18n)
 
 
 // 自动根据浏览器系统语言设置语言
 // 自动根据浏览器系统语言设置语言
-const navLang = navigator.language
-const localLang = (navLang === 'zh-TW' || navLang === 'zh-CN' || navLang === 'en-US') ? navLang : false
-let lang = localLang || 'en-US'
+const navLang = navigator.language.toLowerCase()
+console.log('lang', navLang)
+const localLang = (navLang === 'zh' || navLang === 'zh-tw' || navLang === 'zh-cn' || navLang === 'zh-hk') ? navLang : false
+let lang = localLang || 'en-us'
 
 
 console.log(lang)
 console.log(lang)
 localStorage.setItem('local', lang)
 localStorage.setItem('local', lang)
@@ -21,9 +22,11 @@ Vue.config.lang = lang
 
 
 Vue.locale = () => { }
 Vue.locale = () => { }
 const messages = {
 const messages = {
-  'zh-CN': Object.assign(customZhCn, zhLocale),
-  'zh-TW': Object.assign(customZhTw, twLocale),
-  'en-US': Object.assign(customEnUs, enLocale)
+  'zh-cn': Object.assign(customZhCn, zhLocale),
+  'zh': Object.assign(customZhCn, zhLocale),
+  'zh-tw': Object.assign(customZhTw, twLocale),
+  'zh-hk': Object.assign(customZhTw, twLocale),
+  'en-us': Object.assign(customEnUs, enLocale)
 }
 }
 const i18n = new VueI18n({
 const i18n = new VueI18n({
   locale: lang,
   locale: lang,