UserCenter.vue 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="User-box">
  3. <!-- <h1>UserCenter 个人中心</h1> -->
  4. <div class="user-info">
  5. <img src="../assets/userImag/user.png" />
  6. <div class="info-name">
  7. <div>
  8. <span>旺旺旺</span>
  9. </div>
  10. <div>
  11. <span>18888888888</span>
  12. </div>
  13. </div>
  14. </div>
  15. <div class="button-box">
  16. <van-cell-group>
  17. <van-cell class="user-button" title="注册" @click="handLogin()" />
  18. </van-cell-group>
  19. <!-- <button type="primary" @click="handLogin()">微信授权登录</button> -->
  20. <!-- <van-button class="user-button" type="info" @click="handLogin()">注册</van-button> -->
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. code: '' // 前端获取 code 传给后端调用相应接口
  29. }
  30. },
  31. created() {
  32. // 从 window.location.href 中截取 code 并且赋值
  33. if (window.location.href.indexOf('CSDN_state') !== -1) {
  34. // 此方法仅供参考!!!
  35. this.code = window.location.href.split('?')[1].split('=')[1].split('&')[0]
  36. this.$store.commit('SET_USER_INFO', { code: this.code })
  37. }
  38. if (this.$store.state.user.code) {
  39. // 存在 code 直接调用接口
  40. this.handGetUserInfo(this.code)
  41. }
  42. },
  43. methods: {
  44. handLogin() {
  45. this.$store.commit('SET_USER_INFO', { code: this.code })
  46. // 重定向地址重定到当前页面,在路径获取 code
  47. if (process.env.NODE_ENV === 'development') {
  48. console.log(222)
  49. return
  50. }
  51. /**
  52. * 获取 code
  53. * @returns
  54. */
  55. // function getCode() {
  56. // const matched = /[?&]*code=([^&]+)/.exec(location.search)
  57. // if (matched) {
  58. // return decodeURIComponent(matched[1])
  59. // }
  60. // }
  61. const hrefUrl = window.location.href
  62. // const originCode = localStorage.getItem('wxcode')
  63. // const queryCode = getCode()
  64. // localStorage.setItem('wxcode', queryCode)
  65. // const local = encodeURIComponent(location.origin + location.pathname)
  66. if (this.code === '') {
  67. window.location.href = `
  68. https://open.weixin.qq.com/connect/oauth2/authorize
  69. ?appid=${this.$store.state.user.appid}
  70. &redirect_uri=${encodeURIComponent(hrefUrl)}
  71. &response_type=code
  72. &scope=snsapi_userinfo
  73. &state=Code_state#wechat_redirect
  74. `
  75. }
  76. },
  77. handGetUserInfo(code) {
  78. console.log(
  79. '🚀 ~ file: UserCenter.vue ~ line 44 ~ handGetUserInfo ~ code',
  80. code
  81. )
  82. // 调用后端接口,参数为 code 剩下工作量交给后端即可
  83. // wxLogin({
  84. // data: {
  85. // code: code
  86. // },
  87. // method: 'GET'
  88. // }).then((res) => {
  89. // console.log(
  90. // '🚀 ~ file: UserCenter.vue ~ line 47 ~ handGetUserInfo ~ res',
  91. // res
  92. // )
  93. // // res => 登陆成功!
  94. // })
  95. }
  96. }
  97. }
  98. </script>
  99. <style lang="less" scoped>
  100. .User-box {
  101. width: 100%;
  102. height: 100%;
  103. .user-info {
  104. display: flex;
  105. height: 20%;
  106. padding-left: 24px;
  107. > img {
  108. width: 70px;
  109. height: 70px;
  110. border-radius: 50%;
  111. position: relative;
  112. top: 5vh;
  113. }
  114. .info-name {
  115. position: relative;
  116. font-size: 17px;
  117. height: 20%;
  118. font-weight: 700;
  119. top: 5vh;
  120. left: 6vw;
  121. > div {
  122. margin-top: 10px;
  123. }
  124. }
  125. }
  126. .button-box {
  127. width: 97%;
  128. margin: 0 auto;
  129. // box-shadow: 0px 8px 38px 0px rgba(0, 0, 0, 0.12);
  130. .user-button {
  131. height: 50px;
  132. line-height: 30px;
  133. // box-shadow: 0px 8px 38px 0px rgba(0, 0, 0, 0.12);
  134. }
  135. }
  136. }
  137. </style>