Config.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. {
  26. $this->code = -1;
  27. $params = $this->request->post("row/a");
  28. if ($params)
  29. {
  30. foreach ($params as $k => &$v)
  31. {
  32. $v = is_array($v) ? implode(',', $v) : $v;
  33. }
  34. if ($params['mode'] == 'json')
  35. {
  36. //JSON字段
  37. $fieldarr = $valuearr = [];
  38. $field = $this->request->post('field/a');
  39. $value = $this->request->post('value/a');
  40. foreach ($field as $k => $v)
  41. {
  42. if ($v != '')
  43. {
  44. $fieldarr[] = $field[$k];
  45. $valuearr[] = $value[$k];
  46. }
  47. }
  48. $params['value'] = json_encode(array_combine($fieldarr, $valuearr), JSON_UNESCAPED_UNICODE);
  49. }
  50. unset($params['mode']);
  51. try
  52. {
  53. //是否采用模型验证
  54. if ($this->modelValidate)
  55. {
  56. $name = basename(str_replace('\\', '/', get_class($this->model)));
  57. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  58. $this->model->validate($validate);
  59. }
  60. $result = $this->model->save($params);
  61. if ($result !== false)
  62. {
  63. $this->code = 1;
  64. }
  65. else
  66. {
  67. $this->msg = $this->model->getError();
  68. }
  69. }
  70. catch (\think\exception\PDOException $e)
  71. {
  72. $this->msg = $e->getMessage();
  73. }
  74. }
  75. else
  76. {
  77. $this->msg = __('Parameter %s can not be empty', '');
  78. }
  79. return;
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 编辑
  85. */
  86. public function edit($ids = NULL)
  87. {
  88. $row = $this->model->get($ids);
  89. if (!$row)
  90. $this->error(__('No Results were found'));
  91. if ($this->request->isPost())
  92. {
  93. $this->code = -1;
  94. $params = $this->request->post("row/a");
  95. if ($params)
  96. {
  97. foreach ($params as $k => &$v)
  98. {
  99. $v = is_array($v) ? implode(',', $v) : $v;
  100. }
  101. if ($params['mode'] == 'json')
  102. {
  103. //JSON字段
  104. $fieldarr = $valuearr = [];
  105. $field = $this->request->post('field/a');
  106. $value = $this->request->post('value/a');
  107. foreach ($field as $k => $v)
  108. {
  109. if ($v != '')
  110. {
  111. $fieldarr[] = $field[$k];
  112. $valuearr[] = $value[$k];
  113. }
  114. }
  115. $params['value'] = json_encode(array_combine($fieldarr, $valuearr), JSON_UNESCAPED_UNICODE);
  116. }
  117. unset($params['mode']);
  118. try
  119. {
  120. //是否采用模型验证
  121. if ($this->modelValidate)
  122. {
  123. $name = basename(str_replace('\\', '/', get_class($this->model)));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  125. $row->validate($validate);
  126. }
  127. $result = $row->save($params);
  128. if ($result !== false)
  129. {
  130. $this->code = 1;
  131. }
  132. else
  133. {
  134. $this->msg = $row->getError();
  135. }
  136. }
  137. catch (think\exception\PDOException $e)
  138. {
  139. $this->msg = $e->getMessage();
  140. }
  141. }
  142. else
  143. {
  144. $this->msg = __('Parameter %s can not be empty', '');
  145. }
  146. return;
  147. }
  148. $this->view->assign("row", $row);
  149. $this->view->assign("value", (array) json_decode($row->value, true));
  150. return $this->view->fetch();
  151. }
  152. }