|
|
@@ -1,10 +1,14 @@
|
|
|
-const api = require('./api.js')
|
|
|
+const api = require('./api.js');
|
|
|
+const dont_login_api = require('./config_api.js').dont_login;
|
|
|
+const no_openid_api = require('./config_api.js').no_openid;
|
|
|
class Utils {
|
|
|
/**
|
|
|
* @that {OBject} 挂载对象
|
|
|
*/
|
|
|
constructor(that) {
|
|
|
this.api = api;
|
|
|
+ this._showModel = false;
|
|
|
+ this._loading = false;
|
|
|
// this.message_hint = {
|
|
|
// 100: '继续发送请求'
|
|
|
// };
|
|
|
@@ -68,26 +72,34 @@ class Utils {
|
|
|
@options {Object} request请求的参数
|
|
|
@return 返回获取到的openID加入options并返回
|
|
|
*/
|
|
|
- getOpenid(options) {
|
|
|
+ getOpenid() {
|
|
|
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);
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ const _openid = wx.getStorageSync('openid');
|
|
|
+ if (_openid) {
|
|
|
+ resolve(_openid);
|
|
|
+ } else {
|
|
|
+ wx.login({
|
|
|
+ success: (res) => {
|
|
|
+ const code = res.code;
|
|
|
+ this.request({
|
|
|
+ // url:'',
|
|
|
+ data: {
|
|
|
+ code,
|
|
|
+ },
|
|
|
+ success: res => {
|
|
|
+ wx.setStorage({
|
|
|
+ key: 'openid',
|
|
|
+ data: res.data.openid,
|
|
|
+ success: res => {
|
|
|
+ const openid = res.data.openid
|
|
|
+ resolve(openid);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -98,36 +110,78 @@ class Utils {
|
|
|
* 设置request参数中的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
|
|
|
+ return new Promise((reslove, reject) => {
|
|
|
+ const userid = wx.getStorageSync('userid');
|
|
|
+ const usertoken = wx.getStorageSync('usertoken');
|
|
|
+ options.data.user_id = userid;
|
|
|
+ options.data.user_token = usertoken;
|
|
|
+ reslove(options)
|
|
|
+ })
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 获取网络状态
|
|
|
+ */
|
|
|
+ getNetworkType() {
|
|
|
+ return new Promise((reslove, reject) => {
|
|
|
+ wx.getNetworkType({
|
|
|
+ success: function(res) {
|
|
|
+ if (res.networkType === 'none') {
|
|
|
+ reslove('offline')
|
|
|
+ } else {
|
|
|
+ reslove(res.networkType)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function(res) {
|
|
|
+ reject('fail')
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
|
|
|
+ }
|
|
|
/*
|
|
|
请求方法
|
|
|
@options 请求参数
|
|
|
@status 是否携带userID
|
|
|
*/
|
|
|
- request(options, status = true) {
|
|
|
-
|
|
|
- /* 判断是否传入参数 */
|
|
|
- options.data = options.data ? options.data : {};
|
|
|
-
|
|
|
- /* 设置请求方式 */
|
|
|
- options.method = (options.type ? options.type : 'get').toUpperCase();
|
|
|
-
|
|
|
+ async request(options) {
|
|
|
/* 拼接接口地址 */
|
|
|
if (this.getDataType(options.url) !== 'String') {
|
|
|
throw new Error('接口路径格式错误')
|
|
|
}
|
|
|
+
|
|
|
+ /*判断是否需要登陆才能调用api */
|
|
|
+ options.dont_login = false; //默认全部需要登录
|
|
|
+ if (dont_login_api.length && dont_login_api.includes(options.url)) {
|
|
|
+ options.dont_login = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**判断是否需要openid才能调用api */
|
|
|
+ options.no_openid = false; //默认全部需要
|
|
|
+ if (no_openid_api.length && no_openid_api.includes(options.url)) {
|
|
|
+ options.no_openid = true;
|
|
|
+ }
|
|
|
+
|
|
|
/* 设置URL */
|
|
|
options.url = this.getUrl(options.url);
|
|
|
|
|
|
+ /* 判断是否传入参数 */
|
|
|
+ options.data = options.data ? options.data : {};
|
|
|
+
|
|
|
+ /* 设置请求方式并转换为大写*/
|
|
|
+ options.method = (options.type ? options.type : 'get').toUpperCase();
|
|
|
+
|
|
|
+ /* 判断是否传入请求头 */
|
|
|
+ options.header = options.header || {};
|
|
|
+
|
|
|
+ /* 判断是否传入返回数据格式 */
|
|
|
+ options.dataType = options.dataType || 'json';
|
|
|
+
|
|
|
+ /* 判断是否传入响应数据格式 */
|
|
|
+ options.responseType = options.responseType || 'text';
|
|
|
+
|
|
|
/* 消除空值字段 */
|
|
|
// for (let key in options.data) {
|
|
|
- // if (options.data[key] === undefined || options.data[key] === '') {
|
|
|
+ // if (!options.data[key] && options.data[key] !== 0) {
|
|
|
// delete options.data[key]
|
|
|
// }
|
|
|
// }
|
|
|
@@ -136,41 +190,37 @@ class Utils {
|
|
|
* 检测网络状况 并判断是否发起请求
|
|
|
* 当无网络时传入 offline 并提示无网络
|
|
|
*/
|
|
|
- wx.getNetworkType({
|
|
|
- success: (res) => {
|
|
|
- const networkType = res.networkType
|
|
|
- if (networkType === 'none') {
|
|
|
- wx.showToast({
|
|
|
- icon: 'none',
|
|
|
- title: '网络未连接',
|
|
|
- })
|
|
|
- wx.$emit('offline', true)
|
|
|
- options.success && options.success('offline')
|
|
|
- return
|
|
|
- }
|
|
|
- /* 根据需要判断是否传入openID */
|
|
|
- if (status) {
|
|
|
- /* 获取openID并将入参数 */
|
|
|
- // const openid = wx.getStorageSync('openid');
|
|
|
- // if (openid) {
|
|
|
- // _options.openid = openid;
|
|
|
- // this._request(_options);
|
|
|
- // } else {
|
|
|
- // this.getOpenid(_options).then((data) => {
|
|
|
- // this._request(data);
|
|
|
- // });
|
|
|
- // }
|
|
|
- /* 设置userID和usertoken */
|
|
|
- let _options = this.getUserId(options);
|
|
|
- this._request(_options);
|
|
|
+ const networkType = await this.getNetworkType();
|
|
|
+ if (networkType === 'offline') {
|
|
|
+ wx.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ title: '网络未连接',
|
|
|
+ })
|
|
|
+ wx.$emit('offline', true)
|
|
|
+ options.success && options.success(networkType)
|
|
|
+ return this.getNetworkType()
|
|
|
+ } else {
|
|
|
+ /**根据判断写入openid */
|
|
|
+ // if (!options.no_openid) {
|
|
|
+ // options.data.openid = await this.getOpenid();
|
|
|
+ // }
|
|
|
+ /* 根据dont_login判断是否需要登录 */
|
|
|
+ if (options.dont_login) {
|
|
|
+ return this._request(options);
|
|
|
+ } else {
|
|
|
+ /* 设置userID和usertoken */
|
|
|
+ let _options = await this.getUserId(options);
|
|
|
+ /** 当user_id与user_token不全时 提示登陆重新获取 */
|
|
|
+ if (_options.data.user_id && _options.data.user_token) {
|
|
|
+ return this._request(_options);
|
|
|
} else {
|
|
|
- this._request(options);
|
|
|
+ this._doLogin();
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
- * 实际执行的请求方法体
|
|
|
+ * 实际执行的请求方法体
|
|
|
* @opt 传入的参数对象
|
|
|
* @return 返回promise对象
|
|
|
* 执行完之后既能执行回调也能返回promise对象调用
|
|
|
@@ -178,22 +228,21 @@ class Utils {
|
|
|
_request(opt) {
|
|
|
let route = getCurrentPages();
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- !wx.kx_loading && wx.showLoading({
|
|
|
+ !this._loading && wx.showLoading({
|
|
|
mask: true,
|
|
|
+ title: '加载中...',
|
|
|
success: () => {
|
|
|
- wx.kx_loading = true
|
|
|
+ this._loading = true
|
|
|
}
|
|
|
})
|
|
|
wx.request({
|
|
|
url: opt.url,
|
|
|
- data: opt.data || {},
|
|
|
- header: opt.header || {},
|
|
|
+ data: opt.data,
|
|
|
+ header: opt.header,
|
|
|
method: opt.method,
|
|
|
- dataType: opt.dataType || 'json',
|
|
|
- responseType: opt.responseType || 'text',
|
|
|
+ dataType: opt.dataType,
|
|
|
+ responseType: opt.responseType,
|
|
|
success: function(res) {
|
|
|
- console.log('请求参数=>', opt);
|
|
|
- console.log('服务器响应结果=>', res.statusCode, res.data);
|
|
|
// 设置网络状态
|
|
|
wx.$emit('offline', false)
|
|
|
// 判断是否含有成功方法并执行
|
|
|
@@ -240,14 +289,24 @@ class Utils {
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
- complete: function(res) {
|
|
|
+ complete: (res) => {
|
|
|
+ console.log('请求参数=>', opt);
|
|
|
+ console.log('请求结果=>', res.data.errmsg, res.data);
|
|
|
+ console.log('服务器响应状态=>', res.statusCode);
|
|
|
+ console.log('服务器响应结果=>', res);
|
|
|
+ console.log('当前页面=>', route[route.length - 1].route);
|
|
|
+ console.log(' ');
|
|
|
wx.hideLoading({
|
|
|
success: () => {
|
|
|
- wx.kx_loading = false;
|
|
|
+ this._loading = false;
|
|
|
}
|
|
|
})
|
|
|
- console.log('当前页面=>', route[route.length - 1].route);
|
|
|
- console.log('请求结果=>', res.data.errmsg || res.data);
|
|
|
+ wx.stopPullDownRefresh({
|
|
|
+ success: () => {
|
|
|
+ console.log(route[route.length - 1].route, '=> stopPullDownRefresh')
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
// 判断是否含有complete方法并执行
|
|
|
if (opt.complete && typeof opt.complete === 'function') {
|
|
|
opt.complete(res.data);
|
|
|
@@ -258,31 +317,61 @@ class Utils {
|
|
|
})
|
|
|
}
|
|
|
/**
|
|
|
+ * 执行跳转登录页面操作
|
|
|
+ */
|
|
|
+ _doLogin() {
|
|
|
+ // wx.clearStorageSync();
|
|
|
+ if (!this._showModel) {
|
|
|
+ this._showModel = true;
|
|
|
+ wx.reLaunch({
|
|
|
+ url: '/pages/login/login',
|
|
|
+ success: () => {
|
|
|
+ setTimeout(() => {
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '登陆状态失效,请重新登录',
|
|
|
+ showCancel: false,
|
|
|
+ success: () => {
|
|
|
+ this._showModel = false;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }, 300)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 上传图片
|
|
|
* @pic_arr {Array} 图片数组
|
|
|
* @return {Array}
|
|
|
*/
|
|
|
- upLoadImage(pic_arr, name) {
|
|
|
- let _type = this.getDataType(pic_arr);
|
|
|
+ upLoadImage(options) {
|
|
|
+ const _type = this.getDataType(options.files);
|
|
|
+ options.formData = options.formData || {};
|
|
|
+ const formData = {
|
|
|
+ user_id: wx.getStorageSync('userid'),
|
|
|
+ user_token: wx.getStorageSync('usertoken'),
|
|
|
+ ...options.formData
|
|
|
+ }
|
|
|
return new Promise((reslove, reject) => {
|
|
|
if (_type === "Array") {
|
|
|
- let _arr = [];
|
|
|
- let _len = pic_arr.length;
|
|
|
- pic_arr.forEach((val, key) => {
|
|
|
+ const _arr = [];
|
|
|
+ const _files = options.files;
|
|
|
+ const _len = _files.length - 1;
|
|
|
+ const _url = this.getUrl(options.url);
|
|
|
+ _files.forEach((val, key) => {
|
|
|
wx.uploadFile({
|
|
|
- url: wx.kx_api.hwUser.changeAvatar,
|
|
|
+ url: _url,
|
|
|
filePath: val,
|
|
|
- name,
|
|
|
- formData: {
|
|
|
- user_id: wx.getStorageSync('userid'),
|
|
|
- user_token: wx.getStorageSync('usertoken'),
|
|
|
- },
|
|
|
+ name: options.name,
|
|
|
+ formData,
|
|
|
success: res => {
|
|
|
if (res.statusCode === 200) {
|
|
|
const data = JSON.parse(res.data);
|
|
|
if (data.errcode === 0) {
|
|
|
_arr.push(data.data);
|
|
|
- if (_len - 1 === key) {
|
|
|
+ if (_len === key) {
|
|
|
reslove(_arr)
|
|
|
}
|
|
|
}
|