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.

181 lines
5.0 KiB

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