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

180 lines
5.0 KiB

1 year ago
10 months ago
11 months ago
10 months ago
1 year ago
1 year ago
1 year ago
10 months ago
11 months ago
1 year ago
1 year ago
10 months 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') : {user:{}},
  7. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  8. vuex_TenantId: uni.getStorageSync('vuex_TenantId') ? uni.getStorageSync('vuex_TenantId') : '',
  9. vuex_role: {
  10. schoolManager: '校长',
  11. schoolFinance: '驾校财务',
  12. coach: '实操教练',
  13. examSiteCoach: '考场模拟教练',
  14. bookingSimulationTeacher: '模拟器老师'
  15. },
  16. apiOk: true
  17. },
  18. mutations: {
  19. // 更新用户身份
  20. upDateIdentity(state, val) {
  21. state.identity = val
  22. uni.setStorageSync('identity', val);
  23. },
  24. update_vuex_cityInfo(state, payload) {
  25. state.vuex_cityInfo = payload
  26. uni.setStorageSync('vuex_cityInfo', payload);
  27. },
  28. update_vuex_loginInfo(state, payload) {
  29. state.vuex_loginInfo = payload
  30. uni.setStorageSync('vuex_loginInfo', payload);
  31. },
  32. update_vuex_userInfo(state, payload) {
  33. state.vuex_userInfo = payload
  34. uni.setStorageSync('vuex_userInfo', payload);
  35. },
  36. upDateTenantId(state, val) {
  37. state.vuex_TenantId = val
  38. uni.setStorageSync('vuex_TenantId', val);
  39. uni.$u.http.setConfig((config) => {
  40. config.header['tenant-id'] = val
  41. return config
  42. })
  43. },
  44. update_apiOk(state, payload) {
  45. state.apiOk = payload
  46. },
  47. goLogin(state) {
  48. uni.clearStorageSync()
  49. state.vuex_loginInfo = {}
  50. state.vuex_userInfo = {}
  51. state.vuex_TenantId = ''
  52. uni.navigateTo({
  53. url: '/pages/login/login'
  54. })
  55. }
  56. },
  57. actions: {
  58. // 获取用户信息
  59. async getUserInfo({commit}) {
  60. const http = uni.$u.http
  61. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  62. const res = await http.get('system/auth/get-permission-info-app',config)
  63. commit('update_vuex_userInfo',res.data)
  64. },
  65. // 刷新token
  66. async refreshToken({state, commit}) {
  67. return new Promise(async(resolve, reject)=>{
  68. if(!state.apiOk) {
  69. return state.refreshTokenFn
  70. }
  71. commit('update_apiOk',false)
  72. state.refreshTokenFn = null
  73. const http = uni.$u.http
  74. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  75. let refreshToken = state.vuex_loginInfo.refreshToken
  76. state.refreshTokenFn = await http.post('system/auth/refresh-token?refreshToken='+ refreshToken, config)
  77. if(state.refreshTokenFn.data||state.refreshTokenFn.data.accessToken) {
  78. commit('update_apiOk',true)
  79. commit('update_vuex_loginInfo',state.refreshTokenFn.data)
  80. uni.$u.http.setConfig((config) => {
  81. config.header.Authorization = 'Bearer ' + state.refreshTokenFn.data.accessToken
  82. return config
  83. })
  84. resolve(state.refreshTokenFn)
  85. }else {
  86. commit('update_apiOk',true)
  87. commit('goLogin')
  88. reject('刷新token失败了')
  89. }
  90. })
  91. },
  92. getCity({commit}) {
  93. return new Promise((resolve, reject) => {
  94. // #ifdef APP-PLUS||H5
  95. getCityInfo(resolve, reject,commit)
  96. // #endif
  97. // #ifdef MP-WEIXIN
  98. uni.authorize({
  99. scope: 'scope.userLocation',
  100. success() {
  101. getCityInfo(resolve, reject,commit)
  102. },
  103. fail: function(res4) {
  104. uni.showModal({
  105. title: '提示',
  106. content: '小程序想要获取您的地里位置',
  107. success: function(res) {
  108. if (res.confirm) {
  109. uni.openSetting({
  110. success(res) {
  111. getCityInfo(resolve, reject,commit)
  112. }
  113. });
  114. } else if (res.cancel) {
  115. console.log('用户点击取消');
  116. }
  117. }
  118. })
  119. },
  120. })
  121. // #endif
  122. })
  123. },
  124. }
  125. }
  126. function getCityInfo(resolve, reject, commit) {
  127. uni.showLoading({
  128. title: '加载中...'
  129. })
  130. var qqmapKey = new qqmapWx({
  131. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  132. })
  133. uni.getLocation({
  134. type: 'wgs84',
  135. // type: 'gcj02',
  136. success: function(res) {
  137. console.log('当前位置的经度:' + res.longitude);
  138. console.log('当前位置的纬度:' + res.latitude);
  139. qqmapKey.reverseGeocoder({
  140. location: {
  141. latitude: res.latitude,
  142. longitude: res.longitude
  143. },
  144. success(res2) {
  145. // console.log('城市信息')
  146. // console.log(res2.result)
  147. let result = res2.result
  148. let obj = {
  149. latitude: res.latitude,
  150. longitude: res.longitude,
  151. city: result.address_component.city,
  152. cityCode: result.ad_info.adcode,
  153. province: result.address_component.province,
  154. district: result.address_component.district
  155. }
  156. commit('update_vuex_cityInfo', obj)
  157. resolve(obj)
  158. uni.hideLoading();
  159. },
  160. fail: function(res3) {
  161. reject(res3)
  162. uni.hideLoading();
  163. },
  164. })
  165. },
  166. fail(e) {
  167. console.log(e)
  168. }
  169. })
  170. }
  171. export default user