Commit c9b470b7 authored by XieZhiXiong's avatar XieZhiXiong

Merge branch 'fix-mobx' into 'master'

refactor: 重构mobx See merge request project/zhongran-sbbb-mobile-sale!4
parents 3b788e09 560051cf
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
"expo-blur": "~8.2.0", "expo-blur": "~8.2.0",
"i18next": "^20.5.0", "i18next": "^20.5.0",
"mobx": "^6.7.0", "mobx": "^6.7.0",
"mobx-react": "^7.6.0",
"mobx-react-lite": "^3.4.0", "mobx-react-lite": "^3.4.0",
"prettier": "^2.8.0", "prettier": "^2.8.0",
"qs": "^6.9.4", "qs": "^6.9.4",
......
import React, { createContext, useContext } from 'react';
import { Store } from '../store';
const StoreContext = createContext<Store>(new Store());
const store = new Store();
const StoreProvider = () => {
return <StoreContext.Provider value={store}></StoreContext.Provider>;
};
/**
* 自定义useStores hook,使用React Context共享整个store。
* @returns store实例
*/
export const useStores = () => useContext(StoreContext);
import RootStore from './rootStore'; import { makeAutoObservable } from 'mobx';
import { UserStore } from './userStore';
const rootStore = new RootStore(); export class Store {
/** userStore: UserStore = new UserStore();
* 这里只做根Store的导出
* 预留以后可能会出现 额外的Store
*/
const Store = {
...rootStore,
};
export default Store; constructor() {
makeAutoObservable(this);
}
}
import { makeAutoObservable } from 'mobx';
import UserStore from '../userStore';
import { UserStoreModel } from '../userStore/model';
class RootStore {
userStore: UserStoreModel;
constructor() {
makeAutoObservable(this);
this.userStore = new UserStore(this);
}
}
export default RootStore;
import { UserStoreModel } from '../userStore/model';
export interface RootStoreModel {
userStore: UserStoreModel;
}
import React, {
FC,
createContext,
ReactNode,
ReactElement,
useContext,
} from 'react';
import { RootStoreModel } from './rootStore/model';
export const StoreContext = createContext<RootStoreModel>({} as RootStoreModel);
export type StoreComponent = FC<{
store: RootStoreModel;
children: ReactNode;
}>;
// eslint-disable-next-line react/prop-types
export const StoreProvider: StoreComponent = ({
children,
store,
}): ReactElement => (
<StoreContext.Provider value={store}>{children}</StoreContext.Provider>
);
/**
* 组件具体使用mobx的hooks
*/
export const useStores = (): RootStoreModel => useContext(StoreContext);
export default useStores;
import { makeObservable, observable, runInAction } from 'mobx'; export * from './userStore';
import { export * from './types';
getAsyncStorage,
setAsyncStorage,
removeAsyncStorage,
} from '../../utils/storage';
import { USER_INFO, TOKEN } from '../../constants';
import { RootStoreModel } from '../rootStore/model';
import { UserStoreModel, userInfoType } from './model';
export default class UserStore implements UserStoreModel {
private rootStore: RootStoreModel;
username = 'Bob';
userInfo: userInfoType | null = null;
constructor(rootStore: RootStoreModel) {
makeObservable(this, {
username: observable,
userInfo: observable,
});
this.rootStore = rootStore;
this.fetchUserInfo();
}
setUserName(name: string) {
this.username = name;
}
async fetchUserInfo() {
this.userInfo = await getAsyncStorage(USER_INFO);
}
// 用户登录时,或者修改用户信息的时候更新UserInfo
async setUserInfo(data: userInfoType) {
await setAsyncStorage(USER_INFO, data);
runInAction(() => {
this.userInfo = data;
});
}
async removeUserInfo() {
await removeAsyncStorage(TOKEN);
await removeAsyncStorage(USER_INFO);
// 商品分享口令生成数字
await removeAsyncStorage('SHARE_CODE_NUM');
runInAction(() => {
this.userInfo = null;
});
}
}
export type userInfoType = { export interface UserInfo {
account?: string; account?: string;
company?: string; company?: string;
countryCode?: string; countryCode?: string;
...@@ -20,12 +20,4 @@ export type userInfoType = { ...@@ -20,12 +20,4 @@ export type userInfoType = {
roleId: number; roleId: number;
roleName: string; roleName: string;
}[]; }[];
};
export interface UserStoreModel {
username: string;
setUserName: (name: string) => void;
userInfo: null | userInfoType;
removeUserInfo: () => void;
setUserInfo: (data: any) => void;
} }
import { makeObservable, observable, action, flow } from 'mobx';
import { TOKEN, USER_INFO } from '../../constants';
import { removeAsyncStorage, setAsyncStorage } from '../../utils/storage';
import { UserInfo } from './types';
export class UserStore {
username: string = '';
userInfo: UserInfo | null = null;
constructor() {
makeObservable(this, {
username: observable,
setUsername: action,
setUserInfo: flow,
removeUserInfo: flow,
});
}
setUsername(username: string) {
this.username = username;
}
removeUsername() {
this.username = '';
}
// 使用 flow 代替 async / await,具体可看mobx官方文档https://zh.mobx.js.org/actions.html#%E4%BD%BF%E7%94%A8-flow-%E4%BB%A3%E6%9B%BF-async--await-
*setUserInfo(userInfo: UserInfo) {
try {
yield setAsyncStorage(USER_INFO, userInfo);
this.userInfo = userInfo;
} catch (error) {
console.log('store存储用户信息失败----------', error);
this.userInfo = null;
}
}
*removeUserInfo() {
try {
yield removeAsyncStorage(TOKEN);
yield removeAsyncStorage(USER_INFO);
// 商品分享口令生成数字
yield removeAsyncStorage('SHARE_CODE_NUM');
// console.log('store移除用户信息-----Success');
this.userInfo = null;
} catch (error) {
console.log('store移除用户信息-----Error', error);
}
}
}
import React from 'react'; import React from 'react';
import { View, Button } from 'react-native'; import { View, Button } from 'react-native';
import { RootStackScreenProps } from '../../routers/types'; import { RootStackScreenProps } from '../../routers/types';
import useStores from '../../store/useStores'; import { useStores } from '../../hooks/useStores';
import { ContentItem } from './ContentItem'; import { ContentItem } from './ContentItem';
import { styles } from './styles'; import { styles } from './styles';
...@@ -21,8 +21,7 @@ const AccountInfo: React.FC<TestDetailsScreenNavigationProp> = ({ ...@@ -21,8 +21,7 @@ const AccountInfo: React.FC<TestDetailsScreenNavigationProp> = ({
const { userStore } = useStores(); const { userStore } = useStores();
const loginOut = () => { const loginOut = () => {
// removeUserInfo() userStore.removeUserInfo();
console.log(userStore);
navigation.navigate('BottomTabs', { navigation.navigate('BottomTabs', {
screen: 'PersonalCenter', screen: 'PersonalCenter',
......
...@@ -5,7 +5,7 @@ import { View, Text, Image } from 'react-native'; ...@@ -5,7 +5,7 @@ import { View, Text, Image } from 'react-native';
// import Icon from 'react-native-vector-icons/FontAwesome'; // import Icon from 'react-native-vector-icons/FontAwesome';
import { setAsyncStorage } from '../../utils/storage'; import { setAsyncStorage } from '../../utils/storage';
import { encryptedByAES, decryptedByAES } from '../../utils/cryptoAes'; import { encryptedByAES, decryptedByAES } from '../../utils/cryptoAes';
import useStores from '../../store/useStores'; import { useStores } from '../../hooks/useStores';
import { import {
postMemberMobileWechatAppletLoginPhone, postMemberMobileWechatAppletLoginPhone,
postMemberMobileWechatAppletLoginAccount, postMemberMobileWechatAppletLoginAccount,
......
...@@ -3,6 +3,7 @@ import { StackScreenProps } from '@react-navigation/stack'; ...@@ -3,6 +3,7 @@ import { StackScreenProps } from '@react-navigation/stack';
import React from 'react'; import React from 'react';
import { View, Text } from 'react-native'; import { View, Text } from 'react-native';
import { RootTabScreenProps, RootStackParamList } from '../../routers/types'; import { RootTabScreenProps, RootStackParamList } from '../../routers/types';
import { useStores } from '../../hooks/useStores';
import { styles } from './styles'; import { styles } from './styles';
type PersonalCenterNavigationProp = CompositeScreenProps< type PersonalCenterNavigationProp = CompositeScreenProps<
...@@ -13,8 +14,12 @@ type PersonalCenterNavigationProp = CompositeScreenProps< ...@@ -13,8 +14,12 @@ type PersonalCenterNavigationProp = CompositeScreenProps<
const PersonalCenter: React.FC<PersonalCenterNavigationProp> = ({ const PersonalCenter: React.FC<PersonalCenterNavigationProp> = ({
navigation, navigation,
}) => { }) => {
const { userStore } = useStores();
const goAccountInfo = () => { const goAccountInfo = () => {
navigation.navigate('AccountInfo'); navigation.navigate('AccountInfo');
// 测试mobx
userStore.setUsername('张三');
}; };
return ( return (
......
...@@ -5912,13 +5912,6 @@ mobx-react-lite@^3.4.0: ...@@ -5912,13 +5912,6 @@ mobx-react-lite@^3.4.0:
resolved "http://npm.shushangyun.com/mobx-react-lite/-/mobx-react-lite-3.4.0.tgz#d59156a96889cdadad751e5e4dab95f28926dfff" resolved "http://npm.shushangyun.com/mobx-react-lite/-/mobx-react-lite-3.4.0.tgz#d59156a96889cdadad751e5e4dab95f28926dfff"
integrity sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ== integrity sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==
mobx-react@^7.6.0:
version "7.6.0"
resolved "http://npm.shushangyun.com/mobx-react/-/mobx-react-7.6.0.tgz#ebf0456728a9bd2e5c24fdcf9b36e285a222a7d6"
integrity sha512-+HQUNuh7AoQ9ZnU6c4rvbiVVl+wEkb9WqYsVDzGLng+Dqj1XntHu79PvEWKtSMoMj67vFp/ZPXcElosuJO8ckA==
dependencies:
mobx-react-lite "^3.4.0"
mobx@^6.7.0: mobx@^6.7.0:
version "6.7.0" version "6.7.0"
resolved "http://npm.shushangyun.com/mobx/-/mobx-6.7.0.tgz#2d805610fee1801fd015c54fd5400d2601aa3768" resolved "http://npm.shushangyun.com/mobx/-/mobx-6.7.0.tgz#2d805610fee1801fd015c54fd5400d2601aa3768"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment