index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view class="login-bar flex flex-inline" v-if="!state.isLogin">
  3. <text class="login-bar_title" > 请先登录以享受更多洗车功能</text>
  4. <!-- <uv-button class="login-bar_btn" size="mini" shape="circle" type="primary" open-type="getPhoneNumber"
  5. color="#19A497"
  6. @getphonenumber="handleGetPhone">去登录
  7. </uv-button>-->
  8. <uv-button class="login-bar_btn" size="mini" shape="circle" type="primary"
  9. color="#19A497"
  10. @click="handleGotoLoginPage">去登录
  11. </uv-button>
  12. </view>
  13. </template>
  14. <script setup lang="ts" name="loginBar">
  15. import {login, tryLogin} from "@/utils/auth";
  16. import {onHide, onLoad, onShow} from "@dcloudio/uni-app";
  17. import {reactive} from "vue";
  18. const state= reactive({
  19. isLogin:false
  20. });
  21. onLoad(()=>{
  22. let userData = getApp<any>().globalData.user;
  23. state.isLogin =!!(userData&&userData.id);
  24. console.log("login bar onLoad>>>>", userData, state.isLogin)
  25. })
  26. onShow(()=>{
  27. let userData = getApp<any>().globalData.user;
  28. state.isLogin =!!(userData&&userData.id);
  29. console.log("login bar onShow>>>>", userData, state.isLogin)
  30. addListener();
  31. })
  32. onHide(()=>{
  33. removeListener();
  34. })
  35. const addListener = () => {
  36. uni.$on('login', function (data) {
  37. state.isLogin = data.isLogin;
  38. })
  39. uni.$on('logout', function (data) {
  40. state.isLogin= false;
  41. })
  42. }
  43. const removeListener = () => {
  44. uni.$off('logout');
  45. uni.$off('login');
  46. }
  47. const handleGetPhone = (e:any) => {
  48. console.log(e)
  49. login(e).then((token: string) => {
  50. console.log(">>>>>>>>>",token)
  51. state.isLogin =true;
  52. })
  53. }
  54. const handleGotoLoginPage = () => {
  55. /* uni.navigateTo({
  56. url: '/pages-user/login/index'
  57. })*/
  58. tryLogin().then((token)=>{
  59. console.log(">>>>>>>>>",token)
  60. }).catch(e=>{
  61. console.error("尝试登录失败,跳转登录页注册",e)
  62. uni.navigateTo({
  63. url: '/pages-user/login/index'
  64. })
  65. })
  66. }
  67. </script>
  68. <style scoped lang="scss">
  69. .login-bar {
  70. width: calc(100vw - 40rpx);
  71. justify-content: space-between;
  72. background: #eee;
  73. border-radius: 8rpx;
  74. padding: 16rpx 10rpx;
  75. margin: 20rpx;
  76. box-shadow: 5px 5px 15px 5px rgba(0, 0, 0, 0.3);
  77. position: fixed;
  78. bottom: 160rpx;
  79. left: 0;
  80. &_title{
  81. font-size: 12px;
  82. height: 22px;
  83. line-height: 22px;
  84. }
  85. &_btn {
  86. margin-right: 30rpx;
  87. }
  88. }
  89. </style>