app更新对l2数据显示
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const TokenKey = 'App-Token'
|
||||
const Zinc1TokenKey = 'App-Zinc1-Token'
|
||||
|
||||
export function getToken() {
|
||||
return uni.getStorageSync(TokenKey)
|
||||
@@ -11,3 +12,16 @@ export function setToken(token) {
|
||||
export function removeToken() {
|
||||
return uni.removeStorageSync(TokenKey)
|
||||
}
|
||||
|
||||
// Zinc1系统的token管理
|
||||
export function getZinc1Token() {
|
||||
return uni.getStorageSync(Zinc1TokenKey)
|
||||
}
|
||||
|
||||
export function setZinc1Token(token) {
|
||||
return uni.setStorageSync(Zinc1TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeZinc1Token() {
|
||||
return uni.removeStorageSync(Zinc1TokenKey)
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ const request = config => {
|
||||
url = url.slice(0, -1)
|
||||
config.url = url
|
||||
}
|
||||
|
||||
console.log('请求参数[' + config.method + config.url + ']', config)
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
method: config.method || 'get',
|
||||
|
||||
@@ -13,6 +13,13 @@ export function createMeasureSocket({
|
||||
} = {}) {
|
||||
let socket = null
|
||||
let manualClose = false
|
||||
// 保存回调函数的引用,允许清空
|
||||
let callbacks = {
|
||||
onOpen,
|
||||
onClose,
|
||||
onError,
|
||||
onMessage
|
||||
}
|
||||
|
||||
const wsBase = (config.wsUrl || config.baseUrl || '').replace(/^http/, 'ws')
|
||||
const url = `${wsBase}/websocket?type=${type}`
|
||||
@@ -30,12 +37,12 @@ export function createMeasureSocket({
|
||||
},
|
||||
fail(err) {
|
||||
console.error('connectSocket 调用失败:', err)
|
||||
onError && onError(err)
|
||||
callbacks.onError && callbacks.onError(err)
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('connectSocket 执行异常(可能环境不支持 WebSocket):', err)
|
||||
onError && onError(err)
|
||||
callbacks.onError && callbacks.onError(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,24 +54,52 @@ export function createMeasureSocket({
|
||||
// 正确的事件注册方式:socketTask.onOpen / onMessage / onError / onClose
|
||||
socket.onOpen((res) => {
|
||||
console.log('WebSocket 已打开', res)
|
||||
onOpen && onOpen(res)
|
||||
// 检查回调是否已被清空
|
||||
if (callbacks && callbacks.onOpen) {
|
||||
try {
|
||||
callbacks.onOpen(res)
|
||||
} catch (e) {
|
||||
// 忽略回调错误
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
socket.onMessage((evt) => {
|
||||
// H5 为 evt.data,小程序为 evt.data,统一兼容
|
||||
const data = evt && (evt.data || evt)
|
||||
onMessage && onMessage(data)
|
||||
// 检查回调是否已被清空
|
||||
if (callbacks && callbacks.onMessage) {
|
||||
try {
|
||||
callbacks.onMessage(data)
|
||||
} catch (e) {
|
||||
// 忽略回调错误,可能是组件已销毁
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
socket.onError((err) => {
|
||||
console.error('WebSocket 发生错误:', err)
|
||||
onError && onError(err)
|
||||
// 检查回调是否已被清空
|
||||
if (callbacks && callbacks.onError) {
|
||||
try {
|
||||
callbacks.onError(err)
|
||||
} catch (e) {
|
||||
// 忽略回调错误
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
socket.onClose((evt) => {
|
||||
console.log('WebSocket 已关闭', evt)
|
||||
onClose && onClose(evt)
|
||||
if (!manualClose) {
|
||||
// 检查回调是否已被清空
|
||||
if (callbacks && callbacks.onClose) {
|
||||
try {
|
||||
callbacks.onClose(evt)
|
||||
} catch (e) {
|
||||
// 忽略回调错误
|
||||
}
|
||||
}
|
||||
if (!manualClose && callbacks) {
|
||||
setTimeout(connect, 3000)
|
||||
}
|
||||
})
|
||||
@@ -72,8 +107,19 @@ export function createMeasureSocket({
|
||||
|
||||
function close() {
|
||||
manualClose = true
|
||||
// 清空所有回调,防止在关闭过程中或关闭后触发回调
|
||||
callbacks = {
|
||||
onOpen: null,
|
||||
onClose: null,
|
||||
onError: null,
|
||||
onMessage: null
|
||||
}
|
||||
if (socket) {
|
||||
socket.close(1000, 'client close')
|
||||
try {
|
||||
socket.close(1000, 'client close')
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
socket = null
|
||||
}
|
||||
}
|
||||
@@ -93,6 +139,16 @@ export function createMeasureSocket({
|
||||
})
|
||||
}
|
||||
|
||||
return { connect, close, send }
|
||||
// 清空回调函数的方法
|
||||
function clearCallbacks() {
|
||||
callbacks = {
|
||||
onOpen: null,
|
||||
onClose: null,
|
||||
onError: null,
|
||||
onMessage: null
|
||||
}
|
||||
}
|
||||
|
||||
return { connect, close, send, clearCallbacks }
|
||||
}
|
||||
|
||||
|
||||
70
apps/hand-factory/utils/zinc1Request.js
Normal file
70
apps/hand-factory/utils/zinc1Request.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import store from '@/store'
|
||||
import config from '@/config'
|
||||
import { getZinc1Token } from '@/utils/auth'
|
||||
import errorCode from '@/utils/errorCode'
|
||||
import { toast, showConfirm, tansParams } from '@/utils/common'
|
||||
|
||||
let timeout = 10000
|
||||
// 固定使用 zinc1 的 baseUrl
|
||||
const baseUrl = 'http://140.143.206.120:10082/prod-api'
|
||||
|
||||
const zinc1Request = config => {
|
||||
// 是否需要设置 token
|
||||
const isToken = (config.header || {}).isToken === false
|
||||
config.header = config.header || {}
|
||||
// 使用Zinc1系统的token
|
||||
if (getZinc1Token() && !isToken) {
|
||||
config.header['Authorization'] = 'Bearer ' + getZinc1Token()
|
||||
}
|
||||
// get请求映射params参数
|
||||
if (config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params)
|
||||
url = url.slice(0, -1)
|
||||
config.url = url
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
method: config.method || 'get',
|
||||
timeout: config.timeout || timeout,
|
||||
url: baseUrl + config.url,
|
||||
data: config.data,
|
||||
header: config.header,
|
||||
dataType: 'json'
|
||||
}).then(response => {
|
||||
let [error, res] = response
|
||||
if (error) {
|
||||
toast('后端接口连接异常')
|
||||
reject('后端接口连接异常')
|
||||
return
|
||||
}
|
||||
const code = res.data.code || 200
|
||||
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
||||
if (code === 401) {
|
||||
// Zinc1系统是可选的,如果返回401,静默处理,不显示任何提示
|
||||
reject('Zinc1系统登录状态已过期')
|
||||
} else if (code === 500) {
|
||||
toast(msg)
|
||||
reject('500')
|
||||
} else if (code !== 200) {
|
||||
toast(msg)
|
||||
reject(code)
|
||||
}
|
||||
resolve(res.data)
|
||||
})
|
||||
.catch(error => {
|
||||
let { message } = error
|
||||
if (message === 'Network Error') {
|
||||
message = '后端接口连接异常'
|
||||
} else if (message.includes('timeout')) {
|
||||
message = '系统接口请求超时'
|
||||
} else if (message.includes('Request failed with status code')) {
|
||||
message = '系统接口' + message.substr(message.length - 3) + '异常'
|
||||
}
|
||||
toast(message)
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default zinc1Request
|
||||
Reference in New Issue
Block a user