Account.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. # 官方平台账户
  4. class Account
  5. {
  6. public function update($db, $data)
  7. {
  8. if (isset($data['app_platform']) && $data['app_platform']) {
  9. $app_platform = $data['app_platform'];
  10. if (is_string($app_platform)) {
  11. $app_platform = explode(',', $app_platform);
  12. }
  13. $data['app_id'] = $app_platform[0];
  14. $data['platform_id'] = $app_platform[1] ?? 0;
  15. if (!$data['platform_id']) {
  16. Dever::error('请选择平台');
  17. }
  18. }
  19. return $data;
  20. }
  21. public function run($account, $func, $param = array(), $env = 1, $method = 'run', $project = 'api')
  22. {
  23. if (!is_array($account)) {
  24. $account = Dever::db('account', 'api')->find(array('key' => $account));
  25. }
  26. if (!$account) {
  27. Dever::error('账户无效');
  28. }
  29. if (!is_array($func)) {
  30. $func = Dever::db('app_func', 'api')->find(array('app_id' => $account['app_id'], 'key' => $func));
  31. }
  32. if (!$func) {
  33. Dever::error('功能无效');
  34. }
  35. $api = Dever::load('app', 'api')->getApi($func['id'], $env);
  36. if (!$api) {
  37. Dever::error('接口无效');
  38. }
  39. $param['account_project'] = $project;
  40. $param['account_id'] = $account['id'];
  41. $result = array();
  42. if ($func['type'] == 1) {
  43. # 仅执行第一个
  44. $result = Dever::load('api', 'api')->$method($api[0], $param);
  45. if (is_array($result)) {
  46. $result['account_id'] = $account['id'];
  47. $result['api_id'] = $api[0]['id'];
  48. }
  49. } elseif ($func['type'] == 2) {
  50. # 同步执行
  51. foreach ($api as $k => $v) {
  52. $result = Dever::load('api', 'api')->$method($v, $param);
  53. if ($result && is_array($result)) {
  54. $param = array_merge($result, $param);
  55. }
  56. }
  57. } elseif ($func['type'] == 3) {
  58. # 异步执行
  59. $result = true;
  60. foreach ($api as $k => $v) {
  61. $param['api_id'] = $v['id'];
  62. \Dever\Helper\Cmd::run('task.api', $param, 'api');
  63. }
  64. }
  65. return $result;
  66. }
  67. }