index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <header class="login">
  3. <van-image class="login_logo" width="100" height="100" :src="require('../../assets/img/login_logo.png')"/>
  4. <div class="login_title">工时管家</div>
  5. <div class="login_subtitle">欢迎使用移动端工时管家</div>
  6. <van-form class="login_form" @submit="login">
  7. <van-field class="form_input" v-model="form.username" name="username" label="账号" placeholder="请输入账号" :rules="rules.username" />
  8. <van-field class="form_input" v-model="form.password" type="password" name="password" label="密码" placeholder="请输入密码" :rules="rules.password" />
  9. <div class="form_btn" style="margin: 16px;">
  10. <van-button round block type="info" native-type="submit"> 登录 </van-button>
  11. </div>
  12. <div class="form_jump" v-on:click="jumpTo">企业注册</div>
  13. </van-form>
  14. </header>
  15. </template>
  16. <script>
  17. import { constants } from "crypto";
  18. import * as dd from 'dingtalk-jsapi';
  19. export default {
  20. data() {
  21. return {
  22. defaultHeight: '0', //默认屏幕高度
  23. nowHeight: '0', //实时屏幕高度
  24. form: {
  25. username: "",
  26. password: "",
  27. },
  28. rules: {
  29. username: [{ required: true, message: '请输入账号' }],
  30. password: [{ required: true, message: '请输入密码' }]
  31. }
  32. };
  33. },
  34. methods: {
  35. login() {
  36. const toast = this.$toast.loading({
  37. forbidClick: true,
  38. duration: 0
  39. });
  40. this.$axios.post("/user/loginAdmin", this.form)
  41. .then(res => {
  42. if(res.code == "ok") {
  43. toast.clear();
  44. this.$toast.success('登录成功');
  45. let user = res.data;
  46. this.$store.commit("updateLogin", true);
  47. localStorage.userInfo = JSON.stringify(user);
  48. this.$router.push("/index");
  49. //强制刷新,避免index页面中的mounted不执行
  50. window.location.reload();
  51. } else {
  52. toast.clear();
  53. this.$toast.fail(res.msg);
  54. }
  55. }).catch(err=> {toast.clear();});
  56. },
  57. jumpTo() {
  58. this.$router.push("/register");
  59. },
  60. loginByCode(code, corpId) {
  61. this.$axios.post("/dingding/getUserByCode", {code:code, corpid:corpId})
  62. .then(res => {
  63. if(res.code == "ok") {
  64. let user = res.data;
  65. this.$store.commit("updateLogin", true);
  66. localStorage.userInfo = JSON.stringify(user);
  67. this.$router.push("/index");
  68. //强制刷新,避免index页面中的mounted不执行
  69. // window.location.reload();
  70. } else {
  71. this.$toast.fail(res.msg);
  72. }
  73. }).catch(err=> {toast.clear();});
  74. },
  75. },
  76. created() {
  77. if (localStorage.userInfo != null) {
  78. this.$router.push("/index");
  79. }
  80. },
  81. mounted() {
  82. if (localStorage.userInfo != null) {
  83. this.$router.push("/index");
  84. } else {
  85. //检查环境,如果是钉钉有$CORPID$
  86. var key = '?corpid=';
  87. var url = location.href;
  88. var that = this;
  89. if (url.indexOf(key) > 0) {
  90. var corpId = url.substring(url.indexOf(key)+key.length,url.indexOf('#'));
  91. dd.ready(function() {
  92. dd.runtime.permission.requestAuthCode({
  93. corpId: corpId, // 企业id
  94. onSuccess: function (info) {
  95. var code = info.code // 通过该免登授权码可以获取用户身份
  96. that.loginByCode(code, corpId);
  97. }});
  98. });
  99. }
  100. }
  101. }
  102. };
  103. </script>
  104. <style lang="less" scoped>
  105. .logo {
  106. font-size: 100px !important;
  107. margin-bottom: 150px;
  108. }
  109. /* 本页公共样式 */
  110. .login {
  111. height: 100vh;
  112. background-color: #fff;
  113. }
  114. header {
  115. text-align: center;
  116. }
  117. // 手机号码
  118. .login_logo {
  119. margin: 55px 0 30px;
  120. }
  121. .login_title {
  122. font-size: 24px;
  123. color: #20a0ff;
  124. margin: 0 0 10px 0;
  125. }
  126. .login_subtitle {
  127. font-size: 14px;
  128. color: #afafaf;
  129. margin: 0 0 40px 0;
  130. }
  131. .login_form {
  132. .form_input {
  133. margin: 0 0 30px 0;
  134. }
  135. .form_btn {
  136. width: 80%;
  137. margin: 0 auto !important;
  138. button {
  139. background-color: #20a0ff;
  140. }
  141. }
  142. .form_jump {
  143. margin: 20px 0 0 0;
  144. color: #20a0ff;
  145. font-size: 14px;
  146. }
  147. }
  148. </style>