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

226 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
10 months ago
8 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, 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. console.log(pages)
  38. let url = '/pages/userCenter/login/login'
  39. if(page!= url) {
  40. console.log('当前页面')
  41. console.log(type)
  42. if(type) {
  43. url = url+'?autoBack=yes'
  44. // uni.redirectTo({
  45. // url
  46. // })
  47. }
  48. uni.navigateTo({
  49. url
  50. })
  51. }
  52. },
  53. // 为非平台学员开一条路
  54. updateNonPlatformStudent(state, val) {
  55. state.NonPlatformStudent = val
  56. }
  57. },
  58. actions: {
  59. getCity({commit}) {
  60. return new Promise((resolve, reject) => {
  61. // #ifdef APP-PLUS
  62. getCityInfo(resolve, reject,commit)
  63. // #endif
  64. // #ifdef H5
  65. console.log('h5经纬度的代码')
  66. uni.getLocation({
  67. type: 'wgs84',
  68. // type: 'gcj02',
  69. success: function(res) {
  70. console.log('当前位置的经度:' + res.longitude);
  71. // console.log('当前位置的纬度:' + res);
  72. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  73. }
  74. })
  75. // #endif
  76. })
  77. },
  78. checkLogin({state, commit}) {
  79. let token = state.vuex_loginInfo.accessToken
  80. if(!token) {
  81. commit('goLogin')
  82. return false
  83. }
  84. return true
  85. },
  86. // 获取用户信息
  87. async getUserInfo({state, commit}) {
  88. let token = state.vuex_loginInfo.accessToken
  89. if(!token) return
  90. const http = uni.$u.http
  91. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  92. const res = await http.get('app/student-record/get')
  93. if(res.tenantId) {
  94. uni.$u.http.setConfig((config) => {
  95. config.header['tenant-id'] = res.tenantId
  96. return config
  97. })
  98. }
  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. try{
  166. requestSingleFreshLocation().then(res=>{
  167. let result = res.location
  168. if(result.latitude===0) {
  169. console.log('gogogo来了吗?')
  170. uni.getLocation({
  171. type: 'wgs84',
  172. type: 'gcj02',
  173. success: function(res) {
  174. console.log('只为弹出权限当前位置的经度:' + res.longitude);
  175. // console.log('当前位置的纬度:' + res);
  176. },
  177. })
  178. }else {
  179. console.log('腾讯云经纬度')
  180. console.log(result)
  181. let obj = {
  182. lat: result.latitude,
  183. lng: result.longitude,
  184. city: result.city,
  185. // cityCode: result.code||result.cityCode,
  186. province: result.province,
  187. district: result.district,
  188. }
  189. commit('update_vuex_cityInfo', obj)
  190. resolve(obj)
  191. }
  192. })
  193. }catch(e){
  194. console.log('来到这是里了吗?')
  195. uni.getLocation({
  196. // type: 'wgs84',
  197. type: 'gcj02',
  198. success: function(res) {
  199. console.log('当前位置的经度:' + res.longitude);
  200. // console.log('当前位置的纬度:' + res);
  201. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  202. },
  203. fail(e) {
  204. console.log(e)
  205. uni.hideLoading()
  206. }
  207. })
  208. }
  209. }