u
This commit is contained in:
1 parent
68f08d921a
commit
3656d05f62
97 files changed
+17495
No files matched your search
@@ -0,0 +1,193 @@
|
||||
import request from './index'
|
||||
|
||||
interface LoginParams {
|
||||
orgid: string
|
||||
userid: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export function loginApi(params: LoginParams) {
|
||||
return request.postForm('/login.jsp', params)
|
||||
}
|
||||
|
||||
interface GetAutocompleteDataParams {
|
||||
q: string
|
||||
page: number
|
||||
limit: number
|
||||
viewname: string
|
||||
field1: string
|
||||
field2: string
|
||||
userid?: string
|
||||
}
|
||||
|
||||
export function getAutocompleteDataApi(params: GetAutocompleteDataParams) {
|
||||
return request.postForm('/getAutocompleteData.jsp', params)
|
||||
}
|
||||
|
||||
interface GetAutocompleteDataExParams {
|
||||
q: string
|
||||
page: number
|
||||
limit: number
|
||||
viewname: string
|
||||
field1: string
|
||||
field2: string
|
||||
searchcondition: string
|
||||
userid?: string
|
||||
}
|
||||
|
||||
export function getAutocompleteDataExApi(params: GetAutocompleteDataExParams) {
|
||||
return request.postForm('/getAutocompleteDataEx.jsp', params)
|
||||
}
|
||||
|
||||
interface SaveobjtParams {
|
||||
tables: any
|
||||
insertValues: any
|
||||
delValues: any
|
||||
updateValues: any
|
||||
keys: any
|
||||
}
|
||||
|
||||
export function saveobjtApi(params: SaveobjtParams) {
|
||||
return request.postForm('/saveobjt.jsp', params)
|
||||
}
|
||||
|
||||
interface SavesubtableParams {
|
||||
subtable: any
|
||||
subinsertvalue: any
|
||||
subdelvalue: any
|
||||
subupdatevalue: any
|
||||
father: any
|
||||
}
|
||||
export function savesubtableApi(params: SavesubtableParams) {
|
||||
return request.postForm('/savesubtable.jsp', params)
|
||||
}
|
||||
|
||||
interface SearchdataParams {
|
||||
viewname: string
|
||||
searchcondition: string
|
||||
orderfield: string
|
||||
limit?: number
|
||||
}
|
||||
export function searchdataApi(params: SearchdataParams) {
|
||||
return request.postForm('/searchdata.jsp', params)
|
||||
}
|
||||
|
||||
interface SearchdatapageindexParams {
|
||||
viewname: string
|
||||
searchcondition: string
|
||||
userid: string
|
||||
orderfield: string
|
||||
page: number
|
||||
rows: number
|
||||
}
|
||||
|
||||
export function searchdatapageindexApi(params: SearchdatapageindexParams) {
|
||||
return request.postForm(`/searchdatapageindex.jsp`, params)
|
||||
}
|
||||
interface DelSubRecordParams {
|
||||
id: string
|
||||
module: string
|
||||
bucketName: string
|
||||
}
|
||||
export function delSubRecordApi(params: DelSubRecordParams) {
|
||||
return request.postForm('/DelSubRecord.jsp', params)
|
||||
}
|
||||
|
||||
interface GetaliyunkeyParams {
|
||||
orgid: string
|
||||
bucket: string
|
||||
}
|
||||
|
||||
export function getaliyunkeyApi(params: GetaliyunkeyParams) {
|
||||
return request.postForm(`/getaliyunkey.jsp`, params)
|
||||
}
|
||||
|
||||
interface AliyunpluploadParams {
|
||||
despathfile: string
|
||||
myfilename: string
|
||||
size: string
|
||||
tablename: string
|
||||
sid: string
|
||||
mx_moduleid: string
|
||||
mx_cate_id: string
|
||||
}
|
||||
export function aliyunpluploadApi(params: AliyunpluploadParams) {
|
||||
return request.postForm(`/aliyunplupload.jsp`, params)
|
||||
}
|
||||
|
||||
interface LoaddatabysqlParams {
|
||||
sql: string
|
||||
orderfield: string
|
||||
soption: string
|
||||
}
|
||||
export function loaddatabysqlApi(params: LoaddatabysqlParams) {
|
||||
return request.postForm('/loaddatabysql.jsp', params)
|
||||
}
|
||||
interface GetfieldvaluebyidParams {
|
||||
viewname: string
|
||||
desfield: string
|
||||
idfield: string
|
||||
idvalue: string
|
||||
}
|
||||
export function getfieldvaluebyidApi(params: GetfieldvaluebyidParams) {
|
||||
return request.postForm('/getfieldvaluebyid.jsp', params)
|
||||
}
|
||||
|
||||
interface GetdefaultdataParams {
|
||||
viewname: string
|
||||
}
|
||||
export function getdefaultdataApi(params: GetdefaultdataParams) {
|
||||
return request.postForm('/getdefaultdata.jsp', params)
|
||||
}
|
||||
|
||||
interface GetautoidParams {
|
||||
moduleid: string
|
||||
userid: string
|
||||
}
|
||||
export function getautoidApi(params: GetautoidParams) {
|
||||
return request.postForm('/getautoid.jsp', params)
|
||||
}
|
||||
interface LoaddataParams {
|
||||
soption: string
|
||||
orderfield: string
|
||||
viewname: string
|
||||
searchcondition: string
|
||||
}
|
||||
export function loaddataApi(params: LoaddataParams) {
|
||||
return request.postForm('/loaddata.jsp', params)
|
||||
}
|
||||
|
||||
interface SaveLogParams {
|
||||
mainTable: string
|
||||
strMainUpdate: string
|
||||
strMainDelete: string
|
||||
subTable: string
|
||||
strSubUpdate: string
|
||||
strSubDelete: string
|
||||
recordID: string
|
||||
subID: string
|
||||
Modifyb: string
|
||||
Modifyh: string
|
||||
}
|
||||
export function saveLogApi(params: SaveLogParams) {
|
||||
return request.postForm('/saveLog.jsp', params)
|
||||
}
|
||||
|
||||
interface LoaddataAttachParams {
|
||||
soption: string
|
||||
orderfield: string
|
||||
viewname: string
|
||||
searchcondition: string
|
||||
}
|
||||
|
||||
export function loaddataAttachApi(params: LoaddataAttachParams) {
|
||||
return request.postForm('/loaddata_attach.jsp', params)
|
||||
}
|
||||
|
||||
interface DelObjParams {
|
||||
id: string
|
||||
module: string
|
||||
}
|
||||
export function delObjApi(params: DelObjParams) {
|
||||
return request.postForm('/DelObj.jsp', params)
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
import qs from 'qs'
|
||||
import { toast } from 'sard-uniapp'
|
||||
import * as dbJson from '@/static/db.json'
|
||||
import { useUserStore } from '@/store'
|
||||
import { joinUrl } from '@/utils/common'
|
||||
import { isH5 } from '@/utils/platform'
|
||||
|
||||
function request<T = any>(option: UniApp.RequestOptions): Promise<IResData<T>> & { abort: () => void } {
|
||||
let task: UniApp.RequestTask | undefined
|
||||
let aborted = false
|
||||
|
||||
const promise = new Promise<IResData<T>>((resolve, reject) => {
|
||||
// 判断是否是登录请求
|
||||
const isLogin = option.url?.includes('login')
|
||||
|
||||
const readDbJson = new Promise<Record<string, string>>((res, rej) => {
|
||||
if (isH5) {
|
||||
// H5:用 request 请求,带时间戳保证最新
|
||||
const timestamp = Date.now()
|
||||
uni.request({
|
||||
url: `/g3cdjtd-app/static/db.json?t=${timestamp}`,
|
||||
method: 'GET',
|
||||
success: r => res(r.data as Record<string, string>),
|
||||
fail: err => rej(err),
|
||||
})
|
||||
}
|
||||
else {
|
||||
// 非 H5 直接返回本地导入的 JSON
|
||||
res(dbJson)
|
||||
}
|
||||
})
|
||||
|
||||
readDbJson
|
||||
.then((dbRes) => {
|
||||
if (aborted)
|
||||
return reject(new Error('请求已取消'))
|
||||
|
||||
// 组织请求 URL
|
||||
let reqUrl = ''
|
||||
const userStore = useUserStore()
|
||||
const { token, userid, orgid } = userStore.userInfo || {}
|
||||
|
||||
if (!dbRes || Object.keys(dbRes).length === 0) {
|
||||
toast.fail('机构配置获取失败,请联系管理人员!')
|
||||
return reject(new Error('机构配置获取失败,请联系管理人员!'))
|
||||
}
|
||||
|
||||
if (isLogin) {
|
||||
const dataObj: any = typeof option.data === 'string'
|
||||
? parseFormUrlencoded(option.data)
|
||||
: option.data || {}
|
||||
const orgidFromData = dataObj.orgid || ''
|
||||
|
||||
reqUrl = dbRes[orgidFromData]
|
||||
}
|
||||
else {
|
||||
reqUrl = dbRes[orgid || '']
|
||||
}
|
||||
|
||||
if (!reqUrl) {
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const currentRoute = `/${currentPage.route}`
|
||||
|
||||
if (currentRoute !== '/pages-sub/login/index') {
|
||||
uni.showToast({ title: '当前登录已过期,请重新登录', icon: 'error' })
|
||||
|
||||
uni.reLaunch({
|
||||
url: '/pages-sub/login/index',
|
||||
})
|
||||
}
|
||||
else {
|
||||
uni.showToast({ title: '机构码错误', icon: 'error' })
|
||||
}
|
||||
|
||||
return reject(new Error('机构码错误'))
|
||||
}
|
||||
|
||||
// 设置 headers
|
||||
const headers: Record<string, any> = { ...(option.header || {}) }
|
||||
if (!isLogin) {
|
||||
headers.Authorization = `Bearer ${token}`
|
||||
headers.userid = encodeURIComponent(userid)
|
||||
headers.orgid = orgid
|
||||
}
|
||||
|
||||
// 拼接完整 URL
|
||||
option.url = joinUrl(reqUrl, option.url || '')
|
||||
option.header = headers
|
||||
|
||||
// 发请求
|
||||
task = uni.request({
|
||||
...option,
|
||||
url: option.url,
|
||||
success: (res) => {
|
||||
if (aborted)
|
||||
return reject(new Error('请求已取消'))
|
||||
|
||||
if (res.statusCode !== 200) {
|
||||
console.error('HTTP错误', res.statusCode, res.data)
|
||||
toast.fail(`请求失败:${res.statusCode}`)
|
||||
return reject(new Error(`HTTP错误: ${res.statusCode}`))
|
||||
}
|
||||
|
||||
let data: IResData<T>
|
||||
try {
|
||||
// 解析最外层响应
|
||||
data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
|
||||
// 如果 data.data 是字符串,则解析成对象
|
||||
if (data.data && typeof data.data === 'string') {
|
||||
try {
|
||||
data.data = JSON.parse(data.data)
|
||||
}
|
||||
catch {
|
||||
// 如果解析失败,保持原始字符串即可
|
||||
// console.warn('data.data 不是合法 JSON 字符串', data.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
|
||||
console.log(`解析响应JSON失败:${err}`)
|
||||
toast.fail('响应数据解析失败')
|
||||
return reject(new Error('响应数据解析失败'))
|
||||
}
|
||||
|
||||
if (data.code === 200) {
|
||||
resolve(data)
|
||||
}
|
||||
else {
|
||||
toast.fail(data.message || '请求错误')
|
||||
reject(new Error(data.message || '请求错误'))
|
||||
}
|
||||
|
||||
// 登录失效处理
|
||||
if (data.code === 600) {
|
||||
uni.showToast({ title: '请重新登录', icon: 'none' })
|
||||
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const currentRoute = `/${currentPage.route}`
|
||||
if (currentRoute !== '/pages-sub/login/index') {
|
||||
uni.reLaunch({
|
||||
url: '/pages-sub/login/index',
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
if (aborted)
|
||||
return reject(new Error('请求已取消'))
|
||||
console.log(err)
|
||||
toast.fail('网络请求失败,请联系管理人员')
|
||||
|
||||
reject(new Error(err.errMsg || '网络请求失败'))
|
||||
},
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
toast.fail('机构配置获取失败,请联系管理人员!')
|
||||
console.error(err)
|
||||
reject(new Error('机构配置获取失败,请联系管理人员!'))
|
||||
})
|
||||
}) as Promise<IResData<T>> & { abort: () => void }
|
||||
|
||||
promise.abort = () => {
|
||||
aborted = true
|
||||
task?.abort()
|
||||
}
|
||||
|
||||
return promise
|
||||
}
|
||||
function parseFormUrlencoded(str: string) {
|
||||
return str.split('&').reduce((acc, pair) => {
|
||||
const [key, value] = pair.split('=')
|
||||
acc[key] = decodeURIComponent(value || '')
|
||||
return acc
|
||||
}, {} as Record<string, string>)
|
||||
}
|
||||
|
||||
// GET
|
||||
request.get = <T = any>(url: string, data?: any, option?: UniApp.RequestOptions) => {
|
||||
return request<T>({
|
||||
method: 'GET',
|
||||
url,
|
||||
data,
|
||||
...option,
|
||||
})
|
||||
}
|
||||
|
||||
// PUT
|
||||
request.put = <T = any>(url: string, data?: any, option?: UniApp.RequestOptions) => {
|
||||
return request<T>({
|
||||
method: 'PUT',
|
||||
url,
|
||||
data,
|
||||
...option,
|
||||
})
|
||||
}
|
||||
|
||||
// DELETE
|
||||
request.delete = <T = any>(url: string, data?: any, option?: UniApp.RequestOptions) => {
|
||||
return request<T>({
|
||||
method: 'DELETE',
|
||||
url,
|
||||
data,
|
||||
...option,
|
||||
})
|
||||
}
|
||||
|
||||
// POST JSON
|
||||
request.postJson = <T = any>(url: string, data?: any, option?: UniApp.RequestOptions) => {
|
||||
return request<T>({
|
||||
method: 'POST',
|
||||
url,
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...(option?.header || {}),
|
||||
},
|
||||
...option,
|
||||
})
|
||||
}
|
||||
|
||||
// POST 表单
|
||||
|
||||
request.postForm = <T = any>(url: string, data?: any, option?: UniApp.RequestOptions) => {
|
||||
let payload: string
|
||||
if (data) {
|
||||
// 使用 qs.stringify 或 URLSearchParams
|
||||
payload = qs.stringify(data, { arrayFormat: 'repeat' })
|
||||
// 生成 tables=val1&tables=val2 格式
|
||||
}
|
||||
return request<T>({
|
||||
method: 'POST',
|
||||
url,
|
||||
data: payload,
|
||||
header: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
...(option?.header || {}),
|
||||
},
|
||||
...option,
|
||||
})
|
||||
}
|
||||
|
||||
export default request
|
||||
Reference in new issue
Block a user