海南旅游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.

60 lines
1.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\AdminSetting;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Repositories\Repository;
  6. use EasyWeChat\Factory;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use Illuminate\Pagination\LengthAwarePaginator;
  9. use Illuminate\Support\Facades\Cache;
  10. /**
  11. * 小程序草稿箱
  12. * Class MiniProgramDraft
  13. * @package App\Admin\Repositories
  14. */
  15. class MiniProgramList extends Repository
  16. {
  17. protected $keyName = 'id';
  18. /**
  19. * 查询表格数据
  20. *
  21. * @param Grid\Model $model
  22. * @return LengthAwarePaginator
  23. * @throws GuzzleException
  24. */
  25. public function get(Grid\Model $model): LengthAwarePaginator
  26. {
  27. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  28. $config = [
  29. 'app_id' => $setting['service_appid'],
  30. 'secret' => $setting['service_appsecret'],
  31. 'token' => $setting['service_token'],
  32. 'aes_key' => $setting['service_aeskey'],
  33. ];
  34. $cache_key = 'mini_program:list';
  35. if ($data['subjects'] = Cache::get($cache_key)) {
  36. $data['total'] = count($data['subjects']);
  37. } else {
  38. $openPlatform = Factory::openPlatform($config);
  39. $codeTemplate = $openPlatform['code_template'];
  40. if (empty($codeTemplate) || (!$list = $openPlatform->getAuthorizers())) {
  41. $data['total'] = 0;
  42. $data['subjects'] = [];
  43. } else {
  44. $data['total'] = count($list['list']);
  45. $data['subjects'] = $list['list'] ?? [];
  46. Cache::put($cache_key, $list['list'], 120);
  47. }
  48. }
  49. return $model->makePaginator(
  50. $data['total'] ?? 0, // 传入总记录数
  51. $data['subjects'] ?? [] // 传入数据二维数组
  52. );
  53. }
  54. }