login - 副本.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 class="wxlogin">
  21. <view open-type="getUserInfo" @getuserinfo="wxLogin" class="wxlogin-btn">
  22. <image class="wxlogin-icon" src="/static/img/wx-icon.png" mode=""></image>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import { mapState, mapMutations } from 'vuex';
  29. export default{
  30. components:{
  31. },
  32. props:{
  33. },
  34. data(){
  35. return{
  36. backpage:'',
  37. //用户输入的内容
  38. userName:"",
  39. userPwd:"",
  40. params:{
  41. username: this.userName,
  42. password: this.userPwd,
  43. grant_type:'password',
  44. client_id:this.config.client_id,
  45. client_secret:this.config.client_secret,
  46. },
  47. viewpassword:false,
  48. //验证的规则
  49. rules:{
  50. userName:{
  51. rule:/\S/,
  52. msg:"账号不能为空"
  53. },
  54. userPwd:{
  55. rule:/^[0-9a-zA-Z]{6,16}$/,
  56. msg:"密码应该为6-16位"
  57. }
  58. }
  59. }
  60. },
  61. onLoad(options){
  62. this.backpage = options.backpage
  63. },
  64. methods:{
  65. ...mapMutations(['setLogin']),
  66. login(){
  67. let serf = this;
  68. if(!this.validate('userName')) return;
  69. if(!this.validate("userPwd")) return;
  70. uni.showLoading({
  71. title:"登录中..."
  72. });
  73. this.$api.http.post(this.config.apiBaseurl+'oauth/token',{
  74. username: this.userName,
  75. password: this.userPwd,
  76. grant_type:'password',
  77. client_id:this.config.client_id,
  78. client_secret:this.config.client_secret,
  79. }).then(res => {
  80. console.log(res);
  81. uni.hideLoading();
  82. uni.showToast({
  83. icon:'none',
  84. title:res.data.message,
  85. duration: 2000
  86. });
  87. serf.setLogin(res.data);
  88. if(serf.backpage == '' || !serf.backpage){
  89. uni.redirectTo({
  90. url: '/pages/index/index',
  91. fail:function(e){
  92. console.log(e);
  93. }
  94. })
  95. }else{
  96. uni.navigateTo({
  97. url:serf.backpage
  98. })
  99. }
  100. // setTimeout(()=>{
  101. // uni.redirectTo({
  102. // url: '../index/index'
  103. // });
  104. // },1000);
  105. }).catch(err => {
  106. console.log(err)
  107. });
  108. },
  109. //微信登录
  110. wxLogin(e){
  111. console.log(e);
  112. let serf = this
  113. let userinfo = e.detail.userInfo
  114. uni.login({
  115. provider:'weixin',
  116. success(res){
  117. let code = res.code
  118. let wxparams = {
  119. code:code,
  120. sex:userinfo.gender,
  121. headImgUrl:userinfo.avatarUrl,
  122. nickname:userinfo.nickName
  123. }
  124. serf.$api.http.post("/wechat/app/login",wxparams).then(res=>{
  125. serf.setLogin(res.retBody)
  126. if(serf.backpage == '' || !serf.backpage){
  127. uni.switchTab({
  128. url: '/pages/userCenter/userCenter'
  129. })
  130. }else{
  131. uni.navigateTo({
  132. url:serf.backpage
  133. })
  134. }
  135. })
  136. }
  137. })
  138. },
  139. //判断验证是否符合要求
  140. validate(key){
  141. let bool=true;
  142. if(!this.rules[key].rule.test(this[key])){
  143. //提示信息
  144. uni.showToast({
  145. title:this.rules[key].msg,
  146. })
  147. //取反
  148. bool=false;
  149. return false;
  150. }
  151. return bool;
  152. },
  153. //显示隐藏密码
  154. eyeclick(){
  155. this.viewpassword = !this.viewpassword;
  156. }
  157. }
  158. }
  159. </script>
  160. <style scoped>
  161. page{background-color: #f8f8f8;}
  162. .til{margin: 24rpx 0;}
  163. .login{padding-top: 24rpx;}
  164. .uni-input{width: 100%;box-sizing: border-box;border-radius:100rpx;background-color: #fff;height: 50px;padding-left: 80rpx;}
  165. .uni-form-item{position: relative;margin-bottom: 40rpx;}
  166. .btn{border-radius: 100rpx;}
  167. .uni-form-item-wrap{margin-bottom: 80rpx;}
  168. .eyebtn {position: absolute;height: 100%; width: 50px;top: 0; right: 0;background: transparent;cursor: pointer;border: 0;padding: 0;margin: 0;outline: 0;}
  169. .eyebtn::before,
  170. .eyebtn::after {content:'';position: absolute;top: 0; left: 0; bottom: 0; right: 0;margin: auto;}
  171. .eyebtn::after{transform-origin:unset;transform: scale(0.8);}
  172. .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;}
  173. .eyebtn.password::after {width: 10px;height: 10px;border-radius: 50%;background: #424242;}
  174. .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);}
  175. .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;}
  176. .wxlogin{height: 100rpx;}
  177. .wxlogin-btn{position: fixed;left: 50%;bottom: 50rpx;width: 100rpx;height: 100rpx;margin-left: -50rpx;background-color: #007AFF;border-radius: 50%;}
  178. .wxlogin-btn .wxlogin-icon{width: 100%;height: 100%;transform: scale(0.5);}
  179. </style>