Config.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use think\Controller;
  5. use think\Request;
  6. /**
  7. * 微信配置管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Config extends Backend
  12. {
  13. protected $model = null;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = model('WechatConfig');
  18. }
  19. /**
  20. * 添加
  21. */
  22. public function add()
  23. {
  24. if ($this->request->isPost()) {
  25. $params = $this->request->post("row/a");
  26. if ($params) {
  27. foreach ($params as $k => &$v) {
  28. $v = is_array($v) ? implode(',', $v) : $v;
  29. }
  30. if ($params['mode'] == 'json') {
  31. //JSON字段
  32. $fieldarr = $valuearr = [];
  33. $field = $this->request->post('field/a');
  34. $value = $this->request->post('value/a');
  35. foreach ($field as $k => $v) {
  36. if ($v != '') {
  37. $fieldarr[] = $field[$k];
  38. $valuearr[] = $value[$k];
  39. }
  40. }
  41. $params['value'] = json_encode(array_combine($fieldarr, $valuearr), JSON_UNESCAPED_UNICODE);
  42. }
  43. unset($params['mode']);
  44. try {
  45. //是否采用模型验证
  46. if ($this->modelValidate) {
  47. $name = basename(str_replace('\\', '/', get_class($this->model)));
  48. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  49. $this->model->validate($validate);
  50. }
  51. $result = $this->model->save($params);
  52. if ($result !== false) {
  53. $this->success();
  54. } else {
  55. $this->error($this->model->getError());
  56. }
  57. } catch (\think\exception\PDOException $e) {
  58. $this->error($e->getMessage());
  59. }
  60. }
  61. $this->error(__('Parameter %s can not be empty', ''));
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = null)
  69. {
  70. $row = $this->model->get($ids);
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. foreach ($params as $k => &$v) {
  78. $v = is_array($v) ? implode(',', $v) : $v;
  79. }
  80. if ($params['mode'] == 'json') {
  81. //JSON字段
  82. $fieldarr = $valuearr = [];
  83. $field = $this->request->post('field/a');
  84. $value = $this->request->post('value/a');
  85. foreach ($field as $k => $v) {
  86. if ($v != '') {
  87. $fieldarr[] = $field[$k];
  88. $valuearr[] = $value[$k];
  89. }
  90. }
  91. $params['value'] = json_encode(array_combine($fieldarr, $valuearr), JSON_UNESCAPED_UNICODE);
  92. }
  93. unset($params['mode']);
  94. try {
  95. //是否采用模型验证
  96. if ($this->modelValidate) {
  97. $name = basename(str_replace('\\', '/', get_class($this->model)));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  99. $row->validate($validate);
  100. }
  101. $result = $row->save($params);
  102. if ($result !== false) {
  103. $this->success();
  104. } else {
  105. $this->error($row->getError());
  106. }
  107. } catch (\think\exception\PDOException $e) {
  108. $this->error($e->getMessage());
  109. }
  110. }
  111. $this->error(__('Parameter %s can not be empty', ''));
  112. }
  113. $this->view->assign("row", $row);
  114. $this->view->assign("value", (array)json_decode($row->value, true));
  115. return $this->view->fetch();
  116. }
  117. }