login.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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="password" class="uni-input" maxlength="16" focus placeholder="密码" />
  14. <button type="default" class="eyebtn 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. //验证的规则
  43. rules:{
  44. userName:{
  45. rule:/\S/,
  46. msg:"账号不能为空"
  47. },
  48. userPwd:{
  49. rule:/^[0-9a-zA-Z]{6,16}$/,
  50. msg:"密码应该为6-16位"
  51. }
  52. }
  53. }
  54. },
  55. onLoad(options){
  56. this.backpage = options.backpage
  57. },
  58. methods:{
  59. ...mapMutations(['setLogin']),
  60. login(){
  61. let serf = this;
  62. if(!this.validate('userName')) return;
  63. if(!this.validate("userPwd")) return;
  64. uni.showLoading({
  65. title:"登录中..."
  66. });
  67. this.$api.http.post(this.config.apiBaseurl+'oauth/token',{
  68. username: this.userName,
  69. password: this.userPwd,
  70. grant_type:'password',
  71. client_id:this.config.client_id,
  72. client_secret:this.config.client_secret,
  73. }).then(res => {
  74. console.log(res);
  75. uni.hideLoading();
  76. uni.showToast({
  77. icon:'none',
  78. title:res.data.message,
  79. duration: 2000
  80. });
  81. serf.setLogin(res.data);
  82. if(serf.backpage == '' || !serf.backpage){
  83. uni.redirectTo({
  84. url: '/pages/index/index',
  85. fail:function(e){
  86. console.log(e);
  87. }
  88. })
  89. }else{
  90. uni.navigateTo({
  91. url:serf.backpage
  92. })
  93. }
  94. // setTimeout(()=>{
  95. // uni.redirectTo({
  96. // url: '../index/index'
  97. // });
  98. // },1000);
  99. }).catch(err => {
  100. console.log(err)
  101. });
  102. },
  103. //判断验证是否符合要求
  104. validate(key){
  105. let bool=true;
  106. if(!this.rules[key].rule.test(this[key])){
  107. //提示信息
  108. uni.showToast({
  109. title:this.rules[key].msg,
  110. })
  111. //取反
  112. bool=false;
  113. return false;
  114. }
  115. return bool;
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. /* @import url("login.scss"); */
  122. page{background-color: $uni-bg-color-grey;}
  123. .til{margin: $uni-spacing-col-lg 0;}
  124. .login{padding-top: $uni-spacing-col-lg;}
  125. .uni-input{border-radius:$uni-border-radius-llg;background-color: $uni-bg-color;height: 88rpx;padding-left: 80rpx;}
  126. .uni-form-item{position: relative;margin-bottom: 40rpx;}
  127. .btn{border-radius: $uni-border-radius-llg;}
  128. .uni-form-item-wrap{margin-bottom: 80rpx;}
  129. .eyebtn {position: absolute;height: 100%; width: 50px;top: 0; right: 0;background: transparent;cursor: pointer;border: 0;padding: 0;margin: 0;outline: 0;}
  130. .eyebtn::before,
  131. .eyebtn::after {content:'';position: absolute;top: 0; left: 0; bottom: 0; right: 0;margin: auto;}
  132. .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;}
  133. .eyebtn.password::after {width: 10px;height: 10px;border-radius: 50%;background: #424242;}
  134. .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);}
  135. .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;}
  136. </style>