Setting.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  2. import Card from "@/_health/components/card";
  3. import showActionSheet from "@/components/basic/ActionSheet";
  4. import showAlert from "@/components/basic/Alert";
  5. import Buttons from "@/components/basic/Buttons";
  6. import Footer from "@/components/layout/Footer";
  7. import TableCell from "@/components/layout/TableCell";
  8. import { ColorType, MainColorType } from "@/context/themes/color";
  9. import { ChooseScenarioBtn } from "@/features/common/SpecBtns";
  10. import { APP_VERSION, WX_VERSION } from "@/services/http/api";
  11. import { clear, logout } from "@/services/user";
  12. import { logoutSuccess } from "@/store/user";
  13. import { kIsAndroid, rpxToPx } from "@/utils/tools";
  14. import { View, Text, Button } from "@tarojs/components";
  15. import Taro from "@tarojs/taro";
  16. import { useEffect, useState } from "react";
  17. import { useTranslation } from "react-i18next";
  18. import { useDispatch, useSelector } from "react-redux";
  19. let useNavigation, useActionSheet;
  20. if (process.env.TARO_ENV == 'rn') {
  21. useNavigation = require("@react-navigation/native").useNavigation
  22. useActionSheet = require('@expo/react-native-action-sheet').useActionSheet
  23. }
  24. let hasCache = false;
  25. export default function Page() {
  26. const dispatch = useDispatch();
  27. const { t, i18n } = useTranslation()
  28. const user = useSelector((state: any) => state.user);
  29. const [languageType, setLanguageType] = useState('')
  30. let showActionSheetWithOptions;
  31. if (process.env.TARO_ENV == 'rn') {
  32. showActionSheetWithOptions = useActionSheet()
  33. }
  34. let navigation;
  35. if (useNavigation) {
  36. navigation = useNavigation()
  37. }
  38. useEffect(() => {
  39. Taro.setNavigationBarTitle({
  40. title: t('page.setting.title')
  41. })
  42. if (process.env.TARO_ENV == 'rn') {
  43. navigation.setOptions({ title: t('page.setting.title') ?? '' });
  44. }
  45. Taro.getStorage({ key: 'notification' }).then(res => {
  46. hasCache = true;
  47. })
  48. loadLanguage()
  49. console.log(Taro.getAppBaseInfo().language)
  50. }, [])
  51. async function loadLanguage() {
  52. var lg = await getStorage('language')
  53. if (lg.length > 0) {
  54. switch (lg) {
  55. case 'wechat':
  56. setLanguageType('微信系统语言')
  57. break
  58. case 'zh':
  59. setLanguageType('中文')
  60. break
  61. case 'en':
  62. setLanguageType('English')
  63. break
  64. }
  65. }
  66. else {
  67. setLanguageType('中文')
  68. }
  69. }
  70. async function getStorage(key: string) {
  71. try {
  72. const res = await Taro.getStorage({ key });
  73. return res.data;
  74. } catch {
  75. return '';
  76. }
  77. }
  78. async function logoutF() {
  79. // var showDayRing = await getStorage('showedDisqualifiedAlert') || false;
  80. showAlert({
  81. title: t('feature.common.modal.logout_title'),
  82. content: t('feature.common.modal.logout_content'),
  83. showCancel: true,
  84. confirm: () => {
  85. logout().then(res => {
  86. dispatch(logoutSuccess())
  87. if (hasCache) {
  88. Taro.setStorage({
  89. key: 'notification',
  90. data: true
  91. })
  92. }
  93. if (process.env.TARO_ENV == 'rn') {
  94. var Jto = require('react-native').NativeModules.NativeBridge;
  95. Jto.clearNotification()
  96. }
  97. // if (showDayRing) {
  98. // Taro.setStorage({
  99. // key: 'showedDisqualifiedAlert',
  100. // data: true
  101. // })
  102. // }
  103. if (process.env.TARO_ENV == 'weapp') {
  104. Taro.switchTab({
  105. url: '/pages/clock/Clock'
  106. })
  107. //check
  108. //退出到首页后,点击登录,会再次执行naviback函数
  109. // Taro.navigateBack();
  110. console.log('setting navi back')
  111. }
  112. else {
  113. navigation.goBack()
  114. }
  115. }).catch((e) => {
  116. })
  117. }
  118. })
  119. }
  120. function del() {
  121. showActionSheet({
  122. title: t('feature.common.modal.deluser_action_title'),
  123. showActionSheetWithOptions: showActionSheetWithOptions,
  124. itemList: [t('feature.common.modal.deluser_action_confirm')],
  125. success: (res) => {
  126. switch (res) {
  127. case 0:
  128. {
  129. showAlert({
  130. title: t('feature.common.modal.deluser_title'),
  131. content: t('feature.common.modal.deluser_content'),
  132. showCancel: true,
  133. confirmText: t('feature.common.modal.deluser_cancel'),
  134. cancelText: t('feature.common.modal.deluser_confirm'),
  135. redCancel: true,
  136. cancel: () => {
  137. delUser()
  138. }
  139. })
  140. }
  141. break;
  142. }
  143. }
  144. })
  145. }
  146. function delUser() {
  147. dispatch(clear() as any);
  148. setTimeout(() => {
  149. navigation.goBack()
  150. }, 1500)
  151. }
  152. function changeLanguage() {
  153. showActionSheet({
  154. alertText: '更改语言',
  155. itemList: ['中文', 'English', '微信系统语言'],
  156. success: function (res) {
  157. if (res != -1) {
  158. global.refreshWindow()
  159. if (global.refreshHistory) {
  160. global.refreshHistory()
  161. }
  162. }
  163. switch (res) {
  164. case 0:
  165. global.language = 'zh'
  166. Taro.setStorageSync('language', 'zh')
  167. i18n.changeLanguage('zh')
  168. setLanguageType('中文')
  169. break;
  170. case 1:
  171. global.language = 'en'
  172. Taro.setStorageSync('language', 'en')
  173. i18n.changeLanguage('en')
  174. setLanguageType('English')
  175. break;
  176. case 2:
  177. const isZh = Taro.getAppBaseInfo().language == 'zh_CN'
  178. global.language = isZh ? 'zh' : 'en'
  179. Taro.setStorageSync('language', 'wechat')
  180. i18n.changeLanguage(isZh ? 'zh' : 'en')
  181. setLanguageType('微信系统语言')
  182. break;
  183. }
  184. }
  185. })
  186. // if (global.language == 'en') {
  187. // global.language = 'zh'
  188. // Taro.setStorageSync('language', 'zh')
  189. // i18n.changeLanguage('zh')
  190. // }
  191. // else {
  192. // global.language = 'en'
  193. // Taro.setStorageSync('language', 'en')
  194. // i18n.changeLanguage('en')
  195. // }
  196. }
  197. return <View style={{ color: '#fff', display: 'flex', flexDirection: 'column', flex: 1 }}>
  198. <View style={{ height: 20 }} />
  199. <Card>
  200. <View style={{
  201. display: 'flex', flexDirection: 'row',
  202. justifyContent: 'space-between',
  203. alignItems: 'center',
  204. height: rpxToPx(128),
  205. boxSizing: 'border-box',
  206. paddingLeft: rpxToPx(40),
  207. paddingRight: rpxToPx(40),
  208. position: 'relative'
  209. }}>
  210. <Text className="h34" style={{ color: '#000' }}>{t('page.setting.version')}</Text>
  211. <Text className="h30" style={{ color: MainColorType.g01 }}>{process.env.TARO_ENV == 'weapp' ? WX_VERSION : APP_VERSION}</Text>
  212. <View className="border_footer_line" style={{ left: rpxToPx(40) }} />
  213. </View>
  214. <View
  215. onClick={changeLanguage}
  216. style={{
  217. display: 'flex', flexDirection: 'row',
  218. justifyContent: 'space-between',
  219. alignItems: 'center',
  220. height: rpxToPx(128),
  221. boxSizing: 'border-box',
  222. paddingLeft: rpxToPx(40),
  223. paddingRight: rpxToPx(40),
  224. position: 'relative'
  225. }}>
  226. <Text className="h34" style={{ color: '#000' }}>{t('health.language')}</Text>
  227. <Text className="h30" style={{ color: MainColorType.g01 }}>{languageType}</Text>
  228. </View>
  229. </Card>
  230. {
  231. process.env.TARO_ENV == 'rn' && <View style={{ flex: 1 }} />
  232. }
  233. <Footer>
  234. <View>
  235. <View style={{ marginBottom: rpxToPx(24) }}>
  236. <NewButton
  237. type={NewButtonType.gray}
  238. title={t('page.setting.logout')}
  239. onClick={logoutF}
  240. width={rpxToPx(400)}
  241. height={rpxToPx(96)}
  242. />
  243. </View>
  244. <View style={{ height: rpxToPx(72), display: 'flex' }}>
  245. <NewButton
  246. type={NewButtonType.link}
  247. title={t('feature.common.modal.del')}
  248. onClick={del}
  249. />
  250. </View>
  251. </View>
  252. </Footer>
  253. {
  254. process.env.TARO_ENV == 'rn' && <View style={{ height: 50 }} />
  255. }
  256. {/* <Buttons title='退出登录' onClick={logoutF} btnStyle={{ width: 289, marginBottom: 30 }} /> */}
  257. {/* {
  258. user.test_user && <Buttons title='注销账号' onClick={clearF} btnStyle={{ width: 289, marginBottom: 30 }} />
  259. } */}
  260. </View>
  261. }