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

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