fix:微信公众号登录

This commit is contained in:
MJ 2025-08-03 01:33:06 +08:00
parent b52c1f9bfd
commit 1405cf7aea
6 changed files with 89 additions and 94 deletions

View File

@ -27,28 +27,34 @@ export const sendCodeMessageApi = async data =>
export const smsLoginApi = async data =>
await http.post<any>('/smsLogin', {
...data,
clientType: 'MOBILE',
clientType: 'PARENT',
})
interface WxLoginApiResponse {
accessToken: string
wxUser: any
account: string
phone: string
}
/**
*
* @param data
*/
export const wxLoginApi = async data =>
await http.post<any>('/wxLogin', {
await http.post<WxLoginApiResponse>('/wxLogin', {
...data,
clientType: 'MOBILE',
clientType: 'PARENT',
})
/**
*
* @param data
*/
export const wxBindAccountApi = async data =>
await http.post<any>('/wxBindAccount', {
...data,
clientType: 'MOBILE',
})
// /**
// * 微信绑定账号
// * @param data 请求参数
// */
// export const wxBindAccountApi = async data =>
// await http.post<any>('/wxBindAccount', {
// ...data,
// clientType: 'PARENT',
// })
/**
*

View File

@ -1,3 +1,4 @@
/* eslint-disable complexity */
/* eslint-disable prefer-promise-reject-errors */
import type { RequestOptions, ResponseData, ErrorResponse } from './types'
import { HTTP_STATUS, ERROR_MSG } from './config'
@ -45,40 +46,38 @@ export const responseInterceptor = async <T>(
// 错误拦截器
export const errorInterceptor = (error: any): Promise<ErrorResponse> => {
let message
const { statusCode, errMsg } = error
let toLogin = false
if (statusCode === HTTP_STATUS.SUCCESS) {
message = errMsg
} else {
let message = errMsg
// 处理特定状态码
switch (statusCode) {
case HTTP_STATUS.AUTHENTICATE:
toLogin = true
message = ERROR_MSG.AUTH_ERROR
message = message || ERROR_MSG.AUTH_ERROR
break
case HTTP_STATUS.FORBIDDEN:
toLogin = true
message = ERROR_MSG.FORBIDDEN_ERROR
message = message || ERROR_MSG.FORBIDDEN_ERROR
break
case HTTP_STATUS.NOT_FOUND:
message = ERROR_MSG.NOT_FOUND_ERROR
case HTTP_STATUS.NOT_FOUND: {
message = message || ERROR_MSG.NOT_FOUND_ERROR
break
}
case HTTP_STATUS.SERVER_ERROR:
case HTTP_STATUS.BAD_GATEWAY:
case HTTP_STATUS.SERVICE_UNAVAILABLE:
case HTTP_STATUS.GATEWAY_TIMEOUT:
message = ERROR_MSG.SERVER_ERROR
message = message || ERROR_MSG.SERVER_ERROR
break
default:
message = ERROR_MSG.DEFAULT_ERROR
message = message || ERROR_MSG.DEFAULT_ERROR
break
}
}
// 显示错误提示
if (message) {

View File

@ -30,7 +30,7 @@
class="login_form_item_icon"
></image>
<input
v-model="loginForm.verifyCode"
v-model="verifyCode"
class="input_box"
type="number"
placeholder="请输入验证码"
@ -51,8 +51,8 @@
</mj-page>
</template>
<script setup lang="tsx">
import { ref, computed, nextTick, onMounted } from 'vue'
<script setup lang="ts">
import { ref, computed, nextTick, onMounted, watch } from 'vue'
import {
getBindPhoneTypeApi,
@ -61,7 +61,6 @@ import {
getUserInfoApi,
getAuthUrlApi,
wxLoginApi,
wxBindAccountApi,
} from '@/api'
import { setCache, getCache } from '@/utils'
import { getUserInfo as dx_getUserInfoApi } from '@/api/inspector/mine'
@ -69,29 +68,22 @@ import { user } from '@/store'
import router from '@/router/router'
import db from '@/utils/db'
import hud from '@/utils/hud'
import { onLoad, onShow } from '@dcloudio/uni-app'
const emits = defineEmits(['reloadFun'])
const OSS_URL = import.meta.env.VITE_OSS_HOST
const { getUserInfo, setToken } = user()
const formatPhone = ref('')
const adminPopup = ref()
const userAdmin = ref([]) //
const loginForm = ref({
adminType: 16, //
clientType: 'MOBILE',
phone: '',
verifyCode: '',
})
const verifyCode = ref('')
const codeTime = ref(0)
const codeTimeContet = computed(() => {
return codeTime.value
})
function validatePhone() {
loginForm.value.phone = formatPhone.value.replace(/\s+/g, '')
let reg = /^[1][0,1,2,3, 4, 5, 6, 7, 8, 9][0-9]{9}$/
return reg.test(loginForm.value.phone)
return reg.test(formatPhone.value.replace(/\s+/g, ''))
}
// 344
function phoneDivision() {
@ -106,11 +98,7 @@ function phoneDivision() {
formatPhone.value = value
})
}
//
async function fetchDXUserInfo() {
const data = await dx_getUserInfoApi()
db.set('dxs_userInfo', data)
}
//
async function getCode() {
if (!validatePhone()) {
@ -118,7 +106,7 @@ async function getCode() {
return
}
try {
await sendCodeMessageApi(loginForm.value.phone)
await sendCodeMessageApi(formatPhone.value.replace(/\s+/g, ''))
hud.success('发送成功!')
codeTime.value = 60
let timer = setInterval(() => {
@ -134,38 +122,25 @@ async function getCode() {
}
//
async function handleLogin() {
if (!validatePhone() || !loginForm.value.verifyCode) {
uni.showToast({
icon: 'none',
title: '账号或验证码不正确',
})
if (!validatePhone() || !verifyCode.value) {
hud.error('账号或验证码不正确')
return
}
if (db.get('openid')) {
await wxBindAccount()
} else {
await smsLogin()
}
}
let wxUser: any
async function smsLogin() {
hud.load({
task: async () => {
const token = await smsLoginApi(loginForm.value)
setToken(token)
await getUserInfo()
router.reLaunch('/pages/home/index')
},
option: '登录',
const token = await smsLoginApi({
adminType: 16,
clientType: 'PARENT',
phone: formatPhone.value.replace(/\s+/g, ''),
verifyCode: verifyCode.value,
wxUser,
})
}
async function wxBindAccount() {
hud.load({
task: async () => {
await wxBindAccountApi(loginForm.value)
await getUserInfo()
router.reLaunch('/pages/home/index')
await loginSuccess(token)
},
option: '登录',
})
@ -182,16 +157,27 @@ async function wxLogin() {
hud.load({
task: async () => {
//
const { accessToken, openid } = await wxLoginApi({
const data = await wxLoginApi({
code,
})
db.set('openid', openid)
setToken(accessToken)
if (data.accessToken) {
//
await loginSuccess(data.accessToken)
} else {
//
wxUser = data.wxUser
}
},
option: '微信自动登录',
option: '登录',
})
}
async function loginSuccess(accessToken: any) {
setToken(accessToken)
await getUserInfo()
router.reLaunch('/pages/home/index')
}
onMounted(async () => {
// token
db.pop('token')

View File

@ -80,10 +80,10 @@ function handleJump(i: any) {
}
//
function handlePrefectInfo() {
router.navigateTo({
path: `/pages/mine/prefectInformation/index`,
query: { type: '1' }
})
// router.navigateTo({
// path: `/pages/mine/prefectInformation/index`,
// query: { type: '1' }
// })
}
onMounted(() => {

View File

@ -28,9 +28,9 @@
</view>
<view class="btn">
<button class="single btn_save" @click="saveUpdate">保存</button>
<button v-if="pageType === 1" class="single btn_quit" @click="() => (showCustomPopup = true)">
<!-- <button v-if="pageType === 1" class="single btn_quit" @click="() => (showCustomPopup = true)">
退出登录
</button>
</button> -->
</view>
<!-- 弹出框选项 -->
<view class="picker_box">

View File

@ -98,6 +98,10 @@ function beforeEach(options: RouterOptions) {
// 最终封装成options = {from:{url}, to:{url}}
const { from, to } = beforeOptions
if (!from.path) {
return false
}
// 登录页面下跳转登录页面,跳转到首页
if (from.path === to.path && to.path === route_login) {
return false