import { host } from '../Gdata' 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}` } const httpUtil = (url, method, data, isLoading = true) => { if (isLoading) { wx.showLoading({ title: '加载中' }); } return new Promise(function (resolve, reject) { wx.request({ url: host + url, data: data, method: method, // header: { "Content-Type": "application/x-www-form-urlencoded" }, success: res => { if (isLoading) { wx.hideLoading(); } if (200 == res.data.status) { resolve(res.data.data); } else { wx.showToast({ title: '请求错误!', icon: 'none', duration: 1000 }) } }, fail: res => { wx.hideLoading(); } }) }) } const http = (url, method, data, isLoading = true) => { if (isLoading) { wx.showLoading({ title: '加载中' }); } return new Promise(function (resolve, reject) { let token = getApp().globalData.token let uid = getApp().globalData.uid // if (token && uid) { wx.request({ url: host + url, data: data, method: method, header: { 'token': token, 'uid': uid }, success: res => { if (isLoading) { wx.hideLoading(); } if (200 == res.data.status) { resolve(res.data.data); } else if(401 ==res.data.status||402 ==res.data.status||403 ==res.data.status ){ wx.showModal({ title: '温馨提示', content: '未登录或信息过期,是否前往登录?', confirmText: '前往', success(res) { if (res.confirm) { getApp().globalData.gologin = true wx.switchTab({ url: '/pages/user/user_index/user' }) } else if (res.cancel) { console.log('用户点击取消') } } }) } else { wx.showToast({ title: res.data.status + ':' + res.data.msg, icon: 'none', duration: 2000 }) } }, fail: res => { wx.hideLoading(); } }) // } else { // wx.showToast({ // title: '未登录\r\n点击个人中心头像登录', // icon: 'none', // duration: 2000 // }) // } }) } const login_check = n => { if (!getApp().globalData.token || !getApp().globalData.uid || "undefined" == typeof (getApp().globalData.token) || "undefined" == typeof (getApp().globalData.uid)) { if (n == 1) { wx.showToast({ title: '未登录', icon: 'error', duration: 2000, complete() { function gologin() { getApp().globalData.gologin = true wx.switchTab({ url: '/pages/user/user_index/user' }) } setTimeout(gologin, 2000); } }) } else { wx.showModal({ title: '温馨提示', content: '您还没有登录,是否前往登录?', confirmText: '前往', success(res) { if (res.confirm) { getApp().globalData.gologin = true wx.switchTab({ url: '/pages/user/user_index/user' }) } else if (res.cancel) { console.log('用户点击取消') } } }) } } else { return true } } const location_city = function () { let default_city = arguments[0] return new Promise(function (resolve, reject) { if (default_city == "beijing") { let longitude = 116.404368 let latitude = 39.924338 httpUtil("/api/v1/get-city", "get", { latitude, longitude }).then(res1 => { resolve(res1) }) } else { wx.getLocation({ type: 'wgs84', success(res) { httpUtil("/api/v1/get-city", "get", { longitude: res.longitude, latitude: res.latitude }).then(res1 => { resolve(res1) }) }, fail(res) { wx.getSetting({ //先查看授权情况 success: function (res) { var statu = res.authSetting; if (!statu['scope.userLocation']) { //判断是否授权,没有授权就提示下面的信息 wx.showModal({ title: '需要获取您的地理位置,请确认授权,否则小程序功能将无法使用', cancelColor: '#666666', success: function (tip) { if (tip.confirm) { //查看是否点击确定 wx.openSetting({ //打开设置 success: function (data) { if (data.authSetting["scope.userLocation"] == true) { //到这一步表示打开了位置授权 wx.reLaunch({ url: '/pages/index/index/index', }) } else { wx.showToast({ title: '授权失败', icon: 'error', duration: 1000 }) } } }) } else { wx.showToast({ title: '未同意授权', icon: 'error', duration: 1000 }) } } }) } } }) } }) } }) } module.exports = { formatTime, httpUtil, http, login_check, location_city }