auth.vue 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <template>
  2. </template>
  3. <script>
  4. export default {
  5. data() {
  6. return {
  7. code: '',
  8. authUrl: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+this.config.appId+'&response_type=code&scope=snsapi_userinfo&state=bes&redirect_uri='
  9. }
  10. },
  11. onLoad() {
  12. let code = this.getUrlParam('code')
  13. if(code == null || code == '') {
  14. // 重新获取code
  15. let url = this.authUrl + location.href
  16. window.location.href = url
  17. } else {
  18. // 发送code
  19. this.getAccessToken(code)
  20. }
  21. },
  22. methods: {
  23. // 解析URL 参数
  24. getUrlParam(name) {
  25. let reg = new RegExp('(^|&)'+ name + '=([^&]*)(&|$)')
  26. let r = window.location.search.substr(1).match(reg)
  27. if(r!=null){
  28. return unescape(r[2])
  29. }
  30. return null
  31. },
  32. getAccessToken(url) {
  33. console.log(url)
  34. uni.navigateTo({
  35. url: url
  36. })
  37. }
  38. }
  39. }
  40. </script>
  41. <style>
  42. </style>