feat:新增登录页面与相关功能,移除自动登录,对接用户反馈功能

This commit is contained in:
2025-10-08 02:29:48 +08:00
parent 0ec86fba8e
commit 8560ff778e
10 changed files with 317 additions and 35 deletions
+3 -8
View File
@@ -11,25 +11,20 @@
}, },
onShow: async function() { onShow: async function() {
// if (this.getUserInfoData()) { // 手动登录模式:不再自动登录
// 如需保留可开关逻辑,可在此读取配置决定是否执行 autoLogin
// } else {
// console.log('App Show')
await this.autoLogin()
// }
}, },
onHide: function() { onHide: function() {
console.log('App Hide') console.log('App Hide')
}, },
methods: { methods: {
// 保留方法但不调用
async autoLogin() { async autoLogin() {
try { try {
const loginResult = await wxLogin() const loginResult = await wxLogin()
// await getUserInfo() // await getUserInfo()
} catch (error) { } catch (error) {
console.error('自动登录失败:', error) console.error('自动登录失败:', error)
// 登录失败的处理可以在 wxLogin 中统一处理
// 这里可以添加特殊的错误处理逻辑
} }
}, },
async getUserInfoData() { async getUserInfoData() {
+17 -1
View File
@@ -19,7 +19,7 @@ const request = (option) => {
method: option.method, method: option.method,
data: option.data, data: option.data,
header: { header: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": option.headers && option.headers["Content-Type"] ? option.headers["Content-Type"] : (option.method && option.method.toUpperCase() === 'POST' ? 'application/json' : 'application/x-www-form-urlencoded'),
...option.headers, ...option.headers,
'appid': appid, 'appid': appid,
'Authorization': "Bearer " + uni.getStorageSync('token'), 'Authorization': "Bearer " + uni.getStorageSync('token'),
@@ -42,6 +42,22 @@ const request = (option) => {
return return
} }
// 未登录或 token 失效处理(支持多种后端返回)
if (res.statusCode === 401 || res.data?.code === 401 || res.data?.code === 40101) {
try {
// 清理本地登录态
uni.removeStorageSync('token')
// 计算重定向地址
const pages = getCurrentPages()
const current = pages && pages.length ? pages[pages.length - 1] : null
const route = current && current.route ? ('/' + current.route) : '/pages/index/index'
const query = current && current.options ? Object.keys(current.options).map(k => `${k}=${encodeURIComponent(current.options[k])}`).join('&') : ''
const redirect = encodeURIComponent(query ? `${route}?${query}` : route)
// 跳转到登录页
uni.reLaunch({ url: `/pages/login/index?redirect=${redirect}` })
} catch (e) {}
}
// 检查业务状态码 // 检查业务状态码
if (res.data && res.data.code !== 200) { if (res.data && res.data.code !== 200) {
console.warn(`业务状态码错误: ${res.data.code}`, res.data) console.warn(`业务状态码错误: ${res.data.code}`, res.data)
+2 -2
View File
@@ -1,5 +1,5 @@
export const URL = "https://my.gxfs123.com/api" //正式服务器 // export const URL = "https://my.gxfs123.com/api" //正式服务器
// export const URL = "https://fansdev.gxfs123.com/api" //测试服务器 export const URL = "https://fansdev.gxfs123.com/api" //测试服务器
// export const URL = "http://192.168.5.22:8080" //本地调试 // export const URL = "http://192.168.5.22:8080" //本地调试
// export const URL = "http://127.0.0.1:8080" //本地调试 // export const URL = "http://127.0.0.1:8080" //本地调试
+34
View File
@@ -1,4 +1,5 @@
import request from './http' import request from './http'
import { URL, appid } from './url'
export const login = (data) => { export const login = (data) => {
@@ -204,3 +205,36 @@ export const getPotionsDetail = (data) => {
data data
}) })
} }
// 文件上传到 OSS(若依后端)
export const uploadOssResource = (filePath) => {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: URL + '/resource/oss/upload',
filePath,
name: 'file',
header: {
'appid': appid,
'Authorization': 'Bearer ' + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
},
success: (res) => {
try {
const parsed = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
const codeOk = (res.statusCode === 200 || res.statusCode === 201 || parsed?.code === 200)
const url = parsed?.url || parsed?.data?.url || parsed?.data?.fileUrl || parsed?.fileUrl || parsed?.path || parsed?.data
if (codeOk && url) {
resolve(url)
return
}
reject(parsed || res)
} catch (e) {
reject(e)
}
},
fail: (err) => {
reject(err)
}
})
})
}
+8
View File
@@ -12,6 +12,14 @@
"navigationBarTitleText": "附近场地" "navigationBarTitleText": "附近场地"
} }
}, },
{
"path": "pages/login/index",
"style": {
"navigationBarTitleText": "登录",
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black"
}
},
{ {
"path": "pages/my/index", "path": "pages/my/index",
"style": { "style": {
+53 -10
View File
@@ -1,6 +1,6 @@
<template> <template>
<view class="feedback-container"> <view class="feedback-container">
<form> <!-- <form> -->
<!-- 问题类型选择 --> <!-- 问题类型选择 -->
<view class="type-section"> <view class="type-section">
<view class="section-title">问题类型</view> <view class="section-title">问题类型</view>
@@ -46,7 +46,7 @@
<view class="submit-section"> <view class="submit-section">
<view class="submit-btn" @click="submitFeedback">提交反馈</view> <view class="submit-btn" @click="submitFeedback">提交反馈</view>
</view> </view>
</form> <!-- </form> -->
</view> </view>
</template> </template>
@@ -55,10 +55,11 @@
ref ref
} from 'vue' } from 'vue'
import { import {
URL URL,
appid
} from '../../config/url' } from '../../config/url'
import { import {
addUserFeedback uploadOssResource
} from '../../config/user' } from '../../config/user'
// 响应式数据 // 响应式数据
@@ -78,8 +79,25 @@
const chooseImage = () => { const chooseImage = () => {
uni.chooseImage({ uni.chooseImage({
count: 3 - images.value.length, count: 3 - images.value.length,
success: (res) => { success: async (res) => {
images.value = [...images.value, ...res.tempFilePaths] const toUpload = res.tempFilePaths || []
for (const localPath of toUpload) {
// 先追加本地预览,再上传并替换为远程URL
images.value.push(localPath)
try {
const remoteUrl = await uploadOssResource(localPath)
const idx = images.value.indexOf(localPath)
if (idx !== -1) {
images.value.splice(idx, 1, remoteUrl)
}
} catch (e) {
const idx = images.value.indexOf(localPath)
if (idx !== -1) {
images.value.splice(idx, 1)
}
uni.showToast({ title: '图片上传失败', icon: 'none' })
}
}
} }
}) })
} }
@@ -126,18 +144,43 @@
phone: contact.value, phone: contact.value,
// images: images.value // images: images.value
} }
const res = await addUserFeedback(feedbackData);
if (res.code == 200) { uni.request({
url: `${apiUrl}/app/feedback/add`,
method: 'POST',
data: feedbackData,
header: {
'Content-Type': 'application/json',
'appid': appid,
'Authorization': 'Bearer ' + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
},
dataType: 'json',
success: (res) => {
// 兼容后端返回 { code: 200 } 或 HTTP 200 情况
if ((res.statusCode === 200) && ((res.data && res.data.code === 200) || res.data === true || res.data?.success === true)) {
uni.showToast({ uni.showToast({
title: '反馈成功', title: '反馈成功',
icon: 'success' icon: 'success'
}) })
} else { setTimeout(() => {
uni.navigateBack();
}, 1500);
return
}
uni.showToast({ uni.showToast({
title: '反馈失败', title: (res.data && (res.data.msg || res.data.message)) || '反馈失败',
icon: 'none'
})
},
fail: (err) => {
console.error('feedback request failed:', err)
uni.showToast({
title: '网络错误,请稍后重试',
icon: 'none' icon: 'none'
}) })
} }
})
} }
</script> </script>
+18 -3
View File
@@ -128,6 +128,18 @@
</template> </template>
<script setup> <script setup>
const redirectToLogin = () => {
try {
const pages = getCurrentPages()
const current = pages && pages.length ? pages[pages.length - 1] : null
const route = current && current.route ? ('/' + current.route) : '/pages/index/index'
const query = current && current.options ? Object.keys(current.options).map(k => `${k}=${encodeURIComponent(current.options[k])}`).join('&') : ''
const redirect = encodeURIComponent(query ? `${route}?${query}` : route)
uni.reLaunch({ url: `/pages/login/index?redirect=${redirect}` })
} catch (e) {
uni.reLaunch({ url: '/pages/login/index' })
}
}
import { import {
ref, ref,
computed, computed,
@@ -318,7 +330,8 @@
const loadPositions = async () => { const loadPositions = async () => {
try { try {
if (!uni.getStorageSync('token')) { if (!uni.getStorageSync('token')) {
await wxLogin() redirectToLogin()
return
} }
const res = await uni.request({ const res = await uni.request({
@@ -387,7 +400,8 @@
const loadPositionsByCenter = async (center) => { const loadPositionsByCenter = async (center) => {
try { try {
if (!uni.getStorageSync('token')) { if (!uni.getStorageSync('token')) {
await wxLogin() redirectToLogin()
return
} }
// 使用原有接口获取所有场地 // 使用原有接口获取所有场地
@@ -511,7 +525,8 @@
} }
if (!uni.getStorageSync('token')) { if (!uni.getStorageSync('token')) {
await wxLogin() redirectToLogin()
return
} }
// 检查是否有使用中的订单 // 检查是否有使用中的订单
+150
View File
@@ -0,0 +1,150 @@
<template>
<view class="login-container">
<view class="logo">
<image src="/static/logo.png" mode="aspectFit" />
<text class="app-name">共享风扇</text>
</view>
<view class="title">登录您的账号</view>
<view class="subtitle">为保障使用体验请先完成登录</view>
<!-- 微信一键手机号快捷登录推荐 -->
<button class="btn primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
手机号快捷登录
</button>
<!-- 仅微信登录不授权手机号时使用 -->
<button class="btn outline" @click="onWeChatLogin">仅微信登录</button>
<view class="tips">登录即表示同意用户协议隐私政策</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { wxLogin, getUserPhoneNumber, getUserInfo } from '../../util/index.js'
const redirect = ref('/pages/index/index')
const navigateAfterLogin = async () => {
try {
// 可选:刷新一次用户信息
await getUserInfo().catch(() => {})
} catch (e) {}
// 读取跳转路径(支持 tabBar 页面)
const target = '/pages/index/index'
const tabPages = ['/pages/index/index', '/pages/my/index']
if (tabPages.includes(target)) {
uni.switchTab({ url: target })
return
}
uni.reLaunch({ url: target })
}
const onWeChatLogin = async () => {
try {
await wxLogin()
uni.showToast({ title: '登录成功', icon: 'success' })
await navigateAfterLogin()
} catch (error) {
uni.showToast({ title: error.message || '登录失败', icon: 'none' })
}
}
const onGetPhoneNumber = async (e) => {
if (!e || e.detail.errMsg !== 'getPhoneNumber:ok') {
uni.showToast({ title: '已取消手机号授权', icon: 'none' })
return
}
try {
// 先微信登录,获取 token
await wxLogin()
// 再用微信返回的临时 code 换取手机号
await getUserPhoneNumber(e.detail.code)
uni.showToast({ title: '登录成功', icon: 'success' })
await navigateAfterLogin()
} catch (error) {
uni.showToast({ title: error.message || '登录失败', icon: 'none' })
}
}
onLoad((opts) => {
if (opts && opts.redirect) {
try {
redirect.value = decodeURIComponent(opts.redirect)
} catch (_) {}
}
})
</script>
<style lang="scss" scoped>
.login-container {
min-height: 100vh;
background: #f8f8f8;
padding: 80rpx 40rpx 40rpx;
display: flex;
flex-direction: column;
align-items: center;
box-sizing: border-box;
.logo {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 60rpx;
image {
width: 160rpx;
height: 160rpx;
margin-bottom: 16rpx;
}
.app-name {
font-size: 36rpx;
font-weight: 600;
color: #333;
}
}
.title {
font-size: 40rpx;
font-weight: 600;
color: #222;
margin-bottom: 12rpx;
}
.subtitle {
font-size: 26rpx;
color: #888;
margin-bottom: 60rpx;
}
.btn {
width: 100%;
height: 96rpx;
border-radius: 48rpx;
font-size: 32rpx;
margin-bottom: 24rpx;
}
.primary {
background: #1976D2;
color: #fff;
}
.outline {
background: #fff;
color: #1976D2;
border: 2rpx solid #1976D2;
}
.tips {
margin-top: 24rpx;
font-size: 22rpx;
color: #999;
}
}
</style>
+15 -2
View File
@@ -149,8 +149,8 @@
try { try {
const token = uni.getStorageSync('token'); const token = uni.getStorageSync('token');
if (!token) { if (!token) {
await wxLogin(); redirectToLogin()
return; return
} }
const res = await getUserInfo(); const res = await getUserInfo();
@@ -183,6 +183,19 @@
} }
}; };
const redirectToLogin = () => {
try {
const pages = getCurrentPages()
const current = pages && pages.length ? pages[pages.length - 1] : null
const route = current && current.route ? ('/' + current.route) : '/pages/index/index'
const query = current && current.options ? Object.keys(current.options).map(k => `${k}=${encodeURIComponent(current.options[k])}`).join('&') : ''
const redirect = encodeURIComponent(query ? `${route}?${query}` : route)
uni.reLaunch({ url: `/pages/login/index?redirect=${redirect}` })
} catch (e) {
uni.reLaunch({ url: '/pages/login/index' })
}
}
// 导航到指定页面 // 导航到指定页面
const navigateTo = (url) => { const navigateTo = (url) => {
uni.navigateTo({ uni.navigateTo({
+8
View File
@@ -8,6 +8,9 @@ importers:
.: .:
dependencies: dependencies:
'@climblee/uv-ui':
specifier: ^1.1.20
version: 1.1.20
axios: axios:
specifier: ^1.7.9 specifier: ^1.7.9
version: 1.10.0 version: 1.10.0
@@ -30,6 +33,9 @@ importers:
packages: packages:
'@climblee/uv-ui@1.1.20':
resolution: {integrity: sha512-jkyesHJsPJkF4Nap9ZmG1/ibKlxXA5M8+ntqKXwwloIsYSYL5SOKb0gyPj17aBOU1PkJpmeiZ8PwnTolhK2/HA==}
'@jridgewell/gen-mapping@0.3.8': '@jridgewell/gen-mapping@0.3.8':
resolution: {integrity: sha512-imAbBGkb+ebQyxKgzv5Hu2nmROxoDOXHh80evxdoXNOrvAnVx7zimzc1Oo5h9RlfV4vPXaE2iM5pOFbvOCClWA==} resolution: {integrity: sha512-imAbBGkb+ebQyxKgzv5Hu2nmROxoDOXHh80evxdoXNOrvAnVx7zimzc1Oo5h9RlfV4vPXaE2iM5pOFbvOCClWA==}
engines: {node: '>=6.0.0'} engines: {node: '>=6.0.0'}
@@ -604,6 +610,8 @@ packages:
snapshots: snapshots:
'@climblee/uv-ui@1.1.20': {}
'@jridgewell/gen-mapping@0.3.8': '@jridgewell/gen-mapping@0.3.8':
dependencies: dependencies:
'@jridgewell/set-array': 1.2.1 '@jridgewell/set-array': 1.2.1