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

220 lines
5.7 KiB

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