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

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