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

136 lines
3.7 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. import qqmapWx from '../../common/sdk/qqmap-wx-jssdk.min.js'; // 引入
  2. const user = {
  3. state: {
  4. vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'},
  5. vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {},
  6. vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {},
  7. },
  8. mutations: {
  9. update_vuex_cityInfo(state, payload) {
  10. state.vuex_cityInfo = payload
  11. uni.setStorageSync('vuex_cityInfo', payload);
  12. },
  13. update_vuex_loginInfo(state, payload) {
  14. state.vuex_loginInfo = payload
  15. uni.setStorageSync('vuex_loginInfo', payload);
  16. },
  17. update_vuex_userInfo(state, payload) {
  18. state.vuex_userInfo = payload
  19. uni.setStorageSync('vuex_userInfo', payload);
  20. },
  21. goLogin(state) {
  22. uni.clearStorageSync()
  23. state.vuex_loginInfo = {}
  24. state.vuex_userInfo = {}
  25. uni.navigateTo({
  26. url: '/pages/userCenter/login/loginByPhone'
  27. })
  28. }
  29. },
  30. actions: {
  31. getCity({commit}) {
  32. return new Promise((resolve, reject) => {
  33. // #ifdef APP-PLUS||H5
  34. getCityInfo(resolve, reject,commit)
  35. // #endif
  36. // #ifdef MP-WEIXIN
  37. uni.authorize({
  38. scope: 'scope.userLocation',
  39. success() {
  40. getCityInfo(resolve, reject,commit)
  41. },
  42. fail: function(res4) {
  43. uni.showModal({
  44. title: '提示',
  45. content: '小程序想要获取您的地里位置',
  46. success: function(res) {
  47. if (res.confirm) {
  48. uni.openSetting({
  49. success(res) {
  50. getCityInfo(resolve, reject,commit)
  51. }
  52. });
  53. } else if (res.cancel) {
  54. console.log('用户点击取消');
  55. }
  56. }
  57. })
  58. },
  59. })
  60. // #endif
  61. })
  62. },
  63. // 获取用户信息
  64. async getUserInfo({commit}) {
  65. const http = uni.$u.http
  66. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  67. const res = await http.get('member/user/get',config)
  68. commit('update_vuex_userInfo',res.data)
  69. },
  70. // 刷新token
  71. async refreshToken({state, commit}) {
  72. const http = uni.$u.http
  73. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  74. let refreshToken = state.vuex_loginInfo.refreshToken
  75. const res = await http.post('member/auth/refresh-token',{refreshToken}, config)
  76. console.log('刷新token结果')
  77. console.log(res)
  78. commit('update_vuex_loginInfo',res.data)
  79. uni.$u.http.setConfig((config) => {
  80. config.header.Authorization = 'Bearer ' + res.data.accessToken
  81. return config
  82. })
  83. }
  84. }
  85. }
  86. export default user
  87. function getCityInfo(resolve, reject, commit) {
  88. uni.showLoading({
  89. title: '加载中...'
  90. })
  91. var qqmapKey = new qqmapWx({
  92. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  93. })
  94. uni.getLocation({
  95. type: 'wgs84',
  96. // type: 'gcj02',
  97. success: function(res) {
  98. console.log('当前位置的经度:' + res.longitude);
  99. console.log('当前位置的纬度:' + res.latitude);
  100. qqmapKey.reverseGeocoder({
  101. location: {
  102. latitude: res.latitude,
  103. longitude: res.longitude
  104. },
  105. success(res2) {
  106. // console.log('城市信息')
  107. // console.log(res2.result)
  108. let result = res2.result
  109. let obj = {
  110. latitude: res.latitude,
  111. longitude: res.longitude,
  112. city: result.address_component.city,
  113. cityCode: result.ad_info.adcode,
  114. province: result.address_component.province,
  115. district: result.address_component.district
  116. }
  117. commit('update_vuex_cityInfo', obj)
  118. resolve(obj)
  119. uni.hideLoading();
  120. },
  121. fail: function(res3) {
  122. reject(res3)
  123. uni.hideLoading();
  124. },
  125. })
  126. },
  127. fail(e) {
  128. console.log(e)
  129. }
  130. })
  131. }