REAPI/app/channel/controller/ChannelProduct.php
mzeros 813d07adb9 fix(channel): 修复渠道产品折扣类型显示问题
- 在 ChannelProduct 控制器中添加 percent_type属性赋值
- 更新 percent.html 模板,使用 percent_type变量 instead of vo.percent_type
2025-01-04 15:49:29 +08:00

392 lines
13 KiB
PHP

<?php
namespace app\channel\controller;
use think\admin\Controller;
use app\channel\service\PercentService as ChannelPercent;
use app\merchant\service\PercentService as MerchantService;
/**
* 渠道商品管理
* Class ChannelProduct
* @package app\channel\controller
*/
class ChannelProduct extends Controller
{
/**
* 绑定数据表
* @var string
*/
private $table = 'ChannelProduct';
/**
* 渠道商品管理
* @auth true
* @menu true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->cid = input('cid');
$channel = $this->app->db->name('ChannelList')->where(['id' => $this->cid])->find();
$this->title = $channel['name'] . '的产品与折扣管理';
$query = $this->_query($this->table);
$query->like('name')->equal('status,cid')->dateBetween('create_at');
$query->where(['is_deleted' => 0])->order('status desc,sort desc,id desc')->page();
}
public function shop()
{
$cid = input('cid');
$shops[] = array
(
'id' => -1,
'name' => '无',
);
$shop = $this->app->db->name('ChannelShop')->where(['cid' => $cid, 'is_deleted' => 0])->select()->toArray();
//$shop = $this->app->db->name('ChannelShop')->where(['is_deleted' => 0])->select()->toArray();
$data['shops'] = array_merge($shops, $shop);
$this->fetch('shop', $data);
}
/**
* 数据列表处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _page_filter(&$data)
{
$this->key = MerchantService::instance()->getIsp();
$this->cates = $this->app->db->name('ChannelList')->where(['is_deleted' => 0])->select();
foreach ($data as &$vo) {
$vo['channel_name'] = $this->app->db->name('ChannelList')->where(['id' => $vo['cid']])->value('name');
if ($vo['shop_id'] > 0) {
$vo['shop_name'] = $this->app->db->name('ChannelShop')->where(['id' => $vo['shop_id']])->value('name');
} else {
$vo['shop_name'] = '无';
}
if ($vo['rule']) {
$vo['rule'] = $this->rule($vo['key'], $vo['rule'], $vo['percent_type']);
}
if ($vo['gid_rule']) {
$vo['gid_rule'] = $this->gid_rule($vo['key'], $vo['gid_rule']);
}
if ($vo['percent_type'] == 2) {
$vo['percent_type_name'] = '固定数值';
} else {
$vo['percent_type_name'] = '百分比';
}
}
}
protected function rule($product_key, $rule, $type)
{
if ($rule) {
$rule = json_decode($rule, true);
if ($type == 1) {
$name = '百分比';
} else {
$name = '固定数值';
}
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">'.$name.'</td></tr>';
foreach ($rule as $k => $v) {
if ($product_key == 'dhcz' && isset($this->key[$k]) && $this->key[$k]) {
$k = $this->key[$k];
}
$table .= '<tr><td class="text-left nowrap">'.$k.'</td>
<td class="text-left nowrap">'.$v.'</td></tr>';
}
$table .= '</table>';
return $table;
} else {
return '';
}
}
protected function gid_rule($product_key, $rule)
{
if ($rule) {
$rule = json_decode($rule, true);
$table = '<table class="layui-table margin-top-10">';
$table .= '<tr>
<td class="text-left nowrap">面值</td>
<td class="text-left nowrap">产品ID</td></tr>';
foreach ($rule as $k => $v) {
$table .= '<tr><td class="text-left nowrap">'.$k.'</td>
<td class="text-left nowrap">'.$v.'</td></tr>';
}
$table .= '</table>';
return $table;
} else {
return '';
}
}
/**
* 表单数据处理
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function _form_filter(&$data)
{
if ($this->request->isPost()) {
if (isset($data['auto_value']) && $data['auto_value']) {
$data['auto_value'] = implode(',', $data['auto_value']);
} else {
$data['auto_value'] = '';
}
} else {
$this->channels = $this->app->db->name('ChannelList')->where(['is_deleted' => 0])->select();
if (isset($data['auto_value']) && $data['auto_value'] && is_string($data['auto_value'])) {
$data['auto_value'] = explode(',', $data['auto_value']);
} else {
$data['auto_value'] = array();
}
if (isset($data['value']) && $data['value']) {
$this->value = explode(',', $data['value']);
} else {
$this->value = array();
}
$this->shops[] = array
(
'id' => -1,
'name' => '无',
);
if ($data && isset($data['cid']) && $data['cid']) {
$shop = $this->app->db->name('ChannelShop')->where(['cid' => $data['cid'], 'is_deleted' => 0])->select()->toArray();
//$shop = $this->app->db->name('ChannelShop')->where(['is_deleted' => 0])->select()->toArray();
$this->shops = array_merge($this->shops, $shop);
} elseif (isset($this->channels[0]['id'])) {
$shop = $this->app->db->name('ChannelShop')->where(['cid' => $this->channels[0]['id'], 'is_deleted' => 0])->select()->toArray();
//$shop = $this->app->db->name('ChannelShop')->where(['is_deleted' => 0])->select()->toArray();
$this->shops = array_merge($this->shops, $shop);
}
}
if ($this->request->isPost()) {
if (empty($data['name'])) $this->error('商品名称不能为空!');
if (!isset($data['id'])) {
if ($this->app->db->name($this->table)->where(['key' => $data['key'], 'cid' => $data['cid'],'is_deleted' => 0])->find()) $this->error("KEY:{$data['key']}已经存在!");
}
}
}
/**
* 添加渠道商品
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function add()
{
$this->_form($this->table, 'form');
}
/**
* 编辑渠道商品
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit()
{
$this->_form($this->table, 'form');
}
/**
* 修改渠道商品状态
* @auth true
* @throws \think\db\exception\DbException
*/
public function state()
{
$this->_save($this->table, $this->_vali([
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]));
}
/**
* 删除渠道商品
* @auth true
* @throws \think\db\exception\DbException
*/
public function remove()
{
$this->_delete($this->table);
}
/**
* 折扣设置
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function percent()
{
$this->pid = input('id');
$product = $this->app->db->name($this->table)->where(['id' => $this->pid])->find();
$this->cash = explode(',' , $product['value']);
$this->percent = $product['percent'];
if ($this->request->isGet()) {
$this->rule = array();
$this->gid_rule = $gid_rule = array();
$this->order_limit_rule = $order_limit_rule = array();
$data['percent'] = $product['percent'];
#此处修复前端无法显示折扣类型问题
$this->percent_type = $data['percent_type'] = $product['percent_type'];
if ($product['gid_rule']) {
$this->gid_rule = $gid_rule = json_decode($product['gid_rule'], true);
}
if ($product['order_limit_rule']) {
$this->order_limit_rule = $order_limit_rule = json_decode($product['order_limit_rule'], true);
}
if ($product['rule']) {
$rule = json_decode($product['rule'], true);
if ($product['key'] == 'dhcz') {
$this->rule = array();
$key = MerchantService::instance()->getIsp();
foreach ($rule as $k => $v) {
if (isset($key[$k]) && $key[$k]) {
$this->rule[$key[$k]] = $v;
}
}
if ($product['gid_rule']) {
foreach ($gid_rule as $k => $v) {
if (isset($key[$k]) && $key[$k]) {
$this->gid_rule[$key[$k]] = $v;
}
}
}
if ($product['order_limit_rule']) {
foreach ($order_limit_rule as $k => $v) {
if (isset($key[$k]) && $key[$k]) {
$this->order_limit_rule[$key[$k]] = $v;
}
}
}
} else {
$this->rule = $rule;
$this->gid_rule = $gid_rule;
$this->order_limit_rule = $order_limit_rule;
}
}
$this->gid = $product['gid'];
$this->order_limit = $product['order_limit'];
$this->verify = false;
$this->_form('channel_percent', 'percent');
} else {
$percent = input('percent');
$percent_type = input('percent_type');
if ($this->cash) {
$key = array();
if ($product['key'] == 'dhcz') {
$key = MerchantService::instance()->getIsp(2);
}
$rule = array();
foreach ($this->cash as $k => $v) {
$input = input('cash_percent_' . $k);
if ($input) {
if (isset($key[$v]) && $key[$v]) {
$v = $key[$v];
}
$rule[$v] = $input;
}
}
if ($rule) {
$rule = json_encode($rule);
}
$gid_rule = array();
foreach ($this->cash as $k => $v) {
$input = input('cash_gid_' . $k);
if ($input) {
if (isset($key[$v]) && $key[$v]) {
$v = $key[$v];
}
$gid_rule[$v] = $input;
}
}
if ($gid_rule) {
$gid_rule = json_encode($gid_rule);
}
$order_limit_rule = array();
foreach ($this->cash as $k => $v) {
$input = input('cash_order_limit_' . $k);
if ($input >= 0) {
if (isset($key[$v]) && $key[$v]) {
$v = $key[$v];
}
$order_limit_rule[$v] = $input;
}
}
if ($order_limit_rule) {
$order_limit_rule = json_encode($order_limit_rule);
}
} else {
$rule = '';
$gid_rule = '';
$order_limit_rule = '';
}
$update['percent'] = $percent;
$update['percent_type'] = $percent_type;
if ($rule) {
$update['rule'] = $rule;
}
if ($gid_rule) {
$update['gid_rule'] = $gid_rule;
}
$gid = input('gid');
if ($gid) {
$update['gid'] = $gid;
}
if ($order_limit_rule) {
$update['order_limit_rule'] = $order_limit_rule;
}
$order_limit = input('order_limit');
$update['order_limit'] = $order_limit;
$this->app->db->name($this->table)->where(['id' => $this->pid])->update($update);
$this->success('操作成功!', '');
}
}
}