Domainpool.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wanggb
  5. * Date: 2019/2/15
  6. * Time: 10:08
  7. */
  8. namespace app\admin\controller\general;
  9. use app\common\constants\AppConstants;
  10. use app\common\controller\Backend;
  11. use app\common\library\Redis;
  12. use think\Exception;
  13. class Domainpool extends Backend
  14. {
  15. protected $noNeedRight = ['index', 'wechatinfo', 'test'];
  16. protected $domainPoolModel = '';
  17. protected $domainRedisKey = 'DOMAINPOOLS:';
  18. protected $wechatPoolModel = '';
  19. protected $wechatRedisKey = 'WECHATPOOLS:';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->domainPoolModel = model('DomainPools');
  24. $this->wechatPoolModel = model('WechatPools');
  25. }
  26. public function test()
  27. {
  28. //$data = AppConstants::getRandomFrontDomain();
  29. //$data = AppConstants::getRandomWechatDomain();
  30. }
  31. /**
  32. * 微信防封,前置域名池
  33. * @return string|\think\response\Json
  34. * @throws \think\Exception
  35. */
  36. public function index()
  37. {
  38. if ($_POST) {
  39. if (!$_POST['domainpools']) {
  40. $this->error('数据不能为空');
  41. }
  42. // 删除所有数据
  43. $this->domainPoolModel->trucateDomainPools();
  44. $data = explode(PHP_EOL, $_POST['domainpools']);
  45. $domain_pools_str = '';
  46. foreach ($data as $key => $value) {
  47. if (!empty(trim($value))) {
  48. $this->domainPoolModel->insert(['domain' => trim($value)]);
  49. $domain_pools_str .= trim($value);
  50. if ($key < (count($data) - 1)) {
  51. $domain_pools_str .= ',';
  52. }
  53. }
  54. }
  55. $redis = Redis::instance();
  56. $redis->set($this->domainRedisKey, $domain_pools_str, 7 * 24 * 60 * 60);
  57. $this->success('保存成功', 'general/domainpool');
  58. } else {
  59. $data = $this->domainPoolModel->select();
  60. $domains_str = '';
  61. if ($data) {
  62. foreach ($data as $key => $item) {
  63. $domains_str .= $item->domain;
  64. if ($key < (count($data) - 1)) {
  65. $domains_str .= PHP_EOL;
  66. }
  67. }
  68. }
  69. $this->assign('domains', $domains_str);
  70. return $this->view->fetch();
  71. }
  72. }
  73. /**
  74. * 微信防封,公众号信息设置
  75. * @return string
  76. */
  77. public function wechatinfo()
  78. {
  79. //读取/var/www/cps/public 目录下的文件
  80. if ($_POST) {
  81. if (!$_POST['wechatpools']) {
  82. $this->error('数据不能为空');
  83. }
  84. // 删除所有数据
  85. $this->wechatPoolModel->trucateWechatPools();
  86. $data = explode(PHP_EOL, $_POST['wechatpools']);
  87. foreach ($data as $key => $value) {
  88. $i_data = explode(':', $value);
  89. if (!isset($i_data[0]) || !isset($i_data[1])) {
  90. $this->error('格式不正确,单条信息以":"分隔', '/admin/general/domainpool/wechatinfo');
  91. } else {
  92. $this->wechatPoolModel->insert(['appid' => trim($i_data[0]), 'domain' => trim($i_data[1])]);
  93. $t_data[$key]['appid'] = trim($i_data[0]);
  94. $t_data[$key]['domain'] = trim($i_data[1]);
  95. }
  96. }
  97. $redis = Redis::instance();
  98. $redis->set($this->wechatRedisKey, json_encode($t_data), 7 * 24 * 60 * 60);
  99. $this->success('保存成功', '/admin/general/domainpool/wechatinfo');
  100. } else {
  101. try {
  102. $data = $this->wechatPoolModel->select();
  103. $wechat_str = '';
  104. if ($data) {
  105. foreach ($data as $key => $value) {
  106. $wechat_str .= $value->appid . ':' . $value->domain;
  107. if ($key < (count($data) - 1)) {
  108. $wechat_str .= PHP_EOL;
  109. }
  110. }
  111. }
  112. $this->assign('wechat_domains', $wechat_str);
  113. return $this->view->fetch();
  114. } catch (\Exception $e) {
  115. echo $e->getMessage();
  116. }
  117. }
  118. }
  119. /*public function index()
  120. {
  121. if($_POST){
  122. if(!$_POST['domainpools']){
  123. $this->error('数据不能为空');
  124. }
  125. $data = explode(PHP_EOL, $_POST['domainpools']);
  126. $domains_str = '';
  127. foreach ($data as $key => $value){
  128. if(!empty(trim($value))){
  129. $domains_str .= trim($value);
  130. if($key < (count($data) - 1)){
  131. $domains_str .= ',';
  132. }
  133. }
  134. }
  135. file_put_contents(ROOT_PATH . 'public/front_domains.txt', $domains_str);
  136. $this->success('保存成功', 'general/domainpool');
  137. }else {
  138. //读取/var/www/cps/public 目录下的文件
  139. $domains = @file_get_contents(ROOT_PATH . 'public/front_domains.txt');
  140. $data = explode(',', $domains);
  141. $domains_str = '';
  142. if($data){
  143. foreach ($data as $key=>$value){
  144. $domains_str .= $value;
  145. if($key < (count($data) - 1)){
  146. $domains_str .= PHP_EOL;
  147. }
  148. }
  149. }
  150. $this->assign('domains', $domains_str);
  151. return $this->view->fetch();
  152. }
  153. }*/
  154. /**
  155. * 微信防封,公众号信息设置
  156. * @return string
  157. */
  158. /*public function wechatinfo()
  159. {
  160. //读取/var/www/cps/public 目录下的文件
  161. if ($_POST) {
  162. if (!$_POST['wechatpools']) {
  163. $this->error('数据不能为空');
  164. }
  165. $data = explode(PHP_EOL, $_POST['wechatpools']);
  166. foreach ($data as $key => $value) {
  167. $i_data = explode(':', $value);
  168. if (!isset($i_data[0]) || !isset($i_data[1])) {
  169. $this->error('格式不正确,单条信息以":"分隔', '/admin/general/domainpool/wechatinfo');
  170. } else {
  171. $t_data[$key]['appid'] = trim($i_data[0]);
  172. $t_data[$key]['domain'] = trim($i_data[1]);
  173. }
  174. }
  175. file_put_contents(ROOT_PATH . 'public/wechat_domains.txt', json_encode($t_data));
  176. $this->success('保存成功', '/admin/general/domainpool/wechatinfo');
  177. } else {
  178. try {
  179. $wechat_domains = @file_get_contents(ROOT_PATH . 'public/wechat_domains.txt');
  180. $data = json_decode($wechat_domains, true);
  181. $wechat_str = '';
  182. if ($data) {
  183. foreach ($data as $key => $value) {
  184. $wechat_str .= $value['appid'] . ':' . $value['domain'];
  185. if ($key < (count($data) - 1)) {
  186. $wechat_str .= PHP_EOL;
  187. }
  188. }
  189. }
  190. $this->assign('wechat_domains', $wechat_str);
  191. return $this->view->fetch();
  192. } catch (\Exception $e) {
  193. echo $e->getMessage();
  194. }
  195. }
  196. }*/
  197. }