You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
18 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
  1. <template>
  2. <div>
  3. <div v-if="!isRestPwd">
  4. <div class="login_form">
  5. <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm">
  6. <el-form-item prop="displayUserid" v-show="loginForm.authType=='password_display_userid'">
  7. <el-input class="inp" type="text" v-model="loginForm.displayUserid" autoComplete="on" placeholder="账号"></el-input>
  8. </el-form-item>
  9. <el-form-item prop="password" v-show="loginForm.authType=='password_display_userid'">
  10. <el-input class="inp" show-password v-model="loginForm.password" autoComplete="on" placeholder="密码"></el-input>
  11. </el-form-item>
  12. <el-form-item prop="phoneno" v-show="loginForm.authType=='sms'">
  13. <el-input class="inp" name="phoneno" type="text" v-model="loginForm.phoneno" autoComplete="on" placeholder="手机号码">
  14. </el-input>
  15. </el-form-item>
  16. <el-form-item prop="smsCode" v-show="loginForm.authType=='sms'">
  17. <el-input class="inp smsCode" name="smsCode" type="text" v-model="loginForm.smsCode" autoComplete="on" placeholder="短信验证码">
  18. </el-input>
  19. <el-button class="sendCode" :disabled="abledBut" @click.prevent="sendPhonenoSmsCode('login')">
  20. <span class="text" v-if="!abledBut">发送验证码</span>
  21. <span class="text" v-else>({{setTimeNum}}s)</span>
  22. </el-button>
  23. </el-form-item>
  24. </el-form>
  25. <div class="oper">
  26. <a @click="isRestPwd = true">忘记密码</a>
  27. <a @click="loginByShowAccount">演示账户登录</a>
  28. <a v-if="loginForm.authType == 'password_display_userid' " @click="loginForm.authType = 'sms'">手机号登录</a>
  29. <a v-if="loginForm.authType == 'sms' " @click="loginForm.authType = 'password_display_userid' ">密码登录</a>
  30. </div>
  31. <el-button class="login" :loading="loading" @click.native.prevent="handleLogin">
  32. <span class="text"> </span>
  33. </el-button>
  34. </div>
  35. <div class="bottom">
  36. <el-divider content-position="center">第三方登录方式</el-divider>
  37. <div class="other">
  38. <el-popover trigger="manual" v-model="wxLoginCodeVisible" placement="top-start"> <div id="login_container"></div>
  39. <img slot="reference" src="@/assets/image/module/weixin.png" @click="weixinLogin">
  40. <el-button type="text" @click="wxLoginCodeVisible=false" icon="el-icon-close">关闭</el-button><el-button type="text" @click="weixinLogin" icon="el-icon-refresh"></el-button>
  41. </el-popover>
  42. </div>
  43. </div>
  44. </div>
  45. <div v-if="isRestPwd">
  46. <div class="login_form">
  47. <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm">
  48. <el-form-item prop="phoneno" >
  49. <el-input class="inp" name="phoneno" type="text" v-model="loginForm.phoneno" autoComplete="on" placeholder="手机号码">
  50. </el-input>
  51. </el-form-item>
  52. <el-form-item prop="smsCode">
  53. <el-input class="inp smsCode" name="smsCode" type="text" v-model="loginForm.smsCode" autoComplete="on" placeholder="短信验证码">
  54. </el-input>
  55. <el-button :disabled="abledBut" class="sendCode" @click.prevent="sendPhonenoSmsCode('changePassword')">
  56. <span class="text" v-if="!abledBut">发送验证码</span>
  57. <span class="text" v-else>({{setTimeNum}}s)</span>
  58. </el-button>
  59. </el-form-item>
  60. <el-form-item prop="password" >
  61. <el-input class="inp" show-password name="password" :type="passwordType" v-model="loginForm.password" autoComplete="on" placeholder="新密码">
  62. </el-input>
  63. </el-form-item>
  64. </el-form>
  65. <div class="oper">
  66. <a @click="isRestPwd = false" style="color:#409EFF;"> 返回</a>
  67. </div>
  68. <el-button class="login" :loading="loading" @click.native.prevent="handleResetPasswordByPhoneno">
  69. <span class="text">重置密码</span>
  70. </el-button>
  71. </div>
  72. </div>
  73. </div>
  74. </template>
  75. <script>
  76. import { sendSmsCode } from '@/api/sms/sms';
  77. import { mapGetters } from 'vuex';
  78. import md5 from 'js-md5';
  79. import { resetPasswordByPhoneno,getTpaState } from '@/api/login';
  80. export default {
  81. name: 'login',
  82. computed: {
  83. ...mapGetters([
  84. 'myBranchs','myDepts','userInfo','roles'
  85. ]),
  86. screenWidth:function(){
  87. return screen.width;
  88. }
  89. },
  90. watch: {
  91. /* 使用watch来响应数据的变化 */
  92. //监听倒计时数据的变化,小于0的时候,清除定时器,解开按钮,重置倒计时
  93. setTimeNum(newVal, oldVal) {
  94. if(newVal < 0){
  95. clearInterval(this.timeWrap)
  96. this.abledBut = false
  97. this.setTimeNum = 60
  98. }
  99. }
  100. },
  101. data() {
  102. const validateDisplayUserid = (rule, value, callback) => {
  103. if (this.loginForm.authType=='password_display_userid') {
  104. if(value.length<=6){
  105. callback(new Error('请输入6位以上用户账号'))
  106. }else{
  107. callback()
  108. }
  109. } else {
  110. callback()
  111. }
  112. }
  113. const validatePassword = (rule, value, callback) => {
  114. if (this.loginForm.authType=='password_display_userid') {
  115. if(value.length<6){
  116. callback(new Error('密码最少为六位'))
  117. }else{
  118. callback()
  119. }
  120. } else {
  121. callback()
  122. }
  123. }
  124. const validatePhoneno = (rule, value, callback) => {
  125. if (this.loginForm.authType=='sms') {
  126. if(value.length !=11 ){
  127. callback(new Error('手机号码必须为11位号码'))
  128. }else{
  129. callback()
  130. }
  131. } else {
  132. callback()
  133. }
  134. }
  135. const validateSmsCode = (rule, value, callback) => {
  136. if (this.loginForm.authType=='sms') {
  137. if(value.length !=6 ){
  138. callback(new Error('请输入6位短信验证码'))
  139. }else{
  140. callback()
  141. }
  142. } else {
  143. callback()
  144. }
  145. }
  146. return {
  147. loginForm: {
  148. displayUserid: '',
  149. password: '',
  150. authType:'password_display_userid',//password/sms/password_display_userid 分别为账户密码、短信验证码快捷登录
  151. phoneno:'',//手机号码
  152. smsCode:'',//短信验证码
  153. },
  154. loginRules: {
  155. displayUserid: [{ required: true, trigger: 'blur', validator: validateDisplayUserid }],
  156. password: [{ required: true, trigger: 'blur', validator: validatePassword }],
  157. phoneno: [{ required: true, trigger: 'blur', validator: validatePhoneno }],
  158. smsCode: [{ required: true, trigger: 'blur', validator: validateSmsCode }],
  159. },
  160. passwordType: 'password',
  161. loading: false,
  162. showResetPasswordDialog:false,//显示忘记密码重置密码窗口
  163. showTpLoginDialog: false, //显示第三方登陆对话框
  164. showRegisterDialog: false,//显示注册窗口
  165. deptSelectVisible:false,//显示选择部门对话框
  166. userDeptid:'',//选中的部门编号
  167. userBranchId:'',//选中的部门对应的机构号
  168. addBranchFormVisible:false, //显示添加机构对话框
  169. isRestPwd: false,
  170. abledBut: false, //是否禁止
  171. setTimeNum: 60, // 倒计时时间
  172. timeWrap: null, // 定时器标识
  173. wxLoginCodeVisible:false,
  174. }
  175. },
  176. methods: {
  177. showPwd() {
  178. if (this.passwordType === 'password') {
  179. this.passwordType = ''
  180. } else {
  181. this.passwordType = 'password'
  182. }
  183. },
  184. sendPhonenoSmsCode(scene){
  185. var params={
  186. phoneno:this.loginForm.phoneno,
  187. scene:scene
  188. }
  189. sendSmsCode(params).then(res=>{
  190. if(res.data.tips.isOk){
  191. this.$notify.success("发送成功");
  192. if (this.setTimeNum > 0) {
  193. this.abledBut = true
  194. this.timeWrap = setInterval(() => {
  195. this.setTimeNum -= 1
  196. }, 1000)
  197. }
  198. }else{
  199. this.$notify.error(res.data.tips.msg);
  200. }
  201. })
  202. },
  203. handleResetPasswordByPhoneno() {
  204. this.loading = true
  205. this.$refs.loginForm.validate(valid => {
  206. if (valid) {
  207. let params={
  208. newPassword:md5(this.loginForm.password),
  209. phoneno:this.loginForm.phoneno,
  210. smsCode:this.loginForm.smsCode
  211. }
  212. resetPasswordByPhoneno(params).then(res=>{
  213. this.loading = false
  214. if(res.data.tips.isOk){
  215. this.$notify.success(res.data.tips.msg);
  216. this.isRestPwd = false;
  217. }else{
  218. this.$notify.error(res.data.tips.msg);
  219. }
  220. })
  221. } else {
  222. this.loading = false
  223. return false
  224. }
  225. })
  226. },
  227. handleLogin() {
  228. this.$refs.loginForm.validate(valid => {
  229. if (valid) {
  230. this.loading = true
  231. let params={
  232. displayUserid:this.loginForm.displayUserid,
  233. password:md5(this.loginForm.password),
  234. deptid:this.userDeptid,
  235. authType:this.loginForm.authType,
  236. phoneno:this.loginForm.phoneno,
  237. smsCode:this.loginForm.smsCode,
  238. branchId:this.userBranchId,
  239. }
  240. var loginParams={ }
  241. if(params.authType=='password_display_userid'){
  242. loginParams.userloginid=params.displayUserid
  243. loginParams.password=params.password
  244. loginParams.grantType="password"
  245. loginParams.authType='password_display_userid'
  246. //loginParams.deptid=params.deptid
  247. loginParams.branchId=params.branchId
  248. }else if(params.authType=='sms'){
  249. loginParams.userloginid=params.phoneno
  250. loginParams.password=params.smsCode
  251. loginParams.grantType="password"
  252. loginParams.authType="sms"
  253. //loginParams.deptid=params.deptid
  254. loginParams.branchId=params.branchId
  255. }
  256. this.$store.dispatch("LoginByUserloginid",loginParams).then(res => {
  257. this.loading = false
  258. if(res.data.tips.isOk==true){
  259. this.loading = true;
  260. this.$store.dispatch('GetUserInfo').then((res2)=>{
  261. this.loading = false
  262. if(res2.data.tips.isOk==true){
  263. this.userDeptid=res2.data.userInfo.deptid
  264. this.rolesChecked();
  265. }else{
  266. this.$notify.error(res2.data.tips.msg);
  267. }
  268. }).catch(err=>{
  269. console.log(err);
  270. this.loading = false
  271. });
  272. }else{
  273. this.$notify.error(res.data.tips.msg);
  274. }
  275. }).catch((e) => {
  276. this.loading = false
  277. })
  278. } else {
  279. return false
  280. }
  281. })
  282. },
  283. deptChecked() {
  284. if( !this.userDeptid){
  285. this.$notify.error("请选择登陆的部门")
  286. return
  287. }
  288. if(this.$store.state.user.userInfo.deptid!=this.userDeptid){
  289. let depts=this.myDepts.filter(d=>d.deptid==this.userDeptid)
  290. this.userBranchId=depts[0].branchId
  291. this.handleLogin();
  292. return;
  293. }else{
  294. this.rolesChecked();
  295. }
  296. },
  297. //角色身份验证
  298. rolesChecked(){
  299. var indexPath=sessionStorage.getItem("index-path");
  300. if(!indexPath){
  301. indexPath="/"
  302. }
  303. if(this.userInfo.isSuperAdmin){
  304. this.$router.push({ path: indexPath });
  305. }else if(this.userInfo.isPlatformAdmin){
  306. this.$router.push({ path: indexPath });
  307. }else{
  308. this.$router.push({ path: indexPath });
  309. }
  310. },
  311. afterAddBranchSubmit(branch){
  312. this.addBranchFormVisible=false;
  313. this.handleLogin();
  314. },
  315. weixinLogin(){
  316. var curlDomain=window.location.protocol+"//"+window.location.host; //
  317. var mdpRedirectUri=curlDomain+"/"+process.env.CONTEXT+"/"+process.env.VERSION+"/"
  318. getTpaState().then(res=>{
  319. var tips = res.data.tips;
  320. if(tips.isOk){
  321. this.wxLoginCodeVisible=true;
  322. var state=res.data.data
  323. var obj = new WxLogin({
  324. self_redirect:false,
  325. id:"login_container",
  326. appid: "wx2671d5db8346b6fc",
  327. scope: "snsapi_login",
  328. redirect_uri: encodeURIComponent("https://www.maimengcloud.com/api/m1/tpa/login/token?authType=wechat_wxpub&authId=mmxmcloud_wxopen_pc&redirectUri="+mdpRedirectUri),
  329. state: state,
  330. style: "",
  331. href: ""
  332. });
  333. }
  334. })
  335. },
  336. afterQRScan() {
  337. // const hash = window.location.hash.slice(1)
  338. // const hashObj = getQueryObject(hash)
  339. // const originUrl = window.location.origin
  340. // history.replaceState({}, '', originUrl)
  341. // const codeMap = {
  342. // wechat: 'code',
  343. // tencent: 'code'
  344. // }
  345. // const codeName = hashObj[codeMap[this.auth_type]]
  346. // if (!codeName) {
  347. // alert('第三方登录失败')
  348. // } else {
  349. // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
  350. // this.$router.push({ path: '/' })
  351. // })
  352. // }
  353. },
  354. loginByShowAccount(){
  355. this.loading = true
  356. var loginParams={ }
  357. loginParams.userloginid="demo-branch-01"
  358. loginParams.password=md5("888888")
  359. loginParams.grantType="password"
  360. loginParams.authType='password_display_userid'
  361. this.$store.dispatch("LoginByUserloginid",loginParams).then(res => {
  362. this.loading = false
  363. if(res.data.tips.isOk==true){
  364. this.loading = true;
  365. this.$store.dispatch('GetUserInfo').then((res2)=>{
  366. this.loading = false
  367. if(res2.data.tips.isOk==true){
  368. this.userDeptid=res2.data.userInfo.deptid
  369. this.rolesChecked();
  370. }else{
  371. this.$notify.error(res2.data.tips.msg);
  372. }
  373. }).catch(err=>{
  374. console.log(err);
  375. this.loading = false
  376. });
  377. }else{
  378. this.$notify.error(res.data.tips.msg);
  379. }
  380. }).catch((e) => {
  381. this.loading = false
  382. })
  383. }
  384. },
  385. created() {
  386. // window.addEventListener('hashchange', this.afterQRScan)
  387. },
  388. destroyed() {
  389. // window.removeEventListener('hashchange', this.afterQRScan)
  390. },
  391. mounted(){
  392. const s = document.createElement('script');
  393. s.type = 'text/javascript';
  394. s.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
  395. document.body.appendChild(s);
  396. }
  397. }
  398. </script>
  399. <style lang="scss" scoped>
  400. @import '../index.scss';
  401. </style>