Login.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div class="login">
  3. <div class="shade">
  4. <header class="header">
  5. <img class="logo" src="../../assets/logo.png" alt="慧牧科技LOGO" />
  6. 慧牧科技
  7. </header>
  8. <article class="article">
  9. <h1>登录界面</h1>
  10. <el-form label-position="right" label-width="80px" :model="form">
  11. <el-form-item label="账号:">
  12. <el-input v-model="form.userName" placeholder="请输入账号"></el-input>
  13. </el-form-item>
  14. <el-form-item label="密码:">
  15. <el-input v-model="form.userPwd" placeholder="请输入密码" show-password></el-input>
  16. </el-form-item>
  17. <el-form-item>
  18. <el-button type="primary" @click="onSignIn">登录</el-button>
  19. </el-form-item>
  20. </el-form>
  21. </article>
  22. <el-dialog title="请选择一个组织" :visible.sync="isShowDialog" append-to-body>
  23. <el-select v-model="orgSelected">
  24. <el-option
  25. v-for="item in orgList"
  26. :key="item.id"
  27. :value="item.id"
  28. :label="item.orgName"
  29. ></el-option>
  30. </el-select>
  31. <div slot="footer">
  32. <el-button @click="isShowDialog=false">取 消</el-button>
  33. <el-button type="primary" @click="handleSet">设置</el-button>
  34. </div>
  35. </el-dialog>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import { mapActions,mapMutations } from "vuex";
  41. import sdk from "../../sdk/index";
  42. export default {
  43. name: "login",
  44. data() {
  45. return {
  46. form: {
  47. userName: "yjj",
  48. userPwd: "147414"
  49. },
  50. isShowDialog: false,
  51. orgList: null,
  52. orgSelected: null
  53. };
  54. },
  55. created() {
  56. //注册一个登录失败的事件
  57. sdk.regErrHanlder({
  58. errCode: "login_failed",
  59. action: e => {
  60. if (e.errMsg) {
  61. this.$message.error(e.errMsg);
  62. }
  63. }
  64. });
  65. },
  66. methods: {
  67. ...mapActions(["login", "fetch"]),
  68. ...mapMutations(["setLoginIp"]),
  69. handleSet: function() {
  70. if (!this.orgSelected) {
  71. this.$message.info("请先选择组织");
  72. } else this.setOrganizationId(this.orgSelected);
  73. },
  74. /* 登录按钮 */
  75. onSignIn() {
  76. this.fetch({
  77. api: "/core/auth/login",
  78. method: "POST",
  79. data: this.form,
  80. success: res => {
  81. console.log("登录结果", res);
  82. localStorage.setItem("token", res.token)
  83. this.login(res.token)
  84. this.setLoginIp(res.loginIp)
  85. localStorage.setItem("loginIp", res.loginIp)
  86. // 显示一个dialog 让用户选择组
  87. this.doOrganizationChoose()
  88. },
  89. fail: err => {
  90. console.log("登录失败", err);
  91. }
  92. });
  93. },
  94. doOrganizationChoose() {
  95. this.loading = true;
  96. this.fetch({
  97. api: "/core/employ-relation/under-list",
  98. method: "GET",
  99. success: res => {
  100. if (res == null || res.length == 0) {
  101. this.$message.info("您没有加入任何组织!");
  102. } else if (res.length > 1) {
  103. this.isShowDialog = true;
  104. this.orgList = res;
  105. } else {
  106. this.setOrganizationId(res[0].id);
  107. }
  108. },
  109. fail: err => {},
  110. complete: res => {
  111. this.loading = false;
  112. }
  113. });
  114. },
  115. setOrganizationId(id) {
  116. if (!id) {
  117. this.$message.error("发生了一点不愉快的错误!");
  118. return;
  119. }
  120. this.loading = true;
  121. this.fetch({
  122. api: "/core/auth/choose-org",
  123. method: "POST",
  124. data: { orgId: id },
  125. success: res => {
  126. this.isShowDialog = false;
  127. //导航到 正式页面
  128. this.$router.replace("/deviceType");
  129. },
  130. fail: err => {
  131. if (err.errMsg) this.$message.error(err.errMsg);
  132. else this.$message.error("服务器发生异常");
  133. },
  134. complete: res => {
  135. this.loading = false;
  136. }
  137. });
  138. }
  139. },
  140. mounted() {}
  141. };
  142. </script>
  143. <style lang="scss" scoped>
  144. .login {
  145. width: 100%;
  146. height: 100%;
  147. background: url(../../assets/login_bg.jpg) 0 0 no-repeat;
  148. background-size: 100%;
  149. .shade {
  150. height: 100%;
  151. width: 100%;
  152. background-color: #8899aa44;
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. .header {
  157. font-size: 60px;
  158. color: #eee;
  159. margin: 180px 0 50px 0;
  160. display: flex;
  161. justify-content: center;
  162. align-items: center;
  163. .logo {
  164. height: 70px;
  165. width: 70px;
  166. margin-right: 20px;
  167. }
  168. }
  169. .article {
  170. width: 400px;
  171. background-color: #ffffff44;
  172. padding: 20px 60px 40px 20px;
  173. border-radius: 20px;
  174. h1 {
  175. font-size: 20px;
  176. color: #eee;
  177. margin-left: 25px;
  178. }
  179. /deep/ .el-form-item .el-form-item__label {
  180. color: #eee;
  181. }
  182. }
  183. }
  184. }
  185. </style>