loginController.js 5.25 KB
angular.module('myApp')
	.controller('loginController',[ '$scope','$rootScope','dingdaService','globalService' ,function($scope,$rootScope,dingdaService,globalService){
        var init = function(){
            $scope.phoneNumberVal='';
            $scope.validationVal= '';

            $(".phoneNumber").focus(function(){
                $(".phoneNumber").css('border','1px solid #36d1e7');
            })
            $(".phoneNumber").blur(function(){
                $(".phoneNumber").css('border','1px solid rgba(0,0,0,.2)');
            })
            $(".validationInput").focus(function(){
                $(".validationInput").css('border','1px solid #36d1e7');
            })
            $(".validationInput").blur(function(){
                $(".validationInput").css('border','1px solid rgba(0,0,0,.2)');
            })


        }
        
        function getUrl(name){
			var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r!=null)return  unescape(r[2]); return null;
		}
        init();
        var countdown = function(){
            var wait=60;
            function time(o) {
                if (wait == 0) {
                    o.removeAttribute("disabled");
                    o.value="重发";
                    wait = 60;
                    $('.ValidationBtn').html(o.value);
                    $('.ValidationBtn').css('background','#36d1e7');
                } else {
                    o.setAttribute("disabled", true);
                    o.value=wait + "秒";
                    wait--;
                    $('.ValidationBtn').html(o.value);
                    if(wait!=0){
                        $('.ValidationBtn').css('background','#ccc');
                    }
                    setTimeout(function() {
                        time(o);
                    },1000)
                }
            }
            time($('.ValidationBtn').get(0));
        }

        $scope.Validation = function(){
            if($scope.phoneNumberVal==""){
                layer.open({
                    content: '手机号码不能为空',
                    time:2
                });
                return
            }
            var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
            if(!reg.test($scope.phoneNumberVal)){
                layer.open({
                    content: '手机号码格式不对',
                    time:2
                });
                return
            }
            countdown();
            var res = dingdaService.sendPhoneCode($scope.phoneNumberVal , getUrl("openId"));
                res.success(function(data){
                    console.log(data);
                })
                .error(function(){
                    console.log('error');
                })

        }

        $scope.login= function(){
            if($scope.phoneNumberVal==""){
                layer.open({
                    content: '手机号码不能为空',
                    time:2
                });
                return
            }
            var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
            if(!reg.test($scope.phoneNumberVal)){
                layer.open({
                    content: '手机号码格式不对',
                    time:2
                });
                return
            }

            if($scope.validationVal==''){
                layer.open({
                    content: '请输入验证码',
                    time:2
                });
                return
            }
            $scope.validationVal = "" + $scope.validationVal;
            if($scope.validationVal.length != 4){
                layer.open({
                    content: '验证码位数不对',
                    time:2
                });
                return
            }

			var res = dingdaService.verifyPhoneCode($scope.phoneNumberVal , $scope.validationVal).success(function(data){
				if (data.meta.code == 200) {
					data.data.userName = $scope.phoneNumberVal;
					globalService.saveData('user',data.data);
					window.history.back();
                    layer.open({
                        content: '登录成功',
                        time:1
                    });
                    setTimeout(function() {
                    	location.reload();
                    } , 300);
				} else if (data.meta.code == 401) {
					layer.open({
                        content: data.meta.message,
                        time:1
                    });
//					alert(data.meta.message);
//					layer.msg(data.data.error);
					return;
				}
            }).error(function(){
                    layer.open({
                        content: '登录失败',
                        time:1
                    });
            })
			
        }

        $scope.$watch('validationVal',function(newVal,old){
            if(newVal){
                if($scope.validationVa!="" && !isNaN($scope.validationVal) && $scope.validationVal.toString().length==4){
                    $('.loginBtn').addClass('btnBg');
                }else{
                    $('.loginBtn').removeClass('btnBg');
                }
            }
        })

//        $(window).bind("load resize",function(){
//            $(".loginBox").css({zoom:$(window).width()/320,visibility:"visible"});
//        });
	}])