AccessToken.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\erp;
  12. use crmeb\services\HttpService;
  13. use crmeb\services\CacheService;
  14. use think\facade\Config;
  15. use think\facade\Log;
  16. use think\helper\Str;
  17. use Psr\SimpleCache\CacheInterface;
  18. /**
  19. * Class AccessTokenServeService
  20. * @package crmeb\services
  21. */
  22. class AccessToken extends HttpService
  23. {
  24. /**
  25. * 配置
  26. * @var string
  27. */
  28. protected $account;
  29. /**
  30. * @var string
  31. */
  32. protected $secret;
  33. /**
  34. * @var string
  35. */
  36. protected $apiUrl;
  37. /**
  38. * 授权登录账号
  39. * @var string
  40. */
  41. protected $authAccount;
  42. /**
  43. * 授权登录密码
  44. * @var string
  45. */
  46. protected $authPassword;
  47. /**
  48. * @var CacheInterface
  49. */
  50. protected $cache;
  51. /**
  52. * @var string
  53. */
  54. protected $accessToken;
  55. /**
  56. * 驱动类型
  57. * @var string
  58. */
  59. protected $name;
  60. /**
  61. * 配置文件名
  62. * @var string
  63. */
  64. protected $configFile;
  65. /**
  66. * 缓存token
  67. * @var string
  68. */
  69. protected $cacheTokenPrefix = "_crmeb_erp";
  70. /**
  71. * 刷新token
  72. * @var string
  73. */
  74. protected $cacheRefreshTokenPrefix = "_crmeb_erp_re";
  75. /**
  76. * AccessTokenServeService constructor.
  77. * AccessToken constructor.
  78. * @param string $name
  79. * @param string $configFile
  80. * @param array $config
  81. * @param CacheInterface|null $cache
  82. */
  83. public function __construct(string $name, string $configFile, array $config, ?CacheInterface $cache = null)
  84. {
  85. if (!$cache) {
  86. /** @var CacheService $cache */
  87. $cache = app()->make(CacheService::class);
  88. }
  89. $this->account = isset($config["app_key"]) ? $config["app_key"] : config($configFile . '.stores.' . $name . '.app_key', '');
  90. $this->secret = isset($config["secret"]) ? $config["secret"] : config($configFile . '.stores.' . $name . '.secret', '');
  91. $this->authAccount = $config['login_account'] ?? config($configFile . '.stores.' . $name . '.login_account', '');
  92. $this->authPassword = $config['login_password'] ?? config($configFile . '.stores.' . $name . '.login_password', '');
  93. $this->cache = $cache;
  94. $this->name = $name;
  95. $this->configFile = $configFile;
  96. $this->apiUrl = Config::get($configFile . '.stores.' . $name . '.url', '');
  97. $this->apiUrl = 'https://openapi.jushuitan.com';
  98. }
  99. /**
  100. * 获取配置
  101. * @return array
  102. */
  103. public function getConfig(): array
  104. {
  105. return [
  106. 'account' => $this->account,
  107. 'secret' => $this->secret
  108. ];
  109. }
  110. /**
  111. * 获取请求链接
  112. * @param string $url
  113. * @return string
  114. */
  115. public function getApiUrl(string $url = ''): string
  116. {
  117. return $url ? $this->apiUrl . $url : $this->apiUrl;
  118. }
  119. /**
  120. * 获取appKey
  121. * @return string
  122. */
  123. public function getAccount(): string
  124. {
  125. return $this->account;
  126. }
  127. /**
  128. * @return mixed|string
  129. */
  130. public function getAuthAccount()
  131. {
  132. return $this->authAccount;
  133. }
  134. /**
  135. * @return mixed|string
  136. */
  137. public function getAuthPassword()
  138. {
  139. return $this->authPassword;
  140. }
  141. /**
  142. * 获取appSecret
  143. * @return string
  144. */
  145. public function getSecret(): string
  146. {
  147. return $this->secret;
  148. }
  149. /**
  150. * 获取token
  151. * @return string
  152. * @throws \Exception
  153. */
  154. public function getAccessToken(): ?string
  155. {
  156. if (isset($this->accessToken)) {
  157. return $this->accessToken;
  158. }
  159. /**
  160. * @see getJushuitanAccessToken
  161. */
  162. $action = 'get' . Str::studly($this->name) . 'AccessToken';
  163. if (method_exists($this, $action)) {
  164. return $this->{$action}();
  165. } else {
  166. throw new \RuntimeException(__CLASS__ . '->' . $action . '(),Method not worn in');
  167. }
  168. }
  169. /**
  170. * 获取聚水潭token
  171. * @return mixed|null|string
  172. * @throws \Exception
  173. */
  174. protected function getJushuitanAccessToken(): ?string
  175. {
  176. //读缓存
  177. $cacheKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  178. $this->accessToken = $this->cache->get($cacheKey);
  179. //需要前端异步授权
  180. if (empty($this->accessToken)) {
  181. throw new \RuntimeException("请跳转授权", 610);
  182. }
  183. return $this->accessToken;
  184. }
  185. /**
  186. * 设置AccessToken缓存
  187. * @param string $accessToken
  188. * @param int $expiresIn
  189. * @return bool
  190. */
  191. public function setAccessToken(string $accessToken, int $expiresIn): bool
  192. {
  193. if (empty($accessToken) || !is_numeric($expiresIn) || $expiresIn <= 0) {
  194. return false;
  195. }
  196. //写缓存
  197. $cacheKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  198. $this->cache->redisHandler()->tag('erp_shop')->set($cacheKey, $accessToken, $expiresIn);
  199. $this->accessToken = $accessToken;
  200. return true;
  201. }
  202. /**
  203. * 设置AccessToken提前过期缓存
  204. * @param string $accessToken
  205. * @param int $expiresIn
  206. * @return bool
  207. */
  208. public function setTokenExpire(string $accessToken, int $expiresIn): bool
  209. {
  210. if (empty($accessToken) || !is_numeric($expiresIn) || $expiresIn <= 0) {
  211. return false;
  212. }
  213. //写缓存
  214. $cacheKey = md5($this->account . '_' . $this->secret . '_epr_expire');
  215. $this->cache->redisHandler()->tag('erp_shop')->set($cacheKey, $accessToken, $expiresIn);
  216. $this->accessToken = $accessToken;
  217. return true;
  218. }
  219. /**
  220. * 获取提前过期缓存
  221. * @return bool|mixed|null
  222. * @throws \Psr\SimpleCache\InvalidArgumentException
  223. */
  224. public function getTokenExpire()
  225. {
  226. $cacheKey = md5($this->account . '_' . $this->secret . '_epr_expire');
  227. return $this->cache->get($cacheKey);
  228. }
  229. /**
  230. * 设置refreshToken缓存
  231. * @param string refreshToken
  232. * @return bool
  233. */
  234. public function setRefreshToken(string $refreshToken): bool
  235. {
  236. if (empty($refreshToken)) {
  237. return false;
  238. }
  239. //写缓存
  240. $cacheKey = md5($this->account . '_' . $this->secret . $this->cacheRefreshTokenPrefix);
  241. $this->cache->redisHandler()->tag('erp_shop')->set($cacheKey, $refreshToken);
  242. return true;
  243. }
  244. /**
  245. * 获取refreshToken缓存
  246. * @param string refreshToken
  247. * @return string
  248. */
  249. public function getRefreshToken(): string
  250. {
  251. //读缓存
  252. $cacheKey = md5($this->account . '_' . $this->secret . $this->cacheRefreshTokenPrefix);
  253. return $this->cache->get($cacheKey);
  254. }
  255. }