1.0.8 • Published 2 months ago

haigeek-axios v1.0.8

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

haigeek-axios请求插件

介绍

基于axios封装,适用于调用海尔对外开放能力的http请求插件。

安装

Vue2版本:npm install haigeek-axios

示例

import { haigeekRequest } from 'haigeek-axios'
haigeekRequest('get', url, params).then(res => {})

封装

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

自动跳转登录方法

1.0.8

2 months ago

1.0.7

7 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago