login.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="login-container">
  3. <view class="uni-padding-wrap uni-common-mt">
  4. <form @submit="formSubmit" >
  5. <view class="uni-form-item uni-column" style="background-color: #fff;">
  6. <image style="width:61px;height: 61px;margin: 5px auto;" :src="src" ></image>
  7. </view>
  8. <org-input-text left-text="账号" placeholder="请输入账号" :check-info="{msg:'账号至少2位',reg:'^.{1,}$',required:true}" v-model="username" >
  9. <!-- 使用插槽,自定义左侧文本 -->
  10. <view slot="left" >
  11. <uni-icons type="person" size="30" color="#6666" style="padding-left: 10rpx;"></uni-icons>
  12. </view>
  13. </org-input-text>
  14. <org-input-text left-text="密码" :is-password="true" placeholder="请输入密码" :check-info="{msg:'账号至少4位',reg:'^.{1,}$',required:true}" v-model="password" >
  15. <!-- 使用插槽,自定义左侧文本 -->
  16. <view slot="left" style="padding-left: 10rpx;">
  17. <uni-icons type="eye" size="30" color="#6666"></uni-icons>
  18. </view>
  19. </org-input-text>
  20. <view class="uni-btn-v uni-column">
  21. <button type="primary" form-type="submit" style="border-radius: 15px;" >登录</button>
  22. </view>
  23. </form>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. login,
  30. getInfo,
  31. logout
  32. } from "@/common/api/loginApi.js";
  33. var graceChecker = require("../../common/graceChecker.js");
  34. import {
  35. setToken,getToken
  36. } from "@/common/auth.js"
  37. //import store from '@/store/index.js'
  38. var duration = 2000;
  39. export default {
  40. data() {
  41. return {
  42. username:'',
  43. password:'',
  44. src: '/static/haode.png'
  45. }
  46. },
  47. methods: {
  48. onInput1:function(){
  49. console.info("oninput1");
  50. console.info(this.username);
  51. //console.log(this.username);
  52. },
  53. onInput2:function(){
  54. console.info("oninput2");
  55. console.info(this.password);
  56. //console.log(this.password);
  57. },
  58. formSubmit: function(e) {
  59. //console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
  60. //定义表单规则
  61. var rule = [{
  62. name: "username",
  63. checkType: "string",
  64. checkRule: "2,22",
  65. errorMsg: "账号应为2-22个字符"
  66. },
  67. {
  68. name: "password",
  69. checkType: "string",
  70. checkRule: "4,",
  71. errorMsg: "密码必须大于4位"
  72. }
  73. ];
  74. //console.info(this.username);
  75. //console.info(this.password);
  76. let formData={username:this.username,password:this.password};
  77. //进行表单检查
  78. //var formData = e.detail.value;
  79. var checkRes = graceChecker.check(formData, rule);
  80. //console.info(formData);
  81. //return;
  82. if (checkRes) {
  83. //uni.showToast({title:"验证通过!", icon:"none"});
  84. this.loading = true;
  85. //this.$store.dispatch('user/login', this.formData)
  86. login(formData)
  87. .then((res) => {
  88. console.info(res);
  89. if (!res.isSuccess) //失败,显示错误
  90. {
  91. uni.showToast({
  92. title: res.errMsg,
  93. icon: 'none'
  94. });
  95. return;
  96. }
  97. //设置状态中的各种值
  98. this.$store.commit('setIsLogin',true);
  99. this.$store.commit('setToken',res.tnToken);
  100. this.$store.commit('setAvatar',res.data.avatar);
  101. this.$store.commit('setName',res.data.name);
  102. this.$store.commit('setIntroduction',res.data.introduction);
  103. this.$store.commit('setRoles',res.data.roles);
  104. this.$store.commit('setUser',res.data.user);
  105. this.$store.commit('setStationName',res.data.stationName);
  106. this.$store.commit('setStationCode',res.data.stationCode);
  107. this.$store.commit('setDepartmentName',res.data.departmentName);
  108. this.$store.commit('setDepartmentCode',res.data.departmentCode);
  109. console.info("登录页面state:",this.$store.state);
  110. uni.setStorage({
  111. key:'storage_state',
  112. data:this.$store.state,
  113. success:function(){
  114. console.info("setStorage状态缓存成功");
  115. console.info('storage_state',uni.getStorageSync('storage_state'));
  116. },
  117. fail: () => {
  118. console.error("setStorage状态缓存失败");
  119. }
  120. });
  121. this.loading = false;
  122. if(this.$store.state.projectCode==="")
  123. {
  124. uni.navigateTo({
  125. url: "/pages/SelectProject/SelectProject"
  126. });
  127. return;
  128. }
  129. //导向到首页
  130. uni.navigateTo({
  131. url: "/pages/index/index"
  132. });
  133. })
  134. .catch((error) => {
  135. console.log(error);
  136. this.loading = false;
  137. })
  138. } else {
  139. uni.showToast({
  140. title: graceChecker.error,
  141. icon: "none"
  142. });
  143. }
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .login-container {
  150. display: flex;
  151. width: 400px;
  152. margin: 50px auto;
  153. }
  154. </style>