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

237 lines
6.0 KiB

10 months ago
  1. import qqmapWx from '../../common/sdk/qqmap-wx-jssdk.min.js'; // 引入
  2. import { httpPrefix } from '../../config/site.config.js';
  3. let apiOk =true
  4. let refreshTokenFn = null
  5. const user = {
  6. state: {
  7. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'},
  8. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
  9. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  10. apiOk: true,
  11. NonPlatformStudent: false,
  12. },
  13. mutations: {
  14. update_vuex_cityInfo(state, payload) {
  15. state.vuex_cityInfo = payload
  16. uni.setStorageSync('vuex_cityInfo', payload);
  17. },
  18. update_vuex_loginInfo(state, payload) {
  19. state.vuex_loginInfo = payload
  20. uni.setStorageSync('vuex_loginInfo', payload);
  21. },
  22. update_vuex_userInfo(state, payload) {
  23. state.vuex_userInfo = payload || {}
  24. uni.setStorageSync('vuex_userInfo', payload);
  25. },
  26. update_apiOk(state, payload) {
  27. state.apiOk = payload
  28. },
  29. goLogin(state) {
  30. uni.clearStorageSync()
  31. state.vuex_loginInfo = {}
  32. state.vuex_userInfo = {}
  33. uni.navigateTo({
  34. url: '/pages/userCenter/login/login'
  35. })
  36. },
  37. // 为非平台学员开一条路
  38. updateNonPlatformStudent(state, val) {
  39. state.NonPlatformStudent = val
  40. }
  41. },
  42. actions: {
  43. getCity({commit}) {
  44. return new Promise((resolve, reject) => {
  45. // #ifdef APP-PLUS||H5
  46. console.log('h5经纬度的代码')
  47. getCityInfo(resolve, reject,commit)
  48. // #endif
  49. // #ifdef MP-WEIXIN
  50. uni.authorize({
  51. scope: 'scope.userLocation',
  52. success() {
  53. getCityInfo(resolve, reject,commit)
  54. },
  55. fail: function(res4) {
  56. uni.showModal({
  57. title: '提示',
  58. content: '小程序想要获取您的地里位置',
  59. success: function(res) {
  60. if (res.confirm) {
  61. uni.openSetting({
  62. success(res) {
  63. getCityInfo(resolve, reject,commit)
  64. }
  65. });
  66. } else if (res.cancel) {
  67. console.log('用户点击取消');
  68. }
  69. }
  70. })
  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.tenantId) {
  92. uni.$u.http.setConfig((config) => {
  93. config.header['tenant-id'] = res.tenantId
  94. return config
  95. })
  96. }
  97. commit('update_vuex_userInfo',res.data)
  98. },
  99. // 刷新token
  100. async refreshToken({state, commit}) {
  101. return new Promise(async(resolve, reject)=>{
  102. if(!apiOk&&refreshTokenFn) {
  103. return refreshTokenFn
  104. }
  105. apiOk = false
  106. refreshTokenFn = null
  107. const http = uni.$u.http
  108. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  109. let refreshToken = state.vuex_loginInfo.refreshToken
  110. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  111. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  112. apiOk = true
  113. commit('update_vuex_loginInfo', refreshTokenFn.data)
  114. uni.$u.http.setConfig((config) => {
  115. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  116. return config
  117. })
  118. resolve(refreshTokenFn)
  119. }else {
  120. apiOk = true
  121. commit('goLogin')
  122. reject('刷新token失败了')
  123. }
  124. })
  125. }
  126. }
  127. }
  128. export default user
  129. // h5高德地图
  130. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  131. uni.request({
  132. method: 'GET',
  133. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  134. data: {
  135. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  136. location: `${longitude},${latitude}`,
  137. output: 'JSON'
  138. },
  139. success: (res) => {
  140. console.log(res) // 数据结构见下方
  141. let result = res.data.regeocode.addressComponent
  142. let obj = {
  143. lat: latitude,
  144. lng: longitude,
  145. city: result.city,
  146. cityCode: result.adcode,
  147. province: result.province,
  148. district: result.district
  149. }
  150. commit('update_vuex_cityInfo', obj)
  151. resolve(obj)
  152. uni.hideLoading()
  153. },
  154. fail: res => {
  155. reject(new Error('获取地理位置信息失败'))
  156. uni.hideLoading()
  157. }
  158. })
  159. }
  160. function wxAdsFn(longitude,latitude, commit,resolve, reject,) {
  161. console.log(longitude)
  162. console.log(latitude)
  163. var qqmapKey = new qqmapWx({
  164. key: 'NRWBZ-TKRWV-CSAPH-5PFDS-J4HT6-IWF4O'
  165. })
  166. qqmapKey.reverseGeocoder({
  167. location: {
  168. latitude: latitude,
  169. longitude:longitude
  170. },
  171. success(res2) {
  172. console.log('城市信息')
  173. console.log(res2)
  174. let result = res2.result
  175. let obj = {
  176. lat: latitude,
  177. lng: longitude,
  178. city: result.address_component.city,
  179. cityCode: result.ad_info.adcode,
  180. province: result.address_component.province,
  181. district: result.address_component.district
  182. }
  183. console.log(obj)
  184. commit('update_vuex_cityInfo', obj)
  185. resolve(obj)
  186. uni.hideLoading();
  187. },
  188. fail: function(res3) {
  189. console.log('出什么问题了?')
  190. console.log(res3)
  191. let obj = {
  192. lat: latitude,
  193. lng: longitude
  194. }
  195. // reject(res3)
  196. commit('update_vuex_cityInfo', obj)
  197. uni.hideLoading();
  198. },
  199. })
  200. }
  201. function getCityInfo(resolve, reject, commit) {
  202. // uni.showLoading({
  203. // title: '加载中...'
  204. // })
  205. console.log('来到这是里了吗?')
  206. uni.getLocation({
  207. type: 'wgs84',
  208. // type: 'gcj02',
  209. success: function(res) {
  210. console.log('当前位置的经度:' + res.longitude);
  211. // console.log('当前位置的纬度:' + res);
  212. // #ifdef H5
  213. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  214. // #endif
  215. // #ifdef MP-WEIXIN
  216. wxAdsFn(res.longitude,res.latitude, commit,resolve, reject,)
  217. // #endif
  218. },
  219. fail(e) {
  220. console.log(e)
  221. uni.hideLoading()
  222. }
  223. })
  224. }