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

222 lines
5.8 KiB

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