Platform.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\service\ChangeMenuService;
  6. use think\Cache;
  7. use think\Config;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. * 开放平台管理管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Platform extends Backend
  16. {
  17. protected $noNeedRight = ['export'];
  18. /**
  19. * Platform模型对象
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Platform');
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("isdefaultList", $this->model->getIsdefaultList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 添加
  36. */
  37. public function add()
  38. {
  39. if ($this->request->isPost())
  40. {
  41. $params = $this->request->post("row/a");
  42. if ($params)
  43. {
  44. /*
  45. * 已经弃用,如果为了兼容老版可取消注释
  46. foreach ($params as $k => &$v)
  47. {
  48. $v = is_array($v) ? implode(',', $v) : $v;
  49. }
  50. */
  51. if ($this->dataLimit)
  52. {
  53. $params[$this->dataLimitField] = $this->auth->id;
  54. }
  55. try
  56. {
  57. //是否采用模型验证
  58. if ($this->modelValidate)
  59. {
  60. $name = basename(str_replace('\\', '/', get_class($this->model)));
  61. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  62. $this->model->validate($validate);
  63. }
  64. if($params['isdefault'] == 1){ //新增三方平台禁止设为默认,因为尚未配置业务域名与支付服务号
  65. $params['isdefault'] = 0;
  66. }
  67. $result = $this->model->allowField(true)->save($params);
  68. if ($result !== false)
  69. {
  70. $this->success();
  71. }
  72. else
  73. {
  74. $this->error($this->model->getError());
  75. }
  76. }
  77. catch (\think\exception\PDOException $e)
  78. {
  79. $this->error($e->getMessage());
  80. }
  81. }
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 编辑
  88. */
  89. public function edit($ids = NULL)
  90. {
  91. $row = $this->model->get($ids);
  92. if (!$row)
  93. $this->error(__('No Results were found'));
  94. $adminIds = $this->getDataLimitAdminIds();
  95. if (is_array($adminIds))
  96. {
  97. if (!in_array($row[$this->dataLimitField], $adminIds))
  98. {
  99. $this->error(__('You have no permission'));
  100. }
  101. }
  102. if ($this->request->isPost())
  103. {
  104. $params = $this->request->post("row/a");
  105. if ($params)
  106. {
  107. /*
  108. * 已经弃用,如果为了兼容老版可取消注释
  109. foreach ($params as $k => &$v)
  110. {
  111. $v = is_array($v) ? implode(',', $v) : $v;
  112. }
  113. */
  114. try
  115. {
  116. //是否采用模型验证
  117. if ($this->modelValidate)
  118. {
  119. $name = basename(str_replace('\\', '/', get_class($this->model)));
  120. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  121. $row->validate($validate);
  122. }
  123. if($params['status'] != $row['status'] || $params['isdefault'] != $row['isdefault']){
  124. if($params['status'] == 1){ //启用三方平台,需要校验名下是否存在业务域名与支付服务号
  125. if(!model('Ophost')->getInfo($row['id'])){
  126. $this->error("此三方平台下必须有一个默认的业务域名!");
  127. }
  128. if(!model('Wxpay')->getInfo($row['id'])){
  129. $this->error("此三方平台下必须有一个默认的支付服务号!");
  130. }
  131. }
  132. if($params['status'] == 1 && $params['isdefault'] == 1){
  133. $data['isdefault'] = 0;
  134. $defaultRow = $this->model->where(["isdefault"=>1])->find();
  135. if($defaultRow) {
  136. $defaultRow->allowField(true)->save($data);
  137. }
  138. $this->model->delCacheAll($defaultRow['id']);
  139. }elseif ($params['status'] == 0 && $params['isdefault'] == 1){
  140. $this->error("关闭状态下禁止设为默认");
  141. }elseif ($params['status'] == 0 && $params['isdefault'] == 1 && $row['isdefault'] == 1){
  142. //默认可以关闭 $this->error("默认项不可关闭");
  143. }elseif ($params['isdefault'] == 0 && $row['isdefault'] == 1){
  144. $this->error("默认项不可更改为非默认");
  145. }
  146. if ($params['status'] == 0 && $params['isdefault'] == 1) {
  147. //默认可以关闭 $this->error("变更默认项时,状态不可关闭");
  148. }
  149. if($params['status'] == 0 && $row['status'] == 1){ //关闭三方平台
  150. $map['platform_id'] = $row['id'];
  151. $map['status'] = '1';
  152. //检查平台下的业务域名
  153. if(model('Ophost')->where($map)->find()){
  154. $this->error('该平台下有正在使用的业务域名,不能关闭');
  155. }
  156. //检查平台下的支付域名
  157. if(model('Wxpay')->where($map)->find()){
  158. $this->error('该平台下有正在使用的支付域名,不能关闭');
  159. }
  160. //检查是否有渠道正在使用
  161. if(model('AdminConfig')->where('platform_id',$row['id'])->find()){
  162. $this->error('有渠道的默认平台正在使用,不能关闭');
  163. }
  164. if(model('AdminConfig')->where('menu_platform_id',$row['id'])->find()){
  165. $this->error('有渠道的菜单平台正在使用,不能关闭');
  166. }
  167. $where[]=['exp','FIND_IN_SET("'.$row['id'].'", platform_list)'];
  168. if(model('AdminConfig')->where($where)->find()){
  169. $this->error('有渠道的平台列表正在使用中,不能关闭');
  170. }
  171. }
  172. }
  173. $result = $row->allowField(true)->save($params);
  174. if ($result !== false) {
  175. $this->model->delCacheAll($row['id']);
  176. $this->success();
  177. }
  178. else
  179. {
  180. $this->error($row->getError());
  181. }
  182. }
  183. catch (\think\exception\PDOException $e)
  184. {
  185. $this->error($e->getMessage());
  186. }
  187. }
  188. $this->error(__('Parameter %s can not be empty', ''));
  189. }
  190. if(!model('Ptoken')->where(['platform_id'=>$row['id']])->find()){
  191. $this->assign('isEdit',true);
  192. }
  193. $this->view->assign("row", $row);
  194. return $this->view->fetch();
  195. }
  196. /**
  197. * 切换平台
  198. */
  199. public function switchPlatform(){
  200. if ($this->request->isPost()){
  201. //返回结果
  202. $result =['total'=>0,'success'=>0,'fail'=>0,'unauthorized'=>0,'platform'=>0,'list'=>[],'export'=>false];
  203. //切换前平台信息
  204. $old = $this->request->post("old/a");
  205. //切换后平台信息
  206. $new = $this->request->post("new/a");
  207. //查询信息处理
  208. if($create_by = intval($this->request->param('create_by'))){
  209. $old['create_by'] = $create_by;
  210. }
  211. if($nickname = trim($this->request->param('nickname'))){
  212. $old['nickname'] = ['like',"%$nickname%"];
  213. }
  214. //切换业务域名
  215. if(isset($old['ophost_id']) && isset($new['ophost_id']) && $old['ophost_id'] && $new['ophost_id']){
  216. //获取切换后的业务域名平台ID
  217. $new['platform_id'] = model('Ophost')->where('id',$new['ophost_id'])->value('platform_id');
  218. $this->switchChannelPlatform($old,$new,$result,false);
  219. if($result['list']){
  220. $name = "切换业务域名_{$create_by}_{$nickname}_{$old['ophost_id']}_to_{$new['ophost_id']}_".date('Y-m-d H:i:s');
  221. Cache::set('switch_history_data',$result['list']);
  222. Cache::set('switch_history_title',$name);
  223. unset($result['list']);
  224. $result['export'] = true;
  225. }
  226. }
  227. //切换菜单域名
  228. if(isset($old['menuophost_id']) && isset($new['menuophost_id']) && $old['menuophost_id'] && $new['menuophost_id']){
  229. //获取切换后的菜单域名平台ID
  230. $new['menu_platform_id'] = model('Ophost')->where('id',$new['menuophost_id'])->value('platform_id');
  231. $this->switchChannelPlatform($old,$new,$result,false);
  232. if($result['list']){
  233. $name = "切换菜单域名_{$create_by}_{$nickname}_{$old['menuophost_id']}_to_{$new['menuophost_id']}_".date('Y-m-d H:i:s');
  234. Cache::set('switch_history_data',$result['list']);
  235. Cache::set('switch_history_title',$name);
  236. unset($result['list']);
  237. $result['export'] = true;
  238. }
  239. }
  240. //切换业务支付域名
  241. if(isset($old['wxpay_id']) && isset($new['wxpay_id']) && $old['wxpay_id'] && $new['wxpay_id']){
  242. //检查支付域名状态
  243. if(model('Wxpay')->where('id',$new['wxpay_id'])->value('status')){
  244. $this->switchChannelPlatform($old,$new,$result,true);
  245. if($result['list']){
  246. $name = "切换业务支付域名_{$create_by}_{$nickname}_{$old['wxpay_id']}_to_{$new['wxpay_id']}_".date('Y-m-d H:i:s');
  247. Cache::set('switch_history_data',$result['list']);
  248. Cache::set('switch_history_title',$name);
  249. unset($result['list']);
  250. $result['export'] = true;
  251. }
  252. }
  253. }
  254. //切换菜单支付域名
  255. if(isset($old['menuwxpay_id']) && isset($new['menuwxpay_id']) && $old['menuwxpay_id'] && $new['menuwxpay_id']){
  256. //检查支付域名状态
  257. if(model('Wxpay')->where('id',$new['menuwxpay_id'])->value('status')){
  258. $this->switchChannelPlatform($old,$new,$result,true);
  259. if($result['list']){
  260. $name = "切换菜单支付域名_{$create_by}_{$nickname}_{$old['menuwxpay_id']}_to_{$new['menuwxpay_id']}_".date('Y-m-d H:i:s');
  261. Cache::set('switch_history_data',$result['list']);
  262. Cache::set('switch_history_title',$name);
  263. unset($result['list']);
  264. $result['export'] = true;
  265. }
  266. }
  267. }
  268. $this->success('平台切换成功',null,$result);
  269. }
  270. $ophost_list = model("ophost")->getListFormatByPlatformId();
  271. $this->assign('wxpay_list',model("wxpay")->getListFormatByPlatformId());
  272. $this->assign('ophost_list',$ophost_list);
  273. return $this->fetch();
  274. }
  275. public function export(){
  276. $data = Cache::get('switch_history_data');
  277. $fileName = Cache::get('switch_history_title');
  278. ini_set('memory_limit', '256M'); //内存限制
  279. $columns = [ '渠道商ID', '渠道商昵称'];
  280. header('Content-Description: File Transfer');
  281. header('Content-Type: application/vnd.ms-excel');
  282. header('Content-Disposition: attachment; filename="'.$fileName.'.csv"');
  283. header('Expires: 0');
  284. header('Cache-Control: must-revalidate');
  285. header('Pragma: public');
  286. $fp = fopen('php://output', 'a');//打开output流
  287. mb_convert_variables('GBK', 'UTF-8', $columns);
  288. fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
  289. if($data){
  290. foreach($data as $line){
  291. //需要格式转换,否则会乱码
  292. mb_convert_variables('GBK', 'UTF-8', $line);
  293. fputcsv($fp, $line);
  294. ob_flush();
  295. flush();
  296. }
  297. }
  298. fclose($fp);
  299. exit();
  300. }
  301. /**
  302. * 切换渠道域名或者支付
  303. * @param $old 查询条件
  304. * @param $new 要切换的信息
  305. * @param $result 返回的结果
  306. * @param bool $is_pay 是否是支付
  307. * @throws \Exception
  308. */
  309. protected function switchChannelPlatform($old,$new,&$result,$is_pay = false){
  310. $channel_list = model("AdminConfig")->alias('ac')
  311. ->join("admin_extend ae","ac.admin_id = ae.admin_id")
  312. ->join('admin ad',"ad.id = ac.admin_id")
  313. ->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")
  314. ->where($old)
  315. ->select();
  316. if($channel_list){
  317. foreach($channel_list as $channel){
  318. $result['total']++;
  319. if(!$is_pay){
  320. //检查渠道所有平台是否有授权的
  321. if(model('Ptoken')->checkChannelIsAuth($channel['platform_list'],$channel['admin_id'])){
  322. if(isset($new['platform_id'])){
  323. //渠道有已授权的时候,检查新业务域名平台是否授权
  324. if(!model('Ptoken')->checkChannelIsAuth($new['platform_id'],$channel['admin_id'])){
  325. $result['fail']++;
  326. $result['unauthorized']++;
  327. continue;
  328. }
  329. }else{
  330. //渠道已授权的时候,检查新的菜单域名平台是否授权
  331. if(!model('Ptoken')->checkChannelIsAuth($new['menu_platform_id'],$channel['admin_id'])){
  332. $result['fail']++;
  333. $result['unauthorized']++;
  334. continue;
  335. }
  336. }
  337. }else{
  338. //未授权的渠道切换,检查要切换的平台是否存在于渠道的平台列表中
  339. if(!$channel['platform_list']){
  340. $result['fail']++;
  341. $result['platform']++;
  342. continue;
  343. }
  344. $platform_list = explode(',',$channel['platform_list']);
  345. if(isset($new['platform_id'])){
  346. //检查业务域名平台
  347. if(!in_array($new['platform_id'],$platform_list)){
  348. $result['fail']++;
  349. $result['platform']++;
  350. continue;
  351. }
  352. }else{
  353. //检查菜单域名平台
  354. if(!in_array($new['menu_platform_id'],$platform_list)){
  355. $result['fail']++;
  356. $result['platform']++;
  357. continue;
  358. }
  359. }
  360. }
  361. }
  362. $update = model('AdminConfig')->where('admin_id',$channel['admin_id'])->update($new);
  363. if($update !== false){
  364. $result['success']++;
  365. //返回修改的用户列表
  366. array_push($result['list'],['id'=>$channel['admin_id'],'nickname'=>$channel['nickname']]);
  367. //删除缓存
  368. model('AdminConfig')->delAdminInfoAllCache($channel['admin_id']);
  369. //不是切换支付&是切换菜单域名则推送菜单
  370. if(!$is_pay && isset($new['menu_platform_id'])){
  371. ChangeMenuService::instance()->setChannelId($channel['admin_id'])->setReplaceHostById($new['menuophost_id'])->push();
  372. }
  373. }else{
  374. $result['fail']++;
  375. }
  376. }
  377. }
  378. }
  379. }