diff --git a/src/app.ts b/src/app.ts index 6a55898..d1242a9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,26 +1,13 @@ import { PropsWithChildren } from 'react'; -import Taro, { useLaunch } from '@tarojs/taro'; -import { getUrl } from '@/service'; -import pt from '@/plain-text'; +import { useLaunch } from '@tarojs/taro'; import 'taro-ui/dist/style/index.scss'; import './app.scss'; +import { getLocaleData } from './service/localeData'; function App({ children }: PropsWithChildren) { useLaunch(() => { console.log('App launched.'); - Taro.request({ - url: getUrl('/user/locale/get'), - method: 'GET', - data: { - token: 'token_test', - }, - }).then(res => { - console.log(res.data); - pt.setLang(res.data.data.lang); - Taro.reLaunch({ - url: '/pages/index/index', - }); - }); + getLocaleData(); }); // children 是将要会渲染的页面 diff --git a/src/pages/user/user.tsx b/src/pages/user/user.tsx index 1b42ca8..9e2921a 100644 --- a/src/pages/user/user.tsx +++ b/src/pages/user/user.tsx @@ -1,7 +1,6 @@ import { AtList, AtListItem, AtToast } from 'taro-ui'; import { Component, ReactNode } from 'react'; import { View, Picker, Image, Text } from '@tarojs/components'; -import { getUrl } from '@/service'; import Taro from '@tarojs/taro'; import type CustomTabBar from '@/custom-tab-bar'; import PageFooter from '@/components/PageFooter/PageFooter'; @@ -9,6 +8,7 @@ import aboutIcon from '@/assets/icons/AboutPage/about.svg'; import pt, { Lang } from '@/plain-text'; import wechatUser from '@/wechat'; import logo from '@/assets/images/UserPage/logo.png'; +import { setLocaleData } from '@/service/localeData'; import './user.scss'; const listLangInterval = 20; @@ -70,16 +70,7 @@ export default class UserPage extends Component { }); let currentLang = this.showLangDict[this.state.selector[e.detail.value]]; pt.setLang(currentLang); - Taro.request({ - url: getUrl('/user/locale/update'), - method: 'POST', - data: { - token: 'token_test', - lang: currentLang, - }, - }).then(res => { - console.log(res.data); - }); + setLocaleData(currentLang); Taro.reLaunch({ url: '/pages/user/user', }); diff --git a/src/service/localeData.ts b/src/service/localeData.ts new file mode 100644 index 0000000..174321e --- /dev/null +++ b/src/service/localeData.ts @@ -0,0 +1,32 @@ +import Taro from '@tarojs/taro'; +import pt, { Lang } from '@/plain-text'; +import { getUrl } from '.'; + +export function setLocaleData(lang: Lang) { + Taro.request({ + url: getUrl('/user/locale/update'), + method: 'POST', + data: { + token: 'token_test', + lang: lang, + }, + }).then(res => { + console.log(res.data); + }); +} + +export function getLocaleData() { + Taro.request({ + url: getUrl('/user/locale/get'), + method: 'GET', + data: { + token: 'token_test', + }, + }).then(res => { + console.log(res.data); + pt.setLang(res.data.data.lang); + Taro.reLaunch({ + url: '/pages/index/index', + }); + }); +}