login.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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:"",
  34. userPwd:"",
  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. if(res.data.state == 'error'){
  83. uni.showToast({
  84. icon:'none',
  85. title:res.data.message,
  86. duration: 2000
  87. });
  88. return;
  89. };
  90. serf.setLogin(res.data);
  91. if(serf.backpage == '' || !serf.backpage){
  92. uni.redirectTo({
  93. url: '/pages/index/index',
  94. fail:function(e){
  95. console.log(e);
  96. }
  97. })
  98. }else{
  99. uni.navigateTo({
  100. url:serf.backpage
  101. })
  102. }
  103. // setTimeout(()=>{
  104. // uni.redirectTo({
  105. // url: '../index/index'
  106. // });
  107. // },1000);
  108. }).catch(err => {
  109. console.log(err)
  110. });
  111. },
  112. //判断验证是否符合要求
  113. validate(key){
  114. let bool=true;
  115. if(!this.rules[key].rule.test(this[key])){
  116. //提示信息
  117. uni.showToast({
  118. icon:'none',
  119. title:this.rules[key].msg,
  120. })
  121. //取反
  122. bool=false;
  123. return false;
  124. }
  125. return bool;
  126. },
  127. //显示隐藏密码
  128. eyeclick(){
  129. this.viewpassword = !this.viewpassword;
  130. }
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. /* @import url("login.scss"); */
  136. page{background-color: $uni-bg-color-grey;}
  137. .til{margin: $uni-spacing-col-lg 0;}
  138. .login{padding-top: $uni-spacing-col-lg;}
  139. .uni-input{width: 100%;box-sizing: border-box;border-radius:$uni-border-radius-llg;background-color: $uni-bg-color;height: 50px;padding-left: 80rpx;}
  140. .uni-form-item{position: relative;margin-bottom: 40rpx;}
  141. .btn{border-radius: $uni-border-radius-llg;}
  142. .uni-form-item-wrap{margin-bottom: 80rpx;}
  143. .eyebtn {position: absolute;height: 100%; width: 50px;top: 0; right: 0;background: transparent;cursor: pointer;border: 0;padding: 0;margin: 0;outline: 0;}
  144. .eyebtn::before,
  145. .eyebtn::after {content:'';position: absolute;top: 0; left: 0; bottom: 0; right: 0;margin: auto;}
  146. .eyebtn::after{transform-origin:unset;transform: scale(0.8);}
  147. .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;}
  148. .eyebtn.password::after {width: 10px;height: 10px;border-radius: 50%;background: #424242;}
  149. .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);}
  150. .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;}
  151. </style>