This commit is contained in:
zhouyanli
2026-04-30 14:28:39 +08:00
19 changed files with 2678 additions and 2597 deletions

View File

@@ -0,0 +1,90 @@
import request, {
get,
post,
deleteMethod,
putMethod
} from "./request.js"
// =======公告列表========
export const getResidentNoticeList = (data) => {
return get('/api/resident/notice/list', data)
}
// 我的房屋列表
export const getMyHouseList = (data) => {
return get('/api/resident/my/house/list', data)
}
// 提交房屋注册认证申请
export const myHouseApply = (data) => {
return post('/api/resident/my/house/apply', data)
}
// 查询小区列表
export const getVillageList = (data) => {
return get('/api/resident/village/list', data)
} // 查询楼栋列表
export const getHouseBuildings = (data) => {
return get('/api/resident/house/buildings', data)
}
// 查询单元列表
export const getHouseUnits = (data) => {
return get('/api/resident/house/units', data)
}
// 查询楼层列表
export const getHouseFloors = (data) => {
return get('/api/resident/house/floors', data)
}
// 查询户号列表
export const getHouseHouses = (data) => {
return get('/api/resident/house/houses', data)
}
// 房屋绑定手机获取验证码
export const getHouseAuthSendSmsCode = (data) => {
return post('/api/resident/auth/sendSmsCode', data)
}
// 房屋详情
export const getMyHouseDetail = (data) => {
return get(`/api/resident/my/house/${data.id}`, data)
}
// 删除房屋
export const deleteMyHouse = (data) => {
return deleteMethod(`/api/resident/my/house/${data.id}`, data)
}
// 设置默认房屋
export const setMyHouseDefault = (data) => {
return post(`/api/resident/my/house/default`, data)
}
// 获取当前用户登录信息
export const getUserProfile = (data) => {
return get(`/api/resident/user/profile`, data)
}
// 发送新手机号验证码
export const getAuthSendSmsCode = (data) => {
return post(`/api/resident/auth/sendSmsCode`, data)
}
// 更改手机号
export const getUserChangePhone = (data) => {
return post(`/api/resident/user/changePhone`, data)
}
// 获取消息通知开关
export const getUserNotify = (data) => {
return get(`/api/resident/user/notify`, data)
}
// 更新消息通知开关
export const changeUserNotify = (data) => {
return post(`/api/resident/user/notify`, data)
} // 修改密码(需登录)
export const authChangePassword = (data) => {
return post(`/api/resident/auth/changePassword`, data)
} // 退出登录
export const authLogout = (data) => {
return post(`/api/resident/auth/logout`, data)
}
// 编辑房屋
export const upDateMyHouse = (data) => {
return putMethod(`/api/resident/my/house/${data.id}`, data)
}
// 修改身份证照片
export const uploadFileUploadImage = (data) => {
return post(`/api/resident/file/uploadImage`, data)
}

View File

