ChannelKey.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 渠道密钥管理控制器
  4. */
  5. namespace app\admin\controller;
  6. use app\common\model\ChannelCa;
  7. use app\common\model\Department;
  8. class ChannelKey extends Admin
  9. {
  10. /**
  11. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  12. */
  13. protected function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. /**
  18. * 游戏列表
  19. */
  20. public function index()
  21. {
  22. $channelCaModel = new ChannelCa;
  23. $departmentModel= new Department;
  24. $where = [];
  25. //游戏名称
  26. if(input('request.channel_id')!='')
  27. {
  28. $where['c.ca_channel_id'] = input('request.channel_id',0,'intval');
  29. }
  30. //查询参数
  31. $param = input('get.');
  32. $list = $channelCaModel->alias('c')
  33. ->join('cy_department d','c.ca_channel_id=d.id','left')
  34. ->field('c.*,d.name as channel_name')
  35. ->where($where)
  36. ->order('c.ca_id desc')->paginate(10,false, array('query' => $param));
  37. $departmentList = $departmentModel->field('id,name')->where(['flag'=>['in','2,3']])->select();
  38. $this->assign('list', $list);
  39. $this->assign('page', $list->render());
  40. $this->assign('departmentList', $departmentList);
  41. return $this->fetch('list');
  42. }
  43. /**
  44. * 删除信息
  45. */
  46. public function delete() {
  47. $ca_id = input('ca_id', 0, 'intval');
  48. if(empty($ca_id))
  49. $this->error('删除记录的ID不能为空');
  50. $channelCaModel = new ChannelCa;
  51. // 判断当前秘钥对是否已被禁用
  52. $info = $channelCaModel->where(['ca_id'=>$ca_id])->find();
  53. if(empty($info)) {
  54. $this->error('记录不存在');
  55. }
  56. elseif($info['ca_enable_status'] != 0) {
  57. $this->error('请先禁用该密钥对');
  58. }
  59. if($channelCaModel->where(['ca_id'=>$ca_id])->delete()){
  60. $this->success('删除成功');
  61. }else{
  62. $this->error('删除失败');
  63. }
  64. }
  65. /**
  66. * 新增
  67. * @return mixed|string
  68. */
  69. public function create()
  70. {
  71. if (request()->isPost()) {
  72. $data = ['ca_channel_id' => input('post.ca_channel_id'),
  73. 'ca_enable_status' => input('post.ca_enable_status'),
  74. 'ca_private_key' => input('post.ca_private_key','','trim'),
  75. 'ca_public_key' => input('post.ca_public_key','','trim'),
  76. 'ca_realname' => input('post.ca_realname','','trim'),
  77. 'ca_mobile' => input('post.ca_mobile','','trim'),
  78. 'ca_mail' => input('post.ca_mail','','trim'),
  79. ];
  80. $result = $this->validate($data,
  81. [
  82. ['ca_channel_id', 'require|integer', '请选择渠道|渠道ID必须为整型'],
  83. ['ca_enable_status', 'require|integer', '秘钥是否启用不能为空|秘钥是否启用必须为整型'],
  84. ['ca_private_key', 'require|isValidRsaKey:'.$data['ca_public_key'], '私钥不能为空|不是有效的密钥对'],
  85. ['ca_public_key', 'require', '公钥不能为空'],
  86. ['ca_realname', 'require|max:50', '联系人姓名不能为空|联系人姓名最大不能超过50个字符'],
  87. ['ca_mobile', 'require|mobile', '联系人手机不能为空|联系人手机格式不正确'],
  88. ['ca_mail', 'email', '联系人邮箱格式不正确'],
  89. ]);
  90. if (true !== $result) {
  91. $this->error($result);
  92. }
  93. $data['ca_create_time'] = time();
  94. $data['ca_encryption_key_type'] = 'RSA';
  95. $channelCaModel = new ChannelCa;
  96. if($channelCaModel->insert($data))
  97. $this->success('新增成功','ChannelKey/index');
  98. else
  99. $this->error('新增失败');
  100. }
  101. $departmentModel= new Department;
  102. $departmentList = $departmentModel->field('id,name')->where(['flag'=>['in','2,3']])->select();
  103. $this->assign('department_list', $departmentList);
  104. return $this->fetch('new');
  105. }
  106. /**
  107. * 编辑
  108. * @return mixed|string
  109. */
  110. public function edit()
  111. {
  112. $ca_id = input('ca_id',0,'intval');
  113. if(empty($ca_id))
  114. $this->error('ID不能为空');
  115. $channelCaModel = new ChannelCa;
  116. $info = $channelCaModel->where(['ca_id'=>$ca_id])->find();
  117. if(empty($info))
  118. $this->error('秘钥信息不存在');
  119. if (request()->isPost()) {
  120. $data = [
  121. 'ca_enable_status' => input('post.ca_enable_status'),
  122. 'ca_realname' => input('post.ca_realname','','trim'),
  123. 'ca_mobile' => input('post.ca_mobile','','trim'),
  124. 'ca_mail' => input('post.ca_mail','','trim'),
  125. ];
  126. $result = $this->validate($data,
  127. [
  128. ['ca_enable_status', 'require|integer', '秘钥是否启用不能为空|秘钥是否启用必须为整型'],
  129. ['ca_realname', 'require|max:50', '联系人姓名不能为空|联系人姓名最大不能超过50个字符'],
  130. ['ca_mobile', 'require|mobile', '联系人手机不能为空|联系人手机格式不正确'],
  131. ['ca_mail', 'email', '联系人邮箱格式不正确'],
  132. ]);
  133. if (true !== $result) {
  134. $this->error($result);
  135. }
  136. if($channelCaModel->where(['ca_id'=>$ca_id])->update($data))
  137. $this->success('编辑成功','ChannelKey/index');
  138. else
  139. $this->error('编辑失败');
  140. }
  141. $departmentModel= new Department;
  142. $departmentList = $departmentModel->field('id,name')->where(['flag'=>['in','2,3']])->select();
  143. $this->assign('department_list', $departmentList);
  144. $this->assign('info', $info);
  145. return $this->fetch('edit');
  146. }
  147. }