coupon-admin/app/controller/manager/Config.php

243 lines
7.8 KiB
PHP

<?php
namespace app\controller\manager;
use app\model\CommentRule;
use app\model\CouponMain;
use Exception;
use think\exception\ValidateException;
use think\facade\Config as CConfig;
use think\response\Json;
/**
* 额外配置
* Class Config
* @package app\controller\manager
*/
class Config extends Base
{
private string $extraPath = '';
protected function initialize()
{
parent::initialize();
$this->extraPath = config_path() . 'extra/';
if (!is_dir($this->extraPath)) {
if (is_writable(config_path())) {
mkdir($this->extraPath, 0777, true);
} else {
halt('请联系系统管理人员配置文件夹读写权限!请添加'.$this->extraPath.'文件夹的读写权限');
}
} elseif (!is_writable($this->extraPath)) {
halt('请联系系统管理人员配置文件夹读写权限!请添加'.$this->extraPath.'文件夹的读写权限');
}
}
public function other()
{
if ($this->request->isPost()) {
$data = input("post.");
unset($data['_token']);
$php = var_export($data, true);
file_put_contents($this->extraPath . 'other.php', '<?php' . PHP_EOL . 'return ' . $php . ';');
return $this->json();
} else {
CConfig::load('extra/other', 'other');
$this->data['item'] = config('other');
return $this->view();
}
}
public function wechat()
{
if ($this->request->isPost()) {
$data = input("post.");
unset($data['_token']);
$php = var_export($data, true);
file_put_contents($this->extraPath . 'wechat.php', '<?php' . PHP_EOL . 'return ' . $php . ';');
return $this->json();
} else {
CConfig::load('extra/wechat', 'wechat');
$this->data['item'] = config('wechat');
return $this->view();
}
}
public function alipay()
{
if ($this->request->isPost()) {
$data = input("post.");
unset($data['_token']);
$php = var_export($data, true);
file_put_contents($this->extraPath . 'alipay.php', '<?php' . PHP_EOL . 'return ' . $php . ';');
return $this->json();
} else {
CConfig::load('extra/alipay', 'alipay');
$this->data['item'] = config('alipay');
return $this->view();
}
}
public function commentRule()
{
if ($this->request->isPost()) {
$page = input('param.page/d', 1);
$size = input('param.size/d', 10);
$items = CommentRule::findList([],[], $page, $size);
return $this->json(0, 'ok', $items);
}
return $this->view();
}
public function delCommentRule()
{
if ($this->request->isPost()) {
$ids = input('post.ids/a', []);
if (empty($ids)) {
$ids[] = input('post.id/d');
}
CommentRule::deleteByIds($ids);
return $this->json();
}
return $this->json(4001, '非法请求!');
}
public function addCommentRule()
{
if ($this->request->isPost()) {
$item = input('post.item');
$validate = $this->validateByApi($item, [
'rule|规则' => 'require',
'state|状态' => 'require',
]);
if ($validate !== true) {
return $validate;
}
try {
$item['create_time'] = date('Y-m-d H:i:s');
CommentRule::create($item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(4001, $e->getError());
}
}
return $this->view();
}
/**
* 单个字段编辑
*
* @return Json
* @throws Exception
*/
public function modifyCommentRule(): Json
{
if ($this->request->isPost()) {
$item = input('post.');
$validate = $this->validateByApi($item, [
'field' => 'require',
'value' => 'require',
]);
if ($validate !== true) {
return $validate;
}
if (!$info = CommentRule::findById($item['id'])) {
return $this->json(4001, '记录不存在');
}
$update = [$item['field'] => $item['value']];
try {
$info->save($update);
return $this->json();
} catch (ValidateException $e) {
return $this->json(4001, $e->getError());
}
}
return $this->json(4000, '非法请求');
}
public function __call($name, $args)
{
if ($this->request->isPost()) {
try {
$data = input("post.");
$php = var_export($data, true);
file_put_contents(config_path().'extra/'.$name.'.php', '<?php'.PHP_EOL.'return '.$php.';');
return $this->json();
} catch (Exception $e) {
return $this->json(4001, $e->getMessage());
}
} else {
CConfig::load('extra/'.$name, $name);
$this->data['item'] = config($name);
$this->data['action'] = $name;
return $this->view('manager/config/'.$name);
}
}
/* 分配比例*/
public function distributionProportion(){
if ($this->request->isPost()) {
$data = input("post.");
unset($data['_token']);
$validate = $this->validateByApi($data, [
'agency|渠道商占比' => 'require|number',
'admin|平台占比' => 'require|number',
'consumer|消费者占比' => 'require|number',
]);
if ($validate !== true) {
return $validate;
}
$total = $data['agency'] + $data['admin'] + $data['consumer'];
if ($total != 100) {
return $this->json(5002,"分配比例总和不等于100");
}
$php = var_export($data, true);
file_put_contents($this->extraPath . 'distribution_proportion.php', '<?php' . PHP_EOL . 'return ' . $php . ';');
return $this->json();
} else {
CConfig::load('extra/distribution_proportion', 'distributionProportion');
$this->data['item'] = config('distributionProportion');
$this->data['minPrice'] = CouponMain::min_redpack_money;
return $this->view();
}
}
/* 分配比例-分销*/
public function distributionProportionUser(){
if ($this->request->isPost()) {
$data = input("post.");
unset($data['_token']);
$validate = $this->validateByApi($data, [
'admin|平台占比' => 'require|number',
'distribution|(领券)分销者占比' => 'require|number',
'distributed|(核销)分销者占比' => 'require|number',
]);
if ($validate !== true) {
return $validate;
}
$total = $data['admin'] + $data['distribution'] + $data['distributed'];
if ($total != 100) {
return $this->json(5002,"分配比例总和不等于100");
}
$php = var_export($data, true);
file_put_contents($this->extraPath . 'distribution_proportion_user.php', '<?php' . PHP_EOL . 'return ' . $php . ';');
return $this->json();
} else {
CConfig::load('extra/distribution_proportion_user', 'distributionProportionUser');
$this->data['item'] = config('distributionProportionUser');
$this->data['minPrice'] = CouponMain::min_redpack_money;
return $this->view();
}
}
}