login.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view class="login-container" v-if="success">
  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. <view class="login-container" v-else style="background-color: #F26C69;height:100%;margin-top:-5px;">
  27. <text style="color:white;font-size:35px;margin: auto auto;">移动工程</text>
  28. </view>
  29. </template>
  30. <script>
  31. import {
  32. login,
  33. getInfo,
  34. logout
  35. } from "@/common/api/loginApi.js";
  36. var graceChecker = require("../../common/graceChecker.js");
  37. import {
  38. setToken,getToken
  39. } from "@/common/auth.js"
  40. //import store from '@/store/index.js'
  41. var duration = 2000;
  42. export default {
  43. data() {
  44. return {
  45. username:'',
  46. password:'',
  47. src: '/static/haode.png',
  48. success:false
  49. }
  50. },
  51. created:function(){
  52. var that=this;
  53. console.info('pre',new Date());
  54. uni.setNavigationBarTitle({
  55. title: ''
  56. });
  57. // var pages = getCurrentPages();
  58. // var page = pages[0];
  59. // console.info('page',page);
  60. setTimeout(function(){that.delay();},1000);
  61. },
  62. methods: {
  63. delay:function(){
  64. this.success=true;
  65. uni.setNavigationBarTitle({
  66. title: '登录页'
  67. });
  68. console.info('pre1',new Date());
  69. },
  70. onInput1:function(){
  71. console.info("oninput1");
  72. console.info(this.username);
  73. //console.log(this.username);
  74. },
  75. onInput2:function(){
  76. console.info("oninput2");
  77. console.info(this.password);
  78. //console.log(this.password);
  79. },
  80. formSubmit: function(e) {
  81. //console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
  82. //定义表单规则
  83. var rule = [{
  84. name: "username",
  85. checkType: "string",
  86. checkRule: "2,22",
  87. errorMsg: "账号应为2-22个字符"
  88. },
  89. {
  90. name: "password",
  91. checkType: "string",
  92. checkRule: "4,",
  93. errorMsg: "密码必须大于4位"
  94. }
  95. ];
  96. //console.info(this.username);
  97. //console.info(this.password);
  98. let formData={username:this.username,password:this.password};
  99. //进行表单检查
  100. //var formData = e.detail.value;
  101. var checkRes = graceChecker.check(formData, rule);
  102. //console.info(formData);
  103. //return;
  104. if (checkRes) {
  105. //uni.showToast({title:"验证通过!", icon:"none"});
  106. this.loading = true;
  107. //this.$store.dispatch('user/login', this.formData)
  108. login(formData)
  109. .then((res) => {
  110. console.info(res);
  111. if (!res.isSuccess) //失败,显示错误
  112. {
  113. uni.showToast({
  114. title: res.errMsg,
  115. icon: 'none'
  116. });
  117. return;
  118. }
  119. //设置状态中的各种值
  120. this.$store.commit('setIsLogin',true);
  121. this.$store.commit('setToken',res.tnToken);
  122. this.$store.commit('setAvatar',res.data.avatar);
  123. this.$store.commit('setName',res.data.name);
  124. this.$store.commit('setIntroduction',res.data.introduction);
  125. this.$store.commit('setRoles',res.data.roles);
  126. this.$store.commit('setUser',res.data.user);
  127. this.$store.commit('setStationName',res.data.stationName);
  128. this.$store.commit('setStationCode',res.data.stationCode);
  129. this.$store.commit('setDepartmentName',res.data.departmentName);
  130. this.$store.commit('setDepartmentCode',res.data.departmentCode);
  131. console.info("登录页面state:",this.$store.state);
  132. uni.setStorage({
  133. key:'storage_state',
  134. data:this.$store.state,
  135. success:function(){
  136. console.info("setStorage状态缓存成功");
  137. console.info('storage_state',uni.getStorageSync('storage_state'));
  138. },
  139. fail: () => {
  140. console.error("setStorage状态缓存失败");
  141. }
  142. });
  143. this.loading = false;
  144. if(this.$store.state.projectCode==="")
  145. {
  146. uni.navigateTo({
  147. url: "/pages/SelectProject/SelectProject"
  148. });
  149. return;
  150. }
  151. //导向到首页
  152. uni.navigateTo({
  153. url: "/pages/index/index"
  154. });
  155. })
  156. .catch((error) => {
  157. console.log(error);
  158. this.loading = false;
  159. })
  160. } else {
  161. uni.showToast({
  162. title: graceChecker.error,
  163. icon: "none"
  164. });
  165. }
  166. }
  167. }
  168. }
  169. </script>
  170. <style scoped>
  171. .login-container {
  172. display: flex;
  173. width: 400px;
  174. margin: 50px auto;
  175. }
  176. </style>