123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace addons\formguide\library;
-
- use addons\formguide\model\ModelField;
- use think\Db;
- use think\facade\Validate;
-
- class Service
- {
-
-
- public static function getFieldList($modelId, $id = null)
- {
- $list = ModelField::where('modelid', $modelId)->where('status', 1)->order('listorder DESC,id DESC')->select();
- if (!empty($list)) {
-
- if ($id) {
- $modelInfo = Db::name('Model')->where('id', $modelId)->field('tablename,type')->find();
- $dataInfo = Db::name($modelInfo['tablename'])->where('id', $id)->find();
- }
- foreach ($list as $key => &$value) {
-
- if ($value['iscore']) {
- unset($list[$key]);
- }
-
- $value['fieldArr'] = 'modelField';
- if (isset($dataInfo[$value['name']])) {
- $value['value'] = $dataInfo[$value['name']];
- }
-
- $value['setting'] = unserialize($value['setting']);
- $value['options'] = $value['setting']['options'] ?? '';
-
- if (!$id) {
- $value['value'] = $value['setting']['value'] ?? '';
- }
- if ($value['type'] == 'custom') {
- if ($value['options'] != '') {
- $tpar = explode(".", $value['options'], 2);
- $value['options'] = \think\Response::create('admin@custom/' . $tpar[0], 'view')->assign('vo', $value)->getContent();
- unset($tpar);
- }
- } elseif ($value['options'] != '') {
- $value['options'] = parse_attr($value['options']);
- }
- if ($value['type'] == 'checkbox') {
- $value['value'] = empty($value['value']) ? [] : explode(',', $value['value']);
- }
- if ($value['type'] == 'datetime') {
- $value['value'] = empty($value['value']) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $value['value']);
- }
- if ($value['type'] == 'date') {
- $value['value'] = empty($value['value']) ? '' : date('Y-m-d', $value['value']);
- }
- if ($value['type'] == 'Ueditor' || $value['type'] == 'markdown') {
- $value['value'] = isset($value['value']) ? htmlspecialchars_decode($value['value']) : '';
- }
- }
- }
- return $list;
- }
-
-
- public static function dealModelPostData($modelid, $data)
- {
-
- $query = ModelField::where('modelid', $modelid)->where('status', 1);
-
- $filedTypeList = $query->order('listorder DESC, id DESC')->column('name,title,type,ifsystem,ifrequire,pattern,errortips');
-
- foreach ($filedTypeList as $name => $vo) {
- if (!isset($data[$name])) {
- switch ($vo['type']) {
-
- case 'switch':
- $data[$name] = 0;
- break;
- case 'checkbox':
- $data[$name] = '';
- break;
- }
- } else {
- if (is_array($data[$name])) {
- $data[$name] = implode(',', $data[$name]);
- }
- switch ($vo['type']) {
-
- case 'switch':
- $data[$name] = 1;
- break;
-
- case 'datetime':
-
- $data[$name] = strtotime($data[$name]);
-
- break;
-
- case 'date':
- $data[$name] = strtotime($data[$name]);
- break;
-
- case 'markdown':
- case 'Ueditor':
- $data[$name] = htmlspecialchars(stripslashes($data[$name]));
- break;
- }
- }
-
- if ($vo['ifrequire'] && (!isset($data[$name]) || $data[$name] == '')) {
- throw new \Exception("'" . $vo['title'] . "'必须填写~");
- }
-
- if (isset($data[$name]) && $data[$name] && $vo['pattern'] && !Validate::regex($data[$name], $vo['pattern'])) {
- throw new \Exception("'" . $vo['title'] . "'" . (!empty($vo['errortips']) ? $vo['errortips'] : '正则校验失败') . "");
- }
-
- if (!empty($data[$name]) && in_array($vo['type'], ['number']) && !Validate::isNumber($data[$name])) {
- throw new \Exception("'" . $vo['title'] . "'格式错误~");
-
- } else {
-
- }
- }
- return $data;
- }
- }
|