AliyunSdk.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Api;
  3. abstract class AliyunSdk
  4. {
  5. /**
  6. * AccessKey
  7. * @var mixed|null
  8. */
  9. protected $AccessKey = null;
  10. /**
  11. * AccessKeySecret
  12. * @var mixed|null
  13. */
  14. protected $AccessKeySecret = null;
  15. /**
  16. * 自动加载阿里云类
  17. * @var array
  18. */
  19. protected $autoLoadPath = [];
  20. /**
  21. * 是否打印错误信息
  22. * @var bool
  23. */
  24. protected $showError = false;
  25. /**
  26. * 接口数据集合
  27. * @var array
  28. */
  29. protected $request = [];
  30. /**
  31. * 阿里云接口实例化结果
  32. * @var null
  33. */
  34. protected $client = null;
  35. /**
  36. * 动作名称
  37. * @var array
  38. */
  39. protected $action = [];
  40. /**
  41. * 配置信息
  42. * @var array
  43. */
  44. protected $config;
  45. /**
  46. * 类实例化
  47. * @var null
  48. */
  49. protected static $instance = null;
  50. /**
  51. * 错误信息
  52. * @var null
  53. */
  54. protected static $errorInfo = null;
  55. /**
  56. * 初始化程序
  57. * */
  58. protected function __construct($cofing = [])
  59. {
  60. $this->AccessKey = isset($cofing['AccessKey']) ? $cofing['AccessKey'] : null;
  61. $this->AccessKeySecret = isset($cofing['AccessKeySecret']) ? $cofing['AccessKeySecret'] : null;
  62. $this->config = $cofing;
  63. $this->autoLoaderClass();
  64. $this->_initialize();
  65. }
  66. /**
  67. * 自动加载类
  68. * */
  69. protected function autoLoaderClass()
  70. {
  71. if ($this->autoLoadPath) {
  72. require_once(ROOT_PATH . '/extend/Api/aliyun/aliyun-php-sdk-core/Config.php');
  73. foreach ($this->autoLoadPath as $item) {
  74. \Autoloader::addAutoloadPath('aliyun-php-sdk-' . $item);
  75. }
  76. }
  77. }
  78. /**
  79. * 实例化阿里云接口
  80. * */
  81. abstract protected function _initialize();
  82. /**
  83. * 设置错误信息
  84. * @param object | string $error
  85. * @return boolean
  86. * */
  87. protected static function setErrorInfo($error, $thsiAction = null)
  88. {
  89. $_this = self::instance();
  90. $request = \think\Request::instance();
  91. if ($error instanceof \Exception) {
  92. self::$errorInfo = [
  93. 'line' => $error->getLine(),
  94. 'msg' => $error->getMessage(),
  95. 'code' => $error->getCode(),
  96. 'file' => $error->getFile(),
  97. ];
  98. (!$request->isAjax() && $_this->showError) && dump([
  99. 'msg' => $error->getMessage(),
  100. 'code' => $error->getCode(),
  101. 'file' => $error->getFile(),
  102. 'line' => $error->getLine(),
  103. 'action' => $thsiAction,
  104. ]);
  105. } else {
  106. self::$errorInfo = $error;
  107. (!$request->isAjax() && $_this->showError) && dump($error);
  108. }
  109. return false;
  110. }
  111. /**
  112. * 获取错误信息
  113. * @param string $error
  114. * @return array
  115. * */
  116. public static function getErrorInfo($error = '')
  117. {
  118. if (is_null(self::$errorInfo)) self::$errorInfo = $error;
  119. $errorInfo = self::$errorInfo;
  120. self::$errorInfo = null;
  121. if (!is_array($errorInfo)) {
  122. return ['msg' => $errorInfo];
  123. }
  124. return $errorInfo;
  125. }
  126. /**
  127. * 初始化
  128. * @param array $cofing
  129. * @return $this
  130. */
  131. public static function instance($cofing = [])
  132. {
  133. if (is_null(self::$instance)) self::$instance = new static($cofing);
  134. return self::$instance;
  135. }
  136. /**
  137. * 设置时间格式
  138. * */
  139. public static function setTimeFormat($time = '')
  140. {
  141. $time = $time ? $time : time();
  142. if (is_string($time)) {
  143. if ((int)$time == 0) {
  144. $data = date("Y-m-d\\TH:i:s\\Z", strtotime($time));
  145. } else {
  146. $data = date("Y-m-d\\TH:i:s\\Z", $time);
  147. }
  148. } else {
  149. $data = date("Y-m-d\\TH:i:s\\Z", $time);
  150. }
  151. return $data;
  152. }
  153. /**
  154. * 是否打印错误信息
  155. * @param boolean $showError
  156. * @return $this
  157. * */
  158. public function setShowError($showError)
  159. {
  160. $this->showError = $showError;
  161. return $this;
  162. }
  163. /**
  164. * 设置$AccessKey
  165. * @param string $AccessKey
  166. * @return $this
  167. * */
  168. public function setAccessKey($AccessKey)
  169. {
  170. $this->AccessKey = $AccessKey;
  171. return $this;
  172. }
  173. /**
  174. * 设置$AccessKeySecret
  175. * @param string $AccessKeySecret
  176. * @return $this
  177. * */
  178. public function setAccessKeySecret($AccessKeySecret)
  179. {
  180. $this->AccessKeySecret = $AccessKeySecret;
  181. return $this;
  182. }
  183. }