Config.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller\weixin;
  3. use app\common\controller\Backend;
  4. use app\admin\model\weixin\Config as ConfigModel;
  5. use Exception;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 系统配置
  11. */
  12. class Config extends Backend
  13. {
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = new \app\admin\model\weixin\Config();
  19. }
  20. /**
  21. * 查看
  22. */
  23. public function index()
  24. {
  25. $siteList = [];
  26. $groupList = ConfigModel::getGroupList();
  27. foreach ($groupList as $k => $v) {
  28. $siteList[$k]['name'] = $k;
  29. $siteList[$k]['title'] = $v;
  30. $siteList[$k]['list'] = [];
  31. }
  32. foreach ($this->model->all() as $k => $v) {
  33. if (!isset($siteList[$v['group']])) {
  34. continue;
  35. }
  36. $value = $v->toArray();
  37. $value['title'] = __($value['title']);
  38. if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
  39. $value['value'] = explode(',', $value['value']);
  40. }
  41. $value['content'] = json_decode($value['content'], true);
  42. $value['tip'] = htmlspecialchars($value['tip']);
  43. $siteList[$v['group']]['list'][] = $value;
  44. }
  45. $index = 0;
  46. foreach ($siteList as $k => &$v) {
  47. $v['active'] = !$index ? true : false;
  48. $index++;
  49. }
  50. $this->view->assign('siteList', $siteList);
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 编辑
  55. * @param null $ids
  56. */
  57. public function edit($ids = null)
  58. {
  59. if ($this->request->isPost()) {
  60. $row = $this->request->post("row/a", [], 'trim');
  61. if ($row) {
  62. $configList = [];
  63. foreach ($this->model->all() as $v) {
  64. if (isset($row[$v['name']])) {
  65. $value = $row[$v['name']];
  66. if (is_array($value) && isset($value['field'])) {
  67. $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
  68. } else {
  69. $value = is_array($value) ? implode(',', $value) : $value;
  70. }
  71. $v['value'] = $value;
  72. $configList[] = $v->toArray();
  73. }
  74. }
  75. try {
  76. $this->model->allowField(true)->saveAll($configList);
  77. } catch (Exception $e) {
  78. $this->error($e->getMessage());
  79. }
  80. $this->success();
  81. }
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. }
  85. }