login.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <div class="website-login-box">
  3. <div class="login-box">
  4. <div class="close-icon-box">
  5. <i class="el-icon-circle-close close-icon" @click="$emit('callback')"></i>
  6. </div>
  7. <div class="login-form">
  8. <el-form :model="loginForm" :rules="loginRules" status-icon ref="loginForm" label-position="left">
  9. <el-form-item prop="phone">
  10. <el-input type="text" prefix-icon="iconfont huifont-shoujihao" v-model="loginForm.phone"
  11. placeholder="手机号" maxlength="11">
  12. </el-input>
  13. </el-form-item>
  14. <el-form-item prop="code" class="image-item">
  15. <el-input type="text" prefix-icon="iconfont huifont-tuxingyanzhengma" v-model="loginForm.code"
  16. placeholder="图片验证码" maxlength="4"></el-input>
  17. <img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
  18. </el-form-item>
  19. <el-form-item prop="phoneCode" class="phone-code">
  20. <el-input type="number" prefix-icon="iconfont huifont-duanxinyanzhengma"
  21. v-model="loginForm.phoneCode" placeholder="短信验证码"
  22. oninput="if(value.length > 6) value=value.slice(0, 6)">
  23. </el-input>
  24. <div class="get-code-btn">
  25. <el-button type="primary" size="samll" @click="getPhoneCode"
  26. :disabled="codeName !== '获取验证码'">
  27. {{codeName}}
  28. </el-button>
  29. </div>
  30. </el-form-item>
  31. </el-form>
  32. <el-button class="submit" type="primary" @click="loginSubmit" :loading="loginLoading">登录</el-button>
  33. </div>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. import {
  39. login,
  40. getImgCode,
  41. sendPhoneCode,
  42. getUserInfo,
  43. selectOrangaized
  44. } from '@/api/loginRegister';
  45. import {
  46. setToken,
  47. setComment
  48. } from '@/uitls/auth';
  49. export default {
  50. data() {
  51. return {
  52. loginForm: {
  53. phone: '',
  54. code: '',
  55. phoneCode: ''
  56. },
  57. loginLoading: false,
  58. codeImg: '',
  59. codeName: '获取验证码', //获取验证码text
  60. isDisabled: false, //获取验证码按钮状态
  61. loginRules: {
  62. code: [{
  63. required: true,
  64. message: '请输入图片验证码',
  65. trigger: 'blur'
  66. }],
  67. phone: [{
  68. required: true,
  69. message: '请输入手机号',
  70. trigger: 'blur'
  71. }, {
  72. validator: (rule, value, callback) => {
  73. if (!/^1[123456789]\d{9}$/.test(value)) {
  74. callback(new Error("请输入正确的手机号"));
  75. } else {
  76. callback();
  77. }
  78. },
  79. trigger: 'blur'
  80. }],
  81. phoneCode: [{
  82. required: true,
  83. message: '请输入短信验证码',
  84. trigger: 'blur'
  85. }]
  86. },
  87. };
  88. },
  89. mounted() {
  90. this.imgCodeFunc();
  91. if (this.$store.getters.codeNumber != 60) this.codeReset();
  92. },
  93. methods: {
  94. imgCodeFunc() {
  95. getImgCode().then(res => {
  96. if (res.state) {
  97. this.codeImg = res.data.pngBase64;
  98. }
  99. })
  100. },
  101. getPhoneCode() {
  102. if (this.isDisabled) return;
  103. this.$refs.loginForm.validateField('phone', valid => {
  104. if (!valid) {
  105. this.$refs.loginForm.validateField('code', valid => {
  106. if (!valid) {
  107. sendPhoneCode(this.loginForm.phone, this.loginForm.code).then(res => {
  108. if (res.state) {
  109. this.codeReset();
  110. this.$message.success('发送成功');
  111. }
  112. })
  113. }
  114. })
  115. }
  116. })
  117. },
  118. codeReset() {
  119. //重置获取验证码倒计时
  120. let codeNumber = this.$store.getters.codeNumber;
  121. codeNumber--;
  122. this.handleCode(codeNumber);
  123. let codeRestFn = setInterval(() => {
  124. codeNumber--;
  125. this.handleCode(codeNumber);
  126. if (codeNumber == 0) clearInterval(codeRestFn); //停止
  127. }, 1000);
  128. },
  129. handleCode(codeNumber) {
  130. //code操作
  131. this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
  132. this.isDisabled = codeNumber == 0 ? false : true;
  133. this.$store.dispatch('app/changeCodeNumber', codeNumber == 0 ? 60 : codeNumber);
  134. },
  135. loginSubmit() {
  136. if (this.loginLoading) return;
  137. this.loginLoading = true;
  138. this.$refs['loginForm'].validate(valid => {
  139. if (!valid) return this.loginLoading = false;
  140. this.loginFunc();
  141. })
  142. },
  143. loginFunc() {
  144. let postData = {
  145. phone: this.loginForm.phone,
  146. phoneCode: this.loginForm.phoneCode
  147. }
  148. login(postData).then(res => {
  149. if (res.state) {
  150. setToken(res.data.token);
  151. getUserInfo().then(res => {
  152. if (res.state) {
  153. let user = res.data;
  154. let organization = user.organization;
  155. if (!organization) {
  156. organziation = user.organizationList[0];
  157. selectOrangaized(organization)
  158. }
  159. this.$store.dispatch('app/changeOrganization', organization);
  160. this.$store.dispatch('app/changeUser', user);
  161. this.$store.dispatch('app/changeMenuData', user.workarkResource ? JSON
  162. .parse(user.workarkResource) : []);
  163. setComment(user.workarkMenu ? user.workarkMenu : JSON.stringify([]));
  164. return this.successLogin('/work');
  165. } else {
  166. this.loginLoading = false;
  167. }
  168. })
  169. } else {
  170. this.loginLoading = false;
  171. }
  172. })
  173. },
  174. successLogin(url) {
  175. this.loginLoading = false;
  176. this.$router.push(url);
  177. this.$message.success('登录成功');
  178. }
  179. }
  180. };
  181. </script>
  182. <style lang="scss" scoped>
  183. .website-login-box {
  184. width: 100%;
  185. height: 100%;
  186. display: flex;
  187. align-items: center;
  188. justify-content: center;
  189. }
  190. .close-icon {
  191. font-size: 30px;
  192. color: #c9cdd3;
  193. cursor: pointer;
  194. }
  195. .close-icon-box {
  196. padding: 10px 10px 20px 10px;
  197. text-align: right;
  198. }
  199. .login-box {
  200. width: 440px;
  201. background: $--color-white;
  202. box-shadow: 0px 4px 16px 0px rgba(164, 178, 203, 0.15);
  203. border-radius: 8px;
  204. border: 0px solid $--color-white;
  205. box-sizing: border-box;
  206. }
  207. .login-title {
  208. padding: 52px 52px 48px 52px;
  209. }
  210. .login-title-value {
  211. font-weight: 600;
  212. font-size: 28px;
  213. color: #333E4D;
  214. line-height: 40px;
  215. text-align: left;
  216. font-style: normal;
  217. margin-bottom: 16px;
  218. }
  219. .login-title-line {
  220. width: 80px;
  221. height: 5px;
  222. background: #E3E7EE;
  223. }
  224. .login-form {
  225. padding: 0 52px;
  226. }
  227. ::v-deep.el-form {
  228. display: block;
  229. .el-form-item {
  230. width: 100%;
  231. padding: 0;
  232. margin-bottom: 28px;
  233. position: relative;
  234. }
  235. .el-input__inner {
  236. padding: 15px 15px 15px 63px;
  237. height: 52px;
  238. line-height: 52px;
  239. font-size: 16px;
  240. }
  241. .el-input__icon,
  242. .el-range-separator {
  243. line-height: 52px;
  244. font-size: 16px;
  245. }
  246. .el-input__prefix {
  247. left: 16px;
  248. }
  249. .el-input__prefix::before {
  250. content: '';
  251. top: 16px;
  252. right: -16px;
  253. height: 21px;
  254. width: 1px;
  255. background-color: $--input-border-color;
  256. position: absolute;
  257. }
  258. .el-input__icon.iconfont {
  259. font-size: 20px;
  260. color: #596d8e;
  261. }
  262. .el-input__icon.huifont-mima {
  263. opacity: 0.95;
  264. margin-left: 2px;
  265. }
  266. .image-item {
  267. .el-form-item__content {
  268. display: flex;
  269. }
  270. .el-input {
  271. width: 190px;
  272. margin-right: 12px;
  273. }
  274. .code-image {
  275. width: 131px;
  276. height: 50px;
  277. cursor: pointer;
  278. }
  279. }
  280. .get-code-btn {
  281. position: absolute;
  282. top: 6px;
  283. right: 30px;
  284. }
  285. }
  286. .submit {
  287. width: 100%;
  288. margin-bottom: 60px;
  289. font-weight: 600;
  290. font-size: 20px;
  291. line-height: 28px;
  292. }
  293. </style>