From c3462df82cbd511003190aa563f3d23a5b7eb995 Mon Sep 17 00:00:00 2001 From: Jason <5340635+wen-jason@user.noreply.gitee.com> Date: Thu, 15 Sep 2022 10:53:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/package.json | 5 +- app/scripts/build.h5.mjs | 37 + app/src/App.vue | 11 +- app/src/api/account.ts | 9 + app/src/manifest.json | 9 +- app/src/pages.json | 6 + app/src/pages/empty/empty.vue | 7 + app/src/pages/login/login.vue | 93 +- app/src/stores/app.ts | 16 +- app/src/styles/public.scss | 1 + app/src/utils/client.ts | 9 + app/src/utils/wechat.ts | 112 + app/typings/env.d.ts | 5 + app/yarn.lock | 4787 +++++++++++++++++---------------- 14 files changed, 2761 insertions(+), 2346 deletions(-) create mode 100644 app/scripts/build.h5.mjs create mode 100644 app/src/pages/empty/empty.vue create mode 100644 app/src/utils/wechat.ts diff --git a/app/package.json b/app/package.json index 2a4d7817..1bfc8043 100644 --- a/app/package.json +++ b/app/package.json @@ -18,7 +18,7 @@ "dev:quickapp-webview-union": "uni -p quickapp-webview-union", "build:app": "uni build -p app", "build:custom": "uni build -p", - "build:h5": "uni build", + "build:h5": "node scripts/build.h5.mjs", "build:h5:ssr": "uni build --ssr", "build:mp-alipay": "uni build -p mp-alipay", "build:mp-baidu": "uni build -p mp-baidu", @@ -50,6 +50,7 @@ "vconsole": "^3.14.6", "vue": "^3.2.37", "vue-i18n": "^9.2.2", + "weixin-js-sdk": "^1.6.0", "z-paging": "^2.3.8" }, "devDependencies": { @@ -66,6 +67,8 @@ "autoprefixer": "^10.4.8", "eslint": "^8.22.0", "eslint-plugin-vue": "^9.4.0", + "execa": "^6.1.0", + "fs-extra": "^10.1.0", "postcss": "^8.4.16", "postcss-rem-to-responsive-pixel": "^5.1.3", "prettier": "^2.7.1", diff --git a/app/scripts/build.h5.mjs b/app/scripts/build.h5.mjs new file mode 100644 index 00000000..5f32d018 --- /dev/null +++ b/app/scripts/build.h5.mjs @@ -0,0 +1,37 @@ +import { execaCommand } from 'execa' +import path from 'path' +import fsExtra from 'fs-extra' +const { existsSync, remove, copy } = fsExtra +const cwd = process.cwd() +//打包发布路径,谨慎改动 +const releaseRelativePath = '../h5' +const distPath = path.resolve(cwd, 'dist/build/h5') +const releasePath = path.resolve(cwd, releaseRelativePath) + +async function build() { + await execaCommand('uni build', { stdio: 'inherit', encoding: 'utf-8', cwd }) + if (existsSync(releasePath)) { + await remove(releasePath) + } + console.log(`文件正在复制 ==> ${releaseRelativePath}`) + try { + await copyFile(distPath, releasePath) + } catch (error) { + console.log(`\n ${error}`) + } + console.log(`文件已复制 ==> ${releaseRelativePath}`) +} + +function copyFile(sourceDir, targetDir) { + return new Promise((resolve, reject) => { + copy(sourceDir, targetDir, (err) => { + if (err) { + reject(err) + } else { + resolve() + } + }) + }) +} + +build() diff --git a/app/src/App.vue b/app/src/App.vue index 27472863..46fd5891 100644 --- a/app/src/App.vue +++ b/app/src/App.vue @@ -2,10 +2,17 @@ import { onLaunch } from '@dcloudio/uni-app' import { useAppStore } from './stores/app' import { useUserStore } from './stores/user' -const { getConfig } = useAppStore() +const appStore = useAppStore() const { getUser } = useUserStore() onLaunch(async () => { - await getConfig() + await appStore.getConfig() + // #ifdef H5 + const { status, close, url } = appStore.getH5Config + if (status == 0) { + if (close == 1) return (location.href = url) + uni.reLaunch({ url: '/pages/empty/empty' }) + } + // #endif await getUser() }) diff --git a/app/src/api/account.ts b/app/src/api/account.ts index c9f31de7..365c33a0 100644 --- a/app/src/api/account.ts +++ b/app/src/api/account.ts @@ -15,3 +15,12 @@ export function register(data: Record) { export function forgotPassword(data: Record) { return request.post({ url: '/login/forgotPassword', data }) } + +//向微信请求code的链接 +export function getWxCodeUrl() { + return request.get({ url: '/login/codeUrl', data: { url: location.href } }) +} + +export function OALogin(data: Record) { + return request.get({ url: '/login/oaLogin', data }) +} diff --git a/app/src/manifest.json b/app/src/manifest.json index 6aff3ba6..71321eb8 100644 --- a/app/src/manifest.json +++ b/app/src/manifest.json @@ -68,5 +68,12 @@ "uniStatistics": { "enable": false }, - "vueVersion": "3" + "vueVersion": "3", + "h5": { + "router": { + "mode": "history", + "base": "/mobile" + }, + "title": "加载中" + } } diff --git a/app/src/pages.json b/app/src/pages.json index 9bc91660..77e725c4 100644 --- a/app/src/pages.json +++ b/app/src/pages.json @@ -103,6 +103,12 @@ "navigationBarTitleText": "绑定手机号" } }, + { + "path": "pages/empty/empty", + "style": { + "navigationStyle": "custom" + } + }, { "path": "uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-cropper", "style": { diff --git a/app/src/pages/empty/empty.vue b/app/src/pages/empty/empty.vue new file mode 100644 index 00000000..76bd7485 --- /dev/null +++ b/app/src/pages/empty/empty.vue @@ -0,0 +1,7 @@ + + + + + diff --git a/app/src/pages/login/login.vue b/app/src/pages/login/login.vue index 088ca2d4..4ed432e0 100644 --- a/app/src/pages/login/login.vue +++ b/app/src/pages/login/login.vue @@ -132,7 +132,7 @@ 注册账号 - + 第三方登录
@@ -141,7 +141,7 @@ class="flex flex-col items-center" @click="wxLogin" > - +
微信登录
@@ -158,8 +158,12 @@ import { SMSEnum } from '@/enums/appEnums' import { useLockFn } from '@/hooks/useLockFn' import { useAppStore } from '@/stores/app' import { useUserStore } from '@/stores/user' +import { isWeixinClient } from '@/utils/client' import { currentPage } from '@/utils/util' -import { onShow } from '@dcloudio/uni-app' +// #ifdef H5 +import wechatOa from '@/utils/wechat' +// #endif +import { onLoad, onShow } from '@dcloudio/uni-app' import { computed, reactive, ref, shallowRef, watch } from 'vue' enum LoginTypeEnum { MOBILE = 'mobile', @@ -176,6 +180,10 @@ enum LoginAuthEnum { WX = 1, QQ = 2 } +const isWeixin = ref(true) +// #ifdef H5 +// isWeixin.value = isWeixinClient() +// #endif const userStore = useUserStore() const appStore = useAppStore() @@ -248,40 +256,51 @@ const loginFun = async (scene: LoginTypeEnum, code?: string) => { }) try { const data = await login(params) - const { token, isBindMobile } = data - if (!isBindMobile && isForceBindMobile.value) { - userStore.temToken = token - uni.navigateTo({ - url: '/pages/bind_mobile/bind_mobile' - }) - uni.hideLoading() - return - } - userStore.login(data.token) - await userStore.getUser() - uni.$u.toast('登录成功') - uni.hideLoading() - uni.navigateBack({ - success: () => { - // @ts-ignore - const { onLoad, options } = currentPage() - // 刷新上一个页面 - onLoad && onLoad(options) - } - }) + loginHandle(data) } catch (error: any) { uni.hideLoading() throw new Error(error) } } +const loginHandle = async (data: any) => { + const { token, isBindMobile } = data + if (!isBindMobile && isForceBindMobile.value) { + userStore.temToken = token + uni.navigateTo({ + url: '/pages/bind_mobile/bind_mobile' + }) + uni.hideLoading() + return + } + userStore.login(data.token) + await userStore.getUser() + uni.$u.toast('登录成功') + uni.hideLoading() + uni.navigateBack({ + success: () => { + // @ts-ignore + const { onLoad, options } = currentPage() + // 刷新上一个页面 + onLoad && onLoad(options) + } + }) +} + const { lockFn: handleLogin } = useLockFn(loginFun) const wxLogin = async () => { + // #ifdef MP-WEIXIN const data: any = await uni.login({ provider: 'weixin' }) handleLogin(LoginTypeEnum.MNP, data.code) + // #endif + // #ifdef H5 + if (isWeixin.value) { + wechatOa.getUrl() + } + // #endif } watch( @@ -312,6 +331,32 @@ onShow(async () => { uni.hideLoading() } }) + +onLoad(async (options) => { + if (userStore.isLogin) { + // 已经登录 => 首页 + uni.reLaunch({ + url: '/pages/index/index' + }) + return + } + + const { code } = options + if (code) { + uni.showLoading({ + title: '请稍后...' + }) + // #ifdef H5 + try { + const data = await wechatOa.authLogin(code) + loginHandle(data) + } catch (error: any) { + uni.hideLoading() + throw new Error(error) + } + // #endif + } +})