OutAccountServices.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\out;
  12. use app\dao\out\OutAccountDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AuthException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\FormBuilder as Form;
  17. use crmeb\utils\JwtAuth;
  18. use Firebase\JWT\ExpiredException;
  19. /**
  20. * 获取token
  21. * Class LoginServices
  22. * @package app\services\kefu
  23. * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
  24. * @method update($id, array $data, ?string $key = null)
  25. * @method save(array $data)保存
  26. */
  27. class OutAccountServices extends BaseServices
  28. {
  29. /**
  30. * LoginServices constructor.
  31. * @param OutAccountDao $dao
  32. */
  33. public function __construct(OutAccountDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 账号密码登录
  39. * @param string $appid
  40. * @param string $appsecret
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function authLogin(string $appid, string $appsecret = null)
  47. {
  48. $autInfo = $this->dao->get(['appid' => $appid, 'is_del' => 0]);
  49. if (!$autInfo) {
  50. throw new AuthException(410141);
  51. }
  52. if ($appsecret && !password_verify($appsecret, $autInfo->appsecret)) {
  53. throw new AuthException(400744);
  54. }
  55. if ($autInfo->status == 0) {
  56. throw new AuthException(400595);
  57. }
  58. $token = $this->createToken($autInfo->id, 'out');
  59. $data['last_time'] = time();
  60. $data['ip'] = request()->ip();
  61. $this->update($autInfo['id'], $data);
  62. return [
  63. 'access_token' => $token['token'],
  64. 'exp_time' => $token['params']['exp'],
  65. 'auth_info' => $autInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray()
  66. ];
  67. }
  68. /**
  69. * 解析token
  70. * @param string $token
  71. * @return array
  72. * @throws \Psr\SimpleCache\InvalidArgumentException
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function parseToken(string $token)
  78. {
  79. /** @var CacheService $cacheService */
  80. $cacheService = app()->make(CacheService::class);
  81. /** @var JwtAuth $jwtAuth */
  82. $jwtAuth = app()->make(JwtAuth::class);
  83. //获取token信息
  84. [$md5Token, $id, $type] = $this->verifyToken($token, $jwtAuth, $cacheService);
  85. //获取对外账号
  86. $authInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  87. $this->checkAuth($authInfo, $md5Token, $cacheService);
  88. return $authInfo->hidden(['appsecret', 'ip', 'is_del', 'add_time', 'status', 'last_time'])->toArray();
  89. }
  90. /**
  91. * 获取一条
  92. * @return array|\think\Model|null
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function getOne($where = [])
  98. {
  99. $info = $this->dao->getOne($where);
  100. return $info ? $info->toArray() : [];
  101. }
  102. /**
  103. * 获取列表
  104. * @param array $where
  105. * @return array
  106. */
  107. public function getList(array $where = [])
  108. {
  109. [$page, $limit] = $this->getPageValue();
  110. $where['is_del'] = 0;
  111. $list = $this->dao->getList($where, $page, $limit);
  112. $count = $this->dao->count($where);
  113. if ($list) {
  114. foreach ($list as &$item) {
  115. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  116. $item['last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '暂无';
  117. $item['rules'] = is_null($item['rules']) ? [] : explode(',', $item['rules']);
  118. }
  119. }
  120. return compact('count', 'list');
  121. }
  122. /**
  123. * 刷新token
  124. * @param string $token
  125. * @return array
  126. */
  127. public function refresh(string $token): array
  128. {
  129. /** @var CacheService $cacheService */
  130. $cacheService = app()->make(CacheService::class);
  131. /** @var JwtAuth $jwtAuth */
  132. $jwtAuth = app()->make(JwtAuth::class);
  133. //获取token信息
  134. [$md5Token, $id, $type] = $this->verifyToken($token, $jwtAuth, $cacheService);
  135. //获取对外账号
  136. $authInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  137. $this->checkAuth($authInfo, $md5Token, $cacheService);
  138. $cacheService->clearToken($md5Token);
  139. $token = $jwtAuth->createToken($id, $type);
  140. $data['last_time'] = time();
  141. $data['ip'] = request()->ip();
  142. $this->dao->update($id, $data);
  143. return [
  144. 'access_token' => $token['token'],
  145. 'exp_time' => $token['params']['exp'],
  146. ];
  147. }
  148. /**
  149. * 核对用户
  150. * @param $authInfo
  151. * @param string $md5Token
  152. * @param CacheService $cacheService
  153. * @return bool
  154. */
  155. protected function checkAuth($authInfo, string $md5Token, CacheService $cacheService): bool
  156. {
  157. if (!$authInfo) {
  158. if (!request()->isCli()) {
  159. $cacheService->clearToken($md5Token);
  160. }
  161. throw new AuthException(110003);
  162. }
  163. if ($authInfo->status == 2) {
  164. if (!request()->isCli()) {
  165. $cacheService->clearToken($md5Token);
  166. }
  167. throw new AuthException(400595);
  168. }
  169. return true;
  170. }
  171. /**
  172. * 获取token
  173. * @param string $token
  174. * @param JwtAuth $jwtAuth
  175. * @param CacheService $cacheService
  176. * @return array
  177. * @throws \Psr\SimpleCache\InvalidArgumentException
  178. */
  179. protected function verifyToken(string $token, JwtAuth $jwtAuth, CacheService $cacheService): array
  180. {
  181. if (!$token || $token === 'undefined') {
  182. throw new AuthException(400172);
  183. }
  184. $md5Token = md5($token);
  185. if (!$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
  186. throw new AuthException(110006);
  187. }
  188. //是否超出有效次数
  189. if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
  190. if (!request()->isCli()) {
  191. $cacheService->clearToken($md5Token);
  192. }
  193. throw new AuthException(110006);
  194. }
  195. //解析token
  196. [$id, $type] = $jwtAuth->parseToken($token);
  197. if (!$id || $type != 'out') {
  198. throw new AuthException(400172);
  199. }
  200. try {
  201. $jwtAuth->verifyToken();
  202. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  203. } catch (ExpiredException $e) {
  204. $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
  205. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  206. } catch (\Throwable $e) {
  207. if (!request()->isCli()) {
  208. $cacheService->clearToken($md5Token);
  209. }
  210. throw new AuthException(400172);
  211. }
  212. return [$md5Token, $id, $type];
  213. }
  214. /**
  215. * 设置账号推送接口表单
  216. * @param $id
  217. * @return array
  218. * @throws \FormBuilder\Exception\FormBuilderException
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\DbException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. */
  223. public function outSetUpForm($id)
  224. {
  225. $info = $this->dao->get($id);
  226. $f[] = Form::radio('push_open', '推送开关', $info['push_open'] ?? 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  227. $f[] = Form::input('order_create_push', '订单创建推送', $info['order_create_push'] ?? '');
  228. $f[] = Form::input('order_pay_push', '订单支付推送', $info['order_pay_push'] ?? '');
  229. $f[] = Form::input('refund_create_push', '退款单创建推送', $info['refund_create_push'] ?? '');
  230. $f[] = Form::input('refund_cancel_push', '退款单取消推送', $info['refund_cancel_push'] ?? '');
  231. return create_form('设置推送', $f, $this->url('/setting/system_out_account/set_up/' . $id), 'PUT');
  232. }
  233. /**
  234. * 设置账号推送接口
  235. * @param $id
  236. * @param $data
  237. * @return \crmeb\basic\BaseModel
  238. */
  239. public function outSetUpSave($id, $data)
  240. {
  241. return $this->dao->update($id, $data);
  242. }
  243. }