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

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