<?php
|
|
namespace app\admin\controller\general;
|
|
use app\common\controller\Backend;
|
use app\common\library\Email;
|
use app\common\model\Config as ConfigModel;
|
use think\Cache;
|
use think\Db;
|
use think\Exception;
|
use think\Validate;
|
|
|
/**
|
* 代理配置
|
*
|
*/
|
class Agent extends Backend
|
{
|
|
protected $model = null;
|
protected $noNeedRight = [];
|
|
public function _initialize()
|
{
|
parent::_initialize();
|
// $this->model = model('Config');
|
$this->model = new ConfigModel;
|
}
|
|
/**
|
* 查看
|
*/
|
public function index()
|
{
|
$agent_mode_list = ConfigModel::where(['name'=>'agent_mode'])->value('content');
|
$agent_mode_list = json_decode($agent_mode_list,true);
|
$this->assign('agent_mode_list',$agent_mode_list);
|
if ($this->request->isPost()) {
|
$params = $this->request->post("row/a", [], 'trim');
|
if ($params) {
|
if($params['agent_mode'] == "") $this->error('代理模式不能为空');
|
$data = $params['data'];
|
if(empty($data))$this->error('数据错误');
|
foreach($data as $key => $item){
|
switch($key){
|
case 0:
|
if($item['level_name'] == "") $this->error('低级消费者名称未配置');
|
break;
|
case 1:
|
if($item['level_name'] == "") $this->error('初级代理名称未配置');
|
if($item['own_commission_rate'] == "") $this->error('自购佣金比例不能为空');
|
if($item['direct_commission_rate'] == "") $this->error('直属初级佣金比例不能为空');
|
if($item['indirect_commission_rate'] == "") $this->error('间接初级佣金比例不能为空');
|
break;
|
case 2:
|
if($item['level_name'] == "") $this->error('中级代理名称未配置');
|
if($item['own_commission_rate'] == "") $this->error('自购佣金比例不能为空');
|
if($item['direct_commission_rate'] == "") $this->error('直属初级佣金比例不能为空');
|
if($item['indirect_commission_rate'] == "") $this->error('间接初级佣金比例不能为空');
|
if($item['direct_primary_count'] == "") $this->error('有效直属初级人数不能为空');
|
if($item['indirect_primary_count'] == "") $this->error('有效间接初级人数不能为空');
|
if($item['own_monthly_commission'] == "") $this->error('上月结算佣金不能为空');
|
break;
|
case 3 :
|
if($item['level_name'] == "") $this->error('高级代理名称未配置');
|
if($item['own_commission_rate'] == "") $this->error('自购佣金比例不能为空');
|
if($item['direct_commission_rate'] == "") $this->error('直属初级佣金比例不能为空');
|
if($item['direct_intermediate_rate']== "") $this->error('直属中级佣金比例不能为空');
|
if($item['indirect_commission_rate']== "") $this->error('间接初级佣金比例不能为空');
|
// if($item['direct_primary_count'] == "") $this->error('有效直属初级人数不能为空');
|
// if($item['indirect_primary_count']== "") $this->error('有效间接初级人数不能为空');
|
if($item['own_monthly_commission'] == "") $this->error('上月结算佣金不能为空');
|
if($item['accumulated_commission'] == "") $this->error('累计结算佣金不能为空');
|
break;
|
}
|
}
|
Db::startTrans();
|
$agent_mode = config('site.agent_mode');
|
$flag = false;
|
if($agent_mode != $params['agent_mode']) $flag = true;
|
if($flag){
|
$res_config = Db::name('config')->where(['name'=>'agent_mode'])->update(['value'=>$params['agent_mode']]);
|
if(!$res_config){
|
Db::rollback();
|
$this->error('代理模式设置失败');
|
}
|
}
|
foreach($data as $level => $save_data){
|
$save_data['update_time'] = time();
|
$res = Db::name('user_level')->where(['level'=>$level])->update($save_data);
|
if(!$res){
|
Db::rollback();
|
$this->error('比例设置失败');
|
}
|
}
|
Db::commit();
|
ConfigModel::refreshFile();
|
$this->success();
|
}else{
|
$this->error(__('Parameter %s can not be empty', ''));
|
}
|
}
|
$data_list = Db::name('user_level')->where(['status'=>1])->select();
|
$this->assign('agent_mode',config('site.agent_mode'));
|
$this->assign('row',$data_list);
|
return $this->view->fetch();
|
}
|
|
/**
|
* 添加
|
*/
|
public function add()
|
{
|
if (!config('app_debug')) {
|
$this->error(__('Only work at development environment'));
|
}
|
if ($this->request->isPost()) {
|
$this->token();
|
$params = $this->request->post("row/a", [], 'trim');
|
if ($params) {
|
foreach ($params as $k => &$v) {
|
$v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
|
}
|
if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
|
$params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
|
} else {
|
$params['content'] = '';
|
}
|
try {
|
$result = $this->model->create($params);
|
} catch (Exception $e) {
|
$this->error($e->getMessage());
|
}
|
if ($result !== false) {
|
try {
|
ConfigModel::refreshFile();
|
} catch (Exception $e) {
|
$this->error($e->getMessage());
|
}
|
$this->success();
|
} else {
|
$this->error($this->model->getError());
|
}
|
}
|
$this->error(__('Parameter %s can not be empty', ''));
|
}
|
return $this->view->fetch();
|
}
|
|
/**
|
* 编辑
|
* @param null $ids
|
*/
|
public function edit($ids = null)
|
{
|
if ($this->request->isPost()) {
|
$this->token();
|
$row = $this->request->post("row/a", [], 'trim');
|
if ($row) {
|
$configList = [];
|
foreach ($this->model->all() as $v) {
|
if (isset($row[$v['name']])) {
|
$value = $row[$v['name']];
|
if (is_array($value) && isset($value['field'])) {
|
$value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
|
} else {
|
$value = is_array($value) ? implode(',', $value) : $value;
|
}
|
$v['value'] = $value;
|
$configList[] = $v->toArray();
|
}
|
}
|
try {
|
$this->model->allowField(true)->saveAll($configList);
|
} catch (Exception $e) {
|
$this->error($e->getMessage());
|
}
|
try {
|
ConfigModel::refreshFile();
|
} catch (Exception $e) {
|
$this->error($e->getMessage());
|
}
|
$this->success();
|
}
|
$this->error(__('Parameter %s can not be empty', ''));
|
}
|
}
|
|
|
}
|