app.directive('loadHead',['$http','config','auth','$location','alertService',function($http,config,auth,$location,alertService){
    return{
        restrict: 'A',
        scope: {},
        link: function (scope, element, attrs) {
            element.bind('change', function(){
                var file = event.target.files[0];
                //判断类型是不是图片
                if (!/image\/\w+/.test(file.type)) {
                    alert("非图片");
                    return;
                }

                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function (e) {
//                    alert(this.result);//base64

                    $http.post(config.apiUrl + "users/upload_head",
                        {token: auth.token(),imgdata:this.result}
                    ).then(function (response) {
                        if(response.data.status == 0){
                            scope.$parent.alertService_1.showMessage('提示',"上传成功",function(){
                                scope.$parent.getuserinfo();
                            });
                        }
                        else{
                            scope.$parent.alertService_1.showMessage('提示',response.data.message);
                        }
                    });
                }
            });
        }
    }
}]);