login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //isSupplier:'',
  45. src: '/static/haode.png'
  46. }
  47. },
  48. methods: {
  49. onInput1:function(){
  50. console.info("oninput1");
  51. console.info(this.username);
  52. //console.log(this.username);
  53. },
  54. onInput2:function(){
  55. console.info("oninput2");
  56. console.info(this.password);
  57. //console.log(this.password);
  58. },
  59. formSubmit: function(e) {
  60. //console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
  61. //定义表单规则
  62. var rule = [{
  63. name: "username",
  64. checkType: "string",
  65. checkRule: "2,22",
  66. errorMsg: "账号应为2-22个字符"
  67. },
  68. {
  69. name: "password",
  70. checkType: "string",
  71. checkRule: "4,",
  72. errorMsg: "密码必须大于4位"
  73. }
  74. ];
  75. //console.info(this.username);
  76. //console.info(this.password);
  77. //console.info("isSupplier",this.isSupplier);
  78. //return;
  79. let formData={username:this.username,password:this.password};
  80. //进行表单检查
  81. //var formData = e.detail.value;
  82. var checkRes = graceChecker.check(formData, rule);
  83. //console.info(formData);
  84. //return;
  85. if (checkRes) {
  86. //uni.showToast({title:"验证通过!", icon:"none"});
  87. this.loading = true;
  88. //this.$store.dispatch('user/login', this.formData)
  89. login(formData)
  90. .then((res) => {
  91. console.info(res);
  92. if (!res.isSuccess) //失败,显示错误
  93. {
  94. uni.showToast({
  95. title: res.errMsg,
  96. icon: 'none'
  97. });
  98. return;
  99. }
  100. //设置状态中的各种值
  101. this.$store.commit('setIsLogin',true);
  102. this.$store.commit('setToken',res.tnToken);
  103. this.$store.commit('setAvatar',res.data.avatar);
  104. this.$store.commit('setName',res.data.name);
  105. this.$store.commit('setIntroduction',res.data.introduction);
  106. this.$store.commit('setRoles',res.data.roles);
  107. this.$store.commit('setUser',res.data.user);
  108. this.$store.commit('setStationName',res.data.stationName);
  109. this.$store.commit('setStationCode',res.data.stationCode);
  110. this.$store.commit('setDepartmentName',res.data.departmentName);
  111. this.$store.commit('setDepartmentCode',res.data.departmentCode);
  112. console.info("登录页面state:",this.$store.state);
  113. uni.setStorage({
  114. key:'storage_state',
  115. data:this.$store.state,
  116. success:function(){
  117. console.info("setStorage状态缓存成功");
  118. console.info('storage_state',uni.getStorageSync('storage_state'));
  119. },
  120. fail: () => {
  121. console.error("setStorage状态缓存失败");
  122. }
  123. });
  124. this.loading = false;
  125. if(this.$store.state.projectCode==="")
  126. {
  127. uni.navigateTo({
  128. url: "/pages/SelectProject/SelectProject"
  129. });
  130. return;
  131. }
  132. //导向到首页
  133. uni.navigateTo({
  134. url: "/pages/index/index"
  135. });
  136. })
  137. .catch((error) => {
  138. console.log(error);
  139. this.loading = false;
  140. })
  141. } else {
  142. uni.showToast({
  143. title: graceChecker.error,
  144. icon: "none"
  145. });
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style scoped>
  152. .login-container {
  153. display: flex;
  154. width: 400px;
  155. margin: 50px auto;
  156. }
  157. </style>