123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\service\ChangeMenuService;
- use think\Cache;
- use think\Config;
- use think\Controller;
- use think\Request;
- /**
- * 开放平台管理管理
- *
- * @icon fa fa-circle-o
- */
- class Platform extends Backend
- {
- protected $noNeedRight = ['export'];
-
- /**
- * Platform模型对象
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Platform');
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("isdefaultList", $this->model->getIsdefaultList());
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- /*
- * 已经弃用,如果为了兼容老版可取消注释
- foreach ($params as $k => &$v)
- {
- $v = is_array($v) ? implode(',', $v) : $v;
- }
- */
- if ($this->dataLimit)
- {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try
- {
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- if($params['isdefault'] == 1){ //新增三方平台禁止设为默认,因为尚未配置业务域名与支付服务号
- $params['isdefault'] = 0;
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row)
- $this->error(__('No Results were found'));
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds))
- {
- if (!in_array($row[$this->dataLimitField], $adminIds))
- {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- /*
- * 已经弃用,如果为了兼容老版可取消注释
- foreach ($params as $k => &$v)
- {
- $v = is_array($v) ? implode(',', $v) : $v;
- }
- */
- try
- {
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $row->validate($validate);
- }
- if($params['status'] != $row['status'] || $params['isdefault'] != $row['isdefault']){
- if($params['status'] == 1){ //启用三方平台,需要校验名下是否存在业务域名与支付服务号
- if(!model('Ophost')->getInfo($row['id'])){
- $this->error("此三方平台下必须有一个默认的业务域名!");
- }
- if(!model('Wxpay')->getInfo($row['id'])){
- $this->error("此三方平台下必须有一个默认的支付服务号!");
- }
- }
- if($params['status'] == 1 && $params['isdefault'] == 1){
- $data['isdefault'] = 0;
- $defaultRow = $this->model->where(["isdefault"=>1])->find();
- if($defaultRow) {
- $defaultRow->allowField(true)->save($data);
- }
- $this->model->delCacheAll($defaultRow['id']);
- }elseif ($params['status'] == 0 && $params['isdefault'] == 1){
- $this->error("关闭状态下禁止设为默认");
- }elseif ($params['status'] == 0 && $params['isdefault'] == 1 && $row['isdefault'] == 1){
- //默认可以关闭 $this->error("默认项不可关闭");
- }elseif ($params['isdefault'] == 0 && $row['isdefault'] == 1){
- $this->error("默认项不可更改为非默认");
- }
- if ($params['status'] == 0 && $params['isdefault'] == 1) {
- //默认可以关闭 $this->error("变更默认项时,状态不可关闭");
- }
- if($params['status'] == 0 && $row['status'] == 1){ //关闭三方平台
- $map['platform_id'] = $row['id'];
- $map['status'] = '1';
- //检查平台下的业务域名
- if(model('Ophost')->where($map)->find()){
- $this->error('该平台下有正在使用的业务域名,不能关闭');
- }
- //检查平台下的支付域名
- if(model('Wxpay')->where($map)->find()){
- $this->error('该平台下有正在使用的支付域名,不能关闭');
- }
- //检查是否有渠道正在使用
- if(model('AdminConfig')->where('platform_id',$row['id'])->find()){
- $this->error('有渠道的默认平台正在使用,不能关闭');
- }
- if(model('AdminConfig')->where('menu_platform_id',$row['id'])->find()){
- $this->error('有渠道的菜单平台正在使用,不能关闭');
- }
- $where[]=['exp','FIND_IN_SET("'.$row['id'].'", platform_list)'];
- if(model('AdminConfig')->where($where)->find()){
- $this->error('有渠道的平台列表正在使用中,不能关闭');
- }
- }
- }
- $result = $row->allowField(true)->save($params);
- if ($result !== false) {
- $this->model->delCacheAll($row['id']);
- $this->success();
- }
- else
- {
- $this->error($row->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- if(!model('Ptoken')->where(['platform_id'=>$row['id']])->find()){
- $this->assign('isEdit',true);
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 切换平台
- */
- public function switchPlatform(){
- if ($this->request->isPost()){
- //返回结果
- $result =['total'=>0,'success'=>0,'fail'=>0,'unauthorized'=>0,'platform'=>0,'list'=>[],'export'=>false];
- //切换前平台信息
- $old = $this->request->post("old/a");
- //切换后平台信息
- $new = $this->request->post("new/a");
- //查询信息处理
- if($create_by = intval($this->request->param('create_by'))){
- $old['create_by'] = $create_by;
- }
- if($nickname = trim($this->request->param('nickname'))){
- $old['nickname'] = ['like',"%$nickname%"];
- }
- //切换业务域名
- if(isset($old['ophost_id']) && isset($new['ophost_id']) && $old['ophost_id'] && $new['ophost_id']){
- //获取切换后的业务域名平台ID
- $new['platform_id'] = model('Ophost')->where('id',$new['ophost_id'])->value('platform_id');
- $this->switchChannelPlatform($old,$new,$result,false);
- if($result['list']){
- $name = "切换业务域名_{$create_by}_{$nickname}_{$old['ophost_id']}_to_{$new['ophost_id']}_".date('Y-m-d H:i:s');
- Cache::set('switch_history_data',$result['list']);
- Cache::set('switch_history_title',$name);
- unset($result['list']);
- $result['export'] = true;
- }
- }
- //切换菜单域名
- if(isset($old['menuophost_id']) && isset($new['menuophost_id']) && $old['menuophost_id'] && $new['menuophost_id']){
- //获取切换后的菜单域名平台ID
- $new['menu_platform_id'] = model('Ophost')->where('id',$new['menuophost_id'])->value('platform_id');
- $this->switchChannelPlatform($old,$new,$result,false);
- if($result['list']){
- $name = "切换菜单域名_{$create_by}_{$nickname}_{$old['menuophost_id']}_to_{$new['menuophost_id']}_".date('Y-m-d H:i:s');
- Cache::set('switch_history_data',$result['list']);
- Cache::set('switch_history_title',$name);
- unset($result['list']);
- $result['export'] = true;
- }
- }
- //切换业务支付域名
- if(isset($old['wxpay_id']) && isset($new['wxpay_id']) && $old['wxpay_id'] && $new['wxpay_id']){
- //检查支付域名状态
- if(model('Wxpay')->where('id',$new['wxpay_id'])->value('status')){
- $this->switchChannelPlatform($old,$new,$result,true);
- if($result['list']){
- $name = "切换业务支付域名_{$create_by}_{$nickname}_{$old['wxpay_id']}_to_{$new['wxpay_id']}_".date('Y-m-d H:i:s');
- Cache::set('switch_history_data',$result['list']);
- Cache::set('switch_history_title',$name);
- unset($result['list']);
- $result['export'] = true;
- }
- }
- }
- //切换菜单支付域名
- if(isset($old['menuwxpay_id']) && isset($new['menuwxpay_id']) && $old['menuwxpay_id'] && $new['menuwxpay_id']){
- //检查支付域名状态
- if(model('Wxpay')->where('id',$new['menuwxpay_id'])->value('status')){
- $this->switchChannelPlatform($old,$new,$result,true);
- if($result['list']){
- $name = "切换菜单支付域名_{$create_by}_{$nickname}_{$old['menuwxpay_id']}_to_{$new['menuwxpay_id']}_".date('Y-m-d H:i:s');
- Cache::set('switch_history_data',$result['list']);
- Cache::set('switch_history_title',$name);
- unset($result['list']);
- $result['export'] = true;
- }
- }
- }
- $this->success('平台切换成功',null,$result);
- }
- $ophost_list = model("ophost")->getListFormatByPlatformId();
- $this->assign('wxpay_list',model("wxpay")->getListFormatByPlatformId());
- $this->assign('ophost_list',$ophost_list);
- return $this->fetch();
- }
- public function export(){
- $data = Cache::get('switch_history_data');
- $fileName = Cache::get('switch_history_title');
- ini_set('memory_limit', '256M'); //内存限制
- $columns = [ '渠道商ID', '渠道商昵称'];
- header('Content-Description: File Transfer');
- header('Content-Type: application/vnd.ms-excel');
- header('Content-Disposition: attachment; filename="'.$fileName.'.csv"');
- header('Expires: 0');
- header('Cache-Control: must-revalidate');
- header('Pragma: public');
- $fp = fopen('php://output', 'a');//打开output流
- mb_convert_variables('GBK', 'UTF-8', $columns);
- fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
- if($data){
- foreach($data as $line){
- //需要格式转换,否则会乱码
- mb_convert_variables('GBK', 'UTF-8', $line);
- fputcsv($fp, $line);
- ob_flush();
- flush();
- }
- }
- fclose($fp);
- exit();
- }
- /**
- * 切换渠道域名或者支付
- * @param $old 查询条件
- * @param $new 要切换的信息
- * @param $result 返回的结果
- * @param bool $is_pay 是否是支付
- * @throws \Exception
- */
- protected function switchChannelPlatform($old,$new,&$result,$is_pay = false){
- $channel_list = model("AdminConfig")->alias('ac')
- ->join("admin_extend ae","ac.admin_id = ae.admin_id")
- ->join('admin ad',"ad.id = ac.admin_id")
- ->field("ac.admin_id admin_id,ae.create_by create_by,ad.nickname nickname,ac.platform_list platform_list,ac.platform_id platform_id,ac.menu_platform_id menu_platform_id")
- ->where($old)
- ->select();
- if($channel_list){
- foreach($channel_list as $channel){
- $result['total']++;
- if(!$is_pay){
- //检查渠道所有平台是否有授权的
- if(model('Ptoken')->checkChannelIsAuth($channel['platform_list'],$channel['admin_id'])){
- if(isset($new['platform_id'])){
- //渠道有已授权的时候,检查新业务域名平台是否授权
- if(!model('Ptoken')->checkChannelIsAuth($new['platform_id'],$channel['admin_id'])){
- $result['fail']++;
- $result['unauthorized']++;
- continue;
- }
- }else{
- //渠道已授权的时候,检查新的菜单域名平台是否授权
- if(!model('Ptoken')->checkChannelIsAuth($new['menu_platform_id'],$channel['admin_id'])){
- $result['fail']++;
- $result['unauthorized']++;
- continue;
- }
- }
- }else{
- //未授权的渠道切换,检查要切换的平台是否存在于渠道的平台列表中
- if(!$channel['platform_list']){
- $result['fail']++;
- $result['platform']++;
- continue;
- }
- $platform_list = explode(',',$channel['platform_list']);
- if(isset($new['platform_id'])){
- //检查业务域名平台
- if(!in_array($new['platform_id'],$platform_list)){
- $result['fail']++;
- $result['platform']++;
- continue;
- }
- }else{
- //检查菜单域名平台
- if(!in_array($new['menu_platform_id'],$platform_list)){
- $result['fail']++;
- $result['platform']++;
- continue;
- }
- }
- }
- }
- $update = model('AdminConfig')->where('admin_id',$channel['admin_id'])->update($new);
- if($update !== false){
- $result['success']++;
- //返回修改的用户列表
- array_push($result['list'],['id'=>$channel['admin_id'],'nickname'=>$channel['nickname']]);
- //删除缓存
- model('AdminConfig')->delAdminInfoAllCache($channel['admin_id']);
- //不是切换支付&是切换菜单域名则推送菜单
- if(!$is_pay && isset($new['menu_platform_id'])){
- ChangeMenuService::instance()->setChannelId($channel['admin_id'])->setReplaceHostById($new['menuophost_id'])->push();
- }
- }else{
- $result['fail']++;
- }
- }
- }
- }
- }
|