map.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var api = require('../config/api.js');
  2. var area = require('./area.js');
  3. var util = require('./util.js');
  4. var app = getApp();
  5. function getCity() {
  6. return new Promise(function (resolve, reject) {
  7. //调用wx.getLocation的API
  8. getLocation()
  9. .then((res) => {
  10. resolve(res);
  11. })
  12. .catch((res) => {
  13. reject(res);
  14. });
  15. });
  16. }
  17. // 微信获得经纬度
  18. function getLocation() {
  19. return new Promise(function (resolve, reject) {
  20. uni.getLocation({
  21. type: 'wgs84',
  22. success: function (res) {
  23. var latitude = res.latitude;
  24. var longitude = res.longitude;
  25. console.info(res);
  26. ////reject内容 1:未授权(拿默认城市的默认门店) 2:非业务城市 不在服务范围(拿默认城市的默认门店) 3:异常解析()
  27. let city = res.city;
  28. let address = res.city;
  29. console.info(city);
  30. if (!area.isBusinessCity(city)) {
  31. //非业务城市
  32. reject(2);
  33. } else {
  34. //判断是否有门店服务
  35. util.request(api.OrderBookGetShop, {
  36. lng: longitude,
  37. lat: latitude
  38. })
  39. .then((res) => {
  40. if (res.errno == 0) {
  41. var addressInfo = {
  42. address: address,
  43. lng: longitude,
  44. lat: latitude,
  45. shopId: res.data.shopId,
  46. city: city,
  47. shopName: res.data.shopName
  48. };
  49. uni.setStorageSync('addressInfo', addressInfo);
  50. resolve(addressInfo);
  51. } else {
  52. reject(3);
  53. }
  54. })
  55. .catch((res) => {
  56. reject(3);
  57. });
  58. }
  59. console.info(res);
  60. },
  61. fail: function (res) {
  62. console.log('fail' + JSON.stringify(res));
  63. reject(3);
  64. }
  65. });
  66. });
  67. }
  68. //判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
  69. function getUserLocation() {
  70. let that = this;
  71. uni.getSetting({
  72. success: (res) => {
  73. console.log(JSON.stringify(res));
  74. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  75. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  76. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  77. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  78. uni.showModal({
  79. title: '请求授权当前位置',
  80. content: '需要获取您的地理位置,请确认授权',
  81. success: function (res) {
  82. if (res.cancel) {
  83. uni.showToast({
  84. title: '拒绝授权',
  85. icon: 'none',
  86. duration: 1000
  87. });
  88. } else if (res.confirm) {
  89. uni.openSetting({
  90. success: function (dataAu) {
  91. if (dataAu.authSetting['scope.userLocation'] == true) {
  92. uni.showToast({
  93. title: '授权成功',
  94. icon: 'success',
  95. duration: 1000
  96. });
  97. //再次授权,调用wx.getLocation2的API
  98. that.getLocation2();
  99. } else {
  100. uni.showToast({
  101. title: '授权失败',
  102. icon: 'none',
  103. duration: 1000
  104. });
  105. }
  106. }
  107. });
  108. }
  109. }
  110. });
  111. } else if (res.authSetting['scope.userLocation'] == undefined) {
  112. //调用wx.getLocation2的API
  113. that.getLocation2();
  114. } else {
  115. //调用wx.getLocation2的API
  116. that.getLocation2();
  117. }
  118. }
  119. });
  120. }
  121. // 获取定位当前位置的经纬度
  122. function getLocation2() {
  123. let that = this;
  124. uni.getLocation({
  125. type: 'wgs84',
  126. success: function (res) {
  127. let latitude = res.latitude;
  128. let longitude = res.longitude;
  129. // app.globalData.lat = res.latitude;//
  130. // app.globalData.lng = res.longitude;//把onload定位时候的经纬度存到全局
  131. console.log(res);
  132. app.globalData.currentLocationName = res.city;
  133. app.globalData.city = res.city;
  134. },
  135. fail: function (res) {
  136. console.log('fail' + JSON.stringify(res));
  137. }
  138. });
  139. }
  140. module.exports = {
  141. getCity: getCity,
  142. getLocation: getLocation,
  143. getUserLocation: getUserLocation,
  144. getLocation2: getLocation2
  145. };