海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Extensions\Grid;
  3. use App\Models\AdminSetting;
  4. use App\Models\MiniProgramDraft;
  5. use App\Models\MiniProgramTemplate;
  6. use Dcat\Admin\Grid\RowAction;
  7. use EasyWeChat\Factory;
  8. use Illuminate\Http\Request;
  9. /**
  10. * 获取小程序草稿,并创建模板,并保存草稿和模板列表
  11. * Class UploadMiniProgram
  12. * @package App\Admin\Extensions\Grid
  13. */
  14. class MiniProgramPull extends RowAction
  15. {
  16. protected $title = '更新列表';
  17. protected function html()
  18. {
  19. $this->appendHtmlAttribute('class', 'btn btn-primary');
  20. $this->defaultHtmlAttribute('href', 'javascript:;');
  21. return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
  22. }
  23. /**
  24. * 更新逻辑:
  25. * 1、先获取草稿箱,并保存到数据库;
  26. * 2、如果草稿未创建过模板,通过草稿创建模板;
  27. * 3、将模板列表保存至数据库;
  28. */
  29. public function handle(Request $request)
  30. {
  31. try {
  32. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  33. $config = [
  34. 'app_id' => $setting['service_appid'],
  35. 'secret' => $setting['service_appsecret'],
  36. 'token' => $setting['service_token'],
  37. 'aes_key' => $setting['service_aeskey'],
  38. ];
  39. $openPlatform = Factory::openPlatform($config);
  40. if (empty($openPlatform['code_template'])) {
  41. return $this->response()->error('获取code_template失败');
  42. }
  43. $codeTemplate = $openPlatform['code_template'];
  44. //草稿列表
  45. $list = $codeTemplate->getDrafts();
  46. if (empty($list['draft_list'])) {
  47. return $this->response()->error('获取草稿箱失败或草稿箱为空');
  48. }
  49. MiniProgramDraft::insertOrIgnore($list['draft_list']);
  50. MiniProgramDraft::whereNotIn('draft_id', array_column($list['draft_list'], 'draft_id'))->delete(); //删除不存在的数据
  51. //从草稿创建模板
  52. $ids = MiniProgramDraft::query()->where('is_create_template', 0)->pluck('draft_id');
  53. foreach ($ids as $draft_id) {
  54. $res = $codeTemplate->createFromDraft($draft_id, 1); //第二个参数,0:普通模板;1:标准模板
  55. if (!isset($res['errcode'], $res['errmsg']) || $res['errcode'] != 0 || $res['errmsg'] != 'ok') {
  56. return $this->response()->error("草稿ID $draft_id 创建模板失败");
  57. } else {
  58. MiniProgramDraft::where('draft_id', $draft_id)->update(['is_create_template' => 1]);
  59. }
  60. }
  61. //模板列表
  62. $list = $codeTemplate->list();
  63. if (empty($list['template_list']) || !is_array($list['template_list'])) {
  64. return $this->response()->error('获取模板失败或模板为空');
  65. }
  66. //2021-09-26发现增加了category_list返回参数,
  67. $list['template_list'] = array_map(function ($v) {
  68. if (isset($v['category_list']) && is_array($v['category_list'])) {
  69. $v['category_list'] = json_encode($v['category_list']);
  70. }
  71. return $v;
  72. }, $list['template_list']);
  73. MiniProgramTemplate::insertOrIgnore($list['template_list']);
  74. MiniProgramTemplate::whereNotIn('template_id', array_column($list['template_list'], 'template_id'))->delete(); //删除不存在的数据
  75. return $this->response()->success("更新列表成功")->refresh();
  76. } catch (\Exception $e) {
  77. return $this->response()->error($e->getMessage());
  78. }
  79. }
  80. public function confirm()
  81. {
  82. return ['此操作将数据与腾讯服务器数据同步,是否继续?', ''];
  83. }
  84. }