学员端小程序
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.

120 lines
3.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
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. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  7. },
  8. mutations: {
  9. update_vuex_cityInfo(state, payload) {
  10. state.vuex_cityInfo = payload
  11. uni.setStorageSync('vuex_cityInfo', payload);
  12. },
  13. update_vuex_loginInfo(state, payload) {
  14. state.vuex_loginInfo = payload
  15. uni.setStorageSync('vuex_loginInfo', payload);
  16. },
  17. update_vuex_userInfo(state, payload) {
  18. state.vuex_userInfo = payload
  19. uni.setStorageSync('vuex_userInfo', payload);
  20. }
  21. },
  22. actions: {
  23. getCity({commit}) {
  24. return new Promise((resolve, reject) => {
  25. // #ifdef APP-PLUS||H5
  26. getCityInfo(resolve, reject,commit)
  27. // #endif
  28. // #ifdef MP-WEIXIN
  29. uni.authorize({
  30. scope: 'scope.userLocation',
  31. success() {
  32. getCityInfo(resolve, reject,commit)
  33. },
  34. fail: function(res4) {
  35. uni.showModal({
  36. title: '提示',
  37. content: '小程序想要获取您的地里位置',
  38. success: function(res) {
  39. if (res.confirm) {
  40. uni.openSetting({
  41. success(res) {
  42. getCityInfo(resolve, reject,commit)
  43. }
  44. });
  45. } else if (res.cancel) {
  46. console.log('用户点击取消');
  47. }
  48. }
  49. })
  50. },
  51. })
  52. // #endif
  53. })
  54. },
  55. // 获取用户信息
  56. async getUserInfo({commit}) {
  57. const http = uni.$u.http
  58. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  59. const res = await http.get('member/user/get',config)
  60. commit('update_vuex_userInfo',res.data)
  61. },
  62. // 刷新token
  63. async refreshToken({state}) {
  64. const http = uni.$u.http
  65. let refreshToken = state.vuex_loginInfo.refreshToken
  66. const res = await http.post('member/auth/refresh-token',{refreshToken}, config)
  67. }
  68. }
  69. }
  70. export default user
  71. function getCityInfo(resolve, reject, commit) {
  72. uni.showLoading({
  73. title: '加载中...'
  74. })
  75. var qqmapKey = new qqmapWx({
  76. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  77. })
  78. uni.getLocation({
  79. type: 'wgs84',
  80. // type: 'gcj02',
  81. success: function(res) {
  82. console.log('当前位置的经度:' + res.longitude);
  83. console.log('当前位置的纬度:' + res.latitude);
  84. qqmapKey.reverseGeocoder({
  85. location: {
  86. latitude: res.latitude,
  87. longitude: res.longitude
  88. },
  89. success(res2) {
  90. // console.log('城市信息')
  91. // console.log(res2.result)
  92. let result = res2.result
  93. let obj = {
  94. latitude: res.latitude,
  95. longitude: res.longitude,
  96. city: result.address_component.city,
  97. cityCode: result.ad_info.adcode,
  98. province: result.address_component.province,
  99. district: result.address_component.district
  100. }
  101. commit('update_vuex_cityInfo', obj)
  102. resolve(obj)
  103. uni.hideLoading();
  104. },
  105. fail: function(res3) {
  106. reject(res3)
  107. uni.hideLoading();
  108. },
  109. })
  110. },
  111. fail(e) {
  112. console.log(e)
  113. }
  114. })
  115. }