Administrator
2022-10-09 23f93897d618a5253a18b65b124196f95891f240
提交 | 用户 | 时间
58d006 1 window.I = {}
A 2 var basic = getpath();
3 pagesize = 20;
4 var modelview = function() {
5     var self = this;
6     self.filter = ko.observable();
7     self.role = ko.observable(parseInt($('#roleid').val()));
8     self.role1 = ko.observable(parseInt($('#roleid').val()));
9     self.roles = ko.observableArray();
10     self.users = ko.observableArray();
11     self.user = ko.observable({
12         id : ko.observable(0),
13         username : ko.observable(),
14         email : ko.observable(),
15         role : ko.observable({
16             id : ko.observable(0),
17             name : ko.observable()
18         })
19     });
20     self.edittitle = ko.observable('编辑账户');
21     self.change = function(item) {
22         var roleid = self.role();
23         // 更新users
24         users(0, 1, 0, md.filter(), roleid);
25     };
26     self.valid = function(item) {
27         jQuery.post(basic + '/admin/usercon/valid.htm', {
28             id : item.id
29         }, function(data, textStatus, xhr) {
30             if (data == null)
31                 return;
32             if (data.valid == 1 || data.valid == true)
33                 item.valid(true);
34             else
35                 item.valid(false);
36
37         }, 'json');
38     };
39     self.rm = function(item) {
40         if (confirm("确认要删除吗?")) {
41             jQuery.post(basic + '/admin/usercon/rm.htm', {
42                 id : item.id
43             }, function(data, textStatus, xhr) {
44                 if (data == null)
45                     return;
46                 if (data.code == 0) {
47                     self.users.remove(item);
48                 } else {
49                     alert(data.errmsg);
50                 }
51             }, 'json');
52         }
53     };
54     self.rstpwd = function(item) {
55         jQuery.post(basic + '/admin/usercon/rstpwd.htm', {
56             id : item.id
57         }, function(data, textStatus, xhr) {
58             if (data == null)
59                 return;
60             if (data.code == 0) {
61                 alert('密码修改成功,新密码为:' + data.npwd);
62             } else {
63                 alert(data.errmsg);
64             }
65         }, 'json');
66     };
67     self.edit = function(item) {
68         if (item.role() != null) {
69             self.role1(item.role().id());
70         }
71         self.user(item);
72         self.edittitle('编辑账户');
73         $('#modal1').modal('show');
74     }
75 }
76 jQuery(document).ready(
77         function($) {
78             window.mtype = $('#mtype').val();
79             var md = window.md = new modelview();
80             ko.applyBindings(md);
81             
82             var tid = $('#tid').val();
83             users(0, 1, tid, md.filter(), md.role());
84             roleinit(md.role());
85             $('#searcha').click(function() {
86                 users(0, 1, 0, md.filter(), md.role());
87             });
88             $('#ctbtn').click(function() {
89                 window.md.edittitle('新建账户');
90                 // window.open(basic+'/administrate/system/user.jsf?roleid='+md.role());
91                 md.user({
92                     id : ko.observable(0),
93                     username : ko.observable(),
94                     email : ko.observable(),
95                     role : ko.observable({
96                         id : ko.observable(0),
97                         name : ko.observable()
98                     })
99                 });
100                 $('#modal1').modal('show');
101             });
102             var fm1 = $('#fm1');
103             fm1.validate({
104                 rules : {
105                     username : {
106                         required : true,
107                         remote : {
108                             url : basic + '/admin/usercon/username.htm',
109                             type : 'post',
110                             data : {
111                                 username : function() {
112                                     return $('#username').val();
113                                 },
114                                 id : function() {
115                                     return $('#id').val()
116                                 }
117                             }
118                         }
119                     },
120                     email : {
121                         required : true,
122                         remote : {
123                             url : basic + '/admin/usercon/email.htm',
124                             type : 'post',
125                             data : {
126                                 email : function() {
127                                     return $('#email').val();
128                                 },
129                                 id : function() {
130                                     return $('#id').val();
131                                 }
132                             }
133                         }
134                     }
135                 },
136                 messages : {
137                     username : {
138                         required : '不能为空',
139                         remote : '不能重复',
140                         maxlength : 30,
141                         minlength : 6
142                     },
143                     email : {
144                         required : '不能为空',
145                         maxlength : '最大长度不能超过30',
146                         minlength : '最小长度不能小于6',
147                         remote : '不能重复'
148                     }
149                 }
150             });
151             $('#tjbtn').click(
152                     function() {
153                         if (fm1.valid() == false)
154                             return;
155                         if (window.save == 1)
156                             return;
157                         window.save = 1;
158                         var tt = md.user();
159                         var index = md.users().indexOf(tt);
160                         var obj = ko.mapping.toJS(tt);
161                         obj.role = {};
162                         obj.role.id = md.role1();
163                         if(obj.role.id<=0)
164                         {
165                             window.save=0;
166                             alert('角色不能选所有角色!');
167                             return;
168                         }
169                         jQuery.post(basic + '/admin/usercon/save.htm', obj,
170                                 function(data, textStatus, xhr) {
171                                     window.save=0;
172                                     $('#modal1').modal('hide');
173                                     if (data == null)
174                                         return;
175                                     if (data.code == 1) {
176                                         alert(data.errmsg);
177                                     } else {
178                                         var val = data.user;
179                                         var role = val.role;
180                                         val = ko.mapping.fromJS(val);
181                                         if (role != null
182                                                 && typeof (role) == 'object')
183                                             val.role = ko.observable(val.role);
184                                         if (tt.id() == 0) {
185                                             md.users.push(val);
186                                         } else
187                                             md.users.splice(index, 1, val);
188                                     }
189                                 }, 'json');
190                     });
191         });
192 function users(page, pages, tid, filter, roleid) {
193     jQuery.post(basic + '/admin/usercon/users.htm', {
194         page : page,
195         pagesize : pagesize,
196         pages : pages,
197         tid : tid,
198         filter : filter,
199         roleid : roleid
200     }, function(data, textStatus, xhr) {
201         if (data == null)
202             return;
203         if (data.page != null)
204             page = data.page;
205         if (pages > 0) {
206             $('#pagdiv').unbind('page').empty();
207             if (data.pages > 1) {
208                 $('#pagdiv').bootpag({
209                     total : data.pages,
210                     maxVisible : 10,
211                     page : page + 1
212                 }).on('page', function(event, num) {
213                     users(num - 1, 0, 0, filter, roleid);
214                 });
215             }
216         }
217         window.md.users.removeAll();
218         if (data.items != null) {
219             jQuery.each(data.items, function(index, val) {
220                 var role = val.role;
221                 val = ko.mapping.fromJS(val);
222                 if (role != null && typeof (role) == 'object')
223                     val.role = ko.observable(val.role);
224                 window.md.users.push(val);
225             });
226         }
227     }, 'json');
228 }
229 function roleinit(roleid) {
230     jQuery.post(basic + '/admin/usercon/roleinit.htm', {
231         roleid : roleid
232     }, function(data, textStatus, xhr) {
233         if (data == null)
234             return;
235         window.md.roles.removeAll();
236         window.md.roles.push({id:0,name:'所有角色'});
237         if (!!data.items) {
238             jQuery.each(data.items, function(index, val) {
239                 window.md.roles.push(val);
240             });
241         }
242     }, 'json');
243 }