Web實(shí)戰(zhàn)之黑名單管理


實(shí)現(xiàn)的功能

  • 用戶(hù)可以查閱自己加入黑名單的用戶(hù)
  • 選中批量刪除黑名單內(nèi)名字
  • 單個(gè)刪除黑名單內(nèi)名字
  • 全部刪除黑名單

依賴(lài)的第三方工具

  • AngularJS框架
  • BootStrap框架
  • Express框架

頁(yè)面代碼

由于在我的博客中前端實(shí)戰(zhàn)之用戶(hù)控制臺(tái)中有詳細(xì)的頁(yè)面代碼這里只出示一小部分,方便大家閱讀

<div role="tabpanel" class="tab-pane" id="blacklist" ng-controller="blacklistCtrl">
    <div class="block" ng-init="initBlackList()">
        <p>
            您可以在用戶(hù)狀態(tài)和用戶(hù)主頁(yè)中將其加入黑名單。您將無(wú)法看到加入黑名單的用戶(hù)發(fā)表的狀態(tài)和評(píng)論。
        </p>
        <table class="table table-bordered">
            <tbody>
            <tr>
                <th colspan="2">
                    黑名單用戶(hù)
                </th>
                <th>
                    操作
                </th>
            </tr>
            <tr ng-repeat="person in personList">
                <td>
                    <input class="check-helper" type="checkbox" ng-model="person.checked">
                </td>
                <td>
                    <a ng-href="/users/{{person.name}}">{{person.name}}</a>
                </td>
                <td>
                    <label class="btn-link btn-small unblock" ng-click="remove(person)">
                        從黑名單移除
                    </label>
                </td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <td colspan="3">
                    <input class="check-helper" type="checkbox" ng-click="selectAll()">
                    <span>全選</span>
                    <label class="btn-link  pull-right" style="color:#555555" ng-click="removeAll()">
                        批量移除
                    </label>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
</div>

前端交互代碼

angular.module('settingAPP',['myApp.Services','angularFileUpload']).controller('setCtrl',function($rootScope,$scope,$http,$jquery){
        $rootScope.msg='';
        $rootScope.display=false;
        document.user={};
        $scope.getUser=function(){
            $http.get('/users/getUser').success(function(data){
                if(!data.success){
                    if(!data.err.message) data.err.message="未知錯(cuò)誤,稍后再試"
                    $rootScope.msg=data.err.message;
                    $rootScope.display=true;
                }
                else{
                    document.user=data.content;
                    $scope.$broadcast('userDone');
                }
            }).error(function(data){
                $rootScope.msg='未知錯(cuò)誤,請(qǐng)重試';
                $rootScope.display=true;
            });
        }
    }).controller('blacklistCtrl',function($rootScope,$scope,$http){
        $scope.personList=new Array();

        var initBlackList=function(){
            var tmp=document.user.blacklist;
            for(var i in tmp){
                $scope.personList.push({
                    name:tmp[i],
                    checked:false
                });
            }
        };
        $scope.$on('userDone',initBlackList);
        $scope.selectAll=function(){
            for(var i in $scope.personList){
                $scope.personList[i].checked=!($scope.personList[i].checked);
            }
        };
        $scope.remove=function(person){
            var temp1=[];
            var temp2=[];

            for(var i in $scope.personList){
                if(person==$scope.personList[i]) continue;
                temp1.push($scope.personList[i]);
                temp2.push($scope.personList[i].name);
            }
            $http.post('/settings/removeBlacklist',{persons:temp2}).success(function(data){
                if(!data.success){
                    if(!data.err.message) data.err.message="未知錯(cuò)誤,稍后再試"
                    $rootScope.msg=data.err.message;
                    $rootScope.display=true;
                }
                else{
                    $scope.personList=temp1;
                }
            }).error(function(data){
                $rootScope.msg='未知錯(cuò)誤,請(qǐng)重試';
                $rootScope.display=true;
            });
        };
        $scope.removeAll=function(){
            var temp1=[];
            var temp2=[];
            var person;
            for(var i in $scope.personList){
                person=$scope.personList[i];
                if(!person.checked){
                    temp1.push(person);
                    temp2.push(person.name);
                }
            }
            $http.post('/settings/removeBlacklist',{persons:temp2}).success(function(data){
                if(!data.success){
                    if(!data.err.message) data.err.message="未知錯(cuò)誤,稍后再試"
                    $rootScope.msg=data.err.message;
                    $rootScope.display=true;
                }
                else{
                    $scope.personList=temp1;
                }
            }).error(function(data){
                $rootScope.msg='未知錯(cuò)誤,請(qǐng)重試';
                $rootScope.display=true;
            });
        };

    });

為了一開(kāi)始就能展現(xiàn)黑名單頁(yè)面,我們使用了ng-init,但這還不夠,由于該頁(yè)面是包裹在整個(gè)控制臺(tái)頁(yè)面里,我想直接拿到整個(gè)user的信息而非僅黑名單,那初始化操作就必須在父控制器SetCtrl里做了,可用ng-init的話(huà)黑名單的初始化就早于拿到user了,怎么辦呢?利用事件。

$scope.$broadcast('userDone');//父控制器
$scope.$on('userDone',$scope.initBlackList);//子控制器

后端代碼

function rmBlack(req,res){
    Then(function(cont){
        User.findOne({username:req.session.uname},cont);
    }).then(function(cont,doc){
        if(!doc) return cont(new Err(msg.USER.userNone));
        doc.blacklist=req.body.persons;
        doc.save(cont);
    }).then(function(cont,doc){
        res.json({
            success:true,
            err:null
        });
    }).fail(function(cont,err){
        console.log(err.stack);
        if(!err.message) err.message='后臺(tái)錯(cuò)誤,稍后再試';
        res.json({
            success:false,
            err:err
        });
    });
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評(píng)論 25 708
  • AngularJS是什么?AngularJs(后面就簡(jiǎn)稱(chēng)ng了)是一個(gè)用于設(shè)計(jì)動(dòng)態(tài)web應(yīng)用的結(jié)構(gòu)框架。首先,它是...
    200813閱讀 1,653評(píng)論 0 3
  • 22年12月更新:個(gè)人網(wǎng)站關(guān)停,如果仍舊對(duì)舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,235評(píng)論 22 257
  • 西郵的圖書(shū)館朝北一覽無(wú)余,只是靠近校園正門(mén)時(shí),多出一座噴泉,被形象稱(chēng)為水煮鴿子,水柱噴射,澆灌在中心,一排排展翅待...
    落矢鄴閱讀 588評(píng)論 1 1
  • 前期大熱的韓劇太陽(yáng)的后裔,女生表示看后都紛紛換“老公”,無(wú)疑,男主宋仲基就是新少女殺手。先說(shuō)說(shuō)劇中宋仲基扮演的大韓...
    林幺幺兒閱讀 4,001評(píng)論 0 0