学员端小程序
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.

232 lines
6.0 KiB

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