WxMpMessageController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Admin\Controllers\Mp;
  3. use App\Admin\Repositories\Mp\WxMpMessage;
  4. use App\Lib\WeApp\WeApp;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class WxMpMessageController extends AdminController
  10. {
  11. protected $mp_message_types = [
  12. 'text' => '文本',
  13. 'image' => '图片',
  14. 'voice' => '语音',
  15. 'video' => '视频',
  16. 'music' => '音乐',
  17. 'news' => '外链图文',
  18. 'mpnews' => '公众号图文',
  19. 'wxcard' => '卡卷消息'
  20. ];
  21. protected $mp_message_type_colors = [
  22. 'text' => '#3498db',
  23. 'image' => '#e74c3c',
  24. 'voice' => '#2ecc71',
  25. 'video' => '#f1c40f',
  26. 'music' => '#9b59b6',
  27. 'news' => '#e67e22',
  28. 'mpnews' => '#1abc9c',
  29. 'wxcard' => '#34495e'
  30. ];
  31. /**
  32. * Make a grid builder.
  33. *
  34. * @return Grid
  35. */
  36. protected function grid()
  37. {
  38. return Grid::make(new WxMpMessage(), function (Grid $grid) {
  39. $grid->model()->whereNotIn('msgtype',['material_image', 'material_video', 'material_voice', 'material_music'])->orderBy('id', 'desc');
  40. $grid->column('id')->sortable();
  41. $grid->column('msgtype')->using($this->mp_message_types)->label($this->mp_message_type_colors);
  42. $grid->column('content')->limit(100);
  43. $grid->column('img_url')->image('', 100, 100);
  44. $grid->column('video_url')->display(function ($v){
  45. if($v){
  46. return '<video controls width="150">
  47. <source src="'.$v.'" type="video/mp4">
  48. 您的浏览器不支持 video 标签。
  49. </video>';
  50. }
  51. return '';
  52. });
  53. $grid->column('voice_url')->display(function ($v){
  54. if($v){
  55. return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
  56. }
  57. return '';
  58. });
  59. $grid->column('thumb_url')->image('', 100, 100);
  60. $grid->column('musicurl')->display(function ($v){
  61. if($v){
  62. return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
  63. }
  64. return '';
  65. });
  66. // $grid->column('media_id')->display();
  67. // $grid->column('thumb_media_id');
  68. $grid->column('title');
  69. // $grid->column('description');
  70. // $grid->column('hqmusicurl');
  71. $grid->column('url')->display(function ($v){
  72. if($v){
  73. return '<a href="'.$v.'" target="_blank">链接</a>';
  74. }
  75. return '';
  76. });
  77. // $grid->column('article_id');
  78. // $grid->column('card_id');
  79. // $grid->column('created_at');
  80. $grid->column('updated_at')->sortable();
  81. $grid->filter(function (Grid\Filter $filter) {
  82. $filter->equal('id');
  83. });
  84. });
  85. }
  86. /**
  87. * Make a show builder.
  88. *
  89. * @param mixed $id
  90. *
  91. * @return Show
  92. */
  93. protected function detail($id)
  94. {
  95. return Show::make($id, new WxMpMessage(), function (Show $show) {
  96. $show->field('id');
  97. $show->field('msgtype');
  98. $show->field('content');
  99. $show->field('img_url');
  100. $show->field('video_url');
  101. $show->field('voice_url');
  102. $show->field('thumb_url');
  103. $show->field('musicurl');
  104. $show->field('media_id');
  105. $show->field('thumb_media_id');
  106. $show->field('title');
  107. $show->field('description');
  108. $show->field('hqmusicurl');
  109. $show->field('url');
  110. $show->field('article_id');
  111. $show->field('card_id');
  112. $show->field('created_at');
  113. $show->field('updated_at');
  114. });
  115. }
  116. /**
  117. * Make a form builder.
  118. *
  119. * @return Form
  120. */
  121. protected function form()
  122. {
  123. return Form::make(new WxMpMessage(), function (Form $form) {
  124. $form->display('id');
  125. // 'text' => '文本',
  126. // 'image' => '图片',
  127. // 'voice' => '语音',
  128. // 'video' => '视频',
  129. // 'music' => '音乐',
  130. // 'news' => '外链图文',
  131. // 'mpnews' => '公众号图文',
  132. // 'wxcard' => '卡卷消息'
  133. $form->radio('msgtype')->options($this->mp_message_types)->default('text')
  134. ->when(['video', 'news', 'music'], function (Form $form){
  135. $form->text('title')->rules('required_if:msgtype,video,news,music') // 使用required_if
  136. ->setLabelClass(['asterisk']); // 显示 * 号;
  137. $form->textarea('description')->rules('required_if:msgtype,video,news,music') // 使用required_if
  138. ->setLabelClass(['asterisk']); // 显示 * 号;
  139. })->when(['text'], function (Form $form){
  140. $form->textarea('content')->rules('required_if:msgtype,text') // 使用required_if
  141. ->setLabelClass(['asterisk']); // 显示 * 号;
  142. })->when(['image'], function (Form $form){
  143. $form->image('img_url')->rules('required_if:msgtype,image') // 使用required_if
  144. ->setLabelClass(['asterisk']) // 显示 * 号;
  145. ->uniqueName()->url('files/uploads')->autoUpload();
  146. })->when(['voice'], function (Form $form){
  147. $form->file('voice_url')->maxSize(2048)->accept('mp3')->rules('required_if:msgtype,voice') // 使用required_if
  148. ->setLabelClass(['asterisk']) // 显示 * 号;
  149. ->uniqueName()->url('files/uploads')->autoUpload();
  150. })->when(['video'], function (Form $form){
  151. $form->file('video_url')->maxSize(10240)->accept('mp4')->rules('required_if:msgtype,video') // 使用required_if
  152. ->setLabelClass(['asterisk']) // 显示 * 号;
  153. ->uniqueName()->url('files/uploads')->autoUpload();
  154. })->when(['music'], function (Form $form){
  155. $form->file('musicurl')->accept('mp3')->rules('required_if:msgtype,music') // 使用required_if
  156. ->setLabelClass(['asterisk']) // 显示 * 号;
  157. ->uniqueName()->url('files/uploads')->autoUpload();
  158. })->when(['video', 'news', 'music'], function (Form $form){
  159. $form->image('thumb_url')->rules('required_if:msgtype,video,news,music') // 使用required_if
  160. ->setLabelClass(['asterisk']) // 显示 * 号;
  161. ->uniqueName()->url('files/uploads')->autoUpload();
  162. })->when(['news'], function (Form $form){
  163. $form->text('url')->rules('required_if:msgtype,news') // 使用required_if
  164. ->setLabelClass(['asterisk']); // 显示 * 号;
  165. })->when(['mpnews'], function (Form $form){
  166. $article_ids = [];
  167. $weapp = new WeApp('mp');
  168. if($weapp){
  169. $mp = $weapp->getMpServicer();
  170. if($mp){
  171. $publishs = $mp->getFreepublishBatchget();
  172. if($publishs['code'] == 200){
  173. foreach ($publishs['item'] as $item){
  174. $article_ids[$item['article_id']] = $item['content']['news_item'][0]['title'] . '---' . $item['article_id'];
  175. }
  176. }
  177. }
  178. }
  179. $form->select('article_id')->rules('required_if:msgtype,mpnews') // 使用required_if
  180. ->setLabelClass(['asterisk'])->options($article_ids); // 显示 * 号;
  181. })->when(['wxcard'], function (Form $form){
  182. $form->text('card_id')->rules('required_if:msgtype,wxcard') // 使用required_if
  183. ->setLabelClass(['asterisk']); // 显示 * 号;
  184. });
  185. $form->text('media_id')->display(false);
  186. $form->text('thumb_media_id')->display(false);
  187. $form->text('hqmusicurl')->display(false);
  188. $form->display('created_at');
  189. $form->display('updated_at');
  190. $form->deleting(function (Form $form){
  191. global $__MINI_GLOBAL_TENANT_ID__;
  192. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  193. return $form->response()->error('权限不足,不可以删除其他分站对象');
  194. }
  195. });
  196. });
  197. }
  198. }