江西小程序管理端
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.

156 lines
4.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import addApi from '../../common/sdk/qqmap-wx-jssdk.min.js'; // 引入
  2. const user = {
  3. state: {
  4. identity: uni.getStorageSync('identity')?uni.getStorageSync('identity'):1,
  5. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'},
  6. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
  7. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  8. vuex_TenantId: uni.getStorageSync('vuex_TenantId') ? uni.getStorageSync('vuex_TenantId') : '',
  9. },
  10. mutations: {
  11. // 更新用户身份
  12. upDateIdentity(state, val) {
  13. state.identity = val
  14. uni.setStorageSync('identity', val);
  15. },
  16. update_vuex_cityInfo(state, payload) {
  17. state.vuex_cityInfo = payload
  18. uni.setStorageSync('vuex_cityInfo', payload);
  19. },
  20. update_vuex_loginInfo(state, payload) {
  21. state.vuex_loginInfo = payload
  22. uni.setStorageSync('vuex_loginInfo', payload);
  23. },
  24. update_vuex_userInfo(state, payload) {
  25. state.vuex_userInfo = payload
  26. uni.setStorageSync('vuex_userInfo', payload);
  27. },
  28. upDateTenantId(state, val) {
  29. state.vuex_TenantId = val
  30. uni.setStorageSync('vuex_TenantId', val);
  31. uni.$u.http.setConfig((config) => {
  32. config.header['tenant-id'] = val
  33. return config
  34. })
  35. },
  36. goLogin(state) {
  37. uni.clearStorageSync()
  38. state.vuex_loginInfo = {}
  39. state.vuex_userInfo = {}
  40. state.vuex_TenantId = ''
  41. uni.navigateTo({
  42. url: '/pages/userCenter/login/loginByPhone'
  43. })
  44. }
  45. },
  46. actions: {
  47. // 获取用户信息
  48. async getUserInfo({commit}) {
  49. const http = uni.$u.http
  50. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  51. const res = await http.get('system/user/profile/get',config)
  52. commit('update_vuex_userInfo',res.data)
  53. },
  54. // 刷新token
  55. async refreshToken({state, commit}) {
  56. const http = uni.$u.http
  57. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  58. let refreshToken = state.vuex_loginInfo.refreshToken
  59. const res = await http.post('member/auth/refresh-token',{refreshToken}, config)
  60. console.log('刷新token结果')
  61. console.log(res)
  62. commit('update_vuex_loginInfo',res.data)
  63. uni.$u.http.setConfig((config) => {
  64. config.header.Authorization = 'Bearer ' + res.data.accessToken
  65. return config
  66. })
  67. },
  68. getCity({commit}) {
  69. return new Promise((resolve, reject) => {
  70. // #ifdef APP-PLUS||H5
  71. getCityInfo(resolve, reject,commit)
  72. // #endif
  73. // #ifdef MP-WEIXIN
  74. uni.authorize({
  75. scope: 'scope.userLocation',
  76. success() {
  77. getCityInfo(resolve, reject,commit)
  78. },
  79. fail: function(res4) {
  80. uni.showModal({
  81. title: '提示',
  82. content: '小程序想要获取您的地里位置',
  83. success: function(res) {
  84. if (res.confirm) {
  85. uni.openSetting({
  86. success(res) {
  87. getCityInfo(resolve, reject,commit)
  88. }
  89. });
  90. } else if (res.cancel) {
  91. console.log('用户点击取消');
  92. }
  93. }
  94. })
  95. },
  96. })
  97. // #endif
  98. })
  99. },
  100. }
  101. }
  102. function getCityInfo(resolve, reject, commit) {
  103. uni.showLoading({
  104. title: '加载中...'
  105. })
  106. var qqmapKey = new qqmapWx({
  107. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  108. })
  109. uni.getLocation({
  110. type: 'wgs84',
  111. // type: 'gcj02',
  112. success: function(res) {
  113. console.log('当前位置的经度:' + res.longitude);
  114. console.log('当前位置的纬度:' + res.latitude);
  115. qqmapKey.reverseGeocoder({
  116. location: {
  117. latitude: res.latitude,
  118. longitude: res.longitude
  119. },
  120. success(res2) {
  121. // console.log('城市信息')
  122. // console.log(res2.result)
  123. let result = res2.result
  124. let obj = {
  125. latitude: res.latitude,
  126. longitude: res.longitude,
  127. city: result.address_component.city,
  128. cityCode: result.ad_info.adcode,
  129. province: result.address_component.province,
  130. district: result.address_component.district
  131. }
  132. commit('update_vuex_cityInfo', obj)
  133. resolve(obj)
  134. uni.hideLoading();
  135. },
  136. fail: function(res3) {
  137. reject(res3)
  138. uni.hideLoading();
  139. },
  140. })
  141. },
  142. fail(e) {
  143. console.log(e)
  144. }
  145. })
  146. }
  147. export default user