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.

455 lines
19 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 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" label-position="top">
  6. <el-form-item prop="displayUserid" v-show="loginForm.authType=='password_display_userid'" label="登录账号">
  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'" label="登录密码">
  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'" label="手机号码">
  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'" label="短信验证码">
  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-button class="login" :loading="loading" @click.native.prevent="handleLogin">
  25. <span class="text"> </span>
  26. </el-button>
  27. </el-form>
  28. <div class="oper">
  29. <a @click="isRestPwd = true">忘记密码</a>
  30. <a @click="$refs['starDialog'].open({action:loginByShowAccount})">演示账户登录</a>
  31. <a v-if="loginForm.authType == 'password_display_userid' " @click="loginForm.authType = 'sms'">手机号登录</a>
  32. <a v-if="loginForm.authType == 'sms' " @click="loginForm.authType = 'password_display_userid' ">密码登录</a>
  33. </div>
  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="$refs['starDialog'].open({action: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" label-position="top">
  48. <el-form-item prop="phoneno" label="手机号码">
  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" label="短信验证码">
  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" label="新密码">
  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. <mdp-dialog ref="starDialog" width="600px" title="hi~同学,来支持一下【唛盟开源】吧,为项目点个Star!">
  74. <el-row class="center">
  75. <el-button size="large" @click="toStar" target="_blank" type="warning" icon="el-icon-star-off">唛盟项目</el-button>
  76. </el-row>
  77. <template #footer="{visible,data,dialog}">
  78. <el-row slot="footer">
  79. <el-button @click="dialog.close()">取消</el-button><el-button type="primary" @click="doLoginByAction(data.action)">已点赞继续登陆</el-button>
  80. </el-row>
  81. </template>
  82. </mdp-dialog>
  83. </div>
  84. </template>
  85. <script>
  86. import { sendNoAuthSmsCode } from '@/api/sms/sms';
  87. import { mapGetters } from 'vuex';
  88. import md5 from 'js-md5';
  89. import { resetPasswordByPhoneno,getTpaState } from '@/api/login';
  90. export default {
  91. name: 'login',
  92. computed: {
  93. ...mapGetters([
  94. 'myBranchs','myDepts','userInfo','roles'
  95. ]),
  96. screenWidth:function(){
  97. return screen.width;
  98. }
  99. },
  100. watch: {
  101. /* 使用watch来响应数据的变化 */
  102. //监听倒计时数据的变化,小于0的时候,清除定时器,解开按钮,重置倒计时
  103. setTimeNum(newVal, oldVal) {
  104. if(newVal < 0){
  105. clearInterval(this.timeWrap)
  106. this.abledBut = false
  107. this.setTimeNum = 60
  108. }
  109. },
  110. isRestPwd(){
  111. clearInterval(this.timeWrap)
  112. this.abledBut = false
  113. this.setTimeNum = 60
  114. }
  115. },
  116. data() {
  117. const validateDisplayUserid = (rule, value, callback) => {
  118. if (this.loginForm.authType=='password_display_userid') {
  119. if(value.length<=6){
  120. callback(new Error('请输入6位以上用户账号'))
  121. }else{
  122. callback()
  123. }
  124. } else {
  125. callback()
  126. }
  127. }
  128. const validatePassword = (rule, value, callback) => {
  129. if (this.loginForm.authType=='password_display_userid') {
  130. if(value.length<6){
  131. callback(new Error('密码最少为六位'))
  132. }else{
  133. callback()
  134. }
  135. } else {
  136. callback()
  137. }
  138. }
  139. const validatePhoneno = (rule, value, callback) => {
  140. if (this.loginForm.authType=='sms') {
  141. if(value.length !=11 ){
  142. callback(new Error('手机号码必须为11位号码'))
  143. }else{
  144. callback()
  145. }
  146. } else {
  147. callback()
  148. }
  149. }
  150. const validateSmsCode = (rule, value, callback) => {
  151. if (this.loginForm.authType=='sms') {
  152. if(value.length !=6 ){
  153. callback(new Error('请输入6位短信验证码'))
  154. }else{
  155. callback()
  156. }
  157. } else {
  158. callback()
  159. }
  160. }
  161. return {
  162. loginForm: {
  163. displayUserid: '',
  164. password: '',
  165. authType:'password_display_userid',//password/sms/password_display_userid 分别为账户密码、短信验证码快捷登录
  166. phoneno:'',//手机号码
  167. smsCode:'',//短信验证码
  168. },
  169. loginRules: {
  170. displayUserid: [{ required: true, trigger: 'blur', validator: validateDisplayUserid }],
  171. password: [{ required: true, trigger: 'blur', validator: validatePassword }],
  172. phoneno: [{ required: true, trigger: 'blur', validator: validatePhoneno }],
  173. smsCode: [{ required: true, trigger: 'blur', validator: validateSmsCode }],
  174. },
  175. passwordType: 'password',
  176. loading: false,
  177. showResetPasswordDialog:false,//显示忘记密码重置密码窗口
  178. showTpLoginDialog: false, //显示第三方登陆对话框
  179. showRegisterDialog: false,//显示注册窗口
  180. deptSelectVisible:false,//显示选择部门对话框
  181. userDeptid:'',//选中的部门编号
  182. userBranchId:'',//选中的部门对应的机构号
  183. addBranchFormVisible:false, //显示添加机构对话框
  184. isRestPwd: false,
  185. abledBut: false, //是否禁止
  186. setTimeNum: 60, // 倒计时时间
  187. timeWrap: null, // 定时器标识
  188. wxLoginCodeVisible:false,
  189. }
  190. },
  191. methods: {
  192. showPwd() {
  193. if (this.passwordType === 'password') {
  194. this.passwordType = ''
  195. } else {
  196. this.passwordType = 'password'
  197. }
  198. },
  199. sendPhonenoSmsCode(scene){
  200. var params={
  201. phoneno:this.loginForm.phoneno,
  202. scene:scene
  203. }
  204. sendNoAuthSmsCode(params).then(res=>{
  205. if(res.data.tips.isOk){
  206. this.$notify.success("发送成功");
  207. if (this.setTimeNum > 0) {
  208. this.abledBut = true
  209. this.timeWrap = setInterval(() => {
  210. this.setTimeNum -= 1
  211. }, 1000)
  212. }
  213. }else{
  214. this.$notify.error(res.data.tips.msg);
  215. }
  216. })
  217. },
  218. handleResetPasswordByPhoneno() {
  219. this.loading = true
  220. this.$refs.loginForm.validate(valid => {
  221. if (valid) {
  222. let params={
  223. newPassword:md5(this.loginForm.password),
  224. phoneno:this.loginForm.phoneno,
  225. smsCode:this.loginForm.smsCode
  226. }
  227. resetPasswordByPhoneno(params).then(res=>{
  228. this.loading = false
  229. if(res.data.tips.isOk){
  230. this.$notify.success(res.data.tips.msg);
  231. this.isRestPwd = false;
  232. }else{
  233. this.$notify.error(res.data.tips.msg);
  234. }
  235. })
  236. } else {
  237. this.loading = false
  238. return false
  239. }
  240. })
  241. },
  242. handleLogin() {
  243. this.$refs.loginForm.validate(valid => {
  244. if (valid) {
  245. this.loading = true
  246. let params={
  247. displayUserid:this.loginForm.displayUserid,
  248. password:md5(this.loginForm.password),
  249. deptid:this.userDeptid,
  250. authType:this.loginForm.authType,
  251. phoneno:this.loginForm.phoneno,
  252. smsCode:this.loginForm.smsCode,
  253. branchId:this.userBranchId,
  254. }
  255. var loginParams={ }
  256. if(params.authType=='password_display_userid'){
  257. loginParams.userloginid=params.displayUserid
  258. loginParams.password=params.password
  259. loginParams.grantType="password"
  260. loginParams.authType='password_display_userid'
  261. //loginParams.deptid=params.deptid
  262. loginParams.branchId=params.branchId
  263. }else if(params.authType=='sms'){
  264. loginParams.userloginid=params.phoneno
  265. loginParams.password=params.smsCode
  266. loginParams.grantType="password"
  267. loginParams.authType="sms"
  268. //loginParams.deptid=params.deptid
  269. loginParams.branchId=params.branchId
  270. }
  271. this.$store.dispatch("LoginByUserloginid",loginParams).then(res => {
  272. this.loading = false
  273. if(res.data.tips.isOk==true){
  274. this.loading = true;
  275. this.$store.dispatch('GetUserInfo').then((res2)=>{
  276. this.loading = false
  277. if(res2.data.tips.isOk==true){
  278. this.userDeptid=res2.data.userInfo.deptid
  279. this.rolesChecked();
  280. }else{
  281. this.$notify.error(res2.data.tips.msg);
  282. }
  283. }).catch(err=>{
  284. console.log(err);
  285. this.loading = false
  286. });
  287. }else{
  288. this.$notify.error(res.data.tips.msg);
  289. }
  290. }).catch((e) => {
  291. this.loading = false
  292. })
  293. } else {
  294. return false
  295. }
  296. })
  297. },
  298. deptChecked() {
  299. if( !this.userDeptid){
  300. this.$notify.error("请选择登陆的部门")
  301. return
  302. }
  303. if(this.$store.state.user.userInfo.deptid!=this.userDeptid){
  304. let depts=this.myDepts.filter(d=>d.deptid==this.userDeptid)
  305. this.userBranchId=depts[0].branchId
  306. this.handleLogin();
  307. return;
  308. }else{
  309. this.rolesChecked();
  310. }
  311. },
  312. //角色身份验证
  313. rolesChecked(){
  314. var indexPath=sessionStorage.getItem("index-path");
  315. if(!indexPath){
  316. indexPath="/"
  317. }
  318. if(this.userInfo.isSuperAdmin){
  319. location.replace(indexPath)
  320. }else if(this.userInfo.isPlatformAdmin){
  321. location.replace(indexPath)
  322. }else{
  323. location.replace(indexPath)
  324. }
  325. },
  326. afterAddBranchSubmit(branch){
  327. this.addBranchFormVisible=false;
  328. this.handleLogin();
  329. },
  330. weixinLogin(){
  331. var curlDomain=window.location.protocol+"//"+window.location.host; //
  332. var mdpRedirectUri=curlDomain+"/"+process.env.CONTEXT+"/"+process.env.VERSION+"/"
  333. var tpaContext=this.$mdp.getTpaContext();
  334. var domain=this.$mdp.getFixedDomain();
  335. var appType=this.$mdp.getWxpubConfig().appType;
  336. var scope=this.$mdp.getWxpubConfig().scope
  337. getTpaState().then(res=>{
  338. var tips = res.data.tips;
  339. if(tips.isOk){
  340. this.wxLoginCodeVisible=true;
  341. var state=res.data.data
  342. var obj = new WxLogin({
  343. self_redirect:false,
  344. id:"login_container",
  345. appid: this.$mdp.getWxpubConfig().appid,
  346. scope: scope,
  347. redirect_uri: encodeURIComponent(`${domain}/api/${process.env.VERSION}/${tpaContext}/login/token?authType=wechat_wxpub&appType=${appType}&redirectUri=${mdpRedirectUri}`),
  348. state: state,
  349. style: "",
  350. href: ""
  351. });
  352. }
  353. })
  354. },
  355. doLoginByAction(action){
  356. this.$refs['starDialog'].close();
  357. action();
  358. },
  359. afterQRScan() {
  360. // const hash = window.location.hash.slice(1)
  361. // const hashObj = getQueryObject(hash)
  362. // const originUrl = window.location.origin
  363. // history.replaceState({}, '', originUrl)
  364. // const codeMap = {
  365. // wechat: 'code',
  366. // tencent: 'code'
  367. // }
  368. // const codeName = hashObj[codeMap[this.auth_type]]
  369. // if (!codeName) {
  370. // alert('第三方登录失败')
  371. // } else {
  372. // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
  373. // this.$router.push({ path: '/' })
  374. // })
  375. // }
  376. },
  377. loginByShowAccount(){
  378. this.loading = true
  379. var loginParams={ }
  380. loginParams.userloginid="demo-branch-01"
  381. loginParams.password=md5("888888")
  382. loginParams.grantType="password"
  383. loginParams.authType='password_display_userid'
  384. this.$store.dispatch("LoginByUserloginid",loginParams).then(res => {
  385. this.loading = false
  386. if(res.data.tips.isOk==true){
  387. this.loading = true;
  388. this.$store.dispatch('GetUserInfo').then((res2)=>{
  389. this.loading = false
  390. if(res2.data.tips.isOk==true){
  391. this.userDeptid=res2.data.userInfo.deptid
  392. this.rolesChecked();
  393. }else{
  394. this.$notify.error(res2.data.tips.msg);
  395. }
  396. }).catch(err=>{
  397. console.log(err);
  398. this.loading = false
  399. });
  400. }else{
  401. this.$notify.error(res.data.tips.msg);
  402. }
  403. }).catch((e) => {
  404. this.loading = false
  405. })
  406. },
  407. toStar(){
  408. window.open(this.$mdp.getOpenSourceLink())
  409. }
  410. },
  411. created() {
  412. // window.addEventListener('hashchange', this.afterQRScan)
  413. },
  414. destroyed() {
  415. // window.removeEventListener('hashchange', this.afterQRScan)
  416. },
  417. mounted(){
  418. const s = document.createElement('script');
  419. s.type = 'text/javascript';
  420. s.src = 'https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
  421. document.body.appendChild(s);
  422. }
  423. }
  424. </script>
  425. <style lang="scss" scoped>
  426. @import '../index.scss';
  427. </style>