map.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // #ifdef MP-TOUTIAO
  32. //非业务城市
  33. reject(2);
  34. // #endif
  35. // #ifdef MP-ALIPAY
  36. util.request(api.OrderBookGetShop, {
  37. lng: longitude,
  38. lat: latitude
  39. })
  40. .then((res) => {
  41. if (res.errno == 0) {
  42. var addressInfo = {
  43. address: res.data.address,
  44. lng: longitude,
  45. lat: latitude,
  46. shopId: res.data.shopId,
  47. city: res.data.city,
  48. shopName: res.data.shopName
  49. };
  50. uni.setStorageSync('addressInfo', addressInfo);
  51. resolve(addressInfo);
  52. } else {
  53. reject(3);
  54. }
  55. })
  56. .catch((res) => {
  57. reject(3);
  58. });
  59. // #endif
  60. } else {
  61. //判断是否有门店服务
  62. util.request(api.OrderBookGetShop, {
  63. lng: longitude,
  64. lat: latitude
  65. })
  66. .then((res) => {
  67. if (res.errno == 0) {
  68. var addressInfo = {
  69. address: address,
  70. lng: longitude,
  71. lat: latitude,
  72. shopId: res.data.shopId,
  73. city: city,
  74. shopName: res.data.shopName
  75. };
  76. uni.setStorageSync('addressInfo', addressInfo);
  77. resolve(addressInfo);
  78. } else {
  79. reject(3);
  80. }
  81. })
  82. .catch((res) => {
  83. reject(3);
  84. });
  85. }
  86. console.info(res);
  87. },
  88. fail: function (res) {
  89. console.log('fail' + JSON.stringify(res));
  90. reject(3);
  91. }
  92. });
  93. });
  94. }
  95. //判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权
  96. function getUserLocation() {
  97. return new Promise(function (resolve, reject) {
  98. let that = this;
  99. uni.getSetting({
  100. success: (res) => {
  101. console.log(JSON.stringify(res));
  102. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  103. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  104. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  105. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  106. uni.showModal({
  107. title: '请求授权当前位置',
  108. content: '需要获取您的地理位置,请确认授权',
  109. success: function (res) {
  110. if (res.cancel) {
  111. reject(false);
  112. } else if (res.confirm) {
  113. uni.openSetting({
  114. success: function (dataAu) {
  115. if (dataAu.authSetting['scope.userLocation'] == true) {
  116. resolve(true);
  117. } else {
  118. reject(false);
  119. }
  120. }
  121. });
  122. }
  123. }
  124. });
  125. } else {
  126. //调用wx.getLocation2的API
  127. resolve(true);
  128. }
  129. }
  130. });
  131. });
  132. }
  133. // 获取定位当前位置的经纬度
  134. function getLocation2() {
  135. return new Promise(function (resolve, reject) {
  136. uni.getLocation({
  137. type: 'wgs84',
  138. success: function (res) {
  139. console.info(res);
  140. app.globalData.lat = res.latitude; //
  141. app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局
  142. app.globalData.currentLocationName = res.city;
  143. app.globalData.city = res.city;
  144. resolve(true);
  145. },
  146. fail: function (res) {
  147. console.log('fail' + JSON.stringify(res));
  148. reject(false);
  149. }
  150. });
  151. });
  152. }
  153. module.exports = {
  154. getCity: getCity,
  155. getLocation: getLocation,
  156. getUserLocation: getUserLocation,
  157. getLocation2: getLocation2
  158. };