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

183 lines
4.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. 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. },
  9. mutations: {
  10. update_vuex_cityInfo(state, payload) {
  11. state.vuex_cityInfo = payload
  12. uni.setStorageSync('vuex_cityInfo', payload);
  13. },
  14. update_vuex_loginInfo(state, payload) {
  15. state.vuex_loginInfo = payload
  16. uni.setStorageSync('vuex_loginInfo', payload);
  17. },
  18. update_vuex_userInfo(state, payload) {
  19. state.vuex_userInfo = payload
  20. uni.setStorageSync('vuex_userInfo', payload);
  21. },
  22. goLogin(state) {
  23. uni.clearStorageSync()
  24. state.vuex_loginInfo = {}
  25. state.vuex_userInfo = {}
  26. uni.navigateTo({
  27. url: '/pages/userCenter/login/loginByPhone'
  28. })
  29. }
  30. },
  31. actions: {
  32. getCity({commit}) {
  33. return new Promise((resolve, reject) => {
  34. // #ifdef APP-PLUS||H5
  35. console.log('h5经纬度的代码')
  36. getCityInfo(resolve, reject,commit)
  37. // #endif
  38. // #ifdef MP-WEIXIN
  39. uni.authorize({
  40. scope: 'scope.userLocation',
  41. success() {
  42. getCityInfo(resolve, reject,commit)
  43. },
  44. fail: function(res4) {
  45. uni.showModal({
  46. title: '提示',
  47. content: '小程序想要获取您的地里位置',
  48. success: function(res) {
  49. if (res.confirm) {
  50. uni.openSetting({
  51. success(res) {
  52. getCityInfo(resolve, reject,commit)
  53. }
  54. });
  55. } else if (res.cancel) {
  56. console.log('用户点击取消');
  57. }
  58. }
  59. })
  60. },
  61. })
  62. // #endif
  63. })
  64. },
  65. // 获取用户信息
  66. async getUserInfo({commit}) {
  67. const http = uni.$u.http
  68. let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} }
  69. const res = await http.get('member/user/get',config)
  70. commit('update_vuex_userInfo',res.data)
  71. },
  72. // 刷新token
  73. async refreshToken({state, commit}) {
  74. const http = uni.$u.http
  75. let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
  76. let refreshToken = state.vuex_loginInfo.refreshToken
  77. const res = await http.post('member/auth/refresh-token',{refreshToken}, config)
  78. console.log('刷新token结果')
  79. console.log(res)
  80. commit('update_vuex_loginInfo',res.data)
  81. uni.$u.http.setConfig((config) => {
  82. config.header.Authorization = 'Bearer ' + res.data.accessToken
  83. return config
  84. })
  85. }
  86. }
  87. }
  88. export default user
  89. // h5高德地图
  90. function gaoDeFn(longitude,latitude,commit,resolve, reject,) {
  91. uni.request({
  92. method: 'GET',
  93. url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
  94. data: {
  95. key: 'a0dde4c05390e689ea2c19d8ec447f62',
  96. location: `${longitude},${latitude}`,
  97. output: 'JSON'
  98. },
  99. success: (res) => {
  100. console.log(res) // 数据结构见下方
  101. let result = res.data.regeocode.addressComponent
  102. let obj = {
  103. lat: latitude,
  104. lng: longitude,
  105. city: result.city,
  106. cityCode: result.adcode,
  107. province: result.province,
  108. district: result.district
  109. }
  110. commit('update_vuex_cityInfo', obj)
  111. resolve(obj)
  112. uni.hideLoading()
  113. },
  114. fail: res => {
  115. reject(new Error('获取地理位置信息失败'))
  116. uni.hideLoading()
  117. }
  118. })
  119. }
  120. function wxAdsFn(longitude,latitude, commit,resolve, reject,) {
  121. var qqmapKey = new qqmapWx({
  122. key: '2BTBZ-6BQRB-ZG4UG-NOYYG-KZMH7-B4BYN'
  123. })
  124. qqmapKey.reverseGeocoder({
  125. location: {
  126. latitude: latitude,
  127. longitude:longitude
  128. },
  129. success(res2) {
  130. // console.log('城市信息')
  131. // console.log(res2.result)
  132. let result = res2.result
  133. let obj = {
  134. lat: latitude,
  135. lng: longitude,
  136. city: result.address_component.city,
  137. cityCode: result.ad_info.adcode,
  138. province: result.address_component.province,
  139. district: result.address_component.district
  140. }
  141. commit('update_vuex_cityInfo', obj)
  142. resolve(obj)
  143. uni.hideLoading();
  144. },
  145. fail: function(res3) {
  146. reject(res3)
  147. uni.hideLoading();
  148. },
  149. })
  150. }
  151. function getCityInfo(resolve, reject, commit) {
  152. uni.showLoading({
  153. title: '加载中...'
  154. })
  155. uni.getLocation({
  156. type: 'wgs84',
  157. // type: 'gcj02',
  158. success: function(res) {
  159. console.log('当前位置的经度:' + res.longitude);
  160. console.log('当前位置的纬度:' + res);
  161. // #ifdef APP-PLUS||H5
  162. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  163. // #endif
  164. // #ifdef MP-WEIXIN
  165. wxAdsFn(res.longitude,res.latitude, commit,resolve, reject,)
  166. // #endif
  167. },
  168. fail(e) {
  169. console.log(e)
  170. uni.hideLoading()
  171. }
  172. })
  173. }