REAPI/app/channel/controller/Channel.php

299 lines
9.8 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\channel\controller;
use think\admin\Controller;
use app\channel\service\ChannelService;
use app\channel\service\ProductBaseService;
/**
* 渠道管理
* Class Channel
* @package app\channel\controller
*/
class Channel extends Controller
{
/**
* 绑定数据表
* @var string
*/
public $table = 'ChannelList';
/**
* 渠道列表
* @auth true
* @menu true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '渠道列表';
$query = $this->_query($this->table);
$query->equal('status')->dateBetween('create_at');
$query->like('name,phone,contacts');
$query->where(['is_deleted' => 0]);
$query->order('status desc, id desc')->page();
}
/**
* 添加渠道
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function add()
{
$this->_applyFormToken();
$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->_applyFormToken();
$this->_form($this->table, 'form');
}
/**
* 表单数据处理
* @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 (empty($data['name'])) $this->error('渠道名称不能为空!');
if (!isset($data['id'])) {
$data['channel_num']=uniqid('CH');
//if ($this->app->db->name($this->table)->where(['appid' => $data['appid'],'is_deleted' => 0])->find()) $this->error("APPID:{$data['appid']}已经存在!");
//if ($this->app->db->name($this->table)->where(['key' => $data['key'],'is_deleted' => 0])->find()) $this->error("KEY:{$data['key']}已经存在!");
}
}
}
protected function _form_result($id, $data)
{
ChannelService::instance()->get($id, false);
}
/**
* 修改渠道状态
* @auth true
* @throws \think\db\exception\DbException
*/
public function state()
{
$this->_applyFormToken();
$this->_save($this->table, $this->_vali([
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]));
}
/**
* 删除渠道
* @auth true
* @throws \think\db\exception\DbException
*/
public function remove()
{
$this->_applyFormToken();
$this->_delete($this->table);
}
/**
* 关联产品
* @auth true
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function setting()
{
if ($this->request->isGet()) {
$id = input('cid');
$this->vo = $this->app->db->name($this->table)->where(array('id' => $id))->find();
$this->title = '关联产品';
$w = array
(
'cid' => $id,
);
$channel_product = $this->app->db->name('channel_product')->where($w)->order('sort desc,id desc')->select()->toArray();
$relation = array();
if ($channel_product) {
foreach ($channel_product as $k => $v) {
$relation[$v['key']] = $v;
}
}
$w = array
(
'status' => 1,
);
$service = $this->app->db->name('service_info')->where($w)->select()->toArray();
$this->type = ProductBaseService::instance()->getType();
$this->service = $this->product = array();
foreach ($this->type as $k => $v) {
$this->service[$k] = array();
}
foreach ($service as $k => $v) {
$w['service_id'] = $v['id'];
$v['product'] = $this->app->db->name('service_product')->where($w)->select()->toArray();
if (isset($relation[$v['key']]) && $relation[$v['key']]) {
$v['select'] = 1;
$v['cash'] = $relation[$v['key']]['auto_value'];
$v['cash'] = explode(',', $v['cash']);
} else {
$v['select'] = 2;
$v['cash'] = array();
}
$this->service[$v['type']][] = $v;
}
$this->_form($this->table, 'setting');
} else {
$data = input();
$all = $this->app->db->name('channel_product')->where(array('cid' => $data['cid']))->select()->toArray();
$select = array();
if ($all) {
foreach ($all as $k => $v) {
$select[$v['key']] = $v;
}
}
$service =$data['service']? implode(',', $data['service']):'';
$service = $this->app->db->name('service_info')->whereRaw('id in('.$service.')')->select()->toArray();
if ($service) {
foreach ($service as $k => $v) {
$product = $this->app->db->name('service_product')->where(array('service_id' => $v['id'], 'status' => 1))->order('sort asc, status desc, id desc')->column('value');
$update = array();
$update['service_id'] = $v['id'];
$update['name'] = $v['name'];
$update['key'] = $v['key'];
$update['value'] = implode(',', $product);
$update['auto_value'] = '';
if (isset($data['product_' . $v['id']]) && $data['product_' . $v['id']]) {
$update['auto_value'] = implode(',', $data['product_' . $v['id']]);
}
if (isset($select[$v['key']]) && $select[$v['key']]) {
# 更新
$this->app->db->name('channel_product')->where(array('id' => $select[$v['key']]['id']))->update($update);
unset($select[$v['key']]);
} else {
# 插入
$update['cid'] = $data['cid'];
$this->app->db->name('channel_product')->insert($update);
}
}
}
if ($select) {
# 删除
foreach ($select as $k => $v) {
$this->app->db->name('channel_product')->where(array('id' => $v['id']))->delete();
}
}
$this->success('操作成功', '');
}
}
/**
* 修改ip白名单
* @login true
* @auth true
* @param integer $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function ipWhite($id = 0)
{
if ($this->app->request->isGet()) {
$this->_applyFormToken();
$this->verify = false;
// $this->pkeylist = OrderService::instance()->getProductKeyList(true);
$this->_form($this->table, 'ipWhite','id', [], ['id' => $id]);
} else {
$id = input('id');
$ip_white = input('ip_white');
$up_ipWhite = ChannelService::instance()->update_ipWhite($id,$ip_white);
if($up_ipWhite == '添加防火墙错误'){
$this->error('添加防火墙ip错误请联系技术何时');
}else{
$this->success('IP白名单修改成功', '');
}
}
}
/**
* 查询余额
* @login true
* @auth true
* @param integer $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function queryBalance($id = 0)
{
$id = input('cid');
$channelService = ChannelService::instance();
$day = input('day');
if (!$day) {
$day = date('Y-m-d', strtotime('-1 day'));
}
$account_data = $channelService->call('account', $id, $day);
// var_dump($account_data);die;
if ($account_data) {
$data = $account_data;
$data['balance'] = $account_data['account'];
$this->success('余额查询成功!', $data);
}else{
$this->error('余额查询失败!');
}
}
2024-09-29 15:43:18 +08:00
}