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

221 lines
5.8 KiB

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 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. // 获取用户信息
  75. async getUserInfo({state, commit}) {
  76. const http = uni.$u.http
  77. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  78. const res = await http.get('app/student-record/get')
  79. if(res.tenantId) {
  80. uni.$u.http.setConfig((config) => {
  81. config.header['tenant-id'] = res.tenantId
  82. return config
  83. })
  84. }
  85. commit('update_vuex_userInfo',res.data)
  86. },
  87. // 刷新token
  88. async refreshToken({state, commit}) {
  89. return new Promise(async(resolve, reject)=>{
  90. if(!state.apiOk) {
  91. return state.refreshTokenFn
  92. }
  93. commit('update_apiOk',false)
  94. state.refreshTokenFn = null
  95. const http = uni.$u.http
  96. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  97. let refreshToken = state.vuex_loginInfo.refreshToken
  98. state.refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
  99. if(state.refreshTokenFn.data||state.refreshTokenFn.data.accessToken) {
  100. commit('update_apiOk',true)
  101. commit('update_vuex_loginInfo',state.refreshTokenFn.data)
  102. uni.$u.http.setConfig((config) => {
  103. config.header.Authorization = 'Bearer ' + state.refreshTokenFn.data.accessToken
  104. return config
  105. })
  106. resolve(state.refreshTokenFn)
  107. }else {
  108. commit('update_apiOk',true)
  109. commit('goLogin')
  110. reject('刷新token失败了')
  111. }
  112. })
  113. }
  114. }
  115. }
  116. export default user
  117. // h5高德地图
  118. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  119. uni.request({
  120. method: 'GET',
  121. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  122. data: {
  123. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  124. location: `${longitude},${latitude}`,
  125. output: 'JSON'
  126. },
  127. success: (res) => {
  128. console.log(res) // 数据结构见下方
  129. let result = res.data.regeocode.addressComponent
  130. let obj = {
  131. lat: latitude,
  132. lng: longitude,
  133. city: result.city,
  134. cityCode: result.adcode,
  135. province: result.province,
  136. district: result.district
  137. }
  138. commit('update_vuex_cityInfo', obj)
  139. resolve(obj)
  140. uni.hideLoading()
  141. },
  142. fail: res => {
  143. reject(new Error('获取地理位置信息失败'))
  144. uni.hideLoading()
  145. }
  146. })
  147. }
  148. function wxAdsFn(longitude,latitude, commit,resolve, reject,) {
  149. console.log(longitude)
  150. console.log(latitude)
  151. var qqmapKey = new qqmapWx({
  152. key: 'NRWBZ-TKRWV-CSAPH-5PFDS-J4HT6-IWF4O'
  153. })
  154. qqmapKey.reverseGeocoder({
  155. location: {
  156. latitude: latitude,
  157. longitude:longitude
  158. },
  159. success(res2) {
  160. console.log('城市信息')
  161. console.log(res2)
  162. let result = res2.result
  163. let obj = {
  164. lat: latitude,
  165. lng: longitude,
  166. city: result.address_component.city,
  167. cityCode: result.ad_info.adcode,
  168. province: result.address_component.province,
  169. district: result.address_component.district
  170. }
  171. commit('update_vuex_cityInfo', obj)
  172. resolve(obj)
  173. uni.hideLoading();
  174. },
  175. fail: function(res3) {
  176. console.log('出什么问题了?')
  177. console.log(res3)
  178. let obj = {
  179. lat: latitude,
  180. lng: longitude
  181. }
  182. // reject(res3)
  183. commit('update_vuex_cityInfo', obj)
  184. uni.hideLoading();
  185. },
  186. })
  187. }
  188. function getCityInfo(resolve, reject, commit) {
  189. // uni.showLoading({
  190. // title: '加载中...'
  191. // })
  192. console.log('来到这是里了吗?')
  193. uni.getLocation({
  194. type: 'wgs84',
  195. // type: 'gcj02',
  196. success: function(res) {
  197. console.log('当前位置的经度:' + res.longitude);
  198. // console.log('当前位置的纬度:' + res);
  199. // #ifdef H5
  200. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  201. // #endif
  202. // #ifdef MP-WEIXIN
  203. wxAdsFn(res.longitude,res.latitude, commit,resolve, reject,)
  204. // #endif
  205. },
  206. fail(e) {
  207. console.log(e)
  208. uni.hideLoading()
  209. }
  210. })
  211. }