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

273 lines
7.9 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
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
7 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) return
  92. commit('update_vuex_userInfo',res.data)
  93. },
  94. // 刷新token
  95. async refreshToken({state, commit}) {
  96. return new Promise(async(resolve, reject)=>{
  97. if(!apiOk&&refreshTokenFn) {
  98. return refreshTokenFn
  99. }
  100. apiOk = false
  101. refreshTokenFn = null
  102. const http = uni.$u.http
  103. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  104. let refreshToken = state.vuex_loginInfo.refreshToken
  105. refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  106. if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
  107. apiOk = true
  108. commit('update_vuex_loginInfo', refreshTokenFn.data)
  109. uni.$u.http.setConfig((config) => {
  110. config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
  111. return config
  112. })
  113. resolve(refreshTokenFn)
  114. }else {
  115. apiOk = true
  116. commit('goLogin')
  117. reject('刷新token失败了')
  118. }
  119. })
  120. }
  121. }
  122. }
  123. export default user
  124. // h5高德地图
  125. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  126. uni.request({
  127. method: 'GET',
  128. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  129. data: {
  130. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  131. location: `${longitude},${latitude}`,
  132. output: 'JSON'
  133. },
  134. success: (res) => {
  135. console.log(res) // 数据结构见下方
  136. let result = res.data.regeocode.addressComponent
  137. let obj = {
  138. lat: latitude,
  139. lng: longitude,
  140. city: result.city,
  141. cityCode: result.adcode,
  142. province: result.province,
  143. district: result.district
  144. }
  145. commit('update_vuex_cityInfo', obj)
  146. console.log('h5获得的最终位置对象')
  147. console.log(obj)
  148. resolve(obj)
  149. uni.hideLoading()
  150. },
  151. fail: res => {
  152. reject(new Error('获取地理位置信息失败'))
  153. uni.hideLoading()
  154. }
  155. })
  156. }
  157. function getCityInfo(resolve, reject, commit) {
  158. try{
  159. requestSingleFreshLocation().then(res=>{
  160. let result = res.location
  161. if(result.latitude===0) {
  162. console.log('gogogo来了吗没有获取到经纬度?')
  163. // openGps()
  164. uni.getLocation({
  165. // type: 'wgs84',
  166. type: 'gcj02',
  167. success: function(res) {
  168. console.log('只为弹出权限当前位置的经度:' + res.longitude);
  169. // console.log('当前位置的纬度:' + res);
  170. getCityInfo(resolve, reject, commit)
  171. },
  172. fail() {
  173. uni.showToast({
  174. title: '您的定位权限已关闭,请手动开启定位权限',
  175. icon: 'none'
  176. })
  177. }
  178. })
  179. }else {
  180. console.log('腾讯云经纬度')
  181. console.log(result)
  182. let obj = {
  183. lat: result.latitude,
  184. lng: result.longitude,
  185. city: result.city,
  186. // cityCode: result.code||result.cityCode,
  187. province: result.province,
  188. district: result.district,
  189. }
  190. commit('update_vuex_cityInfo', obj)
  191. resolve(obj)
  192. }
  193. })
  194. }catch(e){
  195. console.log('来到这是里了吗?腾讯云经纬度了')
  196. uni.getLocation({
  197. // type: 'wgs84',
  198. type: 'gcj02',
  199. success: function(res) {
  200. console.log('当前位置的经度:' + res.longitude);
  201. // console.log('当前位置的纬度:' + res);
  202. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  203. },
  204. fail(e) {
  205. console.log(e)
  206. uni.hideLoading()
  207. }
  208. })
  209. }
  210. }
  211. function openGps(){
  212. plus.android.requestPermissions(
  213. ['android.permission.ACCESS_FINE_LOCATION'],
  214. function(resultObj) {
  215. var result = 0;
  216. for (var i = 0; i < resultObj.granted.length; i++) {
  217. var grantedPermission = resultObj.granted[i];
  218. console.log('已获取的权限:' + grantedPermission);
  219. result = 1
  220. }
  221. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  222. var deniedPresentPermission = resultObj.deniedPresent[i];
  223. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  224. result = 0
  225. }
  226. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  227. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  228. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  229. result = -1
  230. }
  231. // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
  232. if (result != 1) {
  233. //如果用户第一次拒绝后,跳转到**应用**的权限页面
  234. var Intent = plus.android.importClass("android.content.Intent");
  235. var Settings = plus.android.importClass("android.provider.Settings");
  236. var Uri = plus.android.importClass("android.net.Uri");
  237. var mainActivity = plus.android.runtimeMainActivity();
  238. var intent = new Intent();
  239. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  240. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  241. intent.setData(uri);
  242. mainActivity.startActivity(intent);
  243. }
  244. },
  245. function(error) {
  246. console.log('申请权限错误:' + error.code + " = " + error.message);
  247. resolve({
  248. code: error.code,
  249. message: error.message
  250. });
  251. }
  252. );
  253. }