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

245 lines
6.6 KiB

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