my-login.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="login-container">
  3. <!-- 提示登录的图标 -->
  4. <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
  5. <!-- 登录按钮 -->
  6. <button type="primary" class="btn-login" @click="getToken">一键登录</button>
  7. <!-- 登录提示 -->
  8. <view class="tips-text">登录后尽享更多权益</view>
  9. </view>
  10. </template>
  11. <script>
  12. // 按需导入 mapMutations 辅助函数
  13. import { mapMutations } from 'vuex'
  14. export default {
  15. name:"my-login",
  16. data() {
  17. return {
  18. };
  19. },
  20. methods: {
  21. // 调用 mapMutations 辅助方法,把 m_user 模块中的 updateUserInfo 映射到当前组件中使用
  22. ...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
  23. // 调用登录接口,换取永久的 token
  24. async getToken() {
  25. // 调用微信登录接口
  26. const [err, res] = await uni.login().catch(err => err)
  27. // 判断是否 wx.login() 调用失败
  28. if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败!')
  29. // 准备参数对象
  30. const query = {
  31. code: res.code,
  32. }
  33. // 换取 token
  34. const { data: loginResult } = await uni.$http.get('/ucenter/mini-program-openid-uid/wxlogin', query)
  35. if (loginResult.code === 20000) {
  36. uni.$showMsg('登录成功!')
  37. }
  38. // console.log(loginResult)
  39. // 更新vuex中的token
  40. this.updateToken(loginResult.data.token)
  41. // // 将用户的基本信息存储到 vuex 中
  42. const userInfo = {
  43. alias: loginResult.data.alias,
  44. avatar: loginResult.data.avatar,
  45. city: loginResult.data.city
  46. }
  47. this.updateUserInfo(userInfo)
  48. }
  49. }
  50. }
  51. </script>
  52. <style lang="scss" scoped>
  53. .login-container {
  54. // 登录盒子的样式
  55. height: 750rpx;
  56. display: flex;
  57. flex-direction: column;
  58. align-items: center;
  59. justify-content: center;
  60. background-color: #f8f8f8;
  61. position: relative;
  62. overflow: hidden;
  63. // 绘制登录盒子底部的半椭圆造型
  64. &::after {
  65. content: ' ';
  66. display: block;
  67. position: absolute;
  68. width: 100%;
  69. height: 40px;
  70. left: 0;
  71. bottom: 0;
  72. background-color: white;
  73. border-radius: 100%;
  74. transform: translateY(50%);
  75. }
  76. // 登录按钮的样式
  77. .btn-login {
  78. width: 90%;
  79. border-radius: 100px;
  80. margin: 15px 0;
  81. background-color: #c00000;
  82. }
  83. // 按钮下方提示消息的样式
  84. .tips-text {
  85. font-size: 12px;
  86. color: gray;
  87. }
  88. }
  89. </style>