235 lines
5.4 KiB
JavaScript
235 lines
5.4 KiB
JavaScript
import request from '@/api/request'
|
||
import common from "@/common/common";
|
||
import {
|
||
queryCurrentValidMascotInfo
|
||
} from "@/api/mascot.js"
|
||
|
||
import {
|
||
queryCurrentStatus
|
||
} from '@/api/pointsAndRank.js';
|
||
|
||
import {
|
||
setToken,
|
||
setUserInfo,
|
||
getUserInfo
|
||
} from "@/common/common";
|
||
import {
|
||
get_base_url
|
||
} from '@/api/request.js'
|
||
|
||
// 账号登录接口
|
||
export const useAccountLoginApp = (data) => request({
|
||
url: '/traapp/access/userLogin',
|
||
method: 'post',
|
||
data
|
||
}).then(async ({
|
||
rtnCode,
|
||
body
|
||
}) => {
|
||
if (rtnCode === '0000') {
|
||
setToken(body)
|
||
const res = await Promise.all([queryCurrentUser(), queryCurrentValidMascotInfo()
|
||
// queryCurrentStatus()
|
||
])
|
||
const userInfo = getUserInfo()
|
||
const mascotInfo = res[1]
|
||
setUserInfo({
|
||
...userInfo,
|
||
'mascotInfo': mascotInfo
|
||
})
|
||
// 积分状态
|
||
// const pointsStatus = res[2].body
|
||
const pointsStatus = {}
|
||
common.setValue('points_data', {
|
||
badgeNum: pointsStatus['badgeNum'] ?? 0, // 徽章数
|
||
currentPoints: pointsStatus['currentPoints'] ?? 0, // 当前积分
|
||
currentRankAddr: pointsStatus['ossAddr'] ?? '', // 当前段位图片
|
||
currentRankName: pointsStatus['currentRankName'] ?? '未知', // 当前段位名称
|
||
})
|
||
return {
|
||
mascotState: !mascotInfo.mascotId
|
||
}
|
||
}
|
||
return Promise.reject({
|
||
rtnCode,
|
||
body
|
||
})
|
||
}).catch(res => {
|
||
return Promise.reject(res)
|
||
})
|
||
|
||
/**
|
||
* 获取个人信息
|
||
* 入参
|
||
gender f是女 m男
|
||
imageAddr: 图片
|
||
loginName ID
|
||
userName 姓名
|
||
*/
|
||
export const queryCurrentUser = (data) => request({
|
||
url: '/traapp/access/queryCurrentUser',
|
||
method: 'post',
|
||
data
|
||
}).then(res => {
|
||
|
||
// deptId: ""
|
||
// deptName: null
|
||
// loginName: "ADMIN" // todo id
|
||
// orgId: "00E200"
|
||
// orgName: "金融科技部"
|
||
// userId: "USER001"
|
||
// userName: "ADMIN" / todo name
|
||
// userStatus: "01"
|
||
const userInfo = getUserInfo() ?? {}
|
||
setUserInfo({
|
||
...userInfo,
|
||
...res.body
|
||
})
|
||
// console.log('预加载头像');
|
||
const base_url = get_base_url()
|
||
// 预加载头像
|
||
// #ifdef APP-PLUS
|
||
// 更新用户信息中的头像路径
|
||
const updateUserAvatar = (imagePath) => {
|
||
const _userInfo = getUserInfo() ?? {}
|
||
setUserInfo({
|
||
..._userInfo,
|
||
...res.body,
|
||
'imagePath': imagePath
|
||
})
|
||
}
|
||
|
||
// 确保目录存在
|
||
const ensureDirectoryExists = (dirPath) => {
|
||
return new Promise((resolve) => {
|
||
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
|
||
fs.root.getDirectory(dirPath, {
|
||
create: true
|
||
}, resolve, resolve)
|
||
})
|
||
})
|
||
}
|
||
|
||
// 生成唯一文件名
|
||
const generateUniqueFilename = (pathName) => {
|
||
if (!pathName || typeof pathName !== 'string') {
|
||
throw new Error('无效的路径');
|
||
}
|
||
|
||
// 直接使用原始文件名(带扩展名)
|
||
const filename = pathName.split('/').pop();
|
||
|
||
if (!filename) {
|
||
throw new Error('无法从路径中提取文件名');
|
||
}
|
||
|
||
// 确保文件扩展名为.png (可选,根据实际需求)
|
||
const ext = filename.split('.').pop().toLowerCase();
|
||
if (ext === 'png' || ext === 'jpg' || ext === 'jpeg' || ext === 'webp') {
|
||
return filename;
|
||
}
|
||
|
||
// 如果没有有效扩展名,添加.png
|
||
return `${filename}.png`;
|
||
};
|
||
|
||
// 下载并保存头像
|
||
const downloadAndSaveAvatar = async (fileUrl, localFilePath) => {
|
||
try {
|
||
await ensureDirectoryExists('avatar')
|
||
|
||
return new Promise((resolve, reject) => {
|
||
console.log('开始下载头像:', fileUrl)
|
||
const dtask = plus.downloader.createDownload(fileUrl, {
|
||
filename: localFilePath
|
||
}, (d, status) => {
|
||
if (status === 200) {
|
||
console.log("保存路径:", d.filename)
|
||
resolve(d.filename)
|
||
} else {
|
||
console.error("文件下载失败:", status)
|
||
reject(new Error(`下载失败,状态码: ${status}`))
|
||
}
|
||
})
|
||
|
||
dtask.start()
|
||
})
|
||
} catch (error) {
|
||
console.error('创建目录失败:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
// 检查文件是否存在
|
||
const checkFileExists = (filePath) => {
|
||
return new Promise((resolve) => {
|
||
uni.getFileInfo({
|
||
filePath,
|
||
success: () => resolve(true),
|
||
fail: () => resolve(false)
|
||
})
|
||
})
|
||
}
|
||
|
||
// 主逻辑
|
||
(async () => {
|
||
try {
|
||
const pathName = res.body.imageAddr
|
||
const fileUrl = base_url + pathName
|
||
|
||
// 生成保存路径
|
||
const filename = generateUniqueFilename(pathName)
|
||
const localFilePath = '_doc/avatar/' + filename
|
||
|
||
// 检查文件是否存在
|
||
const exists = await checkFileExists(localFilePath)
|
||
|
||
if (exists) {
|
||
// console.log('头像已存在,无需下载')
|
||
updateUserAvatar(localFilePath)
|
||
} else {
|
||
const savedPath = await downloadAndSaveAvatar(fileUrl, localFilePath)
|
||
updateUserAvatar(savedPath)
|
||
}
|
||
} catch (error) {
|
||
console.error('头像预加载过程发生错误:', error)
|
||
}
|
||
})()
|
||
// #endif
|
||
|
||
return res.body
|
||
})
|
||
|
||
//获取隐私协议
|
||
export const getYsxy = (data) => request({
|
||
url: '/traapp/dfAgtInfo/queryValidDfAgt',
|
||
method: 'post',
|
||
data
|
||
})
|
||
|
||
//获取服务条款
|
||
export const getFutk = (data) => request({
|
||
url: '/traapp/dfAgtInfo/queryValidDfAgtTerm',
|
||
method: 'post',
|
||
data
|
||
})
|
||
|
||
// 获取验证码(作废)
|
||
export const getLoginAppCheckCode = (data) => request({
|
||
url: '/traapp/access/checkCode',
|
||
method: 'get',
|
||
data
|
||
})
|
||
|
||
// 退出登录
|
||
export const loginOut = (data) => request({
|
||
url: '/traapp/access/loginOut',
|
||
method: 'post',
|
||
data
|
||
}).then(() => {
|
||
setToken(null)
|
||
setUserInfo(null)
|
||
common.setValue('me_statistics_data', null) // 我的页统计数据缓存
|
||
common.setValue('index_statistics_data', null) // 首页统计数据缓存
|
||
common.setValue('points_data', null) // 首页统计数据缓存
|
||
}) |