| | |
| | | import { |
| | | fetch |
| | | } from '@/api/request.js'; |
| | | import { fetch } from '@/api/request.js' |
| | | |
| | | const typeObj = { |
| | | headerGET: { |
| | | "Content-type": 'application/x-www-from-urlencoded' |
| | | }, |
| | | headerPOST: { |
| | | "Content-type": 'application/x-www-from-urlencoded' |
| | | }, |
| | | jsonPOST: { |
| | | "Content-type": 'application/json' |
| | | } |
| | | headerGET: { 'Content-type': 'application/x-www-from-urlencoded' }, |
| | | headerPOST: { 'Content-type': 'application/x-www-from-urlencoded' }, |
| | | jsonPOST: { 'Content-type': 'application/json' }, |
| | | utfPOSt: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' } |
| | | } |
| | | |
| | | // 通用接口请求 |
| | | export const reqAll = (url, params, opt = {}) => { |
| | | opt.data = params; |
| | | opt.header = typeObj['headerPOST']; |
| | | opt.data = params |
| | | opt.header = typeObj['headerPOST'] |
| | | return fetch(url, opt) |
| | | } |
| | | |
| | | export const reqAllJson = (url, params, opt = {}) => { |
| | | opt.data = params |
| | | opt.header = typeObj['jsonPOST'] |
| | | return fetch(url, opt) |
| | | } |
| | | // deltete请求 |
| | | export const reqDelete = (url, params, opt = {}) => { |
| | | opt.header = typeObj['headerGET'] |
| | | opt.method = 'DELETE' |
| | | opt.data = params |
| | | return fetch(url, opt) |
| | | } |
| | | // get请求 |
| | | // urlParam是拼接路径的参数 |
| | | export const reqGet = (url, params, opt = {}) => { |
| | | opt.header = typeObj['headerGET']; |
| | | opt.method = "GET"; |
| | | opt.data = params; |
| | | opt.header = typeObj['headerGET'] |
| | | opt.method = 'GET' |
| | | opt.data = params |
| | | return fetch(url, opt) |
| | | } |
| | | |
| | | // post请求 |
| | | // urlType是指定拼接路径还是传json格式参数 |
| | | export const reqPost = (url, params, urlType, opt = {}) => { |
| | | opt.method = "POST"; |
| | | opt.method = 'POST' |
| | | if (urlType == 'params') { |
| | | Object.keys(params).map(item => { |
| | | // 这里面不能用params.item,点运算符加标识符的形式取值,因为item是Object.keys生成的每一项键都是字符串,不是标识符,可以用[]的形式访问 |
| | | if (params[item] == null) { |
| | | params[item] = ''; |
| | | params[item] = '' |
| | | } |
| | | }) |
| | | opt.params = params; |
| | | opt.header = typeObj['headerPOST']; |
| | | opt.params = params |
| | | opt.header = typeObj['headerPOST'] |
| | | } else if (urlType == 'json') { |
| | | opt.header = typeObj['jsonPOST']; |
| | | opt.header = typeObj['jsonPOST'] |
| | | opt.data = params |
| | | } else if (urlType == 'utf8') { |
| | | opt.header = typeObj['utfPOSt'] |
| | | opt.data = params |
| | | } else { |
| | | opt.data = params; |
| | | opt.header = typeObj['headerPOST']; |
| | | opt.data = params |
| | | opt.header = typeObj['headerPOST'] |
| | | } |
| | | return fetch(url, opt, urlType) |
| | | } |