学员端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
  1. import qqmapWx from '../../common/sdk/qqmap-wx-jssdk.min.js'; // 引入
  2. const user = {
  3. state: {
  4. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'},
  5. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
  6. },
  7. mutations: {
  8. update_vuex_cityInfo(state, payload) {
  9. state.vuex_cityInfo = payload
  10. uni.setStorageSync('vuex_cityInfo', payload);
  11. }
  12. },
  13. actions: {
  14. getCity({commit}) {
  15. return new Promise((resolve, reject) => {
  16. // #ifdef APP-PLUS||H5
  17. getCityInfo(resolve, reject,commit)
  18. // #endif
  19. // #ifdef MP-WEIXIN
  20. uni.authorize({
  21. scope: 'scope.userLocation',
  22. success() {
  23. getCityInfo(resolve, reject,commit)
  24. },
  25. fail: function(res4) {
  26. uni.showModal({
  27. title: '提示',
  28. content: '小程序想要获取您的地里位置',
  29. success: function(res) {
  30. if (res.confirm) {
  31. uni.openSetting({
  32. success(res) {
  33. getCityInfo(resolve, reject,commit)
  34. }
  35. });
  36. } else if (res.cancel) {
  37. console.log('用户点击取消');
  38. }
  39. }
  40. })
  41. },
  42. })
  43. // #endif
  44. })
  45. },
  46. }
  47. }
  48. export default user
  49. function getCityInfo(resolve, reject, commit) {
  50. uni.showLoading({
  51. title: '加载中...'
  52. })
  53. var qqmapKey = new qqmapWx({
  54. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  55. })
  56. uni.getLocation({
  57. type: 'wgs84',
  58. // type: 'gcj02',
  59. success: function(res) {
  60. console.log('当前位置的经度:' + res.longitude);
  61. console.log('当前位置的纬度:' + res.latitude);
  62. qqmapKey.reverseGeocoder({
  63. location: {
  64. latitude: res.latitude,
  65. longitude: res.longitude
  66. },
  67. success(res2) {
  68. // console.log('城市信息')
  69. // console.log(res2.result)
  70. let result = res2.result
  71. let obj = {
  72. latitude: res.latitude,
  73. longitude: res.longitude,
  74. city: result.address_component.city,
  75. cityCode: result.ad_info.adcode,
  76. province: result.address_component.province,
  77. district: result.address_component.district
  78. }
  79. commit('update_vuex_cityInfo', obj)
  80. resolve(obj)
  81. uni.hideLoading();
  82. },
  83. fail: function(res3) {
  84. reject(res3)
  85. uni.hideLoading();
  86. },
  87. })
  88. },
  89. fail(e) {
  90. console.log(e)
  91. }
  92. })
  93. }