index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <div class="login-box">
  3. <div class="login-title">
  4. <div class="login-title-value">登录有极</div>
  5. <div class="login-title-line"></div>
  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)"></el-input>
  23. <div :class="codeName === '获取验证码'?'get-code-btn' : 'get-code-btn-disabled get-code-btn'"
  24. @click="getPhoneCode">
  25. {{codeName}}
  26. </div>
  27. </el-form-item>
  28. </el-form>
  29. <el-button type="primary" @click="loginSubmit" :loading="loginLoading">登录</el-button>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. import {
  35. login,
  36. getUserInfo,
  37. selectProject,
  38. selectOrangaized,
  39. getOrganizedProjectList,
  40. getImgCode,
  41. sendPhoneCode
  42. } from '@/httpApi/loginRegister';
  43. import {
  44. bindProjectDetail
  45. } from '@/httpApi/organization'
  46. import {
  47. getProjectDetailById
  48. } from '@/httpApi/space'
  49. import {
  50. setToken,
  51. setComment
  52. } from '@/uitls/auth';
  53. export default {
  54. data() {
  55. return {
  56. loginForm: {
  57. phone: '18888888888',
  58. code: '8',
  59. phoneCode: '888888'
  60. },
  61. loginLoading: false,
  62. codeImg: '',
  63. codeName: '获取验证码', //获取验证码text
  64. isDisabled: false, //获取验证码按钮状态
  65. loginRules: {
  66. code: [{
  67. required: true,
  68. message: '请输入图片验证码',
  69. trigger: 'blur'
  70. }],
  71. phone: [{
  72. required: true,
  73. message: '请输入手机号',
  74. trigger: 'blur'
  75. }, {
  76. validator: (rule, value, callback) => {
  77. if (!/^1[123456789]\d{9}$/.test(value)) {
  78. callback(new Error("请输入正确的手机号"));
  79. } else {
  80. callback();
  81. }
  82. },
  83. trigger: 'blur'
  84. }],
  85. phoneCode: [{
  86. required: true,
  87. message: '请输入短信验证码',
  88. trigger: 'blur'
  89. }]
  90. },
  91. };
  92. },
  93. created() {
  94. this.imgCodeFunc();
  95. if (this.$store.getters.codeNumber != 60) this.codeReset();
  96. },
  97. methods: {
  98. imgCodeFunc() {
  99. getImgCode().then(res => {
  100. if (res.state) {
  101. this.codeImg = res.data.pngBase64;
  102. }
  103. })
  104. },
  105. getPhoneCode() {
  106. if (this.isDisabled) return;
  107. this.$refs.loginForm.validateField('phone', valid => {
  108. if (!valid) {
  109. this.$refs.loginForm.validateField('code', valid => {
  110. if (!valid) {
  111. sendPhoneCode(this.loginForm.phone, this.loginForm.code).then(res => {
  112. if (res.state) {
  113. this.codeReset();
  114. this.$message.success('发送成功');
  115. }
  116. })
  117. }
  118. })
  119. }
  120. })
  121. },
  122. codeReset() {
  123. //重置获取验证码倒计时
  124. let codeNumber = this.$store.getters.codeNumber;
  125. codeNumber--;
  126. this.handleCode(codeNumber);
  127. let codeRestFn = setInterval(() => {
  128. codeNumber--;
  129. this.handleCode(codeNumber);
  130. if (codeNumber == 0) clearInterval(codeRestFn); //停止
  131. }, 1000);
  132. },
  133. handleCode(codeNumber) {
  134. //code操作
  135. this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
  136. this.isDisabled = codeNumber == 0 ? false : true;
  137. this.$store.dispatch('loginRegister/changeCodeNumber', codeNumber == 0 ? 60 : codeNumber);
  138. },
  139. loginSubmit() {
  140. if (this.loginLoading) return;
  141. this.loginLoading = true;
  142. this.$refs['loginForm'].validate(valid => {
  143. if (!valid) return this.loginLoading = false;
  144. this.loginFunc();
  145. })
  146. },
  147. loginFunc() {
  148. let postData = {
  149. phone: this.loginForm.phone,
  150. phoneCode: this.loginForm.phoneCode
  151. }
  152. login(postData).then(res => {
  153. if (res.state) {
  154. setToken(res.data.token);
  155. getUserInfo().then(res => {
  156. if (res.state) {
  157. let user = res.data;
  158. let organized = this.testOrganized(user);
  159. this.selectOrganized(organized);
  160. this.$store.dispatch('app/changeOrganization', organized);
  161. this.$store.dispatch('app/changeUser', user);
  162. if (user.projectId === -1) {
  163. this.getUserProjectList();
  164. } else {
  165. this.selectProject({
  166. projectId: user.projectId,
  167. identityId: 6
  168. })
  169. }
  170. } else {
  171. this.loginLoading = false;
  172. }
  173. })
  174. } else {
  175. this.loginLoading = false;
  176. }
  177. })
  178. },
  179. testOrganized(data) {
  180. let organized = !data.organization ? data.organizationList[0] : data.organization;
  181. return organized;
  182. },
  183. getUserProjectList() {
  184. bindProjectDetail({
  185. userId: this.$store.getters.user.userId
  186. }).then(res => {
  187. if (res.state) {
  188. let clientData = res.data;
  189. bindProjectDetail({
  190. bindOrganizationId: this.$store.getters.organization.id
  191. }).then(res => {
  192. if (res.state) {
  193. let organizationData = res.data;
  194. getOrganizedProjectList(this.$store.getters.organization.id).then(res => {
  195. if (res.state) {
  196. let data = res.data || [];
  197. let obj = []
  198. if (this.$store.getters.user.phone == this.$store.getters
  199. .organization.contactTel) {
  200. obj = data.map(node => {
  201. return {
  202. projectId: node.id,
  203. projectName: node.name,
  204. organizationId: node.organizationId,
  205. organizationName: this.$store.getters
  206. .user.organizationList.find(item =>
  207. item.id === node.organizationId
  208. ).name,
  209. identityId: 6
  210. }
  211. })
  212. }
  213. let projectList = clientData.concat(organizationData, obj)
  214. .sort((a, b) => a.projectId - b.projectId);
  215. if (projectList.length === 0) {
  216. this.loginLoading = false;
  217. this.$store.dispatch('app/changeMenuData', []);
  218. setComment(JSON.stringify([]));
  219. this.$store.dispatch('projectBase/changeProject', {});
  220. localStorage.setItem('projectId', 0);
  221. this.$store.dispatch('app/changeiIdentityId', 6);
  222. this.$message.success('登录成功');
  223. return this.$router.push('/space/project');
  224. } else {
  225. this.selectProject(projectList[0])
  226. }
  227. } else {
  228. this.loginLoading = false;
  229. }
  230. });
  231. } else {
  232. this.loginLoading = false;
  233. }
  234. })
  235. } else {
  236. this.loginLoading = false;
  237. }
  238. })
  239. },
  240. selectProject(item) {
  241. getProjectDetailById(item.projectId).then(res => {
  242. if (res.state) {
  243. let data = res.data;
  244. selectProject(data.id).then(res => {
  245. if (res.state) {
  246. let user = res.data;
  247. this.$store.dispatch('app/changeiIdentityId', item.identityId);
  248. this.$store.dispatch('projectBase/changeProject', data);
  249. localStorage.setItem('projectId', data.id);
  250. this.$store.dispatch('app/changeUser', user);
  251. this.$store.dispatch('app/changeMenuData', user.resource ? JSON.parse(user
  252. .resource) : []);
  253. setComment(user.menu ? user.menu : JSON.stringify([]));
  254. this.loginLoading = false;
  255. this.$router.push('/');
  256. this.$message.success('登录成功');
  257. } else {
  258. this.loginLoading = false;
  259. }
  260. });
  261. } else {
  262. this.loginLoading = false;
  263. }
  264. })
  265. },
  266. selectOrganized(data) {
  267. selectOrangaized(data)
  268. }
  269. }
  270. };
  271. </script>
  272. <style lang="scss">
  273. </style>