201 lines
5.3 KiB
JavaScript
201 lines
5.3 KiB
JavaScript
// import config from '../config'
|
||
import api from '../utils/apiQuery'
|
||
const app = getApp()
|
||
|
||
/**
|
||
* 获取用户信息
|
||
*/
|
||
function getAjaxUserInfo(){
|
||
return new Promise(function (resolve, reject) {
|
||
api.getUserInfo().then(res=>{
|
||
resolve(res)
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 判断用户是否授权用户信息
|
||
*/
|
||
function isAuthUserInfo() {
|
||
return new Promise(function (resolve, reject) {
|
||
api.getUserInfo().then(res=>{
|
||
resolve(Boolean(res && res.headimg))
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 判断用户是否绑定手机
|
||
*/
|
||
function isBindMobile() {
|
||
return new Promise(function (resolve, reject) {
|
||
api.getUserInfo().then(res=>{
|
||
resolve(res && res.mobile)
|
||
})
|
||
})
|
||
}
|
||
|
||
|
||
/**
|
||
* 是否开卡
|
||
*/
|
||
function isOpenCard() {
|
||
return new Promise(function (resolve, reject) {
|
||
api.getUserInfo().then(res=>{
|
||
resolve(res && res.card_id)
|
||
})
|
||
})
|
||
}
|
||
|
||
|
||
/**
|
||
* 获得用户位置,返回一个promise,同时间只会跑一个
|
||
* {
|
||
* lat,
|
||
* lng,
|
||
* from_storage //true表示是从缓存里获得, false表示从设置里获得
|
||
* }
|
||
*/
|
||
// function getUserLocation() {
|
||
// return new Promise((resolve, reject) => {
|
||
// // const lat = wx.getStorageSync('u_lat');
|
||
// // const lng = wx.getStorageSync('u_lng');
|
||
// // if (lat && lng) {
|
||
// // return resolve({
|
||
// // lat,
|
||
// // lng,
|
||
// // from_storage: true,
|
||
// // })
|
||
// // } else {
|
||
// const loading = wx.getStorageSync('loading_gps');
|
||
// if (loading) {
|
||
// setTimeout(() => {
|
||
// return resolve(this.getUserLocation());
|
||
// }, 30);
|
||
// return;
|
||
// }
|
||
// wx.setStorageSync('loading_gps', 1)
|
||
// wx.getSetting({
|
||
// success(res) {
|
||
// console.log("======获取用户地理位置=====")
|
||
// console.log(res);
|
||
// wx.getLocation({
|
||
// type: 'gcj02',
|
||
// success: function (res) {
|
||
// var lat = res.latitude
|
||
// var lng = res.longitude
|
||
// wx.setStorageSync('u_lat', lat)
|
||
// wx.setStorageSync('u_lng', lng)
|
||
// wx.removeStorageSync('loading_gps')
|
||
// return resolve({
|
||
// lat,
|
||
// lng,
|
||
// from_storage: false,
|
||
// })
|
||
// },
|
||
// fail: function (res) {
|
||
// console.log("用户没有授权地理位置")
|
||
// console.log(res);
|
||
// wx.removeStorageSync('loading_gps')
|
||
// wx.showModal({
|
||
// title: '提示',
|
||
// content: '请先授权地理位置权限',
|
||
// showCancel: false,
|
||
// success(res) {
|
||
// if (res.confirm) {
|
||
// console.log('用户点击确定')
|
||
// wx.openSetting({
|
||
// success(res) {
|
||
// console.log(res.authSetting)
|
||
|
||
// }
|
||
// })
|
||
// } else if (res.cancel) {
|
||
// console.log('用户点击取消')
|
||
// }
|
||
// }
|
||
// })
|
||
// return reject('用户没有授权地理位置')
|
||
// }
|
||
// })
|
||
// },
|
||
// })
|
||
// // }
|
||
// })
|
||
// }
|
||
|
||
|
||
const getUserLocation = function () {
|
||
return new Promise(function (resolve, reject) {
|
||
wx.getLocation({
|
||
type: 'wgs84',
|
||
success(res) {
|
||
app.setStorage('u_lat', res.latitude)
|
||
app.setStorage('u_lng', res.longitude)
|
||
resolve({
|
||
lat:res.latitude,
|
||
lng:res.longitude,
|
||
from_storage: false,
|
||
});
|
||
},
|
||
fail(res) {
|
||
app.printErrorClient('userLocationFail', ['错误信息:'+ JSON.stringify(res)])
|
||
_ModelForGetLocationModel(resolve, reject);
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
|
||
function _ModelForGetLocationModel(resolve, reject) {
|
||
wx.showModal({
|
||
title: '',
|
||
content: '检测到你未打开地理位置的权限,是否前往开启',
|
||
confirmText: '前往开启',
|
||
showCancel:false,
|
||
success(res) {
|
||
if (res.confirm) {
|
||
//成功的时候只执行一次(通过手动的时候就没执行了)
|
||
wx.openSetting({
|
||
success(res) {
|
||
wx.authorize({
|
||
scope: 'scope.userLocation',
|
||
success() {
|
||
// resolve("toggle");
|
||
wx.getLocation({
|
||
type: 'wgs84',
|
||
success(res) {
|
||
app.setStorage('u_lat', res.latitude)
|
||
app.setStorage('u_lng', res.longitude)
|
||
resolve({
|
||
lat:res.latitude,
|
||
lng:res.longitude,
|
||
from_storage: false,
|
||
});
|
||
}
|
||
})
|
||
},
|
||
fail() {
|
||
app.printErrorClient('userLocationFail',['错误信息:打开打开地理位置的权限,未设置'])
|
||
_ModelForGetLocationModel(resolve, reject);
|
||
}
|
||
})
|
||
}
|
||
})
|
||
} else if (res.cancel) {
|
||
app.printErrorClient('userLocationFail',['错误信息:用户点击取消'])
|
||
reject()
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
|
||
module.exports = {
|
||
getAjaxUserInfo,
|
||
isAuthUserInfo,
|
||
isBindMobile,
|
||
isOpenCard,
|
||
getUserLocation
|
||
// getLocation
|
||
} |