import { host } from '../Gdata' import { router_list } from '../config' const getRouter = function () { var pages = getCurrentPages() //获取加载的页面 var currentPage = pages[pages.length - 1] //获取当前页面的对象 let id = '' router_list.forEach(item => { if (item.router_name == currentPage.route) { id = item.id } }) return id } 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 let cps = wx.getStorageSync("cps") || null let header = [] if (cps !== null && cps.vTime >= new Date().getTime()) { header = { 'token': token, 'uid': uid, 'cps': cps.value } } else { header = { 'token': token, 'uid': uid } } // if (token && uid) { wx.request({ url: host + url, data: data, method: method, header, 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.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' // }) wx.navigateTo({ url: '/pages/user/login/login', }) } 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' // }) wx.navigateTo({ url: '/pages/user/login/login', }) } 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, type: 1 }).then(res1 => { resolve(res1) }) } else { wx.getLocation({ type: 'wgs84', success(res) { if (!res.longitude) { setting() return } httpUtil("/api/v1/get-city", "get", { longitude: res.longitude, latitude: res.latitude, type: 1 }).then(res1 => { resolve(res1) }) }, fail(res) { setting() } }) } }) } const setting = function () { 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', // }) wx.navigateBack({ delta: 0, }) } else { wx.showToast({ title: '授权失败', icon: 'error', duration: 1000 }) } } }) } else { wx.showToast({ title: '下拉重新授权!', icon: 'error', duration: 1000 }) } } }) } else { //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位 wx.showModal({ title: '', showCancel: false, content: '请在系统设置中打开微信定位服务', confirmText: '确定', success: function (res) { } }) } } }) } module.exports = { formatTime, httpUtil, http, login_check, location_city, getRouter }