| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- const api = require('./api.js')
- class Utils {
- constructor(that) {
- this.api = api;
- // this.message_hint = {
- // 100: '继续发送请求'
- // };
- that.kx_request = this.request.bind(this);
- that.kx_api = this.api;
- }
- /*
- 获取实际数据类型
- @data 数据
- */
- getDataType(data) {
- return /^\[object (.*)\]$/.exec(Object.prototype.toString.call(data))[1];
- }
- /*
- 格式化时间
- @date 传入的时间
- @status 是否需要时分秒开关
- */
- formatTime(date, status = false) {
- const _date = new Date(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();
- if (status) {
- return [year, month, day].map(this.formatNumber).join('/');
- }
- return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':');
- }
- /* 格式化数字 */
- formatNumber(n) {
- n = n.toString();
- return n[1] ? n : '0' + n;
- }
- /* 获取url */
- getUrl(url) {
- const apiUrl = require('../config.js').apiUrl; // 加载接口地址文件
- if (url.indexOf('https://') === -1 && url.indexOf('http://') === -1) {
- return apiUrl + url
- } else {
- if (url.indexOf('http://') === -1) {
- return url
- }
- return url.replace('http://', 'https://')
- }
- }
- /* 获取Openid */
- getOpenid(options) {
- return new Promise((resolve, reject) => {
- wx.login({
- success: (res) => {
- let code = res.code;
- this.request({
- code,
- success: res => {
- wx.setStorage({
- key: 'openid',
- data: res.data.openid,
- success: res => {
- options.openid = res.data.openid
- resolve(options);
- }
- })
- }
- })
- }
- })
- })
- }
- /* 获取userid与userToken */
- getUserId(options) {
- const userid = wx.getStorageSync('userid');
- const usertoken = wx.getStorageSync('usertoken');
- options.data.user_id = userid;
- options.data.user_token = usertoken;
- return options
- }
- /*
- 请求方法
- @options 请求参数
- @status 是否携带userID
- */
- request(options, status = true) {
- /* 判断是否传入参数 */
- options.data = options.data ? options.data : {};
- /* 设置请求方式 */
- options.method = (options.type ? options.type : 'get').toUpperCase();
- /* 拼接接口地址 */
- if (this.getDataType(options.url) !== 'String') {
- throw new Error('接口路径格式错误')
- }
- options.url = this.getUrl(options.url);
- /* 消除空值字段 */
- // for (let key in options.data) {
- // if (options.data[key] === undefined || options.data[key] === '') {
- // delete options.data[key]
- // }
- // }
- /* 检测网络状况 并判断是否发起请求 */
- wx.getNetworkType({
- success: (res) => {
- const networkType = res.networkType
- if (networkType === 'none') {
- options.success && options.success('offline')
- return
- }
- /* 根据需要判断是否传入openID */
- if (status) {
- // const openid = wx.getStorageSync('openid');
- // if (openid) {
- // options.openid = openid;
- // this._request(options);
- // } else {
- // this.getOpenid(options).then((data) => {
- // this._request(data);
- // });
- // }
- this._request(this.getUserId(options));
- } else {
- this._request(options);
- }
- }
- })
- }
- /*
- 实际执行的请求方法体
- @opt 传入的参数对象
- */
- _request(opt) {
- return new Promise((resolve, reject) => {
- wx.request({
- url: opt.url,
- data: opt.data || {},
- header: opt.header || {},
- method: opt.method,
- dataType: opt.dataType || 'json',
- responseType: opt.responseType || 'text',
- success: function(res) {
- console.log('请求参数=>', opt);
- console.log('服务器响应结果=>',res);
- console.log('当前页面=>', getCurrentPages().map(val => val.route).pop());
- // 判断是否含有成功方法并执行
- if (opt.success && typeof opt.success === 'function') {
- if (res.statusCode === 200) {
- if (res.data.errcode === 0) {
- opt.success(res.data);
- resolve(res.data);
- } else {
- // 判断是否含有失败方法并执行
- if (opt.fail && typeof opt.fail === 'function') {
- opt.fail(res.data);
- reject(res.data);
- }
- if (!opt.model) {
- wx.showToast({
- icon: 'none',
- title: res.data.errmsg || '网络异常,请检查后重试',
- })
- }
- }
- } else {
- opt.success && opt.success('offline')
- wx.showToast({
- icon: 'none',
- title: '网络异常,请检查后重试',
- })
- }
- }
- },
- fail: function(res) {
- // 判断是否含有失败方法并执行
- if (opt.fail && typeof opt.fail === 'function') {
- opt.success && opt.success('offline')
- opt.fail(res.data);
- reject(res.data);
- wx.showToast({
- icon: 'none',
- title: '网络异常,请检查后重试',
- })
- }
- },
- complete: function(res) {
- // 判断是否含有complete方法并执行
- if (opt.complete && typeof opt.complete === 'function') {
- opt.complete(res.data);
- resolve(res.data);
- }
- },
- })
- })
- }
- }
- module.exports = {
- Utils: Utils
- }
|