洛阳学员端
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.

199 lines
5.1 KiB

10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
  1. import { httpPrefix } from '../../config/site.config.js';
  2. // #ifdef APP-PLUS
  3. import { requestSingleFreshLocation } from '@/common/js/qqLatLng.js'
  4. // #endif
  5. let apiOk =true
  6. let refreshTokenFn = null
  7. const user = {
  8. state: {
  9. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: ''},
  10. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
  11. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  12. apiOk: true,
  13. NonPlatformStudent: false,
  14. },
  15. mutations: {
  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. update_apiOk(state, payload) {
  29. state.apiOk = payload
  30. },
  31. goLogin(state) {
  32. uni.clearStorageSync()
  33. state.vuex_loginInfo = {}
  34. state.vuex_userInfo = {}
  35. uni.navigateTo({
  36. url: '/pages/userCenter/login/login'
  37. })
  38. },
  39. // 为非平台学员开一条路
  40. updateNonPlatformStudent(state, val) {
  41. state.NonPlatformStudent = val
  42. }
  43. },
  44. actions: {
  45. getCity({commit}) {
  46. return new Promise((resolve, reject) => {
  47. // #ifdef APP-PLUS
  48. getCityInfo(resolve, reject,commit)
  49. // #endif
  50. // #ifdef H5
  51. console.log('h5经纬度的代码')
  52. uni.getLocation({
  53. type: 'wgs84',
  54. // type: 'gcj02',
  55. success: function(res) {
  56. console.log('当前位置的经度:' + res.longitude);
  57. // console.log('当前位置的纬度:' + res);
  58. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  59. },
  60. fail(e) {
  61. console.log(e)
  62. uni.hideLoading()
  63. }
  64. })
  65. // #endif
  66. })
  67. },
  68. checkLogin({state, commit}) {
  69. let token = state.vuex_loginInfo.accessToken
  70. if(!token) {
  71. commit('goLogin')
  72. return false
  73. }
  74. return true
  75. },
  76. // 获取用户信息
  77. async getUserInfo({state, commit}) {
  78. let token = state.vuex_loginInfo.accessToken
  79. if(!token) return
  80. const http = uni.$u.http
  81. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  82. const res = await http.get('app/student-record/get')
  83. if(res.tenantId) {
  84. uni.$u.http.setConfig((config) => {
  85. config.header['tenant-id'] = res.tenantId
  86. return config
  87. })
  88. }
  89. commit('update_vuex_userInfo',res.data)
  90. },
  91. // 刷新token
  92. async refreshToken({state, commit}) {
  93. return new Promise(async(resolve, reject)=>{
  94. if(!apiOk&&refreshTokenFn) {
  95. return refreshTokenFn
  96. }
  97. apiOk = false
  98. refreshTokenFn = null
  99. const http = uni.$u.http
  100. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  101. let refreshToken = state.vuex_loginInfo.refreshToken
  102. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  103. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  104. apiOk = true
  105. commit('update_vuex_loginInfo', refreshTokenFn.data)
  106. uni.$u.http.setConfig((config) => {
  107. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  108. return config
  109. })
  110. resolve(refreshTokenFn)
  111. }else {
  112. apiOk = true
  113. commit('goLogin')
  114. reject('刷新token失败了')
  115. }
  116. })
  117. }
  118. }
  119. }
  120. export default user
  121. // h5高德地图
  122. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  123. uni.request({
  124. method: 'GET',
  125. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  126. data: {
  127. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  128. location: `${longitude},${latitude}`,
  129. output: 'JSON'
  130. },
  131. success: (res) => {
  132. console.log(res) // 数据结构见下方
  133. let result = res.data.regeocode.addressComponent
  134. let obj = {
  135. lat: latitude,
  136. lng: longitude,
  137. city: result.city,
  138. cityCode: result.adcode,
  139. province: result.province,
  140. district: result.district
  141. }
  142. commit('update_vuex_cityInfo', obj)
  143. resolve(obj)
  144. uni.hideLoading()
  145. },
  146. fail: res => {
  147. reject(new Error('获取地理位置信息失败'))
  148. uni.hideLoading()
  149. }
  150. })
  151. }
  152. function getCityInfo(resolve, reject, commit) {
  153. try{
  154. requestSingleFreshLocation().then(res=>{
  155. let result = res.location
  156. console.log('腾讯云经纬度')
  157. console.log(result)
  158. let obj = {
  159. lat: result.latitude,
  160. lng: result.longitude,
  161. city: result.city,
  162. // cityCode: result.code||result.cityCode,
  163. province: result.province,
  164. district: result.district,
  165. }
  166. commit('update_vuex_cityInfo', obj)
  167. })
  168. }catch(e){
  169. console.log('来到这是里了吗?')
  170. uni.getLocation({
  171. // type: 'wgs84',
  172. type: 'gcj02',
  173. success: function(res) {
  174. console.log('当前位置的经度:' + res.longitude);
  175. // console.log('当前位置的纬度:' + res);
  176. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  177. },
  178. fail(e) {
  179. console.log(e)
  180. uni.hideLoading()
  181. }
  182. })
  183. }
  184. }