微信小程序一键登录

This commit is contained in:
dzc committed 2026-03-10 11:05:33 +08:00
1 parent 955c13429a
commit f31015deb0
12 files changed
+132 -86

No files matched your search

+51 -47
View File
@@ -1,73 +1,59 @@
<script setup lang="ts">
import { ref, reactive } from "vue"
import { onLoad, onShow } from "@dcloudio/uni-app"
import { onShow } from "@dcloudio/uni-app"
import { constant, router } from "@@/index"
import { userApi } from "@@/api"
import { userApi, miniPlatformApi } from "@@/api"
import { getAppInfo } from "@@/utils/app"
import AffirmCheck from "@/sheep/components/common/affirm-check.vue"
import AffirmModal from "@/sheep/components/common/affirm-modal.vue"
import ServiceText from "@/sheep/components/common/service-text.vue"
const affirmModalRef = ref()
const state = reactive<any>({
userInfo: null,
isAgree: false,
})
const applyForm = reactive<any>({
username: "",
password: "",
loginErrorVisible: false,
loginErrorCode: null,
})
onLoad(() => {
const rememberUser = uni.getStorageSync(constant.storageConstant.REMEMBER_USER)
if (rememberUser) {
applyForm.username = rememberUser.username
applyForm.password = rememberUser.password
}
})
const serviceTextRef = ref<any>(null)
onShow(() => {
const userInfoKey = constant.storageConstant.USERINFO
state.userInfo = uni.getStorageSync(userInfoKey)
})
const applyFormRef = ref<any>(null)
const doSubmit = async () => {
applyFormRef.value.submit()
const { isPass } = applyFormRef.value.validate()
if (!isPass) return
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
const rememberUser = constant.storageConstant.REMEMBER_USER
uni.removeStorageSync(tokenKey)
uni.showLoading({ title: "登录中...", mask: true })
const res: any = await userApi.login({
...applyForm,
grant_type: "password",
scope: "server",
})
if (res.code != 0) {
uni.showToast({ title: res.msg + ",登录失败,请重新登录", icon: "error" })
return
}
uni.setStorageSync(tokenKey, res.data)
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(userInfoKey, userData)
uni.setStorageSync(rememberUser, {
username: applyForm.username,
password: applyForm.password,
} as GlobalSetting.RememberUser)
uni.hideLoading()
uni.showToast({ title: "登录成功", icon: "none" })
router.jump("/pages/tabbar/canteen")
}
// 小程序获取手机号
const minAppGetphone = (e) => {
const minAppGetphone = async (e) => {
const phoneDetail = e?.detail
if (!phoneDetail) {
uni.showToast({ title: "获取手机号失败", icon: "none" })
return
}
try {
uni.showLoading({ title: '登录中...', mask: true })
// 获取用户手机号
const { data, code }: any = await miniPlatformApi.autoAdminLogin({
miniPlatformType: await getAppInfo(),
code: phoneDetail.code,
})
if (data.errorCode) {
state.loginErrorCode = data.errorCode
state.loginErrorVisible = true
uni.hideLoading()
return
}
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(constant.storageConstant.USERINFO, userData)
uni.setStorageSync(constant.storageConstant.TOKEN, data)
uni.hideLoading()
uni.showToast({ title: "登录成功", icon: "success" })
router.jump("/pages/tabbar/canteen")
} catch (error) {
console.error("登录错误", error);
}
}
// 没有授权点击登录
@@ -79,6 +65,9 @@ const doLogin = () => {
}
}
const affirmConfirm = () => {
state.isAgree = true
}
</script>
@@ -103,7 +92,22 @@ const doLogin = () => {
</view>
<affirm-check v-model="state.isAgree" class="absolute bottom-30rpx" />
<affirm-modal ref="affirmModalRef" @confirm="state.isAgree = true" />
<affirm-modal ref="affirmModalRef" @confirm="affirmConfirm" />
<service-text ref="serviceTextRef" :showPhoneText="false" />
<tm-modal :show="state.loginErrorVisible" title="登录错误" cancelText="取消" confirmText="联系客服" height="auto"
@close="state.loginErrorVisible = false" @confirm="serviceTextRef?.openModal?.()">
<view class="px-20rpx bottom-40rpx">
<view v-if="state.loginErrorCode == '10'" class="sub-text-26">
<text>登录错误,获取本机微信手机号失败,请联系客服</text>
</view>
<view v-if="state.loginErrorCode == '20'" class="sub-text-26">
<text>登录错误,本系统只为浦江县机关事务服务,如有疑问请联系客服</text>
</view>
<view v-if="state.loginErrorCode == '30'" class="sub-text-26">
<text>登录错误,请联系客服</text>
</view>
</view>
</tm-modal>
</crud-layout>
</template>
+6 -4
View File
@@ -16,14 +16,16 @@ const state = reactive<any>({
onLoad(() => {
const app = getApp({ allowDefault: true })
console.log("app:", app);
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
})
onShow(() => {
const app = getApp({ allowDefault: true })
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
reportPageAccess("pages/tabbar/canteen")
if (pageRef.value) {
pageRef.value.reload(false)
+14 -13
View File
@@ -1,6 +1,5 @@
<template>
<crud-layout title="我的" mainIsScroll bg="/static/page/blue-bg.png" navRightIcon="setting"
@navRightClick="onNavRightClick">
<crud-layout title="我的" mainIsScroll bg="/static/page/blue-bg.png">
<template #main-top>
<view class="flex items-center py-3 mx-30rpx">
<image v-if="state.userInfo?.sysUser?.avatar && false" class="size-100rpx rounded-full"
@@ -18,7 +17,7 @@
</view>
</template>
<view class="mt-10rpx rounded-20rpx shadow-main p-30rpx pb-40rpx bg-white">
<view class="mt-30rpx rounded-20rpx shadow-main p-30rpx pb-40rpx bg-white">
<view class="service_item flex items-center justify-between" v-for="(v, i) in state.menuList" :key="i"
@tap="onTabMenu(v)">
<view class="flex-row-center">
@@ -71,12 +70,12 @@ const state = reactive<any>({
wallet: [],
userInfo: {},
menuList: [
// { text: "扫一扫", icon: "/static/icon/scan.png", path: "" },
{ text: "钱包", icon: "/static/icon/service_3.png", path: "/pages/wallet/list" },
{ text: "喜好", icon: "/static/icon/service_4.png", path: "/pages/tabbar/canteenAnalyse" },
// { text: "我的人脸", icon: "/static/icon/service_5.png", path: "/pages/human-face/list" },
{ text: "赚米", icon: "/static/icon/service_7.png", path: "/pages/ad/make-money" },
// { text: "车辆管理", icon: "/static/icon/service_6.png", path: "/pages/car/user-car/list" },
{ text: "扫一扫", icon: "/static/icon/scan.png", path: "" },
{ text: "我参加的活动", icon: "/static/icon/act.png", path: "/pages/merchant/my-act-list" },
{ text: "我的钱包", icon: "/static/icon/service_3.png", path: "/pages/wallet/list" },
{ text: "我的人脸", icon: "/static/icon/service_5.png", path: "/pages/human-face/list" },
{ text: "车辆管理", icon: "/static/icon/service_6.png", path: "/pages/car/user-car/list" },
{ text: "清除缓存", icon: "/static/icon/clear-storage.png", path: "" },
],
appInfo: "",
actModalShow: false,
@@ -339,7 +338,9 @@ const initMenuConfig = () => {
onLoad(async () => {
const app = getApp({ allowDefault: true })
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
// 初始化配置菜单
initMenuConfig()
// 获取应用信息
@@ -352,12 +353,12 @@ onLoad(async () => {
})
onShow(() => {
const app = getApp({ allowDefault: true })
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
reportPageAccess("pages/tabbar/my")
// 获取存储中的用户信息
storage.getUserInfo()
// 刷新用户
app._getUserInfo()
})
+6 -2
View File
@@ -17,12 +17,16 @@ const state = reactive<any>({
onLoad(() => {
const app = getApp({ allowDefault: true })
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
})
onShow(() => {
const app = getApp({ allowDefault: true })
app._initTabbar()
if (app._initTabbar) {
app._initTabbar()
}
reportPageAccess("pages/tabbar/canteen")
if (pageRef.value) {
pageRef.value.reload(false)