fix:微信公众号登录
This commit is contained in:
parent
b52c1f9bfd
commit
1405cf7aea
@ -27,28 +27,34 @@ export const sendCodeMessageApi = async data =>
|
|||||||
export const smsLoginApi = async data =>
|
export const smsLoginApi = async data =>
|
||||||
await http.post<any>('/smsLogin', {
|
await http.post<any>('/smsLogin', {
|
||||||
...data,
|
...data,
|
||||||
clientType: 'MOBILE',
|
clientType: 'PARENT',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
interface WxLoginApiResponse {
|
||||||
|
accessToken: string
|
||||||
|
wxUser: any
|
||||||
|
account: string
|
||||||
|
phone: string
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 微信登录
|
* 微信登录
|
||||||
* @param data 请求参数
|
* @param data 请求参数
|
||||||
*/
|
*/
|
||||||
export const wxLoginApi = async data =>
|
export const wxLoginApi = async data =>
|
||||||
await http.post<any>('/wxLogin', {
|
await http.post<WxLoginApiResponse>('/wxLogin', {
|
||||||
...data,
|
...data,
|
||||||
clientType: 'MOBILE',
|
clientType: 'PARENT',
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* 微信绑定账号
|
// * 微信绑定账号
|
||||||
* @param data 请求参数
|
// * @param data 请求参数
|
||||||
*/
|
// */
|
||||||
export const wxBindAccountApi = async data =>
|
// export const wxBindAccountApi = async data =>
|
||||||
await http.post<any>('/wxBindAccount', {
|
// await http.post<any>('/wxBindAccount', {
|
||||||
...data,
|
// ...data,
|
||||||
clientType: 'MOBILE',
|
// clientType: 'PARENT',
|
||||||
})
|
// })
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息
|
* 获取用户信息
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable complexity */
|
||||||
/* eslint-disable prefer-promise-reject-errors */
|
/* eslint-disable prefer-promise-reject-errors */
|
||||||
import type { RequestOptions, ResponseData, ErrorResponse } from './types'
|
import type { RequestOptions, ResponseData, ErrorResponse } from './types'
|
||||||
import { HTTP_STATUS, ERROR_MSG } from './config'
|
import { HTTP_STATUS, ERROR_MSG } from './config'
|
||||||
@ -45,40 +46,38 @@ export const responseInterceptor = async <T>(
|
|||||||
|
|
||||||
// 错误拦截器
|
// 错误拦截器
|
||||||
export const errorInterceptor = (error: any): Promise<ErrorResponse> => {
|
export const errorInterceptor = (error: any): Promise<ErrorResponse> => {
|
||||||
let message
|
|
||||||
const { statusCode, errMsg } = error
|
const { statusCode, errMsg } = error
|
||||||
let toLogin = false
|
let toLogin = false
|
||||||
if (statusCode === HTTP_STATUS.SUCCESS) {
|
|
||||||
message = errMsg
|
let message = errMsg
|
||||||
} else {
|
|
||||||
// 处理特定状态码
|
// 处理特定状态码
|
||||||
switch (statusCode) {
|
switch (statusCode) {
|
||||||
case HTTP_STATUS.AUTHENTICATE:
|
case HTTP_STATUS.AUTHENTICATE:
|
||||||
toLogin = true
|
toLogin = true
|
||||||
message = ERROR_MSG.AUTH_ERROR
|
message = message || ERROR_MSG.AUTH_ERROR
|
||||||
break
|
break
|
||||||
|
|
||||||
case HTTP_STATUS.FORBIDDEN:
|
case HTTP_STATUS.FORBIDDEN:
|
||||||
toLogin = true
|
toLogin = true
|
||||||
message = ERROR_MSG.FORBIDDEN_ERROR
|
message = message || ERROR_MSG.FORBIDDEN_ERROR
|
||||||
break
|
break
|
||||||
|
|
||||||
case HTTP_STATUS.NOT_FOUND:
|
case HTTP_STATUS.NOT_FOUND: {
|
||||||
message = ERROR_MSG.NOT_FOUND_ERROR
|
message = message || ERROR_MSG.NOT_FOUND_ERROR
|
||||||
break
|
break
|
||||||
|
}
|
||||||
|
|
||||||
case HTTP_STATUS.SERVER_ERROR:
|
case HTTP_STATUS.SERVER_ERROR:
|
||||||
case HTTP_STATUS.BAD_GATEWAY:
|
case HTTP_STATUS.BAD_GATEWAY:
|
||||||
case HTTP_STATUS.SERVICE_UNAVAILABLE:
|
case HTTP_STATUS.SERVICE_UNAVAILABLE:
|
||||||
case HTTP_STATUS.GATEWAY_TIMEOUT:
|
case HTTP_STATUS.GATEWAY_TIMEOUT:
|
||||||
message = ERROR_MSG.SERVER_ERROR
|
message = message || ERROR_MSG.SERVER_ERROR
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
message = ERROR_MSG.DEFAULT_ERROR
|
message = message || ERROR_MSG.DEFAULT_ERROR
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 显示错误提示
|
// 显示错误提示
|
||||||
if (message) {
|
if (message) {
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
class="login_form_item_icon"
|
class="login_form_item_icon"
|
||||||
></image>
|
></image>
|
||||||
<input
|
<input
|
||||||
v-model="loginForm.verifyCode"
|
v-model="verifyCode"
|
||||||
class="input_box"
|
class="input_box"
|
||||||
type="number"
|
type="number"
|
||||||
placeholder="请输入验证码"
|
placeholder="请输入验证码"
|
||||||
@ -51,8 +51,8 @@
|
|||||||
</mj-page>
|
</mj-page>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="tsx">
|
<script setup lang="ts">
|
||||||
import { ref, computed, nextTick, onMounted } from 'vue'
|
import { ref, computed, nextTick, onMounted, watch } from 'vue'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getBindPhoneTypeApi,
|
getBindPhoneTypeApi,
|
||||||
@ -61,7 +61,6 @@ import {
|
|||||||
getUserInfoApi,
|
getUserInfoApi,
|
||||||
getAuthUrlApi,
|
getAuthUrlApi,
|
||||||
wxLoginApi,
|
wxLoginApi,
|
||||||
wxBindAccountApi,
|
|
||||||
} from '@/api'
|
} from '@/api'
|
||||||
import { setCache, getCache } from '@/utils'
|
import { setCache, getCache } from '@/utils'
|
||||||
import { getUserInfo as dx_getUserInfoApi } from '@/api/inspector/mine'
|
import { getUserInfo as dx_getUserInfoApi } from '@/api/inspector/mine'
|
||||||
@ -69,29 +68,22 @@ import { user } from '@/store'
|
|||||||
import router from '@/router/router'
|
import router from '@/router/router'
|
||||||
import db from '@/utils/db'
|
import db from '@/utils/db'
|
||||||
import hud from '@/utils/hud'
|
import hud from '@/utils/hud'
|
||||||
|
import { onLoad, onShow } from '@dcloudio/uni-app'
|
||||||
|
|
||||||
const emits = defineEmits(['reloadFun'])
|
const emits = defineEmits(['reloadFun'])
|
||||||
const OSS_URL = import.meta.env.VITE_OSS_HOST
|
const OSS_URL = import.meta.env.VITE_OSS_HOST
|
||||||
const { getUserInfo, setToken } = user()
|
const { getUserInfo, setToken } = user()
|
||||||
|
|
||||||
const formatPhone = ref('')
|
const formatPhone = ref('')
|
||||||
const adminPopup = ref()
|
const verifyCode = ref('')
|
||||||
const userAdmin = ref([]) // 用户身份列表
|
|
||||||
const loginForm = ref({
|
|
||||||
adminType: 16, // 家长
|
|
||||||
clientType: 'MOBILE',
|
|
||||||
phone: '',
|
|
||||||
verifyCode: '',
|
|
||||||
})
|
|
||||||
const codeTime = ref(0)
|
const codeTime = ref(0)
|
||||||
const codeTimeContet = computed(() => {
|
const codeTimeContet = computed(() => {
|
||||||
return codeTime.value
|
return codeTime.value
|
||||||
})
|
})
|
||||||
|
|
||||||
function validatePhone() {
|
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}$/
|
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分割
|
// 手机344分割
|
||||||
function phoneDivision() {
|
function phoneDivision() {
|
||||||
@ -106,11 +98,7 @@ function phoneDivision() {
|
|||||||
formatPhone.value = value
|
formatPhone.value = value
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// 督学师个人资料
|
|
||||||
async function fetchDXUserInfo() {
|
|
||||||
const data = await dx_getUserInfoApi()
|
|
||||||
db.set('dxs_userInfo', data)
|
|
||||||
}
|
|
||||||
// 获取验证码
|
// 获取验证码
|
||||||
async function getCode() {
|
async function getCode() {
|
||||||
if (!validatePhone()) {
|
if (!validatePhone()) {
|
||||||
@ -118,7 +106,7 @@ async function getCode() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await sendCodeMessageApi(loginForm.value.phone)
|
await sendCodeMessageApi(formatPhone.value.replace(/\s+/g, ''))
|
||||||
hud.success('发送成功!')
|
hud.success('发送成功!')
|
||||||
codeTime.value = 60
|
codeTime.value = 60
|
||||||
let timer = setInterval(() => {
|
let timer = setInterval(() => {
|
||||||
@ -134,38 +122,25 @@ async function getCode() {
|
|||||||
}
|
}
|
||||||
// 登录
|
// 登录
|
||||||
async function handleLogin() {
|
async function handleLogin() {
|
||||||
if (!validatePhone() || !loginForm.value.verifyCode) {
|
if (!validatePhone() || !verifyCode.value) {
|
||||||
uni.showToast({
|
hud.error('账号或验证码不正确')
|
||||||
icon: 'none',
|
|
||||||
title: '账号或验证码不正确',
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (db.get('openid')) {
|
|
||||||
await wxBindAccount()
|
|
||||||
} else {
|
|
||||||
await smsLogin()
|
await smsLogin()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
let wxUser: any
|
||||||
async function smsLogin() {
|
async function smsLogin() {
|
||||||
hud.load({
|
hud.load({
|
||||||
task: async () => {
|
task: async () => {
|
||||||
const token = await smsLoginApi(loginForm.value)
|
const token = await smsLoginApi({
|
||||||
setToken(token)
|
adminType: 16,
|
||||||
await getUserInfo()
|
clientType: 'PARENT',
|
||||||
router.reLaunch('/pages/home/index')
|
phone: formatPhone.value.replace(/\s+/g, ''),
|
||||||
},
|
verifyCode: verifyCode.value,
|
||||||
option: '登录',
|
wxUser,
|
||||||
})
|
})
|
||||||
}
|
await loginSuccess(token)
|
||||||
|
|
||||||
async function wxBindAccount() {
|
|
||||||
hud.load({
|
|
||||||
task: async () => {
|
|
||||||
await wxBindAccountApi(loginForm.value)
|
|
||||||
await getUserInfo()
|
|
||||||
router.reLaunch('/pages/home/index')
|
|
||||||
},
|
},
|
||||||
option: '登录',
|
option: '登录',
|
||||||
})
|
})
|
||||||
@ -182,16 +157,27 @@ async function wxLogin() {
|
|||||||
hud.load({
|
hud.load({
|
||||||
task: async () => {
|
task: async () => {
|
||||||
// 登录逻辑
|
// 登录逻辑
|
||||||
const { accessToken, openid } = await wxLoginApi({
|
const data = await wxLoginApi({
|
||||||
code,
|
code,
|
||||||
})
|
})
|
||||||
db.set('openid', openid)
|
if (data.accessToken) {
|
||||||
setToken(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 () => {
|
onMounted(async () => {
|
||||||
// 清空token
|
// 清空token
|
||||||
db.pop('token')
|
db.pop('token')
|
||||||
|
@ -80,10 +80,10 @@ function handleJump(i: any) {
|
|||||||
}
|
}
|
||||||
// 完善信息
|
// 完善信息
|
||||||
function handlePrefectInfo() {
|
function handlePrefectInfo() {
|
||||||
router.navigateTo({
|
// router.navigateTo({
|
||||||
path: `/pages/mine/prefectInformation/index`,
|
// path: `/pages/mine/prefectInformation/index`,
|
||||||
query: { type: '1' }
|
// query: { type: '1' }
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
@ -28,9 +28,9 @@
|
|||||||
</view>
|
</view>
|
||||||
<view class="btn">
|
<view class="btn">
|
||||||
<button class="single btn_save" @click="saveUpdate">保存</button>
|
<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>
|
||||||
<!-- 弹出框选项 -->
|
<!-- 弹出框选项 -->
|
||||||
<view class="picker_box">
|
<view class="picker_box">
|
||||||
|
@ -98,6 +98,10 @@ function beforeEach(options: RouterOptions) {
|
|||||||
// 最终封装成options = {from:{url}, to:{url}}
|
// 最终封装成options = {from:{url}, to:{url}}
|
||||||
const { from, to } = beforeOptions
|
const { from, to } = beforeOptions
|
||||||
|
|
||||||
|
if (!from.path) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// 登录页面下跳转登录页面,跳转到首页
|
// 登录页面下跳转登录页面,跳转到首页
|
||||||
if (from.path === to.path && to.path === route_login) {
|
if (from.path === to.path && to.path === route_login) {
|
||||||
return false
|
return false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user