WxMpMaterialController.php 7.7 KB

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