| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /**
- * 常用工具函数
- */
- class Utils {
- /*
- 格式化时间
- @date {Date} 传入的时间
- @status {Boolean} 是否需要时分秒开关 true 关闭时分秒选项 false 打开时分秒选项
- */
- formatTime(date, status = false) {
- date = date.replace(getRegExp('-', 'g'), '/');
- 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;
- }
- /*
- 获取实际数据类型
- @data {any} 数据
- */
- getDataType(data) {
- return /^\[object (.*)\]$/.exec(Object.prototype.toString.call(data))[1];
- }
- /*
- 获取url
- @url {String} 待处理的URL
- @return {String} 处理过的字符串
- 根据URL是否有http或者https判断是否拼接字符串
- */
- getUrl(url) {
- return new Promise(reslove => {
- const apiUrl = require('../config.js').apiUrl; // 加载接口地址文件
- if (url.indexOf('https://') === -1 && url.indexOf('http://') === -1) {
- reslove(apiUrl + url)
- } else {
- if (url.indexOf('http://') === -1) {
- reslove(url)
- return
- }
- reslove(url.replace('http://', 'https://'))
- }
- })
- }
- /**
- * 获取网络状态
- */
- getNetworkType() {
- return new Promise((reslove, reject) => {
- wx.getNetworkType({
- success: function (res) {
- if (res.networkType === 'none') {
- wx.$emit('offline', true)
- reslove('offline')
- } else {
- wx.$emit('offline', false)
- reslove(res.networkType)
- }
- },
- fail: function (res) {
- reject('fail')
- }
- })
- })
- }
- /**
- * 获取userid与userToken
- * @options {Object} 传入的对象参数
- * @return 返回参数对象
- * 设置request参数中的userID和usertoken
- */
- getUserId(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)
- })
- }
- /**
- * 上传图片
- * @pic_arr {Array} 图片数组
- * @return {Array}
- */
- 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
- };
- wx.showLoading({
- mask: true,
- title: '上传中...'
- })
- return new Promise(async (reslove, reject) => {
- if (_type === "Array") {
- const _arr = [];
- const _files = options.files;
- const _len = _files.length - 1;
- const _url = await this.getUrl(options.url);
- _files.forEach((val, key) => {
- wx.uploadFile({
- url: _url,
- filePath: val,
- 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 === key) {
- wx.hideLoading()
- reslove(_arr)
- }
- }
- }
- },
- fail: (e) => {
- console.log(e)
- reject({
- type: this.getDataType(val)
- })
- wx.hideLoading()
- throw new Error(e)
- }
- })
- })
- } else {
- reject({
- type: _type
- })
- wx.hideLoading()
- throw new Error('输入格式有误')
- }
- })
- }
- /**
- * 获取用户信息
- */
- getHwUserInfo(that) {
- return new Promise(reslove => {
- wx.kx_request({
- url: wx.kx_api.hwUser.getHwUserInfo,
- success: res => {
- if (res.data) {
- //成功后存入缓存
- wx.setStorageSync('userinfo', res.data.hwUser);
- reslove(res.data.hwUser)
- // 判断是否提交页面数据
- that && that.setData({
- userinfo: res.data.hwUser
- })
- }
- }
- })
- })
- }
- }
- module.exports = {
- Utils: new Utils()
- }
|