1
+ <?php
2
+
3
+ namespace Phpcxy \WechatManager \Http \Controllers ;
4
+
5
+ use Encore \Admin \Controllers \AdminController ;
6
+ use Encore \Admin \Form ;
7
+ use Encore \Admin \Layout \Content ;
8
+ use Encore \Admin \Tree \Tools ;
9
+ use Illuminate \Support \Str ;
10
+ use Phpcxy \WechatManager \Models \AdminWechatMenu ;
11
+ use Phpcxy \WechatManager \Models \AdminWechatReply ;
12
+ use Phpcxy \WechatManager \Tools \ApplyMenu ;
13
+
14
+ class AdminWechatMenuController extends AdminController
15
+ {
16
+ /**
17
+ * @var array
18
+ */
19
+ private $ types ;
20
+
21
+ /**
22
+ * Title for current resource.
23
+ *
24
+ * @var string
25
+ */
26
+ protected $ title = '微信自定义菜单 ' ;
27
+
28
+ public function __construct ()
29
+ {
30
+ $ this ->types = [
31
+ 'view ' => '链接 ' ,
32
+ 'text ' => '文字 ' ,
33
+ 'click ' => '事件 '
34
+ ];
35
+ }
36
+
37
+ public function index (Content $ content )
38
+ {
39
+ $ content ->header ('微信自定义菜单 ' );
40
+ $ content ->body (AdminWechatMenu::tree (function ($ tree ) {
41
+ $ tree ->tools (function (Tools $ tools ) {
42
+ $ tools ->add (new ApplyMenu ());
43
+ });
44
+
45
+ $ tree ->branch (function ($ branch ) {
46
+ return "{$ branch ['title ' ]} <span class='label label-success'> {$ this ->types [$ branch ['type ' ]]}</span> " ;
47
+ });
48
+ }));
49
+
50
+ return $ content ;
51
+ }
52
+
53
+ /**
54
+ * Redirect to edit page.
55
+ *
56
+ * @param int $id
57
+ *
58
+ * @return \Illuminate\Http\RedirectResponse
59
+ */
60
+ public function show ($ id )
61
+ {
62
+ return redirect ()->route ('menu.edit ' , ['id ' => $ id ]);
63
+ }
64
+
65
+ /**
66
+ * Make a form builder.
67
+ *
68
+ * @return Form
69
+ */
70
+ protected function form ()
71
+ {
72
+ $ form = new Form (new AdminWechatMenu );
73
+ $ form ->text ('title ' , __ ('名称 ' ))->rules ('required ' );
74
+ $ form ->select ('parent_id ' , '父级菜单 ' )->options (AdminWechatMenu::selectOptions ())
75
+ ->rules ('required ' );
76
+
77
+ $ form ->select ('type ' , '类型 ' )->options ($ this ->types )->help ("如果是一级菜单下还有二级菜单,无需选择 " )
78
+ ->rules (function ($ form ) {
79
+ if (request ()->get ('parent_id ' ) > 0 ) {
80
+ return 'required ' ;
81
+ }
82
+ });
83
+
84
+ $ form ->textarea ('value ' , '内容 ' )->help ("如果是一级菜单下还有二级菜单,无需填写 " )
85
+ ->rules (function ($ form ) {
86
+ if (request ()->get ('type ' )) {
87
+ return 'required ' ;
88
+ }
89
+ });
90
+
91
+ // 表单回调
92
+ $ form ->saved (function (Form $ form ) {
93
+ if ($ form ->type == 'text ' ) {
94
+ if (!$ form ->model ()->key ) {
95
+ $ form ->model ()->key = 'M_ ' . Str::random (6 );
96
+ $ form ->model ()->save ();
97
+ }
98
+
99
+ $ reply = AdminWechatReply::where ('key ' , $ form ->model ()->key )->first ();
100
+ if ($ reply ) {
101
+ $ reply ->value = $ form ->value ;
102
+ $ reply ->save ();
103
+ } else {
104
+ AdminWechatReply::create ([
105
+ 'key ' => $ form ->model ()->key ,
106
+ 'source ' => 'menu ' ,
107
+ 'type ' => 'text ' ,
108
+ 'value ' => $ form ->value
109
+ ]);
110
+ }
111
+ }
112
+ });
113
+
114
+ return $ form ;
115
+ }
116
+ }
0 commit comments