| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\models\TaxConfig;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 配置
- * TaxConfigService class
- * @package addons\AvoidTax\common\service
- */
- class TaxConfigService extends BaseService
- {
- /**
- * 添加
- *
- * @param array $data
- * @return boolean
- */
- public function add(array $data): bool
- {
- $model = TaxConfig::getModelByMallId($this->mall_id);
-
- $alipay = !empty($data['alipay']) ? $data['alipay'] : [];
- $data = array_merge($data, $alipay);
- $data['use_scene'] = empty($data['use_scene']) ? '' : implode(',', $data['use_scene']);
- $data['ali_config'] = Json::encode($alipay);
- $data['tax_config'] = Json::encode($data['tax']);
- $data['ly_config'] = Json::encode($data['ly']);
- $data['mall_id'] = $this->mall_id;
- Yii::$app->services->setting->addons->set(
- $this->mall_id,'AvoidTax', ['basic' => $data['basic']]
- );
- if ($model->load($data, '') && $model->save()) {
- $this->setCacheConfig(
- $this->getDbConfig()
- );
- return true;
- } else {
- return $this->error(array_values($model->getFirstErrors())[0]);
- }
- }
- /**
- * 获取配置
- *
- * @return array
- */
- public function getConfig()
- {
- $basic = Yii::$app->services->setting->addons->get($this->mall_id,'AvoidTax', 'basic');
- if(isset($basic['withdrawal_no_check'])){
- $basic['withdrawal_no_check'] = intval($basic['withdrawal_no_check']);
- }else{
- $basic['withdrawal_no_check'] = 0;
- }
- if ($cache = $this->getCacheConfig()) {
- $cache['basic'] = $basic;
- return $cache;
- }
- $cache = $this->getDbConfig();
- $this->setCacheConfig($cache);
- $cache['basic'] = $basic;
- return $cache;
- }
- /**
- * 获取配置的配置类型
- *
- * @return int
- */
- public function getType()
- {
- return $this->getConfig()['type'];
- }
- /**
- * 个人版
- *
- * @return boolean
- */
- public function isPersonalType()
- {
- return $this->getType() == TaxConfig::PERSONAL;
- }
- /**
- * 服务商版本
- *
- * @return boolean
- */
- public function isServiceType()
- {
- return $this->getType() == TaxConfig::SERVICE;
- }
- /**
- * 获取配置的AlipayUserId
- *
- * @return string
- */
- public function getAlipayUserId()
- {
- return $this->getConfig()['ali_config']['alipay_user_id'];
- }
- /**
- * 获取缓存里面的配置
- *
- * @return array|null
- */
- public function getCacheConfig()
- {
- return Json::decode(Yii::$app->redis->get(
- $this->getCacheField()
- ));
- }
- /**
- * 设置缓存配置
- *
- * @param array $config
- * @return void
- */
- public function setCacheConfig(array $config, int $seconds = 10)
- {
- Yii::$app->redis->setex(
- $this->getCacheField(),
- $seconds,
- Json::encode($config)
- );
- }
- /**
- * 获取数据库里的配置
- *
- * @return array
- * @throws Exception
- */
- public function getDbConfig()
- {
- $config = TaxConfig::getConfig($this->mall_id);
- if (empty($config)) throw new Exception('未设置配置信息');
- $config['type'] = (int) $config['type'];
- $config['api_type'] = (int) $config['api_type'];
- $config['settle'] = (int) $config['settle'];
- $config['payment_flow_status'] = (int) ($config['payment_flow_status'] ?? 0);
- $config['ali_config'] = Json::decode($config['ali_config']);
- $config['tax_config'] = Json::decode($config['tax_config']);
- $config['ly_config'] = Json::decode($config['ly_config']);
- return $config;
- }
- /**
- * 获取支付宝配置
- *
- * @return array
- * @throws Exception
- */
- public function getAlipayConfig()
- {
- $type = $this->getType();
- // 个人
- if ($type == TaxConfig::PERSONAL) {
- return $this->getConfig()['ali_config'];
- }
-
- // 服务商
- if ($type == TaxConfig::SERVICE) {
- return (new TaxConfig())->getServiceConfig();
- }
- throw new Exception('配置有误');
- }
- /**
- * API类型
- *
- * @return integer
- */
- public function getApiType()
- {
- return $this->getConfig()['api_type'];
- }
- /**
- * 获取lyconfig
- *
- * @return array
- */
- public function getLYConfig()
- {
- return $this->getConfig()['ly_config'];
- }
- /**
- * 获取xinlong
- *
- * @return array
- */
- public function getXLConfig()
- {
- return $this->getConfig()['tax_config'];
- }
- /**
- * 缓存字段
- *
- * @return string
- */
- protected function getCacheField()
- {
- return 'tax_config_mall_' . $this->mall_id;
- }
- /**
- * 获取费用
- *
- * @return float
- */
- public function getTaxFee()
- {
- return $this->getTaxConfig()['taxFee'];
- }
- /**
- * xlapi
- *
- * @return boolean
- */
- public function isXLApiType()
- {
- return $this->getApiType() === TaxConfig::XIONLONG;
- }
- /**
- * lyapi
- *
- * @return boolean
- */
- public function isLYApiType()
- {
- return $this->getApiType() === TaxConfig::LUYONG;
- }
- /**
- * accountId
- *
- * @return string
- */
- public function getAccountId()
- {
- return $this->getLYConfig()['accountId'];
- }
- /**
- * xl uid
- *
- * @return string
- */
- public function getUid()
- {
- return $this->getXLConfig()['uid'];
- }
- /**
- * @return int
- */
- public function getPaymentFlowStatus()
- {
- return $this->getConfig()['payment_flow_status'] ?? 0;
- }
- /**
- * 获取手续费打款账户
- *
- * @return array
- */
- public function getTaxConfig()
- {
- // xinlong
- if ($this->getApiType() == TaxConfig::XIONLONG) {
- $config = $this->getXLConfig();
- }
- // luyong
- if ($this->getApiType() == TaxConfig::LUYONG) {
- $config = $this->getLYConfig();
- }
- return $config;
- }
- }
|