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.

229 lines
6.3 KiB

9 months ago
3 weeks ago
9 months ago
9 months ago
9 months ago
3 weeks ago
9 months ago
3 weeks ago
9 months ago
3 weeks ago
9 months ago
3 weeks ago
3 weeks ago
9 months ago
3 weeks ago
9 months ago
3 weeks ago
9 months ago
3 weeks ago
9 months ago
  1. const user = {
  2. state: {
  3. identity: uni.getStorageSync('identity')?uni.getStorageSync('identity'):'理论教练',
  4. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: ''},
  5. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {user:{}},
  6. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  7. vuex_TenantId: uni.getStorageSync('vuex_TenantId') ? uni.getStorageSync('vuex_TenantId') : '',
  8. vuex_role: {
  9. // schoolManager
  10. manager: '校长',
  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.post('business/coach/getCoachInfo',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
  95. getCityInfo(resolve, reject,commit)
  96. // #endif
  97. // #ifdef H5
  98. console.log('h5经纬度的代码')
  99. uni.getLocation({
  100. type: 'wgs84',
  101. // type: 'gcj02',
  102. success: function(res) {
  103. console.log('当前位置的经度:' + res.longitude);
  104. // console.log('当前位置的纬度:' + res);
  105. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  106. }
  107. })
  108. // #endif
  109. })
  110. },
  111. }
  112. }
  113. function getCityInfo(resolve, reject, commit) {
  114. // import { requestSingleFreshLocation } from '@/common/js/qqLatLng.js'
  115. let qqLatLng = require('@/common/js/qqLatLng.js')
  116. try{
  117. uni.showLoading({
  118. title: '正在更新位置...'
  119. })
  120. qqLatLng.requestSingleFreshLocation().then(res=>{
  121. let result = res.location
  122. if(result.latitude===0) {
  123. console.log('gogogo来了吗没有获取到经纬度?')
  124. // openGps()
  125. uni.getLocation({
  126. // type: 'wgs84',
  127. type: 'gcj02',
  128. success: function(res) {
  129. console.log('只为弹出权限当前位置的经度:' + res.longitude);
  130. // console.log('当前位置的纬度:' + res);
  131. getCityInfo(resolve, reject, commit)
  132. uni.hideLoading()
  133. },
  134. fail() {
  135. uni.showToast({
  136. title: '您的定位权限已关闭,请手动开启定位权限',
  137. icon: 'none'
  138. })
  139. uni.hideLoading()
  140. }
  141. })
  142. }else {
  143. console.log('腾讯云经纬度')
  144. console.log(result)
  145. let obj = {
  146. lat: result.latitude,
  147. lng: result.longitude,
  148. city: result.city,
  149. // cityCode: result.code||result.cityCode,
  150. province: result.province,
  151. district: result.district,
  152. name: result.name,
  153. }
  154. commit('update_vuex_cityInfo', obj)
  155. resolve(obj)
  156. uni.hideLoading()
  157. }
  158. })
  159. }catch(e){
  160. console.log('来到这是里了吗?腾讯云经纬度了')
  161. uni.hideLoading()
  162. uni.getLocation({
  163. // type: 'wgs84',
  164. type: 'gcj02',
  165. success: function(res) {
  166. console.log('当前位置的经度:' + res.longitude);
  167. // console.log('当前位置的纬度:' + res);
  168. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  169. },
  170. fail(e) {
  171. console.log(e)
  172. uni.hideLoading()
  173. }
  174. })
  175. }
  176. }
  177. // h5高德地图
  178. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  179. uni.showLoading({
  180. title: '正在更新位置...'
  181. })
  182. uni.request({
  183. method: 'GET',
  184. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  185. data: {
  186. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  187. location: `${longitude},${latitude}`,
  188. output: 'JSON'
  189. },
  190. success: (res) => {
  191. console.log(res) // 数据结构见下方
  192. let result = res.data.regeocode.addressComponent
  193. let obj = {
  194. lat: latitude,
  195. lng: longitude,
  196. city: result.city,
  197. cityCode: result.adcode,
  198. province: result.province,
  199. district: result.district
  200. }
  201. commit('update_vuex_cityInfo', obj)
  202. console.log('h5获得的最终位置对象')
  203. console.log(obj)
  204. resolve(obj)
  205. uni.hideLoading()
  206. },
  207. fail: res => {
  208. reject(new Error('获取地理位置信息失败'))
  209. uni.hideLoading()
  210. }
  211. })
  212. }
  213. export default user