Me.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, post, showModal} 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: require('../../../static/img/unlogin.png'),
  25. nickName: '未登录'
  26. }
  27. }
  28. },
  29. created () {
  30. this.onShow()
  31. },
  32. methods: {
  33. async addBook (isbn) {
  34. const res = await post('/weapp/addbook', {
  35. isbn,
  36. openid: this.userInfo.openId
  37. })
  38. console.log(res)
  39. showModal('添加成功', `${res.title}添加成功`)
  40. },
  41. scanBook () {
  42. wx.scanCode({
  43. success: (res) => {
  44. if (res.result) {
  45. console.log(res.result)
  46. this.addBook(res.result)
  47. }
  48. }
  49. })
  50. },
  51. login () {
  52. let user = wx.getStorageSync('userInfo')
  53. if (!user) {
  54. qcloud.setLoginUrl(config.loginUrl)
  55. qcloud.login({
  56. success: (userInfo) => {
  57. qcloud.request({
  58. url: config.userUrl,
  59. login: true,
  60. success: (userRes) => {
  61. showSuccess('登录成功')
  62. wx.setStorageSync('userInfo', userRes.data.data)
  63. this.userInfo = userRes.data.data
  64. }
  65. })
  66. },
  67. fail: function (err) {
  68. console.log('登录失败', err)
  69. }
  70. })
  71. }
  72. },
  73. onShow () {
  74. let userInfo = wx.getStorageSync('userInfo')
  75. if (userInfo) {
  76. this.userInfo = userInfo
  77. }
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang='scss'>
  83. .container{
  84. padding:0 30rpx;
  85. }
  86. .userinfo{
  87. margin-top:100rpx;
  88. text-align:center;
  89. img{
  90. width: 150rpx;
  91. height:150rpx;
  92. margin: 20rpx;
  93. border-radius: 50%;
  94. }
  95. }
  96. </style>