login.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="pages">
  3. <view class="wrap login">
  4. <form @submit="" @reset="">
  5. <view class="til">
  6. 登录
  7. </view>
  8. <view class="uni-form-item-wrap">
  9. <view class="uni-form-item">
  10. <input class="uni-input" v-model="userName" maxlength="11" focus placeholder="手机号" />
  11. </view>
  12. <view class="uni-form-item">
  13. <input v-model="userPwd" :type="viewpassword?'text':'password'" class="uni-input" maxlength="16" focus placeholder="密码" />
  14. <button type="default" @click="eyeclick" class="eyebtn" :class="viewpassword?'text':'password'"></button>
  15. </view>
  16. </view>
  17. <button form-type="submit" type="primary" @click="login" class="btn">登录</button>
  18. </form>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import { mapState, mapMutations } from 'vuex';
  24. export default{
  25. components:{
  26. },
  27. props:{
  28. },
  29. data(){
  30. return{
  31. backpage:'',
  32. //用户输入的内容
  33. userName:"demo",
  34. userPwd:"123456",
  35. params:{
  36. username: this.userName,
  37. password: this.userPwd,
  38. grant_type:'password',
  39. client_id:this.config.client_id,
  40. client_secret:this.config.client_secret,
  41. },
  42. viewpassword:false,
  43. //验证的规则
  44. rules:{
  45. userName:{
  46. rule:/\S/,
  47. msg:"账号不能为空"
  48. },
  49. userPwd:{
  50. rule:/^[0-9a-zA-Z]{6,16}$/,
  51. msg:"密码应该为6-16位"
  52. }
  53. }
  54. }
  55. },
  56. onLoad(options){
  57. this.backpage = options.backpage
  58. },
  59. methods:{
  60. ...mapMutations(['setLogin']),
  61. login(){
  62. let serf = this;
  63. if(!this.validate('userName')) return;
  64. if(!this.validate("userPwd")) return;
  65. uni.showLoading({
  66. title:"登录中..."
  67. });
  68. this.$api.http.post(this.config.apiBaseurl+'oauth/token',{
  69. username: this.userName,
  70. password: this.userPwd,
  71. grant_type:'password',
  72. client_id:this.config.client_id,
  73. client_secret:this.config.client_secret,
  74. }).then(res => {
  75. console.log(res);
  76. uni.hideLoading();
  77. uni.showToast({
  78. icon:'none',
  79. title:res.data.message,
  80. duration: 2000
  81. });
  82. serf.setLogin(res.data);
  83. if(serf.backpage == '' || !serf.backpage){
  84. uni.redirectTo({
  85. url: '/pages/index/index',
  86. fail:function(e){
  87. console.log(e);
  88. }
  89. })
  90. }else{
  91. uni.navigateTo({
  92. url:serf.backpage
  93. })
  94. }
  95. // setTimeout(()=>{
  96. // uni.redirectTo({
  97. // url: '../index/index'
  98. // });
  99. // },1000);
  100. }).catch(err => {
  101. console.log(err)
  102. });
  103. },
  104. //判断验证是否符合要求
  105. validate(key){
  106. let bool=true;
  107. if(!this.rules[key].rule.test(this[key])){
  108. //提示信息
  109. uni.showToast({
  110. title:this.rules[key].msg,
  111. })
  112. //取反
  113. bool=false;
  114. return false;
  115. }
  116. return bool;
  117. },
  118. //显示隐藏密码
  119. eyeclick(){
  120. this.viewpassword = !this.viewpassword;
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss" scoped>
  126. /* @import url("login.scss"); */
  127. page{background-color: $uni-bg-color-grey;}
  128. .til{margin: $uni-spacing-col-lg 0;}
  129. .login{padding-top: $uni-spacing-col-lg;}
  130. .uni-input{width: 100%;box-sizing: border-box;border-radius:$uni-border-radius-llg;background-color: $uni-bg-color;height: 50px;padding-left: 80rpx;}
  131. .uni-form-item{position: relative;margin-bottom: 40rpx;}
  132. .btn{border-radius: $uni-border-radius-llg;}
  133. .uni-form-item-wrap{margin-bottom: 80rpx;}
  134. .eyebtn {position: absolute;height: 100%; width: 50px;top: 0; right: 0;background: transparent;cursor: pointer;border: 0;padding: 0;margin: 0;outline: 0;}
  135. .eyebtn::before,
  136. .eyebtn::after {content:'';position: absolute;top: 0; left: 0; bottom: 0; right: 0;margin: auto;}
  137. .eyebtn::after{transform-origin:unset;transform: scale(0.8);}
  138. .eyebtn.password::before {width: 20px;height: 20px;background: #e0e0e0;border-radius: 15px 0 15px 0;-webkit-transform: rotate(45deg);-ms-transform: rotate(45deg);transform: rotate(45deg);-webkit-transition: height .168s;transition: height .168s;}
  139. .eyebtn.password::after {width: 10px;height: 10px;border-radius: 50%;background: #424242;}
  140. .eyebtn.text::before {width: 15px;height: 15px;background: transparent;border: 3px solid #e0e0e0;border-radius: 15px 0 15px 0;-webkit-transform: rotate(45deg);-ms-transform: rotate(45deg);transform: rotate(45deg);}
  141. .eyebtn.text::after {width: 3px;height: 30px;border-radius: 0;-webkit-transform: rotate(30deg);-ms-transform: rotate(30deg);transform: rotate(30deg);background: #e0e0e0;-webkit-transition: height .132s;transition: height .132s;}
  142. </style>