Field.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use think\Session;
  6. /**
  7. * 配置模型
  8. */
  9. class Field extends Model
  10. {
  11. // 表名,不含前缀
  12. protected $name = 'qingdongams_field';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = false;
  15. // 定义时间戳字段名
  16. protected $createTime = false;
  17. protected $updateTime = false;
  18. //获取字段
  19. public static function getField($name,$type=null)
  20. {
  21. $where = [];
  22. if($type){
  23. $where['type'] = $type;
  24. }
  25. $data=self::where($where)->where(['name' => $name])->value('data');
  26. if(empty($data)){
  27. return [];
  28. }
  29. return json_decode($data,true);
  30. }
  31. //修改字段
  32. public function fields()
  33. {
  34. return $this->hasOne(self::class, 'type', 'type')->field('type,data');
  35. }
  36. //设置form 表单选项
  37. public function setFormField($type, $data)
  38. {
  39. $insertAll = [];
  40. foreach ($data as $v) {
  41. if ($v['component'] == 'select') {
  42. $insertAll[] = [
  43. 'type' => $type,
  44. 'name' => $v['config']['label'],
  45. 'data' => json_encode($this->getDataFields($v['config']['content']), JSON_UNESCAPED_UNICODE)
  46. ];
  47. }
  48. }
  49. $model = new self;
  50. $model->where(['type' => $type])->delete();
  51. if(empty($insertAll)){
  52. return true;
  53. }
  54. if ($model->insertAll($insertAll) == false) {
  55. throw new Exception('新增表单失败');
  56. }
  57. return true;
  58. }
  59. //循环获取data里面的字段
  60. private function getDataFields($data, $fields = [])
  61. {
  62. foreach ($data as $v) {
  63. $fields[] = $v['label'];
  64. if (isset($v['children']) && $v['children']) {
  65. $fields = self::getDataFields($data['children'], $fields);
  66. }
  67. }
  68. return $fields;
  69. }
  70. }