1.1.3 • Published 5 months ago
haigeek-axios v1.1.3
haigeek-axios请求插件
介绍
基于axios封装,适用于调用海尔对外开放能力的http请求插件。
安装
npm install haigeek-axios
示例
import { haigeekRequest } from 'haigeek-axios'
haigeekRequest('get', url, params).then(res => {})
import axios from 'haigeek-axios'
axios......
封装
import { haigeekRequest } from '@/assets/js/http'
const env = process.env.NODE_ENV
export const baseapi = env === 'development' ? '/apiprod' : 'https://ys-openservice.haigeek.com'
/**
* @description: 封装请求类
* @param {method} method 请求方式
* @param {path} path 请求路径
* @param {params} params 参数
* @param {headers} headers 请求头
*/
export const iotRequest = (method, path, params, headers) => {
return haigeekRequest(
method,
baseapi + path,
params,
{
hgAppId: 'MB-IYD10511011511-0000',
hgAppKey: '08764dc218830a9a05cbfa1814020cdf',
hgUrlPrefix: baseapi,
hgLoading: true,
hgAutoError: '1',
hgSuccessCodeKey: 'retCode', // 默认retCode,可不传
hgSuccessCode: '00000', // 默认00000,可不传
hgErrorMsgKey: 'retInfo', // 默认retInfo,可不传
hgEncryptLevel: '0', // 默认0,可不传
hgLoginCode: '403',
fnShowError: fnShowError,
fnStartLoading: fnStartLoading,
fnEndLoading: fnEndLoading,
fnLogin: fnLogin
},
headers
)
}
const fnStartLoading = () => {
document.getElementById('loading').style.display = 'block';
}
const fnEndLoading = () => {
document.getElementById('loading').style.display = 'none';
}
const fnShowError = (msg) => {
document.getElementById('showMsg').innerHTML = msg;
document.getElementById('showMsg').style.display = 'block';
setTimeout(() => {
document.getElementById('showMsg').style.display = 'none';
}, 3000);
}
const fnLogin = () => {
console.log('跳转至登录')
}
// 使用
import { iotRequest } from '@/api/api.js'
iotRequest(
'post',
'接口地址',
{
"参数1":"111",
}
).then(res => {
console.log(res)
}).catch(e => {
console.log(e)
})
haigeekRequest(method, ajaxurl, params, config, headers, other): 说明
method
请求方式:get/post
ajaxurl
请求地址
params
入参:json类型(其他类型时通过config.hgEncryptLevel控制)
config主要配置项
1>hgAppId:string
对应智能应用appId, 验证签名-必传
2>hgAppKey:string
对应智能应用appKey, 验证签名-必传
3>hgAutoError:string
0:不自动提示(默认值)
1:自动提示后台异常信息(注意需要传fnShowError)
4>hgLoading:bool
false:请求时不显示loading(默认值)
true:请求时自动显示loading(注意需要传fnStartLoading,fnEndLoading)
5>hgEncryptLevel:string
0:入参为json类型数据(默认值)
1:入参为application/x-www-form-urlencoded类型数据
2:入参为multipart/form-data类型数据
6>hgHeadSignLog:bool
false:不打印header参数sign(默认值)
true:打印header参数sign(用于调试)
7>hgSuccessCodeKey:string | number
'retCode': 默认值
其他:自定义code的key
8>hgSuccessCode:string | number
'00000': 默认值
其他:自定义请求成功code值
9>hgErrorMsgKey: string
'retInfo': 默认值
其他:自定义请求失败错误信息key值
10>hgUrlPrefix: string
请求前缀--签名时去除使用,生产环境一般传域名
11>fnStartLoading: function
打开loading
12>fnEndLoading: function
关闭loading
13>fnShowError: function
自动错误提示方法,参数为提示的错误信息
14>hgLoginCode: string
用户信息失效,需要登录使用;登录失效返回的code值(默认不自动登录)
15>fnLogin: function
自动跳转登录方法
16>hgErrorType: string
接口报错类型:不传:接口必须正常返回,通过code值判断是否请求失败(默认值),1:接口请求失败时直接返回错误信息
17>hgSuccessResult: string
请求成功返回结果对应的属性数据,默认无(返回整个结果数据)