mirror of
https://gitee.com/h_mo/uniapp-vue3-vite-ts-template
synced 2025-04-06 03:58:03 +08:00
wip: 完善部分示例
This commit is contained in:
parent
a772075200
commit
9dd45b79eb
@ -50,6 +50,7 @@
|
|||||||
"@dcloudio/uni-mp-toutiao": "3.0.0-alpha-4010920240607001",
|
"@dcloudio/uni-mp-toutiao": "3.0.0-alpha-4010920240607001",
|
||||||
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-4010920240607001",
|
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-4010920240607001",
|
||||||
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-4010920240607001",
|
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-4010920240607001",
|
||||||
|
"@multiavatar/multiavatar": "^1.0.7",
|
||||||
"alova": "^2.21.3",
|
"alova": "^2.21.3",
|
||||||
"crypto-js": "^4.2.0",
|
"crypto-js": "^4.2.0",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
@ -84,6 +85,7 @@
|
|||||||
"uni-read-pages-vite": "^0.0.6",
|
"uni-read-pages-vite": "^0.0.6",
|
||||||
"unocss": "^0.61.0",
|
"unocss": "^0.61.0",
|
||||||
"unocss-preset-weapp": "^0.60.1",
|
"unocss-preset-weapp": "^0.60.1",
|
||||||
|
"unplugin-auto-import": "^0.17.6",
|
||||||
"unplugin-transform-class": "^0.5.3",
|
"unplugin-transform-class": "^0.5.3",
|
||||||
"vite": "^5.3.1",
|
"vite": "^5.3.1",
|
||||||
"vite-plugin-restart": "^0.4.0"
|
"vite-plugin-restart": "^0.4.0"
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import { defineMock } from '@alova/mock';
|
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 { createMock } from '@/mock/utils';
|
||||||
import { ResultEnum } from '@/enums/httpEnum';
|
import { ResultEnum } from '@/enums/httpEnum';
|
||||||
|
import { getRandomChsString } from '@/utils/character';
|
||||||
|
|
||||||
function createRandomToken(len = 36 * 6) {
|
function createRandomToken(len = 36 * 6) {
|
||||||
const token = join(sampleSize('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-', len), '');
|
const token = join(sampleSize('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-', len), '');
|
||||||
@ -16,7 +18,23 @@ export const authMocks = defineMock({
|
|||||||
const token = createRandomToken();
|
const token = createRandomToken();
|
||||||
return createMock({ data: { token } });
|
return createMock({ data: { token } });
|
||||||
}
|
}
|
||||||
console.log('模拟登录');
|
|
||||||
return createMock({ data: [], code: ResultEnum.FAIL, message: '邮箱或密码错误' });
|
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',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useRouter } from 'uni-mini-router';
|
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
function onClick() {
|
function onClick() {
|
||||||
console.log('click');
|
|
||||||
router.push({ name: 'Login' });
|
router.push({ name: 'Login' });
|
||||||
}
|
}
|
||||||
|
function goLog() {
|
||||||
|
router.push({ name: 'Log' });
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -15,6 +16,9 @@ function onClick() {
|
|||||||
<button @click="onClick">
|
<button @click="onClick">
|
||||||
登录
|
登录
|
||||||
</button>
|
</button>
|
||||||
|
<button @click="goLog">
|
||||||
|
Log
|
||||||
|
</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
onLoad((query) => {
|
import { useUserStore } from '@/stores/modules/user';
|
||||||
console.log('log onLoad query', query);
|
|
||||||
});
|
const userStore = useUserStore();
|
||||||
|
// onLoad((query) => {
|
||||||
|
// console.log('log onLoad query', query);
|
||||||
|
// });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<view style="line-height: 88rpx; text-align: center">
|
<view class="text-center">
|
||||||
登录后访问log
|
登录后访问log
|
||||||
</view>
|
</view>
|
||||||
|
<image class="" :src="userStore.userInfo?.avatar" mode="aspectFit" lazy-load="false" binderror="" bindload="" />
|
||||||
|
<view>用户昵称:{{ userStore.userInfo?.nickname }}</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="scss" scoped></style>
|
<style lang="scss" scoped></style>
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
<script setup lang="ts">
|
<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 { omit } from 'lodash-es';
|
||||||
import { useUserStore } from '@/stores/modules/user';
|
import { useUserStore } from '@/stores/modules/user';
|
||||||
import { Toast } from '@/utils/uniapi/prompt';
|
import { Toast } from '@/utils/uniapi/prompt';
|
||||||
@ -20,7 +17,6 @@ const form = reactive({
|
|||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
|
|
||||||
function submit(e: any) {
|
function submit(e: any) {
|
||||||
console.log('====>userStore', userStore.user, userStore);
|
|
||||||
userStore.login(e.detail.value).then(() => {
|
userStore.login(e.detail.value).then(() => {
|
||||||
Toast('登录成功', { duration: 1500 });
|
Toast('登录成功', { duration: 1500 });
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -1,2 +1,13 @@
|
|||||||
// import { request } from '@/utils/http';
|
import { request } from '@/utils/http';
|
||||||
export {};
|
import type { UserInfoModel } from '@/services/model/userModel';
|
||||||
|
|
||||||
|
enum Api {
|
||||||
|
GET_USER_INFO = '/users',
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*/
|
||||||
|
export function getUserInfoApi() {
|
||||||
|
return request.Get<UserInfoModel>(Api.GET_USER_INFO);
|
||||||
|
}
|
||||||
|
18
src/services/model/userModel.d.ts
vendored
18
src/services/model/userModel.d.ts
vendored
@ -0,0 +1,18 @@
|
|||||||
|
export interface UserInfoModel {
|
||||||
|
/**
|
||||||
|
* 用户id
|
||||||
|
*/
|
||||||
|
id?: number
|
||||||
|
/**
|
||||||
|
* 昵称
|
||||||
|
*/
|
||||||
|
nickname?: string
|
||||||
|
/**
|
||||||
|
* 头像
|
||||||
|
*/
|
||||||
|
avatar?: string
|
||||||
|
/**
|
||||||
|
* 邮箱
|
||||||
|
*/
|
||||||
|
email?: string
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { useRequest } from 'alova';
|
import { useRequest } from 'alova';
|
||||||
import { computed, ref } from 'vue';
|
|
||||||
import { getCache, removeCache, setCache } from '@/utils/cache';
|
import { getCache, removeCache, setCache } from '@/utils/cache';
|
||||||
import { TOKEN_KEY } from '@/enums/cacheEnum';
|
import { TOKEN_KEY } from '@/enums/cacheEnum';
|
||||||
import { login as loginApi, logout as logoutApi } from '@/services/api/auth';
|
import { login as loginApi, logout as logoutApi } from '@/services/api/auth';
|
||||||
|
@ -1,25 +1,36 @@
|
|||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { ref } from 'vue';
|
import { useRequest } from 'alova';
|
||||||
import { useAuthStore } from './auth';
|
import { useAuthStore } from './auth';
|
||||||
|
import { getUserInfoApi } from '@/services/api/user';
|
||||||
interface UserState {
|
import type { UserInfoModel } from '@/services/model/userModel';
|
||||||
id?: string | number
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useUserStore = defineStore('UserStore', () => {
|
export const useUserStore = defineStore('UserStore', () => {
|
||||||
const user = ref<UserState>({});
|
const userInfo = ref<UserInfoModel | null>(null);
|
||||||
|
|
||||||
const authStore = useAuthStore();
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
|
const { send: _getUserInfo } = useRequest(getUserInfoApi, { initialData: null, immediate: false });
|
||||||
async function login(params: LoginParams) {
|
async function login(params: LoginParams) {
|
||||||
try {
|
try {
|
||||||
await authStore.login(params);
|
await authStore.login(params);
|
||||||
|
await getUserInfo();
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取用户信息
|
||||||
|
async function getUserInfo() {
|
||||||
|
try {
|
||||||
|
userInfo.value = await _getUserInfo();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user,
|
userInfo,
|
||||||
login,
|
login,
|
||||||
|
getUserInfo,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
21
src/utils/character.ts
Normal file
21
src/utils/character.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
const CHS_RANGE_START = 0x4E00; // 简体中文编码范围开始
|
||||||
|
const CHS_RANGE_END = 0x9FA5; // 简体中文编码范围结束
|
||||||
|
|
||||||
|
// 生成随机简体中文字符的函数
|
||||||
|
function getRandomChsChar() {
|
||||||
|
// 生成 CHS_RANGE_START 和 CHS_RANGE_END 之间的随机整数
|
||||||
|
const randomCharCode
|
||||||
|
= Math.floor(Math.random() * (CHS_RANGE_END - CHS_RANGE_START + 1)) + CHS_RANGE_START;
|
||||||
|
|
||||||
|
// 将随机整数转换为字符
|
||||||
|
return String.fromCharCode(randomCharCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成指定长度的随机简体中文字符串
|
||||||
|
export function getRandomChsString(length: number) {
|
||||||
|
let result = '';
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
result += getRandomChsChar();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
@ -53,9 +53,7 @@
|
|||||||
"src/**/*.tsx",
|
"src/**/*.tsx",
|
||||||
"src/**/*.vue",
|
"src/**/*.vue",
|
||||||
"vite.config.*",
|
"vite.config.*",
|
||||||
"typings/*.ts",
|
"typings/*.d.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"
|
|
||||||
], // 指定要包含在编译中的文件匹配列表
|
], // 指定要包含在编译中的文件匹配列表
|
||||||
"exclude": []
|
"exclude": []
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import UnoCSS from 'unocss/vite';
|
|||||||
import transformClass from 'unplugin-transform-class/vite';
|
import transformClass from 'unplugin-transform-class/vite';
|
||||||
import { visualizer } from 'rollup-plugin-visualizer';
|
import { visualizer } from 'rollup-plugin-visualizer';
|
||||||
import ViteRestart from 'vite-plugin-restart';
|
import ViteRestart from 'vite-plugin-restart';
|
||||||
|
import AutoImport from 'unplugin-auto-import/vite';
|
||||||
|
|
||||||
export default defineConfig(async ({ mode }) => {
|
export default defineConfig(async ({ mode }) => {
|
||||||
const root = process.cwd();
|
const root = process.cwd();
|
||||||
@ -48,6 +49,25 @@ export default defineConfig(async ({ mode }) => {
|
|||||||
plugins: [
|
plugins: [
|
||||||
// @ts-expect-error TODO uni() 会报错:uni is not a function,暂时使用此方式解决
|
// @ts-expect-error TODO uni() 会报错:uni is not a function,暂时使用此方式解决
|
||||||
uni?.default(),
|
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(),
|
UnoCSS(),
|
||||||
transformClass(),
|
transformClass(),
|
||||||
ViteRestart({
|
ViteRestart({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user