123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- var api = require('../config/api.js');
- var area = require('./area.js');
- var util = require('./util.js');
- var app = getApp();
- function getCity() {
- return new Promise(function (resolve, reject) {
- //调用wx.getLocation的API
- getLocation()
- .then((res) => {
- resolve(res);
- })
- .catch((res) => {
- reject(res);
- });
- });
- }
- // 微信获得经纬度
- function getLocation() {
- return new Promise(function (resolve, reject) {
- uni.getLocation({
- // type: 'wgs84',
- success: function (res) {
- var latitude = res.latitude;
- var longitude = res.longitude;
- console.info(res);
- ////reject内容 1:未授权(拿默认城市的默认门店) 2:非业务城市 不在服务范围(拿默认城市的默认门店) 3:异常解析()
- let city = res.city;
- let address = res.city;
- console.info(city);
- if (!area.isBusinessCity(city)) {
-
- // #ifdef MP-TOUTIAO
- //非业务城市
- reject(2);
- // #endif
-
- // #ifdef MP-ALIPAY
-
- util.request(api.OrderBookGetShop, {
- lng: longitude,
- lat: latitude
- })
- .then((res) => {
- if (res.errno == 0) {
- var addressInfo = {
- address: res.data.address,
- lng: longitude,
- lat: latitude,
- shopId: res.data.shopId,
- city: res.data.city,
- shopName: res.data.shopName
- };
- uni.setStorageSync('addressInfo', addressInfo);
- resolve(addressInfo);
- } else {
- reject(3);
- }
- })
- .catch((res) => {
- reject(3);
- });
- // #endif
- } else {
- //判断是否有门店服务
- util.request(api.OrderBookGetShop, {
- lng: longitude,
- lat: latitude
- })
- .then((res) => {
- if (res.errno == 0) {
- var addressInfo = {
- address: address,
- lng: longitude,
- lat: latitude,
- shopId: res.data.shopId,
- city: city,
- shopName: res.data.shopName
- };
- uni.setStorageSync('addressInfo', addressInfo);
- resolve(addressInfo);
- } else {
- reject(3);
- }
- })
- .catch((res) => {
- reject(3);
- });
- }
- console.info(res);
- },
- fail: function (res) {
- console.log('fail' + JSON.stringify(res));
- reject(3);
- }
- });
- });
- }
- //判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
- function getUserLocation() {
- return new Promise(function (resolve, reject) {
- let that = this;
- uni.getSetting({
- success: (res) => {
- console.log(JSON.stringify(res));
- // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
- // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
- // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
- if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
- uni.showModal({
- title: '请求授权当前位置',
- content: '需要获取您的地理位置,请确认授权',
- success: function (res) {
- if (res.cancel) {
- reject(false);
- } else if (res.confirm) {
- uni.openSetting({
- success: function (dataAu) {
- if (dataAu.authSetting['scope.userLocation'] == true) {
- resolve(true);
- } else {
- reject(false);
- }
- }
- });
- }
- }
- });
- } else {
- //调用wx.getLocation2的API
- resolve(true);
- }
- }
- });
- });
- }
- // 获取定位当前位置的经纬度
- function getLocation2() {
- return new Promise(function (resolve, reject) {
- uni.getLocation({
- type: 'wgs84',
- success: function (res) {
- console.info(res);
- app.globalData.lat = res.latitude; //
- app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局
- app.globalData.currentLocationName = res.city;
- app.globalData.city = res.city;
- resolve(true);
- },
- fail: function (res) {
- console.log('fail' + JSON.stringify(res));
- reject(false);
- }
- });
- });
- }
- module.exports = {
- getCity: getCity,
- getLocation: getLocation,
- getUserLocation: getUserLocation,
- getLocation2: getLocation2
- };
|