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

278 lines
8.0 KiB

10 months ago
8 months ago
10 months ago
5 months ago
10 months ago
8 months ago
10 months ago
10 months ago
5 months ago
5 months ago
10 months ago
8 months ago
10 months ago
8 months ago
10 months ago
5 months ago
10 months ago
8 months ago
10 months ago
8 months ago
10 months ago
8 months ago
8 months ago
5 months ago
5 months ago
8 months ago
10 months ago
8 months ago
8 months ago
8 months ago
8 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.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. console.log('h5获得的最终位置对象')
  152. console.log(obj)
  153. resolve(obj)
  154. uni.hideLoading()
  155. },
  156. fail: res => {
  157. reject(new Error('获取地理位置信息失败'))
  158. uni.hideLoading()
  159. }
  160. })
  161. }
  162. function getCityInfo(resolve, reject, commit) {
  163. try{
  164. requestSingleFreshLocation().then(res=>{
  165. let result = res.location
  166. if(result.latitude===0) {
  167. console.log('gogogo来了吗没有获取到经纬度?')
  168. // openGps()
  169. uni.getLocation({
  170. // type: 'wgs84',
  171. type: 'gcj02',
  172. success: function(res) {
  173. console.log('只为弹出权限当前位置的经度:' + res.longitude);
  174. // console.log('当前位置的纬度:' + res);
  175. getCityInfo(resolve, reject, commit)
  176. },
  177. fail() {
  178. uni.showToast({
  179. title: '您的定位权限已关闭,请手动开启定位权限',
  180. icon: 'none'
  181. })
  182. }
  183. })
  184. }else {
  185. console.log('腾讯云经纬度')
  186. console.log(result)
  187. let obj = {
  188. lat: result.latitude,
  189. lng: result.longitude,
  190. city: result.city,
  191. // cityCode: result.code||result.cityCode,
  192. province: result.province,
  193. district: result.district,
  194. }
  195. commit('update_vuex_cityInfo', obj)
  196. resolve(obj)
  197. }
  198. })
  199. }catch(e){
  200. console.log('来到这是里了吗?腾讯云经纬度了')
  201. uni.getLocation({
  202. // type: 'wgs84',
  203. type: 'gcj02',
  204. success: function(res) {
  205. console.log('当前位置的经度:' + res.longitude);
  206. // console.log('当前位置的纬度:' + res);
  207. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  208. },
  209. fail(e) {
  210. console.log(e)
  211. uni.hideLoading()
  212. }
  213. })
  214. }
  215. }
  216. function openGps(){
  217. plus.android.requestPermissions(
  218. ['android.permission.ACCESS_FINE_LOCATION'],
  219. function(resultObj) {
  220. var result = 0;
  221. for (var i = 0; i < resultObj.granted.length; i++) {
  222. var grantedPermission = resultObj.granted[i];
  223. console.log('已获取的权限:' + grantedPermission);
  224. result = 1
  225. }
  226. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  227. var deniedPresentPermission = resultObj.deniedPresent[i];
  228. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  229. result = 0
  230. }
  231. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  232. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  233. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  234. result = -1
  235. }
  236. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  237. if (result != 1) {
  238. //如果用户第一次拒绝后,跳转到**应用**的权限页面
  239. var Intent = plus.android.importClass("android.content.Intent");
  240. var Settings = plus.android.importClass("android.provider.Settings");
  241. var Uri = plus.android.importClass("android.net.Uri");
  242. var mainActivity = plus.android.runtimeMainActivity();
  243. var intent = new Intent();
  244. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  245. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  246. intent.setData(uri);
  247. mainActivity.startActivity(intent);
  248. }
  249. },
  250. function(error) {
  251. console.log('申请权限错误:' + error.code + " = " + error.message);
  252. resolve({
  253. code: error.code,
  254. message: error.message
  255. });
  256. }
  257. );
  258. }