@@ -1,25 +1,29 @@
let baseUrl = '';
let baseUrl = '';
if (process.env.NODE_ENV === 'development') {
baseUrl = 'http://192.168.1.215:8080'; // 开发环境
// http://192.168.1.215
// http://192.168.0.224
baseUrl = 'http://192.168.0.224:8080'; // 开发环境
} else {
baseUrl = 'http://192.168.1.224:8080'; // 生产环境
baseUrl = 'http://192.168.0.224:8080'; // 生产环境
}
export default function request(url, data = {}, method = "GET") {
return new Promise((resolve, reject) => {
// 加载中提示(提升用户体验)
uni.showLoading({ title: '请求中...', mask: true });
uni.showLoading({
title: '请求中...',
mask: true
});
uni.request({
url: baseUrl + url,
data: data,
method: method,
header: {
"Authorization": "Bearer " + (uni.getStorageSync("token") || ''),
"clientid": "resident",
'content-type': 'application/json'
},
"Authorization": "Bearer " + (uni.getStorageSync("token") || ''),
"clientid": "resident",
'content-type': 'application/json'
},
// header: {
// "token": uni.getStorageSync("token") || '',
// 'content-type': 'application/json'
@@ -30,9 +34,13 @@ export default function request(url, data = {}, method = "GET") {
// 处理HTTP状态码网络层面
if (res.statusCode !== 200) {
const errMsg = `请求失败(${res.statusCode}`;
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
uni.showToast({
title: errMsg,
icon: "none",
duration: 2000
});
reject(errMsg);
return;
return;
}
// 处理业务状态码后端返回的data里
@@ -40,10 +48,16 @@ export default function request(url, data = {}, method = "GET") {
// 优先处理token过期401
if (resData.code === 401) {
uni.showToast({ title: '登录过期,请重新登录', icon: 'none', duration: 2000 });
uni.showToast({
title: '登录过期,请重新登录',
icon: 'none',
duration: 2000
});
uni.clearStorageSync('token');
setTimeout(() => {
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' });
uni.reLaunch({
url: '/pageSubPack/loginSub/pwd-login'
});
}, 1500);
reject('token 过期');
return;
@@ -56,20 +70,31 @@ export default function request(url, data = {}, method = "GET") {
// 业务失败code=500
else if (resData.code === 500) {
const errMsg = resData.msg || '请求失败';
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
uni.showToast({
title: errMsg,
icon: "none",
duration: 2000
});
// resolve(null)
reject(errMsg);
}
else {
} else {
const errMsg = resData.msg || '业务异常';
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
uni.showToast({
title: errMsg,
icon: "none",
duration: 2000
});
reject(errMsg);
}
},
fail: (err) => {
uni.hideLoading(); // 关闭加载提示
const errMsg = err.msg || '网络请求失败';
uni.showToast({ title: errMsg, icon: "none", duration: 2000 });
uni.showToast({
title: errMsg,
icon: "none",
duration: 2000
});
reject(err);
}
});
@@ -77,35 +102,5 @@ export default function request(url, data = {}, method = "GET") {
}
export const get = (url, data) => request(url, data, 'GET');
export const post = (url, data) => request(url, data, 'POST');
// 通用文件上传
export const upload = (filePath, url) => {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl + url,
filePath: filePath,
name: 'file',
header: {
"Authorization": "Bearer " + (uni.getStorageSync("token") || ''),
"clientid": "resident"
},
success: (res) => {
if (res.statusCode === 200) {
const data = JSON.parse(res.data);
if (data.code === 200) {
resolve(data);
} else {
reject(data.msg || '上传失败');
}
} else {
reject('上传失败');
}
},
fail: (err) => {
reject(err);
}
});
});
};
export const deleteMethod = (url, data) => request(url, data, 'DELETE');
export const putMethod = (url, data) => request(url, data, 'PUT');

View File

@@ -0,0 +1,173 @@
// 服务器地址(与 request.js 保持一致)
const baseUrl = 'http://192.168.0.224:8080';
/**
* 图片上传 Hook
* 支持 uniapp 和微信小程序
* 解决编辑回显后重新上传的问题
*/
import { ref } from 'vue'
/**
* @param {Object} options 配置
* @param {string} options.uploadUrl - 上传接口地址
* @param {string} options.fileKey - 上传文件的字段名,默认 'file'
* @returns {Object}
*/
export function useImageUpload(options = {}) {
const {
uploadUrl = '/api/resident/file/uploadImage',
fileKey = 'file'
} = options
// 当前显示的图片(前端展示用,支持 base64/blob URL/网络URL
const previewUrl = ref('')
// 服务器返回的 URL提交时使用
const serverUrl = ref('')
// 临时的文件路径(重新上传时用)
const tempFilePath = ref(null)
// 上传状态
const uploading = ref(false)
/**
* 设置回显图片(编辑时从后端获取)
* @param {string} url - 后端返回的图片URL
*/
const setPreviewUrl = (url) => {
if (url) {
previewUrl.value = url
serverUrl.value = url
tempFilePath.value = null // 重置临时文件,表示没有重新上传
}
}
/**
* 选择图片并预览(不上传)
* @returns {Promise<string>} 返回临时文件路径
*/
const chooseImage = () => {
return new Promise((resolve, reject) => {
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['camera', 'album'],
success: (res) => {
const path = res.tempFilePaths[0]
// 设置预览
previewUrl.value = path
// 记录临时文件路径
tempFilePath.value = path
// 置空服务器URL等实际上传后再获取
serverUrl.value = ''
resolve(path)
},
fail: (err) => {
reject(err)
}
})
})
}
/**
* 上传图片到服务器
* @returns {Promise<string>} 返回服务器上的URL
*/
const uploadImage = () => {
// 如果没有新选择临时文件直接返回已有的服务器URL
if (!tempFilePath.value) {
return Promise.resolve(serverUrl.value)
}
// 如果已经有服务器URL且没有重新选择也直接返回
if (serverUrl.value && !tempFilePath.value) {
return Promise.resolve(serverUrl.value)
}
uploading.value = true
return new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl + uploadUrl,
filePath: tempFilePath.value,
name: fileKey,
header: {
'Authorization': 'Bearer ' + (uni.getStorageSync('token') || ''),
'clientid': 'resident'
},
success: (res) => {
try {
const data = JSON.parse(res.data)
if (data.code === 200 && data.data) {
// 更新服务器URL确保存的是字符串
const url = typeof data.data === 'string' ? data.data : (data.data?.url || '')
serverUrl.value = url
// 清空临时文件
tempFilePath.value = null
resolve(url)
} else {
uni.showToast({
title: data.msg || '上传失败',
icon: 'none'
})
reject(new Error(data.msg || '上传失败'))
}
} catch (e) {
uni.showToast({
title: '上传失败',
icon: 'none'
})
reject(e)
}
},
fail: (err) => {
uni.showToast({
title: err.message || '上传失败',
icon: 'none'
})
reject(err)
},
complete: () => {
uploading.value = false
}
})
})
}
/**
* 获取最终可提交的URL
* 如果有新上传的图片先上传再返回URL
* 如果没有新上传直接返回已有的服务器URL
* @returns {Promise<string>}
*/
const getSubmitUrl = async () => {
// 如果重新选择了新图片,先上传
if (tempFilePath.value) {
return await uploadImage()
}
// 否则返回已有的服务器URL
return serverUrl.value
}
/**
* 重置状态
*/
const reset = () => {
previewUrl.value = ''
serverUrl.value = ''
tempFilePath.value = null
uploading.value = false
}
return {
previewUrl, // 前端展示用
serverUrl, // 提交时使用的URL
tempFilePath, // 临时文件路径
uploading, // 上传中状态
setPreviewUrl, // 设置回显图片(编辑时用)
chooseImage, // 选择图片(预览用,不上传)
uploadImage, // 上传到服务器
getSubmitUrl, // 获取最终可提交的URL自动处理上传
reset // 重置
}
}