wxmanager.php
9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* description...
*
* @author TRice
* @package WeixinManagerController
*/
class WxmanagerController extends Controller{
public $layout='admin';
public $needRightActions = array('*'=>true);
private $weChat;
public function init(){
$menu = new Menu();
$this->assign('mainMenu',$menu->getMenu());
$menu_index = $menu->current_menu();
$this->assign('menu_index',$menu_index);
$this->assign('subMenu',$menu->getSubMenu($menu_index['menu']));
$this->assign('menu',$menu);
$nav_act = Req::get('act')==null?$this->defaultAction:Req::get('act');
$nav_act = preg_replace("/(_edit)$/", "_list", $nav_act);
if($nav_act=='menu') $nav_act = 'wx_public_list';
if($nav_act=='wx_kf_list') $nav_act = 'wx_public_list';
$this->assign('nav_link','/'.Req::get('con').'/'.$nav_act);
$this->assign('node_index',$menu->currentNode());
$this->safebox = Safebox::getInstance();
$this->manager = $this->safebox->get('manager');
$this->assign('manager',$this->safebox->get('manager'));
$currentNode = $menu->currentNode();
if(isset($currentNode['name']))$this->assign('admin_title',$currentNode['name']);
}
public function noRight(){
$this->redirect("/admin/noright");
}
public function index()
{
$wx = $this->weChat;
$echostr = Req::args('echostr');
if($echostr) $wx->checkSign();
}
//菜单管理
public function menu()
{
$id = Filter::int(Req::args('id'));
if($id>0){
$model = new Model('wx_public');
$obj = $model->where('id='.$id)->find();
if($obj){
if($obj['menus']!=null){
$menus = $obj['menus'];
}else{
$menus = '{"button":[]}';
}
$this->assign('id',$id);
$this->assign('menus',$menus);
$this->assign('name',$obj['name']);
$this->redirect();
exit;
}
}
$this->redirect('wx_public_list');
}
//自动保存微信公众号、服务号
public function menu_update()
{
$id = Filter::int(Req::args('id'));
$json = Req::args('json');
$info = array('status'=>'error','更新失败!');
if($id>0){
$model = new Model('wx_public');
$model->data(array('menus'=>$json))->where('id='.$id)->update();
$info = array('status'=>'success','更新更新!');
}
echo JSON::encode($info);
}
//同步更新微信菜单
public function menu_syn()
{
$info = array('status'=>'error','msg'=>'同步的菜单不存在!');
$id = Filter::int(Req::args('id'));
$wx_model = new Model('wx_public');
$wx_obj = $wx_model->where("id=$id")->find();
if($wx_obj){
$this->weChat = new WeChat($wx_obj['app_id'],$wx_obj['app_secret'],$wx_obj['token']);
if($id>0){
$model = new Model('wx_public');
$obj = $model->where('id='.$id)->find();
if($obj){
if($obj['menus']!=null){
$menus = $obj['menus'];
}else{
$menus = '{"button":[]}';
}
$info = $this->weChat->commitMenu($menus);
}
}
}
echo JSON::encode($info);
}
public function wx_public_validator()
{
$token = Filter::sql(Req::args('token'));
$id = Filter::int(Req::args('id'));
$model = new Model('wx_public');
$obj = $model->where("token='$token'")->find();
if($obj){
if($id!=$obj['id']) return array('msg'=>'请保证各公众号的token唯一性');
}
return null;
}
public function wx_response_validator()
{
$event_key = Req::args('event_key');
$type = Req::args('type');
if($event_key == ""){
$event_key = CHash::random(20,'char');
Req::args('event_key',$event_key);
}
$content = array();
if($type=='text'){
$content = Req::args('content');
$content = array('content'=>$content);
}else if($type=='app'){
$app = Req::args('app');
$appConfig = WeixinService::appConfig();
$appFields = $appConfig[$app]['config'];
$content['app'] = $app;
foreach ($appFields as $item) {
if($item['type']=='checkbox'){
$content[$item['field'].'[]'] = implode(',',Req::args($item['field']));
}else{
$content[$item['field']] = Req::args($item['field']);
}
}
}
$content = serialize($content);
Req::args('content',$content);
return null;
}
public function wx_kf_list(){
$id = Filter::int(Req::args('id'));
$weChat = $this->getWeChatById($id);
if($weChat){
$kfList = $weChat->getKfList();
}else{
$kfList = array();
}
$this->assign('kfList',$kfList);
$this->assign('id',$id);
$this->redirect();
}
public function wx_kf_add()
{
$id = Filter::int(Req::args('id'));
$kf_account = Filter::sql(Req::args('kf_account'));
$nickname = Filter::sql(Req::args('nickname'));
$weChat = $this->getWeChatById($id);
$info = array("status"=>"fail");
if($weChat){
$result = $weChat->addKf($kf_account,$nickname);
if($result->errcode == 0){
$info = array("status"=>"success");
}else{
$info = array("status"=>"fail",'msg'=>$weChat->errorCodeMsg($result->errcode));
}
}
echo JSON::encode($info);
}
public function wx_kf_del()
{
$id = Filter::int(Req::args('id'));
$kf_account = Filter::sql(Req::args('kf_account'));
$weChat = $this->getWeChatById($id);
$info = array("status"=>"fail");
if($weChat){
$result = $weChat->delKf($kf_account);
if($result->errcode == 0){
$info = array("status"=>"success");
}else{
$info = array("status"=>"fail",'msg'=>$weChat->errorCodeMsg($result->errcode));
}
}
echo JSON::encode($info);
}
public function wx_kf_update()
{
$id = Filter::int(Req::args('id'));
$kf_account = Filter::sql(Req::args('kf_account'));
$nickname = Filter::sql(Req::args('nickname'));
$weChat = $this->getWeChatById($id);
$info = array("status"=>"fail");
if($weChat){
$result = $weChat->updateKf($kf_account,$nickname);
if($result->errcode == 0){
$info = array("status"=>"success");
}else{
$info = array("status"=>"fail",'msg'=>$weChat->errorCodeMsg($result->errcode));
}
}
echo JSON::encode($info);
}
public function wx_kf_headimg()
{
$file_name = $_FILES['imgFile']['name'];
$type = $_FILES['imgFile']['type'];
$tmp_name = $_FILES['imgFile']['tmp_name'];
$datas = array(
'media' => '@'.realpath($tmp_name).";type=".$type.";filename=".$file_name
);
$id = Filter::int(Req::args('id'));
$kf_account = Filter::sql(Req::args('kf_account'));
$weChat = $this->getWeChatById($id);
$reinfo = array("status"=>"fail");
if($weChat){
$result = $weChat->uploadKfHeadimg($kf_account,$datas);
if($result->errcode == 0){
$reinfo = array("status"=>"success");
}else{
$reinfo = array("status"=>"fail",'msg'=>$weChat->errorCodeMsg($result->errcode));
}
}
echo JSON::encode($reinfo);
}
public function wx_kf_invite()
{
$id = Filter::int(Req::args('id'));
$kf_account = Filter::sql(Req::args('kf_account'));
$wx = Filter::sql(Req::args('wx'));
$weChat = $this->getWeChatById($id);
$info = array("status"=>"fail",'msg'=>'ddd');
if($weChat){
$result = $weChat->inviteKfWx($kf_account,$wx);
if($result->errcode == 0){
$info = array("status"=>"success");
}else{
$info = array("status"=>"fail",'msg'=>$weChat->errorCodeMsg($result->errcode));
}
}
echo JSON::encode($info);
}
public function select_resources()
{
$this->layout = "blank";
$condition = Req::args('condition');
$condition_str = Common::str2where($condition);
if($condition_str)$this->assign("where",$condition_str);
else $this->assign("where","1=1");
$this->assign("condition",$condition);
$this->redirect();
}
private function getWeChatById($id)
{
$wx_model = new Model('wx_public');
$wx_obj = $wx_model->where("id=$id")->find();
if($wx_obj){
$this->weChat = new WeChat($wx_obj['app_id'],$wx_obj['app_secret'],$wx_obj['token']);
return $this->weChat;
}else{
return null;
}
}
}