123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- // const { baseUrl } = require('./env.js').prod; // 正式
- // const {
- // baseUrl
- // } = require('./env.js').test; // 测试
- // token
- let Authorization = "";
- const jsonType = {
- "content-type": "application/json"
- }
- const urlencodeType = {
- "content-type": "application/x-www-form-urlencoded"
- }
- import strings from './strings.js';
- // 错误类型
- let statusCode = {
- '1': "请求数据失败",
- "400": "参数列表错误",
- "401": "请先登录",
- "403": "禁止访问",
- "404": "资源,服务未找到",
- "405": "错误请求方法",
- "409": "冲突",
- "413": "上传文件太大",
- "500": "服务器错误",
- "1000": "账户已在别处登录"
- }
- const formatTime = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
- }
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : `0${n}`
- };
- // 封装wx.request
- function _show_error(error_code) {
- wx.showToast({
- "title": statusCode[error_code],
- "icon": "error",
- "duration": 1500
- });
- }
- //
- function request(url, data = {}, method = "GET", header = urlencodeType, isToken = true) {
- if (isToken) {
- try {
- var value = wx.getStorageSync('token') || "";
- if (!strings.isEmpty(value)) {
- const resData = JSON.parse(value);
- Authorization = resData.token;
- }
- } catch (e) {
- Authorization = "";
- }
- }
- // 拼接地址
- var baseUrl = getApp().globalData.baseUrl;
- let fullurl = `${baseUrl}${url}`;
- //返回异步操作成功的结果和返回异步操作失败的结果
- if (!strings.isTrue(data.unShowLoad)) {
- wx.showLoading({
- title: '加载中',
- });
- }
- console.log("请求参数:", fullurl, data)
- return new Promise((resolve, reject) => {
- wx.request({
- url: fullurl,
- method,
- data,
- header: Object.assign({
- "content-type": "application/x-www-form-urlencoded",
- "appid": "wx08f94a3e90881910",
- "Authorization": isToken ? Authorization : undefined
- }, header),
- success(res) {
- console.log("接口:", fullurl, res)
- if (res.statusCode !== 200) {
- wx.hideLoading();
- wx.showToast({
- "title": "请求错误",
- "icon": "error",
- "duration": 1500
- });
- resolve(res.data);
- return;
- }
- if (!strings.isTrue(data.unShowLoad)) {
- wx.hideLoading();
- }
- if (res.data.code === 0) {
- resolve(res.data.data);
- } else if (res.data.code === 401) {
- resolve(res.data);
- var pages = getCurrentPages();
- pages[0].onLoginLoad();
- // token过期
- // try {
- // getCurrentPages()[0].logOut();
- // } catch (e) {
- // };
- } else {
- resolve(res.data);
- _show_error(res.data.code)
- }
- },
- fail: (err) => {
- console.log("请求失败" + JSON.stringify(err));
- _show_error("1");
- wx.hideLoading();
- reject({
- code: -1,
- msg: JSON.stringify(err)
- })
- },
- complete() {}
- })
- })
- }
- function isCN(str) {
- if (/^[\u3220-\uFA29]+$/.test(str)) {
- return true;
- } else {
- return false;
- }
- }
- module.exports = {
- formatTime,
- request,
- formatNumber,
- isCN,
- urlencodeType,
- jsonType
- }
|