1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php namespace Api\Lib;
- use Dever;
- # 官方平台账户
- class Account
- {
- public function update($db, $data)
- {
- if (isset($data['app_platform']) && $data['app_platform']) {
- $app_platform = $data['app_platform'];
- if (is_string($app_platform)) {
- $app_platform = explode(',', $app_platform);
- }
- $data['app_id'] = $app_platform[0];
- $data['platform_id'] = $app_platform[1] ?? 0;
- if (!$data['platform_id']) {
- Dever::error('请选择平台');
- }
- }
- return $data;
- }
- public function run($account, $func, $param = array(), $env = 1, $method = 'run', $project = 'api')
- {
- if (!is_array($account)) {
- $account = Dever::db('account', 'api')->find(array('key' => $account));
- }
- if (!$account) {
- Dever::error('账户无效');
- }
- if (!is_array($func)) {
- $func = Dever::db('app_func', 'api')->find(array('app_id' => $account['app_id'], 'key' => $func));
- }
- if (!$func) {
- Dever::error('功能无效');
- }
- $api = Dever::load('app', 'api')->getApi($func['id'], $env);
- if (!$api) {
- Dever::error('接口无效');
- }
- $param['account_project'] = $project;
- $param['account_id'] = $account['id'];
- $result = array();
- if ($func['type'] == 1) {
- # 仅执行第一个
- $result = Dever::load('api', 'api')->$method($api[0], $param);
- if (is_array($result)) {
- $result['account_id'] = $account['id'];
- $result['api_id'] = $api[0]['id'];
- }
- } elseif ($func['type'] == 2) {
- # 同步执行
- foreach ($api as $k => $v) {
- $result = Dever::load('api', 'api')->$method($v, $param);
- if ($result && is_array($result)) {
- $param = array_merge($result, $param);
- }
- }
- } elseif ($func['type'] == 3) {
- # 异步执行
- $result = true;
- foreach ($api as $k => $v) {
- $param['api_id'] = $v['id'];
- \Dever\Helper\Cmd::run('task.api', $param, 'api');
- }
- }
- return $result;
- }
- }
|