Field.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\admin\controller\qingdong\general;
  3. use app\admin\controller\qingdong\Base;
  4. use app\common\library\Auth;
  5. use addons\qingdong\model\Form;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 字段管理
  10. */
  11. class Field extends Base {
  12. protected $relationSearch = true;
  13. /**
  14. * @var \addons\qingdong\model\Field
  15. */
  16. protected $model = null;
  17. public function _initialize() {
  18. parent::_initialize();
  19. $this->model = new \addons\qingdong\model\Field;
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index() {
  25. //设置过滤方法
  26. $this->request->filter(['strip_tags', 'trim']);
  27. if ($this->request->isAjax()) {
  28. $types = $this->model->select();
  29. $types=collection($types)->toArray();
  30. $result = array("total" => count($types), "rows" => $types);
  31. return json($result);
  32. }
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 修改
  37. */
  38. public function edit($ids = null) {
  39. $row = $this->model->get($ids);
  40. if ($this->request->isPost()) {
  41. $params = $this->request->post("row/a");
  42. if ($params) {
  43. if(empty($params['data'])){
  44. $this->error('参数不能为空');
  45. }
  46. $data=[];
  47. $params['data']=json_decode($params['data'],true);
  48. foreach ($params['data'] as $v){
  49. $data[]=isset($v['field']) ? $v['field'] : $v;
  50. }
  51. $save=[
  52. 'data'=>json_encode($data,JSON_UNESCAPED_UNICODE)
  53. ];
  54. $form = Form::where(array('type'=>$row['type']))->find();
  55. if(!isset($form['data'])){
  56. $form['data'] = '';
  57. }
  58. if(!$form['data']){
  59. $form['data'] = json_encode(array());
  60. }
  61. $formdata = json_decode($form['data'],true);
  62. if(isset($formdata['data']) && $form['data']){
  63. foreach($formdata['data'] as $k=>$v){
  64. if($v['config']['label'] == $params['name']){
  65. $v['config']['content'] = [];
  66. if($data){
  67. foreach($data as $ks=>$vs){
  68. $v['config']['content'][] = array(
  69. "key" => "",
  70. "value" => $params['name'].($ks+1),
  71. "__label" => $vs,
  72. "__value" => array($params['name'].($ks+1)),
  73. "nodeKey" => 1,
  74. "isEdit" => false,
  75. "label" => $vs,
  76. );
  77. }
  78. }
  79. $formdata['data'][$k] = $v;
  80. }
  81. }
  82. $formdata = json_encode($formdata,JSON_UNESCAPED_UNICODE);
  83. }
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. $result= $this->model->save($save,['id'=>$ids]);
  88. if(isset($formdata) && $formdata){
  89. $resForm = Form::where(array('id'=>$form['id']))->update(array('data'=>$formdata));
  90. }
  91. Db::commit();
  92. } catch (Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result !== false || !$resForm) {
  97. $this->success();
  98. } else {
  99. $this->error(__('No rows were inserted'));
  100. }
  101. }
  102. $this->error(__('Parameter %s can not be empty', ''));
  103. }
  104. if(empty($row)){
  105. $this->error('数据不存在');
  106. }
  107. $this->assign('row', $row);
  108. return $this->view->fetch();
  109. }
  110. }