| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { log } from '@/utils/base.js';
- import store from "@/store/index.js";
- const host = "https://www.shvber.com/"
- const baseUrl = "api/"
- const request = (url, data, type = "get", header = {}, onlyHost = false, loading = true) => {
- if (loading) {
- uni.showLoading();
- }
- return new Promise((resolve, reject) => {
- uni.request({
- method: type,
- url: `${host}${onlyHost?"":baseUrl}${url}`,
- data: data,
- dataType: "json",
- header: {
- "content-type": "application/json;charset=UTF-8",
- "Authorization": store.state.token,
- "cid": "",
- ...header
- },
- success: (res) => {
- log(`${type}==>${url}`, data, res);
- resolve(res.data);
- },
- fail: (err) => {
- reject(err);
- },
- complete: () => {
- if (loading) {
- setTimeout(() => { uni.hideLoading(); }, 200)
- }
- }
- })
- })
- }
- const defaultOptions = {
- url: "",
- data: undefined,
- type: "post",
- header: undefined,
- onlyHost: false,
- loading: true
- }
- const post = (url, options) => {
- let opts = { ...defaultOptions };
- if (typeof url == "string") {
- opts.url = url;
- if (typeof options == "object") {
- opts = { ...opts, ...options }
- }
- } else {
- opts = { ...opts, ...url }
- }
- if (!opts.url) {
- log("POST请求url不能为空")
- throw new Error("POST请求url不能为空")
- }
- return request(opts.url, opts.data, "post", opts.header, opts.onlyHost, opts.loading);
- }
- const get = (url, options) => {
- let opts = { ...defaultOptions };
- if (typeof url == "string") {
- opts.url = url;
- if (typeof options == "object") {
- opts = { ...opts, ...options }
- }
- } else {
- opts = { ...opts, ...options }
- }
- if (!opts.url) {
- log("GET请求url不能为空")
- throw new Error("GET请求url不能为空")
- }
- return request(opts.url, opts.data, "get", opts.header, opts.onlyHost, opts.loading);
- }
- export {
- request,
- post,
- get
- }
|