Base.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. if (!defined('QCLOUDAPI_ROOT_PATH')) {
  3. // 目录入口
  4. define('QCLOUDAPI_ROOT_PATH', dirname(dirname(__FILE__)));
  5. }
  6. require_once QCLOUDAPI_ROOT_PATH . '/Common/Base.php';
  7. /**
  8. * QcloudApi_Module_Base
  9. * 模块基类
  10. */
  11. abstract class QcloudApi_Module_Base extends QcloudApi_Common_Base
  12. {
  13. /**
  14. * $_serverHost
  15. * 接口域名
  16. * @var string
  17. */
  18. protected $_serverHost = '';
  19. /**
  20. * $_serverUri
  21. * url路径
  22. * @var string
  23. */
  24. protected $_serverUri = '/v2/index.php';
  25. /**
  26. * $_secretId
  27. * secretId
  28. * @var string
  29. */
  30. protected $_secretId = "";
  31. /**
  32. * $_secretKey
  33. * secretKey
  34. * @var string
  35. */
  36. protected $_secretKey = "";
  37. /**
  38. * $_defaultRegion
  39. * 区域参数
  40. * @var string
  41. */
  42. protected $_defaultRegion = "";
  43. /**
  44. * $_requestMethod
  45. * 请求方法
  46. * @var string
  47. */
  48. protected $_requestMethod = "GET";
  49. /**
  50. * __construct
  51. * @param array $config [description]
  52. */
  53. public function __construct($config = array())
  54. {
  55. if (!empty($config))
  56. $this->setConfig($config);
  57. }
  58. /**
  59. * setConfig
  60. * 设置配置
  61. * @param array $config 模块配置
  62. */
  63. public function setConfig($config)
  64. {
  65. if (!is_array($config) || !count($config))
  66. return false;
  67. foreach ($config as $key => $val) {
  68. switch ($key) {
  69. case 'SecretId':
  70. $this->setConfigSecretId($val);
  71. break;
  72. case 'SecretKey':
  73. $this->setConfigSecretKey($val);
  74. break;
  75. case 'DefaultRegion':
  76. $this->setConfigDefaultRegion($val);
  77. break;
  78. case 'RequestMethod':
  79. $this->setConfigRequestMethod($val);
  80. break;
  81. default:
  82. ;
  83. break;
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * setConfigSecretId
  90. * 设置secretId
  91. * @param string $secretId secretId
  92. */
  93. public function setConfigSecretId($secretId)
  94. {
  95. $this->_secretId = $secretId;
  96. return $this;
  97. }
  98. /**
  99. * setConfigSecretKey
  100. * 设置secretKey
  101. * @param string $secretKey
  102. */
  103. public function setConfigSecretKey($secretKey)
  104. {
  105. $this->_secretKey = $secretKey;
  106. return $this;
  107. }
  108. /**
  109. * setConfigDefaultRegion
  110. * 设置区域参数
  111. * @param string $region
  112. */
  113. public function setConfigDefaultRegion($region)
  114. {
  115. $this->_defaultRegion = $region;
  116. return $this;
  117. }
  118. /**
  119. * setConfigRequestMethod
  120. * 设置请求方法
  121. * @param string $method
  122. */
  123. public function setConfigRequestMethod($method)
  124. {
  125. $this->_requestMethod = strtoupper($method);
  126. return $this;
  127. }
  128. /**
  129. * getLastRequest
  130. * 获取上次请求的url
  131. * @return
  132. */
  133. public function getLastRequest()
  134. {
  135. require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
  136. return QcloudApi_Common_Request::getRequestUrl();
  137. }
  138. /**
  139. * getLastResponse
  140. * 获取请求的原始返回
  141. * @return
  142. */
  143. public function getLastResponse()
  144. {
  145. require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
  146. return QcloudApi_Common_Request::getRawResponse();
  147. }
  148. /**
  149. * generateUrl
  150. * 生成请求的URL,不发起请求
  151. * @param string $name 接口方法名
  152. * @param array $params 请求参数
  153. * @return
  154. */
  155. public function generateUrl($name, $params)
  156. {
  157. require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
  158. $action = ucfirst($name);
  159. $params['Action'] = $action;
  160. if (!isset($params['Region']))
  161. $params['Region'] = $this->_defaultRegion;
  162. return QcloudApi_Common_Request::generateUrl($params, $this->_secretId, $this->_secretKey, $this->_requestMethod,
  163. $this->_serverHost, $this->_serverUri);
  164. }
  165. /**
  166. * __call
  167. * 通过__call转发请求
  168. * @param string $name 方法名
  169. * @param array $arguments 参数
  170. * @return
  171. */
  172. public function __call($name, $arguments)
  173. {
  174. $response = $this->_dispatchRequest($name, $arguments);
  175. return $this->_dealResponse($response);
  176. }
  177. /**
  178. * _dispatchRequest
  179. * 发起接口请求
  180. * @param string $name 接口名
  181. * @param array $arguments 接口参数
  182. * @return
  183. */
  184. protected function _dispatchRequest($name, $arguments)
  185. {
  186. $action = ucfirst($name);
  187. $params = array();
  188. if (is_array($arguments) && !empty($arguments)) {
  189. $params = (array) $arguments[0];
  190. }
  191. $params['Action'] = $action;
  192. if (!isset($params['Region']))
  193. $params['Region'] = $this->_defaultRegion;
  194. require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php';
  195. $response = QcloudApi_Common_Request::send($params, $this->_secretId, $this->_secretKey, $this->_requestMethod,
  196. $this->_serverHost, $this->_serverUri);
  197. return $response;
  198. }
  199. /**
  200. * _dealResponse
  201. * 处理返回
  202. * @param array $rawResponse
  203. * @return
  204. */
  205. protected function _dealResponse($rawResponse)
  206. {
  207. if (!is_array($rawResponse) || !isset($rawResponse['code'])) {
  208. $this->setError("", 'request falied!');
  209. return false;
  210. }
  211. if ($rawResponse['code']) {
  212. $ext = '';
  213. require_once QCLOUDAPI_ROOT_PATH . '/Common/Error.php';
  214. if (isset($rawResponse['detail'])) {
  215. // 批量异步操作,返回任务失败信息
  216. $ext = $rawResponse['detail'];
  217. }
  218. $this->setError($rawResponse['code'], $rawResponse['message'], $ext);
  219. return false;
  220. }
  221. unset($rawResponse['code'], $rawResponse['message']);
  222. if (count($rawResponse))
  223. return $rawResponse;
  224. else
  225. return true;
  226. }
  227. }