wip: 完善部分示例

This commit is contained in:
h_mo 2024-06-15 16:45:21 +08:00
parent a772075200
commit 2c4b5258b0
11 changed files with 108 additions and 26 deletions

View File

@ -50,6 +50,7 @@
"@dcloudio/uni-mp-toutiao": "3.0.0-alpha-4010920240607001",
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-4010920240607001",
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-4010920240607001",
"@multiavatar/multiavatar": "^1.0.7",
"alova": "^2.21.3",
"crypto-js": "^4.2.0",
"lodash-es": "^4.17.21",
@ -84,6 +85,7 @@
"uni-read-pages-vite": "^0.0.6",
"unocss": "^0.61.0",
"unocss-preset-weapp": "^0.60.1",
"unplugin-auto-import": "^0.17.6",
"unplugin-transform-class": "^0.5.3",
"vite": "^5.3.1",
"vite-plugin-restart": "^0.4.0"

View File

@ -1,7 +1,9 @@
import { defineMock } from '@alova/mock';
import { join, sampleSize } from 'lodash-es';
import { join, random, sampleSize } from 'lodash-es';
import multiavatar from '@multiavatar/multiavatar';
import { createMock } from '@/mock/utils';
import { ResultEnum } from '@/enums/httpEnum';
import { getRandomChsString } from '@/utils/character';
function createRandomToken(len = 36 * 6) {
const token = join(sampleSize('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-', len), '');
@ -16,7 +18,23 @@ export const authMocks = defineMock({
const token = createRandomToken();
return createMock({ data: { token } });
}
console.log('模拟登录');
return createMock({ data: [], code: ResultEnum.FAIL, message: '邮箱或密码错误' });
},
// 获取用户信息
'[GET]/api/users': () => {
const generateNicknames = getRandomChsString(random(2, 6));
const svgCode = multiavatar(generateNicknames);
const base64SVG = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(
svgCode,
)}`;
return createMock({
data: {
id: 1,
nickname: generateNicknames,
avatar: base64SVG,
email: 'uni-app@test.com',
},
});
},
});

View File

@ -1,11 +1,12 @@
<script setup lang="ts">
import { useRouter } from 'uni-mini-router';
const router = useRouter();
function onClick() {
console.log('click');
router.push({ name: 'Login' });
}
function goLog() {
router.push({ name: 'Log' });
}
</script>
<template>
@ -15,6 +16,9 @@ function onClick() {
<button @click="onClick">
登录
</button>
<button @click="goLog">
Log
</button>
</template>
<style lang="scss">

View File

@ -1,13 +1,18 @@
<script lang="ts" setup>
onLoad((query) => {
console.log('log onLoad query', query);
});
import { useUserStore } from '@/stores/modules/user';
const userStore = useUserStore();
// onLoad((query) => {
// console.log('log onLoad query', query);
// });
</script>
<template>
<view style="line-height: 88rpx; text-align: center">
<view class="text-center">
登录后访问log
</view>
<image class="" :src="userStore.userInfo?.avatar" mode="aspectFit" lazy-load="false" binderror="" bindload="" />
<view>用户昵称{{ userStore.userInfo?.nickname }}</view>
</template>
<style lang="scss" scoped></style>

View File

@ -1,7 +1,4 @@
<script setup lang="ts">
import { useRouter } from 'uni-mini-router';
import { reactive, ref, unref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { omit } from 'lodash-es';
import { useUserStore } from '@/stores/modules/user';
import { Toast } from '@/utils/uniapi/prompt';
@ -20,7 +17,6 @@ const form = reactive({
const userStore = useUserStore();
function submit(e: any) {
console.log('====>userStore', userStore.user, userStore);
userStore.login(e.detail.value).then(() => {
Toast('登录成功', { duration: 1500 });
setTimeout(() => {

View File

@ -1,2 +1,13 @@
// import { request } from '@/utils/http';
export {};
import { request } from '@/utils/http';
import type { UserInfoModel } from '@/services/model/userModel';
enum Api {
GET_USER_INFO = '/users',
}
/**
*
*/
export function getUserInfoApi() {
return request.Get<UserInfoModel>(Api.GET_USER_INFO);
}

View File

@ -0,0 +1,18 @@
export interface UserInfoModel {
/**
* id
*/
id?: number
/**
*
*/
nickname?: string
/**
*
*/
avatar?: string
/**
*
*/
email?: string
}

View File

@ -1,6 +1,5 @@
import { defineStore } from 'pinia';
import { useRequest } from 'alova';
import { computed, ref } from 'vue';
import { getCache, removeCache, setCache } from '@/utils/cache';
import { TOKEN_KEY } from '@/enums/cacheEnum';
import { login as loginApi, logout as logoutApi } from '@/services/api/auth';

View File

@ -1,25 +1,36 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useRequest } from 'alova';
import { useAuthStore } from './auth';
interface UserState {
id?: string | number
}
import { getUserInfoApi } from '@/services/api/user';
import type { UserInfoModel } from '@/services/model/userModel';
export const useUserStore = defineStore('UserStore', () => {
const user = ref<UserState>({});
const userInfo = ref<UserInfoModel | null>(null);
const authStore = useAuthStore();
const { send: _getUserInfo } = useRequest(getUserInfoApi, { initialData: null, immediate: false });
async function login(params: LoginParams) {
try {
await authStore.login(params);
await getUserInfo();
} catch (error) {
console.log(error);
}
}
// 获取用户信息
async function getUserInfo() {
try {
userInfo.value = await _getUserInfo();
} catch (error) {
console.log(error);
}
}
return {
user,
userInfo,
login,
getUserInfo,
};
});

View File

@ -53,9 +53,7 @@
"src/**/*.tsx",
"src/**/*.vue",
"vite.config.*",
"typings/*.ts",
"typings/*.d.ts",
"node_modules/.pnpm/unplugin-auto-import@0.16.7_rollup@4.18.0/node_modules/unplugin-auto-import/auto-imports.d.ts"
"typings/*.d.ts"
], //
"exclude": []
}

View File

@ -13,6 +13,7 @@ import UnoCSS from 'unocss/vite';
import transformClass from 'unplugin-transform-class/vite';
import { visualizer } from 'rollup-plugin-visualizer';
import ViteRestart from 'vite-plugin-restart';
import AutoImport from 'unplugin-auto-import/vite';
export default defineConfig(async ({ mode }) => {
const root = process.cwd();
@ -48,6 +49,25 @@ export default defineConfig(async ({ mode }) => {
plugins: [
// @ts-expect-error TODO uni() 会报错uni is not a function,暂时使用此方式解决
uni?.default(),
AutoImport({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
],
imports: [
'vue',
'uni-app',
'pinia',
{
'uni-mini-router': ['useRouter', 'useRoute'],
},
],
dts: 'typings/auto-imports.d.ts',
eslintrc: {
enabled: true,
},
}),
UnoCSS(),
transformClass(),
ViteRestart({