From 76da09be915e58a8af9f18a153a57de940f7d665 Mon Sep 17 00:00:00 2001 From: wangxiaowei <1121133807@qq.com> Date: Tue, 14 Oct 2025 18:08:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env/.env | 2 +- src/App.vue | 2 +- src/api/home.ts | 27 +++++++++++ src/api/login.ts | 18 +++++++- src/api/types/home.ts | 6 +++ src/hooks/useWeiXin.ts | 94 +++++++++++++++++++++----------------- src/http/alova.ts | 31 +++++++++++-- src/http/tools/enum.ts | 2 +- src/http/types.ts | 2 +- src/pages/index/index.vue | 28 ++++++++---- src/pages/login/mobile.vue | 37 ++++++++++----- src/utils/teaSpecialist.ts | 10 ++-- 12 files changed, 180 insertions(+), 79 deletions(-) create mode 100644 src/api/home.ts create mode 100644 src/api/types/home.ts diff --git a/env/.env b/env/.env index 01b2c76..b85e6c8 100644 --- a/env/.env +++ b/env/.env @@ -8,7 +8,7 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b' VITE_APP_PUBLIC_BASE = '/h5' # 登录页面 -VITE_LOGIN_URL = '/pages/login/login' +VITE_LOGIN_URL = '/pages/login/mobile' # 第一个请求地址 VITE_SERVER_BASEURL = 'https://cz.stnav.com' diff --git a/src/App.vue b/src/App.vue index a04de0e..8bb200c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -17,7 +17,7 @@ } // 微信静默授权 - // snsapiBaseAuthorize() + snsapiBaseAuthorize() initJweixinSDK() }) onShow((options) => { diff --git a/src/api/home.ts b/src/api/home.ts new file mode 100644 index 0000000..4f23fae --- /dev/null +++ b/src/api/home.ts @@ -0,0 +1,27 @@ +import { http } from '@/http/alova' +import type { IIndexResult } from '@/api/types/home' + +/** + * 获取首页数据 + */ +export interface IIndexData { + id: number +} + +export function getDecorate(data: IIndexData) { + return http.Get('/api/index/decorate', {params: data}) +} + +/** + * 获取茶艺师等级 + */ +export function getTeaSpecialistLevels() { + return http.Post('/api/Teamaster/teamasterLevel') +} + +/** + * 获取茶艺师列表 + */ +export function getTeaSpecialist() { + return http.Post('/api/Teamaster/teamasterLevel') +} diff --git a/src/api/login.ts b/src/api/login.ts index d5a0396..852f4d5 100644 --- a/src/api/login.ts +++ b/src/api/login.ts @@ -11,7 +11,9 @@ export interface IWxSnsapiBaseLoginForm { } export function wxSnsapiBaseLogin(data: IWxSnsapiBaseLoginForm) { - return http.Post('/api/login/oaLogin', data) + return http.Post('/api/login/oaLogin', data, { + meta: { ignoreAuth: true }, // 忽略认证 + }) } /** @@ -24,7 +26,19 @@ export interface IPhoneLoginForm { } export function phoneLogin(data: IPhoneLoginForm) { - return http.Post('/api/login/oaLogin', {params: data}) + return http.Post('/api/login/oaLogin', data) +} + +/** + * 获取验证码 + */ +export interface ISMSLogin { + mobile: number, + scene: string +} + +export function smsCode(data: ISMSLogin) { + return http.Post('/api/sms/sendCode',data) } /** diff --git a/src/api/types/home.ts b/src/api/types/home.ts new file mode 100644 index 0000000..ccfbde6 --- /dev/null +++ b/src/api/types/home.ts @@ -0,0 +1,6 @@ +/** + * 首页初始化返回数据 + */ +export interface IIndexResult { + data: String +} diff --git a/src/hooks/useWeiXin.ts b/src/hooks/useWeiXin.ts index 63ed709..eced22f 100644 --- a/src/hooks/useWeiXin.ts +++ b/src/hooks/useWeiXin.ts @@ -25,49 +25,57 @@ const getUrlCode = (): { [key: string]: string | undefined } => { */ export function snsapiBaseAuthorize() { // TODO 测试代码 - const res = { - account: "u39317465", - avatar: "https://cz.stnav.com/uploads/user/avatar/c2b3c5f94e3f20c8a989bd302519b4c7.jpeg", - channel:2, - is_new_user: 1, - id: 1, - sn:"39317465", - token: "14a11310d926121e352afec2ef1d2f7f", - nickname: '微信用户', - mobile: '15005837859' - } - useUserStore().setUserInfo(res) - console.log(useUserStore().userInfo) - return + // wxSnsapiBaseLogin({code: '0316MW1w32OBM53meU2w3GPdbe16MW1p'}).then((res: IUserInfoVo) => { + // console.log("登录成功 ~ snsapiBaseAuthorize ~ res:", res) + // // 映射 IUserLogin 到 IUserInfoVo + // useUserStore().setUserInfo(res) + // }).catch(err => { + // // 失败就重新授权 + // uni.setStorageSync('wechatCode', 0) + // console.log('请求失败', err) + // }) + // const res = { + // account: "u39317465", + // avatar: "https://cz.stnav.com/uploads/user/avatar/c2b3c5f94e3f20c8a989bd302519b4c7.jpeg", + // channel:2, + // is_new_user: 1, + // id: 4, + // sn:"39317465", + // token: "c6416f9ebed9f06d3807d01e9c9a9693", + // nickname: '微信用户', + // mobile: '15005837859' + // } + // useUserStore().setUserInfo(res) + // // console.log(useUserStore().userInfo) + // return - let local = window.location.href // 获取页面url - let appid = import.meta.env.VITE_WX_SERVICE_ACCOUNT_APPID // 公众号的APPID - console.log("🚀 ~ snsapiBaseAuthorize ~ appid:", appid) - let code = getUrlCode().code // 截取code - // 获取之前的code - let oldCode = uni.getStorageSync('wechatCode') + // let local = window.location.href // 获取页面url + // let appid = import.meta.env.VITE_WX_SERVICE_ACCOUNT_APPID // 公众号的APPID + // console.log("🚀 ~ snsapiBaseAuthorize ~ appid:", appid) + // let code = getUrlCode().code // 截取code + // // 获取之前的code + // let oldCode = uni.getStorageSync('wechatCode') - if (code == null || code === '' || code == 'undefined' || code == oldCode) { - // 如果没有code,就去请求获取code - console.log('当前没有code,进入授权页面') - let uri = encodeURIComponent(local) - // 设置旧的code为0,避免死循环 - uni.setStorageSync('wechatCode',0) - window.location.href = - `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect` - } else { - // 保存最新code - uni.setStorageSync('wechatCode',code) - // 使用code换取用户信息 - wxSnsapiBaseLogin({code}).then((res: IUserInfoVo) => { - console.log("登录成功 ~ snsapiBaseAuthorize ~ res:", res) - // 映射 IUserLogin 到 IUserInfoVo - useUserStore().setUserInfo(res) - console.log('登录成功 ~ ' + useUserStore().userInfo) - }).catch(err => { - // 失败就重新授权 - uni.setStorageSync('wechatCode', 0) - console.log('请求失败', err) - }) - } + // if (code == null || code === '' || code == 'undefined' || code == oldCode) { + // // 如果没有code,就去请求获取code + // console.log('当前没有code,进入授权页面') + // let uri = encodeURIComponent(local) + // // 设置旧的code为0,避免死循环 + // uni.setStorageSync('wechatCode',0) + // window.location.href = + // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect` + // } else { + // // 保存最新code + // uni.setStorageSync('wechatCode',code) + // // 使用code换取用户信息 + // wxSnsapiBaseLogin({code}).then((res: IUserInfoVo) => { + // console.log("登录成功 ~ snsapiBaseAuthorize ~ res:", res) + // // 映射 IUserLogin 到 IUserInfoVo + // useUserStore().setUserInfo(res) + // }).catch(err => { + // // 失败就重新授权 + // uni.setStorageSync('wechatCode', 0) + // console.log('请求失败', err) + // }) + // } } \ No newline at end of file diff --git a/src/http/alova.ts b/src/http/alova.ts index 766672e..ac81be8 100644 --- a/src/http/alova.ts +++ b/src/http/alova.ts @@ -56,15 +56,26 @@ const alovaInstance = createAlova({ } const { config } = method + console.log("🚀 ~ config.meta?.ignoreAuth:", config) + const ignoreAuth = !config.meta?.ignoreAuth console.log('ignoreAuth===>', ignoreAuth) // 处理认证信息 自行处理认证问题 if (ignoreAuth) { - const token = 'getToken()' + // const token = 'getToken()' + // if (!token) { + // throw new Error('[请求错误]:未登录') + // } + // method.config.headers.token = token; + + const userStore = useUserStore() + const { token } = userStore.userInfo as unknown as IUserInfo + console.log("🚀 ~ userStore.userInfo:", userStore.userInfo) + console.log("🚀 ~ token:", token) if (!token) { throw new Error('[请求错误]:未登录') } - // method.config.headers.token = token; + method.config.headers.token = token; } // 处理动态域名 @@ -102,12 +113,22 @@ const alovaInstance = createAlova({ // } // 处理业务逻辑错误 - const { code, message, data } = rawData as IResponse + const { code, msg, data } = rawData as IResponse + // if (code === ResultEnum.Unauthorized) { + // if (config.meta?.toast !== false) { + // toast.info(msg) + // setTimeout(() => { + // uni.navigateTo({ url: '/pages/login/mobile' }) + // }) + // } + // throw new Error(`登录超时[${code}]:${msg}`) + // } + if (code !== ResultEnum.Success) { if (config.meta?.toast !== false) { - toast.warning(message) + toast.warning(msg) } - throw new Error(`请求错误[${code}]:${message}`) + throw new Error(`请求错误[${code}]:${msg}`) } // 处理成功响应,返回业务数据 return data diff --git a/src/http/tools/enum.ts b/src/http/tools/enum.ts index 6e73fe8..c2b3aed 100644 --- a/src/http/tools/enum.ts +++ b/src/http/tools/enum.ts @@ -1,7 +1,7 @@ export enum ResultEnum { Success = 1, // 成功 Error = 0, // 错误 - Unauthorized = 401, // 未授权 + Unauthorized = -1, // 未授权 Forbidden = 403, // 禁止访问(原为forbidden) NotFound = 404, // 未找到(原为notFound) MethodNotAllowed = 405, // 方法不允许(原为methodNotAllowed) diff --git a/src/http/types.ts b/src/http/types.ts index 25cf472..753f263 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -11,7 +11,7 @@ export type CustomRequestOptions = UniApp.RequestOptions & { export interface IResponse { code: number | string data: T - message: string + msg: string status: string | number } diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 6881ee1..483c4df 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -74,11 +74,11 @@ - {{ item.label }} + class="h-64rpx rounded-12rpx px-24rpx py-12rpx flex items-center justify-center font-400 text-28rpx mr-20rpx" + :class="selectedLevel.includes(item.id) ? 'bg-[#4C9F44] text-[#fff]' : 'bg-[#FFF] text-[#606266]'" + @click="Index.handleToggleTeaSpecialistLevel(item.id)"> + {{ item.level_name}} @@ -146,22 +146,34 @@