UNPKG

unserver-unify

Version:

629 lines (626 loc) 22.4 kB
'use strict'; angular.module('bamboo').controller('LoginCtrl', function($scope, $rootScope, $state, $timeout, deviceDetector, $translate, loginService, ApiService, CommonService, AppConfig, $uibModal, $filter, Lightbox, $window, pwdService, LocalConfig, $localStorage, siteConfigSvs, $q) { $scope.agreeFlag = true; var _self = this; this.showDefaultLanguage = true; // console.log('-- login controller --'); _self.showMobileSid = false; //init the var to show/hide sidebar for mobile view if (loginService.school) { this.imageUrlPrefix = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + loginService.school._id + "/"; } //$scope.showMobileLogo = true; //console.info($rootScope.showMobileLogo); //toastrHelper.showToastrSuccess("Information",10000); var headerConfig = siteConfigSvs.getConfig().HeaderConfig || {}; var loginfooter = siteConfigSvs.getConfig().LoginFooter || {}; this.footerConfig = siteConfigSvs.getConfig().footerConfig || {}; _self.HIDE_APPLICATIONS = headerConfig.HIDE_APPLICATIONS; _self.COM_RIGHT = loginfooter.companyRights || "Bamboosys System Technology 2017"; // console.log(_self.HIDE_APPLICATIONS); // console.log(_self.COM_RIGHT); _self.toggleSideBar = function() { _self.showMobileSid = !_self.showMobileSid; } $scope.doTheBack = function() { window.history.back(); }; this.appUrl = ApiService.APPURL; this.loginService = loginService; this.searchCompany = function(q) { $state.go('index.searchcompany', { key: q }); } if ($state.current.name.indexOf('index.mobile.login') == 0 && loginService.subDomain == 'caregiverasia') { console.log('Care Giver Asia Login '); var URL = '//api.sgedushare.com/ui/57da553bfc58753752883bad/oauth/caregiveasia'; $window.location.href = URL; return; } this.googleLoginURL = ApiService.getFullUrl('/oauth/google/' + loginService.school.key); this.linkedInLoginURL = ApiService.getFullUrl('/oauth/linkedin/' + loginService.school.key); this.facebookLoginURL = ApiService.getFullUrl('/oauth/facebook/' + loginService.school.key); this.twitterLoginURL = ApiService.getFullUrl('/oauth/twitter/' + loginService.school.key); this.caregiveLoginURL = ApiService.getFullUrl('/oauth/caregiveasia'); this.WechatLoginURL = ApiService.getFullUrl('/oauth/wechat/' + loginService.school.key); if (deviceDetector.isMobile() && !deviceDetector.isTablet()) { this.WechatLoginURL = ApiService.getFullUrl('/oauth/wechat/' + loginService.school.key + 'mobile'); } this.wsfedUrl = ApiService.getFullUrl('/oauth/wsfed'); this.registeUrl = "index.setting.signup"; this.CURR_YEAR = new Date().getFullYear(); var resetPwdOpts = { size: "md-lg vertical-align-center", animation: true, backdrop: 'static', component: 'resetPwdComponent', keyboard: false, resolve: { reason: function() { return _self.changePwdReason; } } } // $uibModal.open(resetPwdOpts); this.requestTempPassword = function() { console.log(_self.tempIC); console.log(_self.birthday); var info = { ic: _self.tempIC, birth: _self.birthday, ownername: loginService.school.name, }; ApiService.post('/request_pass', info).then(function(result) { //console.log(result); if (result.data.success) { CommonService.showNoBlockInfo(result.data.info); } else { CommonService.showError(result.data.error || result.data.info); } }); } this.searchCourses = function(q) { $rootScope.courseQ = (q && q.name) ? q.name : ''; $timeout(function() { $state.go('index.courses'); }, 10); } function initRegisteUrl() { if (!$rootScope.currentSchool) { console.log("return 2"); return; } if ($rootScope.currentSchool.translationStyle == 'poiz') { _self.registeUrl = "index.setting.presignupshop"; } else if ($rootScope.currentSchool.translationStyle == 'gsx') { _self.registeUrl = "index.setting.presignupschool"; } else if ($rootScope.currentSchool.homepage == 'stec') { _self.registeUrl = "index.setting.signup"; } else if ($rootScope.currentSchool.jobFlag) { _self.registeUrl = "index.setting.presignup"; } else { _self.registeUrl = "index.setting.signup"; } // console.info(_self.registeUrl); // console.info($rootScope.currentSchool.translationStyle); } initRegisteUrl(); $scope.login = { domain: loginService.subDomain, userType: 'student' }; this.unreadmsgs = 0; if (loginService.user) { if (loginService.user.parentFlag) { console.log('--- parent Login --'); $window.location.href = ApiService.PARENTURL + "/mystudy/activitySummary"; } var info = { action: 'getmyunreadmsgcounter', } ApiService.post('/user', info).then(function(result) { // console.log(result); if (result.data.success && result.data.data) { _self.unreadmsgs = result.data.data.messages; _self.tasks = result.data.data.tasks; } }) } // console.log(loginService.school); if (loginService.school) { // console.log(this.imageUrlPrefix); // console.log(loginService.school); $scope.customStyle = { "background-color": loginService.school.footercolor }; if (loginService.school.themecolors) { $scope.coursebtmStyle = { "background-color": loginService.school.themecolors.coursewidgetbtmcolor }; $scope.themeColor = loginService.school.themecolors.theme3headercolor; } if (ApiService.DID && ApiService.department.icon) { this.department = ApiService.department; this.logoUrl = this.imageUrlPrefix + ApiService.department.icon; } else if (loginService.school.icon) { this.logoUrl = this.imageUrlPrefix + loginService.school.icon; // console.log(this.logoUrl); } else { this.logoUrl = "//s3-ap-southeast-1.amazonaws.com/edushare/public/resg/school/54321ef40f6006a95112893f/Logo.png"; } this.companyName = loginService.school.name; if ($rootScope.currentLanguage == "Chinese") { this.companyDescription = loginService.school.descriptionCN || loginService.school.description; } else { this.companyDescription = loginService.school.description || loginService.school.descriptionCN; } this.descriptionCN = loginService.school.descriptionCN; $scope.school = loginService.school; if (loginService.school.links) { var links = []; angular.forEach(loginService.school.links, function(link) { if (link.enable) { links.push(link) } }) $scope.links = links; } if (loginService.school.languages) { _self.languages = loginService.school.languages; console.log(_self.languages); var count = 0; angular.forEach(loginService.school.languages, function(val) { if (val) { count++; } }); if (count != 0) { _self.showDefaultLanguage = false; } } var info = { start: 0, limit: 10, type: 'school', action: "getProductInfos" }; ApiService.post('/advinfo', info).then(function(result) { if (result.data.success) { _self.productsList = result.data.data.items; } }); if (loginService.subDomain == 'bamboosys' || loginService.subDomain == 'wdbamboo') { var info = { limit: 1, start: 0, }; ApiService.post("/techerinfos", info).then(function(result) { if (result.data.success) { _self.teacherTotalCount = result.data.data.counter; } }); } } else { this.logoUrl = 'assets/images/bamboo_logo.png'; this.companyName = 'Bamboosys System Technology'; this.companyDescription = ''; } if (deviceDetector.isMobile() && !deviceDetector.isTablet()) { $timeout(function() { $rootScope.showMobileLogo = true }, 100); if ($state.current.name.indexOf("index.home") < 0) { console.info("---!!!----"); $timeout(function() { $rootScope.showMobileLogo = false; }, 100); } $scope.$on('$destroy', function() { $rootScope.showMobileLogo = false; }); } $scope.selectForum = function() { $state.go('index.forums'); }; $scope.needLoginButton = function() { return ApiService.getSchoolInfo(loginService.subDomain).then(function(school) { _self.school = school; $rootScope.currentSchool = school; $rootScope.initLang(); loginService.school = school; }, function(err) { console.log(" School Service Retry counter : " + $rootScope.apiRetry); if (!$rootScope.apiRetry) { $rootScope.apiRetry = 1; $state.go('index.home'); } else if ($rootScope.apiRetry < 4) { $rootScope.apiRetry++; $state.go('index.home'); } else { $state.go('errstate'); } }); }; $rootScope.showCourseLoginDialog = this.showCourseLoginDialog = function() { if (loginService.user) { CommonService.showInfo($filter('trans')({ eng: 'If you want to start this course, Please enroll first!', chn: '如果需要开始课程学习,请首先报名!' })); } else { _self.openLoginDialog(); } } if ($state.current.name.indexOf('403') == 0) { $timeout(function() { openLoginDialog() }, 500); } this.openLoginDialog = $scope.openLoginDialog = function() { if (loginService.school.userLoginAddress && loginService.school.userLoginAddress.length > 2) { if (loginService.school.userLoginAddress.indexOf('//') > -1) { $window.location.href = loginService.school.userLoginAddress; return; } else if (loginService.school.userLoginAddress.indexOf('index') == 0) { $state.go(loginService.school.userLoginAddress); return; } } // if (loginService.subDomain == "caregiverasia") { // var URL = '//api.sgedushare.com/ui/57da553bfc58753752883bad/oauth/caregiveasia'; // $window.location.href = URL; // return; // } // if (loginService.subDomain == "stec") { // $state.go('index.pagelogin'); // return; // } if (loginService.subDomain == "sgpower") { $state.go('index.sglogin'); return; } if (loginService.school.seprateLogin) { //$state.go('index.enterpriselogin'); window.open($state.href('index.enterpriselogin'), '_blank'); return; } var options = { templateUrl: 'app/login/login.dlg.html', controller: 'LoginDlgCtrl', controllerAs: 'ctrl', size: "md-lg vertical-align-center", backdrop: 'static' } if (loginService.school.loginTheme) { switch (loginService.school.loginTheme) { case 't1': options.templateUrl = "app/login/login.dlg1.html"; break; case 't2': options.templateUrl = "app/login/login.dlg2.html"; break; } } $uibModal.open(options).result.then(function(login) { console.log(login); if (!login) {} else if (login.forgotPassword) { $state.go('index.request-reset-pwd'); } else if (login.showagreement) { $timeout(function() { $state.go('index.agreement'); }, 500) } else if (login.enviroment) { $timeout(function() { $state.go('index.enviroment'); }, 500) } else { $scope.login = login; $scope.loginMe(); } }); }; this.gotoLogin = function() { console.log("gotoLogin"); _self.openLoginDialog(); } $scope.loginSubmit = function() { $scope.submitted = true; //$scope.shaking = true; if ($scope.form.$invalid) { $scope.shaking = true; $timeout(function() { $scope.shaking = false; }, 500); return; } $scope.loginMe(); } $scope.clearErrorMsg = function() { $scope.LOGINERROR = false; $rootScope.MOBILE_LOGIN_ERROR = undefined; } $scope.loginMe = function() { // setup promise, and 'working' flag // console.log(loginService) if ($scope.login.userType == 'parent') { $scope.login.parentFlag = true; } $scope.login.domain = loginService.subDomain; $scope.login.ownername = loginService.school.name; $scope.login.url = ApiService.HOST + "#/active_account"; var loginFunction = loginService.login; if (loginService.school.apiOptions && loginService.school.apiOptions.hdbftp) { loginFunction = loginService.ldapLogin; console.log("--- ldap login"); } //loginService.login($scope.login).then(function(user) { loginFunction($scope.login).then(function(user) { console.info(user); _self.readNoticeId = (user.readNoticeId) ? user.readNoticeId : ''; if (!user) { $scope.LOGINERROR = true; return; //CommonService.showError($translate.instant(AppConfig.LOGINERROR)); } if (user.parentFlag) { $window.location.href = ApiService.PARENTURL + "/mystudy/activitySummary"; } else if ((!user.loginCounter || user.loginCounter < 2) && loginService.school.welcomedoc) { ApiService.get("/schooldoc/" + loginService.school.welcomedoc).then(function(result) { if (result.data.success) { var content = result.data.data.detail; if (content && content.length > 10) {} } }) // return; } var now = new Date(); var expireDate = new Date(loginService.school.expire); var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds var diffDays = Math.round(Math.abs((expireDate.getTime() - now.getTime()) / (oneDay))); if (diffDays <= 14 && user.role == "admin") CommonService.showNoBlockErr('Your account will expire after ' + diffDays + ' days !'); // console.log(user); //$rootScope.userType = $scope.login.userType; $localStorage.USER_TYPE = $scope.login.userType; console.log($localStorage.USER_TYPE); console.info($state.current.name); /* if ($state.current.name == 'index.reset-pwd') { $state.go('index.home'); } */ /*if (user.loginCounter < 2 || !user.loginCounter) { _self.changePwdReason = $translate.instant('ChangePwdReason1'); $uibModal.open(resetPwdOpts); $localStorage.FORCE_RESET_PWD = true; $localStorage.FORCE_RESET_PWD_REASON = _self.changePwdReason; return; } else */ if (user.passNeedToUpdate && !user.is_admin) { _self.changePwdReason = $translate.instant('ChangePwdReason2'); $uibModal.open(resetPwdOpts); $localStorage.FORCE_RESET_PWD = true; $localStorage.FORCE_RESET_PWD_REASON = _self.changePwdReason; return; } else if (navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1 || typeof navigator.wxuserAgent !== "undefined") { $state.reload(); return; } else if (deviceDetector.isMobile() && !deviceDetector.isTablet()) { $state.go('index.home', {}, { reload: true }); return; } else if ($state.current.name == '403' || $state.current.name == 'index.reset-pwd') { $state.go('index.home'); return; } else if (!LocalConfig.DEBUG_LOGIN && (user.is_admin || (user.grole && user.grole != 'user'))) { _self.showAnnounDialog().then(function(ret) { $window.location = "/admin"; }); return; } else if (loginService.school.defaultPageAfterLogin) { _self.showAnnounDialog().then(function(ret) { CommonService.alertReleaseNotesDlg(); switch (loginService.school.defaultPageAfterLogin) { case 'myspace': $state.go('index.myspace.activitySummary'); break; case 'myexam': $state.go('index.myspace.exams'); break; case 'mycourse': $state.go('index.myspace.mycourses'); break; case 'calendar': $state.go('index.myspace.events'); break; case 'survey': $state.go('index.myspace.surveys'); break; case 'home': default: $state.reload(); break; } console.log('--3'); }); } else { console.log('--4'); if ($state.current.name.indexOf('login') > -0) { $state.go('index.home', {}, { reload: true }); } else { $state.reload(); } } if (loginService.subDomain == 'aklc' && $scope.login.userType == 'student' && !deviceDetector.isMobile() && !deviceDetector.isTablet()) { var options = { templateUrl: 'app/login/topscoreusers.html', controller: 'TopScoreUsersCtrl', controllerAs: 'ctrl', size: "lg", backdrop: 'static' } $uibModal.open(options); } }, function(reason) { // if (reason.data.errorcode && reason.data.errorcode == 1001) { // CommonService.showError($filter('trans')({ // eng: 'The account is not actived, please check email ' + reason.data.email, // chn: '账户还未激活,请检查邮箱 ' + reason.data.email // })); // } console.log(reason.data.error); // CommonService.showError(reason.data.error); }); }; //stec logout function stecLogoutDone() { $window.location = "http://www.stecs.com.sg/"; } this.logoutMe = $scope.logoutMe = function() { console.log("logoutMe"); if (loginService.subDomain == 'stec') { loginService.logout(stecLogoutDone); } else { loginService.logout(); } }; $scope.requestPasswordReset = function(email) { var info = { email: email, url: ApiService.HOST + "#/reset-pwd", ownername: loginService.school.name, }; ApiService.post('/search_pass', info).then(function(result) { //console.log(result); if (result.data.success) { CommonService.showNoBlockInfo(result.data.info); } else { CommonService.showError(result.data.error || result.data.info); } }); }; this.showbarcode = function() { Lightbox.openModal([{ url: "assets/images/qrcode.jpeg", }], 0); } $rootScope.shareSiteInfos = { info: { site: window.location.protocol + "//" + window.location.host, title: $rootScope.currentSchool ? $rootScope.infotoshare || $rootScope.currentSchool.description : '' }, socials: [{ name: "facebook", title: "Facebook" }, { name: "twitter", title: "Twitter" }, { name: "google_plusone_share", title: "Google+", icon: "google-plus" }, { name: "sinaweibo", title: "Sina Weibo", icon: "weibo" }, { name: "email", title: "Email", icon: "envelope" }, { name: "addthis", title: "Addthis", icon: "plus", addThis: true }] }; if ($state.current.name.indexOf('index.sglogin') == 0 && loginService.user) { $state.go('index.home'); console.log("return 1"); return; } $scope.setLang = function(key) { $rootScope.setLang(key); if ($state.current.name === 'index.home.genx' || (loginService.subDomain == 'cap' && $state.current.name === 'index.home') || $state.current.name === 'index.courses') { $state.reload(); } } this.showAnnounDialog = function() { var deferred = $q.defer(); var notice = (loginService.school.loginNotice) ? loginService.school.loginNotice : ''; console.log(_self.readNoticeId); if (notice != '' && notice != _self.readNoticeId) { ApiService.get("/notice/" + notice).then(function(result) { if (result.data.success) { $uibModal.open({ templateUrl: "components/dialog/announcement-dlg.html", controller: 'AnnouncementDlgCtrl', size: "md-lg vertical-align-center", controllerAs: 'ctrl', resolve: { info: function() { return result.data.data; } } }).result.then(function(id) { if (id && id != '') { var info = { action: 'readAnnoucement', aid: id } ApiService.post('/news', info).then(function(res) { console.log(res); deferred.resolve(true); }); } else { deferred.resolve(true); } }); } else { deferred.resolve(true); } }); } else { deferred.resolve(true); } return deferred.promise; } this.category = ApiService.category; if (loginService.school.theme == 't8') { _self.catList = []; getArray(_self.category, _self.catList, 0, ''); console.log(_self.catList); } function getArray(obj, array, level, parent) { if ((typeof obj) === "object") { for (var key in obj) { var fullkey; if (parent == '') { fullkey = key; } else { fullkey = parent + "," + key; } var _i = { key: fullkey, value: level, title: key, items: [], }; array.push(_i); var info = obj[key]; if (typeof info === "object") { getArray(info, _i.items, level + 1, fullkey); } } } } this.searchCoursesByCate = function(key) { $rootScope.courseCat = (key) ? key : ''; $state.go('index.courses', {}, { reload: true }); } }); function selectForum() { $state.go('index.newses'); }