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

268 lines
7.8 KiB

7 months ago
7 months ago
7 months ago
7 months ago
3 months ago
7 months ago
7 months ago
7 months ago
7 months ago
6 months ago
6 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. 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. setTimeout(()=>{
  42. uni.navigateTo({
  43. url: url+'?redirect='+ page
  44. })
  45. },500)
  46. }
  47. },
  48. // 为非平台学员开一条路
  49. updateNonPlatformStudent(state, val) {
  50. state.NonPlatformStudent = val
  51. }
  52. },
  53. actions: {
  54. getCity({commit}) {
  55. return new Promise((resolve, reject) => {
  56. // #ifdef APP-PLUS
  57. getCityInfo(resolve, reject,commit)
  58. // #endif
  59. // #ifdef H5
  60. console.log('h5经纬度的代码')
  61. uni.getLocation({
  62. type: 'wgs84',
  63. // type: 'gcj02',
  64. success: function(res) {
  65. console.log('当前位置的经度:' + res.longitude);
  66. // console.log('当前位置的纬度:' + res);
  67. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  68. }
  69. })
  70. // #endif
  71. })
  72. },
  73. checkLogin({state, commit}) {
  74. let token = state.vuex_loginInfo.accessToken
  75. if(!token) {
  76. commit('goLogin')
  77. return false
  78. }
  79. return true
  80. },
  81. // 获取用户信息
  82. async getUserInfo({state, commit}) {
  83. let token = state.vuex_loginInfo.accessToken
  84. if(!token) return
  85. const http = uni.$u.http
  86. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  87. const res = await http.get('app/student-record/get')
  88. // if(res.tenantId) {
  89. // uni.$u.http.setConfig((config) => {
  90. // config.header['tenant-id'] = res.tenantId
  91. // return config
  92. // })
  93. // }
  94. commit('update_vuex_userInfo',res.data)
  95. },
  96. // 刷新token
  97. async refreshToken({state, commit}) {
  98. return new Promise(async(resolve, reject)=>{
  99. if(!apiOk&&refreshTokenFn) {
  100. return refreshTokenFn
  101. }
  102. apiOk = false
  103. refreshTokenFn = null
  104. const http = uni.$u.http
  105. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  106. let refreshToken = state.vuex_loginInfo.refreshToken
  107. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  108. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  109. apiOk = true
  110. commit('update_vuex_loginInfo', refreshTokenFn.data)
  111. uni.$u.http.setConfig((config) => {
  112. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  113. return config
  114. })
  115. resolve(refreshTokenFn)
  116. }else {
  117. apiOk = true
  118. commit('goLogin')
  119. reject('刷新token失败了')
  120. }
  121. })
  122. }
  123. }
  124. }
  125. export default user
  126. // h5高德地图
  127. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  128. uni.request({
  129. method: 'GET',
  130. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  131. data: {
  132. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  133. location: `${longitude},${latitude}`,
  134. output: 'JSON'
  135. },
  136. success: (res) => {
  137. console.log(res) // 数据结构见下方
  138. let result = res.data.regeocode.addressComponent
  139. let obj = {
  140. lat: latitude,
  141. lng: longitude,
  142. city: result.city,
  143. cityCode: result.adcode,
  144. province: result.province,
  145. district: result.district
  146. }
  147. commit('update_vuex_cityInfo', obj)
  148. console.log('h5获得的最终位置对象')
  149. console.log(obj)
  150. resolve(obj)
  151. uni.hideLoading()
  152. },
  153. fail: res => {
  154. reject(new Error('获取地理位置信息失败'))
  155. uni.hideLoading()
  156. }
  157. })
  158. }
  159. function getCityInfo(resolve, reject, commit) {
  160. try{
  161. 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. // },
  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. }
  206. function openGps(){
  207. plus.android.requestPermissions(
  208. ['android.permission.ACCESS_FINE_LOCATION'],
  209. function(resultObj) {
  210. var result = 0;
  211. for (var i = 0; i < resultObj.granted.length; i++) {
  212. var grantedPermission = resultObj.granted[i];
  213. console.log('已获取的权限:' + grantedPermission);
  214. result = 1
  215. }
  216. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  217. var deniedPresentPermission = resultObj.deniedPresent[i];
  218. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  219. result = 0
  220. }
  221. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  222. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  223. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  224. result = -1
  225. }
  226. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  227. if (result != 1) {
  228. //如果用户第一次拒绝后,跳转到**应用**的权限页面
  229. var Intent = plus.android.importClass("android.content.Intent");
  230. var Settings = plus.android.importClass("android.provider.Settings");
  231. var Uri = plus.android.importClass("android.net.Uri");
  232. var mainActivity = plus.android.runtimeMainActivity();
  233. var intent = new Intent();
  234. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  235. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  236. intent.setData(uri);
  237. mainActivity.startActivity(intent);
  238. }
  239. },
  240. function(error) {
  241. console.log('申请权限错误:' + error.code + " = " + error.message);
  242. resolve({
  243. code: error.code,
  244. message: error.message
  245. });
  246. }
  247. );
  248. }