| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- interface AccountPassword {
- account: string,
- password: string,
- };
- class LocalStorage {
- // 存储账号密码
- setAccountPassword = (accountPassword?: AccountPassword) => {
- if (accountPassword) {
- uni.setStorageSync('accountPassword', accountPassword);
- } else {
- uni.removeStorageSync('accountPassword');
- }
- }
- // 存储凭证
- setToken = (token: string) => {
- uni.setStorageSync('token', token);
- }
- // 获取账号密码
- getAccountPassword = () => {
- const accountPassword: AccountPassword | undefined = uni.getStorageSync('accountPassword');
- return accountPassword;
- }
- // 获取凭证
- getToken = () => {
- const token = uni.getStorageSync('token');
- if (token) {
- return token;
- } else {
- this.clear();
- return undefined;
- }
- }
- // 清除
- clear = () => {
- /*
- * 保留密码
- */
- const accountPassword = this.getAccountPassword();
- const cloneAccountPassword = accountPassword ? { ...accountPassword } : undefined;
- uni.clearStorageSync();
- this.setAccountPassword(cloneAccountPassword);
- }
- }
- export default new LocalStorage();
|