<?php
namespace app\store\model\dealer;
use think\Cache;
use app\common\exception\BaseException;
use app\common\model\dealer\Setting as SettingModel;
class Setting extends SettingModel
{
private $describe = [
'basic' => '基础设置',
'condition' => '分销商条件',
'commission' => '佣金设置',
'settlement' => '结算',
'words' => '自定义文字',
'license' => '申请协议',
'background' => '页面背景图',
'qrcode' => '分销海报',
'jl' => '奖励金',
];
public function edit($data)
{
$this->startTrans();
try {
foreach ($data as $key => $values)
$this->saveValues($key, $values);
$this->commit();
Cache::rm('dealer_setting_' . self::$wxapp_id);
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
$this->rollback();
return false;
}
}
private function saveValues($key, $values)
{
$model = self::detail($key) ?: new self;
if (!$this->validValues($key, $values)) {
throw new BaseException(['msg' => $this->error]);
}
return $model->save([
'key' => $key,
'describe' => $this->describe[$key],
'values' => $values,
'wxapp_id' => self::$wxapp_id,
]);
}
private function validValues($key, $values)
{
if ($key === 'settlement') {
return $this->validSettlement($values);
} return true;
}
private function validSettlement($values)
{
if (!isset($values['pay_type']) || empty($values['pay_type'])) {
$this->error = '请设置 结算-提现方式';
return false;
}
return true;
}
}