| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { baseUrl, appid } from "../config";
- const App = getApp();
- function getHeader() {
- if (wx.getStorageSync("accessToken")) {
- return {
- "content-type": "application/json",
- "X-Mp-App-Id": appid,
- // "X-Store-Id": "",
- "Accept-Language": "zh-CN",
- "Accept": "application/json",
- "Authorization": "Bearer " + wx.getStorageSync("accessToken"),
- };
- }
- return {
- "content-type": "application/json",
- };
- }
- function showErrToast(e) {
- wx.showToast({
- title: typeof e === "string" ? e : e.errMsg || e.toString(),
- icon: "none",
- duration: 1500,
- });
- }
- function getPromise(url, data, method) {
- return new Promise((resolve, reject) => {
- let quest = ()=>{
- wx.request({
- url: `${baseUrl}${url}`,
- header: getHeader(),
- method: method,
- data: data,
- success: function (res) {
- if (res.statusCode === 200) {
- resolve(res.data);
- } else {
- if (res.statusCode === 401) {
- reject(401);
- } else {
- reject(res.data.message);
- }
- }
- },
- fail: function (err) {
- reject(err);
- },
- });
- }
- App.checkLoginStatus().then(()=>{
- quest();
- })
- }).catch((e) => {
- console.error(`in promise error, url: ${baseUrl}${url}`);
- console.error(e);
- if (e) {
- showErrToast(e);
- }
- });
- }
- const http = {
- get: function (url, data) {
- return getPromise(url, data, "GET");
- },
- post: function (url, data) {
- return getPromise(url, data, "POST");
- },
- };
- export default http;
|