Me.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="container">
  3. <button v-if='!userinfo.openId' open-type="getUserInfo" @click="login">登录</button>
  4. <div class="userinfo" >
  5. <img :src="userInfo.avatarUrl" alt="">
  6. <p>{{userInfo.nickName}}</p>
  7. </div>
  8. <YearProgress></YearProgress>
  9. <button v-if='userinfo.openId' @click="scanBook" class='btn'>添加图书</button>
  10. </div>
  11. </template>
  12. <script>
  13. import {showSuccess} from '@/util'
  14. import config from '@/config'
  15. import qcloud from 'wafer2-client-sdk'
  16. import YearProgress from '@/components/YearProgress'
  17. export default {
  18. components: {
  19. YearProgress
  20. },
  21. data () {
  22. return {
  23. userInfo: {
  24. avatarUrl: '../../../static/img/unlogin.png',
  25. nickName: '未登录'
  26. }
  27. }
  28. },
  29. methods: {
  30. login () {
  31. let user = wx.getStorageSync('userInfo')
  32. if (!user) {
  33. qcloud.setLoginUrl(config.loginUrl)
  34. qcloud.login({
  35. success: (userInfo) => {
  36. qcloud.request({
  37. url: config.userUrl,
  38. login: true,
  39. success: (userRes) => {
  40. showSuccess('登录成功')
  41. wx.setStorageSync('userInfo', userRes.data.data)
  42. this.userInfo = userRes.data.data
  43. }
  44. })
  45. },
  46. fail: function (err) {
  47. console.log('登录失败', err)
  48. }
  49. })
  50. }
  51. },
  52. onShow () {
  53. let userInfo = wx.getStorageSync('userInfo')
  54. if (userInfo) {
  55. this.userInfo = userInfo
  56. }
  57. },
  58. scanBook () {
  59. wx.scanCode({
  60. success: (res) => {
  61. if (res.result) {
  62. console.log(res.result)
  63. }
  64. }
  65. })
  66. }
  67. }
  68. }
  69. // import qcloud from 'wafer2-client-sdk'
  70. // import config from '@/config'
  71. // export default {
  72. // data () {
  73. // return {
  74. // userInfo: { }
  75. // }
  76. // },
  77. // methods: {
  78. // login(){
  79. // console.log('1122334455667799')
  80. // // qcloud.setLoginUrl(config.loginUrl);
  81. // // qcloud.login({
  82. // // success: function (userInfo) {
  83. // // console.log('登录成功', userInfo);
  84. // // wx.setStorageSync('userInfo', userInfo)
  85. // // },
  86. // // fail: function (err) {
  87. // // console.log('登录失败', err);
  88. // // }
  89. // // });
  90. // // this.userInfo = wx.getStorageSync('userInfo')
  91. // }
  92. // }
  93. // }
  94. </script>
  95. <style lang='scss'>
  96. .container{
  97. padding:0 30rpx;
  98. }
  99. .userinfo{
  100. margin-top:100rpx;
  101. text-align:center;
  102. img{
  103. width: 150rpx;
  104. height:150rpx;
  105. margin: 20rpx;
  106. border-radius: 50%;
  107. }
  108. }
  109. </style>