TaxConfigService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\models\TaxConfig;
  4. use Exception;
  5. use Yii;
  6. use yii\helpers\Json;
  7. /**
  8. * 配置
  9. * TaxConfigService class
  10. * @package addons\AvoidTax\common\service
  11. */
  12. class TaxConfigService extends BaseService
  13. {
  14. /**
  15. * 添加
  16. *
  17. * @param array $data
  18. * @return boolean
  19. */
  20. public function add(array $data): bool
  21. {
  22. $model = TaxConfig::getModelByMallId($this->mall_id);
  23. $alipay = !empty($data['alipay']) ? $data['alipay'] : [];
  24. $data = array_merge($data, $alipay);
  25. $data['use_scene'] = empty($data['use_scene']) ? '' : implode(',', $data['use_scene']);
  26. $data['ali_config'] = Json::encode($alipay);
  27. $data['tax_config'] = Json::encode($data['tax']);
  28. $data['ly_config'] = Json::encode($data['ly']);
  29. $data['mall_id'] = $this->mall_id;
  30. Yii::$app->services->setting->addons->set(
  31. $this->mall_id,'AvoidTax', ['basic' => $data['basic']]
  32. );
  33. if ($model->load($data, '') && $model->save()) {
  34. $this->setCacheConfig(
  35. $this->getDbConfig()
  36. );
  37. return true;
  38. } else {
  39. return $this->error(array_values($model->getFirstErrors())[0]);
  40. }
  41. }
  42. /**
  43. * 获取配置
  44. *
  45. * @return array
  46. */
  47. public function getConfig()
  48. {
  49. $basic = Yii::$app->services->setting->addons->get($this->mall_id,'AvoidTax', 'basic');
  50. if(isset($basic['withdrawal_no_check'])){
  51. $basic['withdrawal_no_check'] = intval($basic['withdrawal_no_check']);
  52. }else{
  53. $basic['withdrawal_no_check'] = 0;
  54. }
  55. if ($cache = $this->getCacheConfig()) {
  56. $cache['basic'] = $basic;
  57. return $cache;
  58. }
  59. $cache = $this->getDbConfig();
  60. $this->setCacheConfig($cache);
  61. $cache['basic'] = $basic;
  62. return $cache;
  63. }
  64. /**
  65. * 获取配置的配置类型
  66. *
  67. * @return int
  68. */
  69. public function getType()
  70. {
  71. return $this->getConfig()['type'];
  72. }
  73. /**
  74. * 个人版
  75. *
  76. * @return boolean
  77. */
  78. public function isPersonalType()
  79. {
  80. return $this->getType() == TaxConfig::PERSONAL;
  81. }
  82. /**
  83. * 服务商版本
  84. *
  85. * @return boolean
  86. */
  87. public function isServiceType()
  88. {
  89. return $this->getType() == TaxConfig::SERVICE;
  90. }
  91. /**
  92. * 获取配置的AlipayUserId
  93. *
  94. * @return string
  95. */
  96. public function getAlipayUserId()
  97. {
  98. return $this->getConfig()['ali_config']['alipay_user_id'];
  99. }
  100. /**
  101. * 获取缓存里面的配置
  102. *
  103. * @return array|null
  104. */
  105. public function getCacheConfig()
  106. {
  107. return Json::decode(Yii::$app->redis->get(
  108. $this->getCacheField()
  109. ));
  110. }
  111. /**
  112. * 设置缓存配置
  113. *
  114. * @param array $config
  115. * @return void
  116. */
  117. public function setCacheConfig(array $config, int $seconds = 10)
  118. {
  119. Yii::$app->redis->setex(
  120. $this->getCacheField(),
  121. $seconds,
  122. Json::encode($config)
  123. );
  124. }
  125. /**
  126. * 获取数据库里的配置
  127. *
  128. * @return array
  129. * @throws Exception
  130. */
  131. public function getDbConfig()
  132. {
  133. $config = TaxConfig::getConfig($this->mall_id);
  134. if (empty($config)) throw new Exception('未设置配置信息');
  135. $config['type'] = (int) $config['type'];
  136. $config['api_type'] = (int) $config['api_type'];
  137. $config['settle'] = (int) $config['settle'];
  138. $config['payment_flow_status'] = (int) ($config['payment_flow_status'] ?? 0);
  139. $config['ali_config'] = Json::decode($config['ali_config']);
  140. $config['tax_config'] = Json::decode($config['tax_config']);
  141. $config['ly_config'] = Json::decode($config['ly_config']);
  142. return $config;
  143. }
  144. /**
  145. * 获取支付宝配置
  146. *
  147. * @return array
  148. * @throws Exception
  149. */
  150. public function getAlipayConfig()
  151. {
  152. $type = $this->getType();
  153. // 个人
  154. if ($type == TaxConfig::PERSONAL) {
  155. return $this->getConfig()['ali_config'];
  156. }
  157. // 服务商
  158. if ($type == TaxConfig::SERVICE) {
  159. return (new TaxConfig())->getServiceConfig();
  160. }
  161. throw new Exception('配置有误');
  162. }
  163. /**
  164. * API类型
  165. *
  166. * @return integer
  167. */
  168. public function getApiType()
  169. {
  170. return $this->getConfig()['api_type'];
  171. }
  172. /**
  173. * 获取lyconfig
  174. *
  175. * @return array
  176. */
  177. public function getLYConfig()
  178. {
  179. return $this->getConfig()['ly_config'];
  180. }
  181. /**
  182. * 获取xinlong
  183. *
  184. * @return array
  185. */
  186. public function getXLConfig()
  187. {
  188. return $this->getConfig()['tax_config'];
  189. }
  190. /**
  191. * 缓存字段
  192. *
  193. * @return string
  194. */
  195. protected function getCacheField()
  196. {
  197. return 'tax_config_mall_' . $this->mall_id;
  198. }
  199. /**
  200. * 获取费用
  201. *
  202. * @return float
  203. */
  204. public function getTaxFee()
  205. {
  206. return $this->getTaxConfig()['taxFee'];
  207. }
  208. /**
  209. * xlapi
  210. *
  211. * @return boolean
  212. */
  213. public function isXLApiType()
  214. {
  215. return $this->getApiType() === TaxConfig::XIONLONG;
  216. }
  217. /**
  218. * lyapi
  219. *
  220. * @return boolean
  221. */
  222. public function isLYApiType()
  223. {
  224. return $this->getApiType() === TaxConfig::LUYONG;
  225. }
  226. /**
  227. * accountId
  228. *
  229. * @return string
  230. */
  231. public function getAccountId()
  232. {
  233. return $this->getLYConfig()['accountId'];
  234. }
  235. /**
  236. * xl uid
  237. *
  238. * @return string
  239. */
  240. public function getUid()
  241. {
  242. return $this->getXLConfig()['uid'];
  243. }
  244. /**
  245. * @return int
  246. */
  247. public function getPaymentFlowStatus()
  248. {
  249. return $this->getConfig()['payment_flow_status'] ?? 0;
  250. }
  251. /**
  252. * 获取手续费打款账户
  253. *
  254. * @return array
  255. */
  256. public function getTaxConfig()
  257. {
  258. // xinlong
  259. if ($this->getApiType() == TaxConfig::XIONLONG) {
  260. $config = $this->getXLConfig();
  261. }
  262. // luyong
  263. if ($this->getApiType() == TaxConfig::LUYONG) {
  264. $config = $this->getLYConfig();
  265. }
  266. return $config;
  267. }
  268. }