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

193 lines
5.0 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. 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. console.log(longitude)
  122. console.log(latitude)
  123. var qqmapKey = new qqmapWx({
  124. key: 'NRWBZ-TKRWV-CSAPH-5PFDS-J4HT6-IWF4O'
  125. })
  126. qqmapKey.reverseGeocoder({
  127. location: {
  128. latitude: latitude,
  129. longitude:longitude
  130. },
  131. success(res2) {
  132. console.log('城市信息')
  133. console.log(res2)
  134. let result = res2.result
  135. let obj = {
  136. lat: latitude,
  137. lng: longitude,
  138. city: result.address_component.city,
  139. cityCode: result.ad_info.adcode,
  140. province: result.address_component.province,
  141. district: result.address_component.district
  142. }
  143. commit('update_vuex_cityInfo', obj)
  144. resolve(obj)
  145. uni.hideLoading();
  146. },
  147. fail: function(res3) {
  148. console.log('出什么问题了?')
  149. console.log(res3)
  150. let obj = {
  151. lat: latitude,
  152. lng: longitude
  153. }
  154. // reject(res3)
  155. commit('update_vuex_cityInfo', obj)
  156. uni.hideLoading();
  157. },
  158. })
  159. }
  160. function getCityInfo(resolve, reject, commit) {
  161. // uni.showLoading({
  162. // title: '加载中...'
  163. // })
  164. console.log('来到这是里了吗?')
  165. uni.getLocation({
  166. type: 'wgs84',
  167. // type: 'gcj02',
  168. success: function(res) {
  169. console.log('当前位置的经度:' + res.longitude);
  170. // console.log('当前位置的纬度:' + res);
  171. // #ifdef H5
  172. gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,);
  173. // #endif
  174. // #ifdef MP-WEIXIN
  175. wxAdsFn(res.longitude,res.latitude, commit,resolve, reject,)
  176. // #endif
  177. },
  178. fail(e) {
  179. console.log(e)
  180. uni.hideLoading()
  181. }
  182. })
  183. }