SystemConfigServices.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\config;
  12. use app\dao\system\config\SystemConfigDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder;
  16. use think\exception\ValidateException;
  17. /**
  18. * 系统配置
  19. * Class SystemConfigServices
  20. * @package app\services\system\config
  21. * @method count(array $where = []) 获取指定条件下的count
  22. * @method save(array $data) 保存数据
  23. * @method get(int $id, ?array $field = []) 获取一条数据
  24. * @method update($id, array $data, ?string $key = null) 修改数据
  25. * @method delete(int $id, ?string $key = null) 删除数据
  26. * @method getUploadTypeList(string $configName) 获取上传配置中的上传类型
  27. */
  28. class SystemConfigServices extends BaseServices
  29. {
  30. /**
  31. * form表单句柄
  32. * @var FormBuilder
  33. */
  34. protected $builder;
  35. /**
  36. * 表单数据切割符号
  37. * @var string
  38. */
  39. protected $cuttingStr = '=>';
  40. /**
  41. * 表单提交url
  42. * @var string[]
  43. */
  44. protected $postUrl = [
  45. 'setting' => [
  46. 'url' => '/setting/config/save_basics',
  47. 'auth' => [],
  48. ],
  49. 'serve' => [
  50. 'url' => '/serve/sms_config/save_basics',
  51. 'auth' => ['short_letter_switch'],
  52. ],
  53. 'freight' => [
  54. 'url' => '/freight/config/save_basics',
  55. 'auth' => ['express'],
  56. ],
  57. 'agent' => [
  58. 'url' => '/agent/config/save_basics',
  59. 'auth' => ['fenxiao','brokerage_type','brokerage_set','extract_set'],
  60. ],
  61. 'marketing' => [
  62. 'url' => '/marketing/integral_config/save_basics',
  63. 'auth' => ['point'],
  64. ]
  65. ];
  66. /**
  67. * 子集控制规则
  68. * @var array[]
  69. */
  70. protected $relatedRule = [
  71. 'brokerage_func_status' => [
  72. 'son_type' => [
  73. 'brokerage_bindind' => '',
  74. 'store_brokerage_ratio' => '',
  75. 'store_brokerage_two' => '',
  76. 'user_extract_min_price' => '',
  77. 'user_extract_bank' => '',
  78. 'extract_time' => '',
  79. 'spread_banner' => '',
  80. 'store_brokerage_statu' => [
  81. 'son_type' => ['store_brokerage_price' => ''],
  82. 'show_value' => 3
  83. ],
  84. ],
  85. 'show_value' => 1
  86. ],
  87. 'pay_success_printing_switch' => [
  88. 'son_type' => [
  89. 'develop_id' => '',
  90. 'printing_api_key' => '',
  91. 'printing_client_id' => '',
  92. 'terminal_number' => '',
  93. ],
  94. 'show_value' => 1
  95. ],
  96. 'wss_open' => [
  97. 'son_type' => [
  98. 'wss_local_cert' => '',
  99. 'wss_local_pk' => '',
  100. ],
  101. 'show_value' => 1
  102. ],
  103. 'invoice_func_status' => [
  104. 'son_type' => [
  105. 'special_invoice_status' => '',
  106. ],
  107. 'show_value' => 1
  108. ],
  109. 'member_func_status' => [
  110. 'son_type' => [
  111. 'member_price_status' => '',
  112. 'order_give_exp' => '',
  113. 'sign_give_exp' => '',
  114. 'invite_user_exp' => ''
  115. ],
  116. 'show_value' => 1
  117. ],
  118. 'balance_func_status' => [
  119. 'son_type' => [
  120. 'recharge_attention' => '',
  121. 'recharge_switch' => '',
  122. 'store_user_min_recharge' => '',
  123. ],
  124. 'show_value' => 1
  125. ],
  126. 'system_product_copy_type' => [
  127. 'son_type' => [
  128. 'copy_product_apikey' => '',
  129. ],
  130. 'show_value' => 2
  131. ],
  132. 'logistics_type' => [
  133. 'son_type' => [
  134. 'system_express_app_code' => '',
  135. ],
  136. 'show_value' => 2
  137. ],
  138. 'ali_pay_status' => [
  139. 'son_type' => [
  140. 'ali_pay_appid' => '',
  141. 'alipay_merchant_private_key' => '',
  142. 'alipay_public_key' => '',
  143. ],
  144. 'show_value' => 1
  145. ],
  146. 'pay_weixin_open' => [
  147. 'son_type' => [
  148. 'paydir' => '',
  149. 'pay_weixin_key' => '',
  150. 'pay_weixin_client_key' => '',
  151. 'pay_weixin_client_cert' => '',
  152. 'pay_weixin_mchid' => '',
  153. 'pay_weixin_appsecret' => '',
  154. 'pay_weixin_appid' => '',
  155. ],
  156. 'show_value' => 1
  157. ],
  158. ];
  159. /**
  160. * SystemConfigServices constructor.
  161. * @param SystemConfigDao $dao
  162. */
  163. public function __construct(SystemConfigDao $dao, FormBuilder $builder)
  164. {
  165. $this->dao = $dao;
  166. $this->builder = $builder;
  167. }
  168. public function getSonConfig()
  169. {
  170. $sonConfig = [];
  171. $rolateRule = $this->relatedRule;
  172. if ($rolateRule) {
  173. foreach ($rolateRule as $key => $value) {
  174. $sonConfig = array_merge($sonConfig, array_keys($value['son_type']));
  175. foreach ($value['son_type'] as $k => $v) {
  176. if (isset($v['son_type'])) {
  177. $sonConfig = array_merge($sonConfig, array_keys($v['son_type']));
  178. }
  179. }
  180. }
  181. }
  182. return $sonConfig;
  183. }
  184. /**
  185. * 获取单个系统配置
  186. * @param string $configName
  187. * @param null $default
  188. * @return mixed|null
  189. */
  190. public function getConfigValue(string $configName, $default = null)
  191. {
  192. $value = $this->dao->getConfigValue($configName);
  193. return is_null($value) ? $default : json_decode($value,true);
  194. }
  195. /**
  196. * 获取全部配置
  197. * @param array $configName
  198. * @return array
  199. */
  200. public function getConfigAll(array $configName = [])
  201. {
  202. return array_map(function ($item) {
  203. return json_decode($item, true);
  204. }, $this->dao->getConfigAll($configName));
  205. }
  206. /**
  207. * 获取配置并分页
  208. * @param array $where
  209. * @return array
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\DbException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. */
  214. public function getConfigList(array $where)
  215. {
  216. [$page, $limit] = $this->getPageValue();
  217. $list = $this->dao->getConfigList($where, $page, $limit);
  218. $count = $this->dao->count($where);
  219. foreach ($list as &$item) {
  220. $item['value'] = $item['value'] ? json_decode($item['value'], true) ?: '' : '';
  221. if ($item['type'] == 'radio' || $item['type'] == 'checkbox') {
  222. $item['value'] = $this->getRadioOrCheckboxValueInfo($item['menu_name'], $item['value']);
  223. }
  224. if ($item['type'] == 'upload' && !empty($item['value'])) {
  225. if ($item['upload_type'] == 1 || $item['upload_type'] == 3) {
  226. $item['value'] = [set_file_url($item['value'])];
  227. }elseif ($item['upload_type'] == 2){
  228. $item['value'] = set_file_url($item['value']);
  229. }
  230. foreach ($item['value'] as $key => $value) {
  231. $tidy_srr[$key]['filepath'] = $value;
  232. $tidy_srr[$key]['filename'] = basename($value);
  233. }
  234. $item['value'] = $tidy_srr;
  235. }
  236. }
  237. return compact('count', 'list');
  238. }
  239. /**
  240. * 获取单选按钮或者多选按钮的显示值
  241. * @param $menu_name
  242. * @param $value
  243. * @return string
  244. * @throws \think\db\exception\DataNotFoundException
  245. * @throws \think\db\exception\ModelNotFoundException
  246. * @throws \think\exception\DbException
  247. */
  248. public function getRadioOrCheckboxValueInfo(string $menu_name, $value): string
  249. {
  250. $option = [];
  251. $config_one = $this->dao->getOne(['menu_name' => $menu_name]);
  252. if (!$config_one) {
  253. return '';
  254. }
  255. $parameter = explode("\n", $config_one['parameter']);
  256. foreach ($parameter as $k => $v) {
  257. if (isset($v) && strlen($v) > 0) {
  258. $data = explode('=>', $v);
  259. $option[$data[0]] = $data[1];
  260. }
  261. }
  262. $str = '';
  263. if (is_array($value)) {
  264. foreach ($value as $v) {
  265. $str .= $option[$v] . ',';
  266. }
  267. } else {
  268. $str .= !empty($value) ? $option[$value] ?? '' : $option[0] ?? '';
  269. }
  270. return $str;
  271. }
  272. /**
  273. * 获取系统配置信息
  274. * @param int $tabId
  275. * @return array
  276. * @throws \think\db\exception\DataNotFoundException
  277. * @throws \think\db\exception\DbException
  278. * @throws \think\db\exception\ModelNotFoundException
  279. */
  280. public function getReadList(int $tabId)
  281. {
  282. $info = $this->dao->getConfigTabAllList($tabId);
  283. foreach ($info as $k => $v) {
  284. if (!is_null(json_decode($v['value'])))
  285. $info[$k]['value'] = json_decode($v['value'], true);
  286. if ($v['type'] == 'upload' && !empty($v['value'])) {
  287. if ($v['upload_type'] == 1 || $v['upload_type'] == 3) $info[$k]['value'] = explode(',', $v['value']);
  288. }
  289. }
  290. return $info;
  291. }
  292. /**
  293. * 创建单行表单
  294. * @param string $type
  295. * @param array $data
  296. * @return array
  297. */
  298. public function createTextForm(string $type, array $data)
  299. {
  300. $formbuider = [];
  301. switch ($type) {
  302. case 'input':
  303. $data['value'] = isset($data['value']) ? json_decode($data['value'], true) : '';
  304. $formbuider[] = $this->builder->input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  305. break;
  306. case 'number':
  307. $data['value'] = isset($data['value']) ? json_decode($data['value'], true) : 0;
  308. $formbuider[] = $this->builder->number($data['menu_name'], $data['info'], (float)$data['value'])->info($data['desc']);
  309. break;
  310. case 'dateTime':
  311. $formbuider[] = $this->builder->dateTime($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  312. break;
  313. case 'color':
  314. $data['value'] = isset($data['value']) ? json_decode($data['value'], true) : '';
  315. $formbuider[] = $this->builder->color($data['menu_name'], $data['info'], $data['value'])->info($data['desc']);
  316. break;
  317. default:
  318. $data['value'] = isset($data['value']) ? json_decode($data['value'], true) : '';
  319. $formbuider[] = $this->builder->input($data['menu_name'], $data['info'], $data['value'])->info($data['desc'])->placeholder($data['desc'])->col(13);
  320. break;
  321. }
  322. return $formbuider;
  323. }
  324. /**
  325. * 创建多行文本框
  326. * @param array $data
  327. * @return mixed
  328. */
  329. public function createTextareaForm(array $data)
  330. {
  331. $data['value'] = json_decode($data['value'], true) ?: '';
  332. $formbuider[] = $this->builder->textarea($data['menu_name'], $data['info'], $data['value'])->placeholder($data['desc'])->info($data['desc'])->rows(6)->col(13);
  333. return $formbuider;
  334. }
  335. /**
  336. * 创建当选表单
  337. * @param array $data
  338. * @return array
  339. * @throws \FormBuilder\Exception\FormBuilderException
  340. */
  341. public function createRadioForm(array $data, $control = false)
  342. {
  343. $formbuider = [];
  344. $data['value'] = json_decode($data['value'], true) ?: '0';
  345. $parameter = explode("\n", $data['parameter']);
  346. $options = [];
  347. if ($parameter) {
  348. foreach ($parameter as $v) {
  349. if (strstr($v, $this->cuttingStr) !== false) {
  350. $pdata = explode($this->cuttingStr, $v);
  351. $options[] = ['label' => $pdata[1], 'value' => (int)$pdata[0]];
  352. }
  353. }
  354. $formbuider[] = $radio = $this->builder->radio($data['menu_name'], $data['info'], (int)$data['value'])->options($options)->info($data['desc'])->col(13);
  355. if ($control) {
  356. $radio->appendControl(isset($data['show_value']) ? $data['show_value'] : 1, is_array($control) ? $control : [$control]);
  357. }
  358. return $formbuider;
  359. }
  360. }
  361. /**
  362. * 创建上传组件表单
  363. * @param int $type
  364. * @param array $data
  365. * @return array
  366. */
  367. public function createUpoadForm(int $type, array $data)
  368. {
  369. $formbuider = [];
  370. switch ($type) {
  371. case 1:
  372. $data['value'] = json_decode($data['value'], true) ?: '';
  373. if($data['value'] != '') $data['value'] = set_file_url($data['value']);
  374. $formbuider[] = $this->builder->frameImage($data['menu_name'], $data['info'], $this->url('admin/widget.images/index', ['fodder' => $data['menu_name']], true), $data['value'])
  375. ->icon('ios-image')->width('950px')->height('420px')->info($data['desc'])->col(13);
  376. break;
  377. case 2:
  378. $data['value'] = json_decode($data['value'], true) ?: [];
  379. if($data['value'])
  380. $data['value'] = set_file_url($data['value']);
  381. $formbuider[] = $this->builder->frameImages($data['menu_name'], $data['info'], $this->url('admin/widget.images/index', ['fodder' => $data['menu_name'], 'type' => 'many', 'maxLength' => 5], true), $data['value'])
  382. ->maxLength(5)->icon('ios-images')->width('950px')->height('420px')
  383. ->info($data['desc'])->col(13);
  384. break;
  385. case 3:
  386. $data['value'] = json_decode($data['value'], true) ?: '';
  387. if($data['value'] != '') $data['value'] = set_file_url($data['value']);
  388. $formbuider[] = $this->builder->uploadFile($data['menu_name'], $data['info'], $this->url('/adminapi/file/upload/1', ['type' => 1], false, true), $data['value'])
  389. ->name('file')->info($data['desc'])->col(13)->headers([
  390. 'Authori-zation' => app()->request->header('Authori-zation'),
  391. ]);
  392. break;
  393. }
  394. return $formbuider;
  395. }
  396. /**
  397. * 创建单选框
  398. * @param array $data
  399. * @return array
  400. * @throws \FormBuilder\Exception\FormBuilderException
  401. */
  402. public function createCheckboxForm(array $data)
  403. {
  404. $formbuider = [];
  405. $data['value'] = json_decode($data['value'], true) ?: [];
  406. $parameter = explode("\n", $data['parameter']);
  407. $options = [];
  408. if ($parameter) {
  409. foreach ($parameter as $v) {
  410. if (strstr($v, $this->cuttingStr) !== false) {
  411. $pdata = explode($this->cuttingStr, $v);
  412. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  413. }
  414. }
  415. $formbuider[] = $this->builder->checkbox($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  416. }
  417. return $formbuider;
  418. }
  419. /**
  420. * 创建选择框表单
  421. * @param array $data
  422. * @return array
  423. * @throws \FormBuilder\Exception\FormBuilderException
  424. */
  425. public function createSelectForm(array $data)
  426. {
  427. $formbuider = [];
  428. $data['value'] = json_decode($data['value'], true) ?: [];
  429. $parameter = explode("\n", $data['parameter']);
  430. $options = [];
  431. if ($parameter) {
  432. foreach ($parameter as $v) {
  433. if (strstr($v, $this->cuttingStr) !== false) {
  434. $pdata = explode($this->cuttingStr, $v);
  435. $options[] = ['label' => $pdata[1], 'value' => $pdata[0]];
  436. }
  437. }
  438. $formbuider[] = $this->builder->select($data['menu_name'], $data['info'], $data['value'])->options($options)->info($data['desc'])->col(13);
  439. }
  440. return $formbuider;
  441. }
  442. public function bindBuilderData($data, $relatedRule)
  443. {
  444. if (!$data) return false;
  445. $p_list = array();
  446. foreach ($relatedRule as $rk => $rv) {
  447. $p_list[$rk] = $data[$rk];
  448. if (isset($rv['son_type']) && is_array($rv['son_type'])) {
  449. foreach ($rv['son_type'] as $sk => $sv) {
  450. if (is_array($sv) && isset($sv['son_type'])) {
  451. foreach ($sv['son_type'] as $ssk => $ssv) {
  452. $tmp = $data[$sk];
  453. $tmp['console'] = $data[$ssk];
  454. $p_list[$rk]['console'][] = $tmp;
  455. }
  456. } else {
  457. $p_list[$rk]['console'][] = $data[$sk];
  458. }
  459. }
  460. }
  461. }
  462. return array_values($p_list);
  463. }
  464. /**
  465. * 获取系统配置表单
  466. * @param int $id
  467. * @param array $formData
  468. * @return array
  469. * @throws \think\db\exception\DataNotFoundException
  470. * @throws \think\db\exception\DbException
  471. * @throws \think\db\exception\ModelNotFoundException
  472. */
  473. public function formTypeShine($data, $control = false)
  474. {
  475. switch ($data['type']) {
  476. case 'text'://文本框
  477. return $this->createTextForm($data['input_type'], $data);
  478. break;
  479. case 'radio'://单选框
  480. return $this->createRadioForm($data, $control);
  481. break;
  482. case 'textarea'://多行文本框
  483. return $this->createTextareaForm($data);
  484. break;
  485. case 'upload'://文件上传
  486. return $this->createUpoadForm((int)$data['upload_type'], $data);
  487. break;
  488. case 'checkbox'://多选框
  489. return $this->createCheckboxForm($data);
  490. break;
  491. case 'select'://多选框
  492. return $this->createSelectForm($data);
  493. break;
  494. }
  495. }
  496. /**
  497. * @param int $tabId
  498. * @param array $formData
  499. * @param array $relatedRule
  500. * @return array|bool
  501. * @throws \think\db\exception\DataNotFoundException
  502. * @throws \think\db\exception\DbException
  503. * @throws \think\db\exception\ModelNotFoundException
  504. */
  505. public function createConfigForm(int $tabId, array $relatedRule)
  506. {
  507. $list = $this->dao->getConfigTabAllList($tabId);
  508. if (!$relatedRule) {
  509. $formbuider = $this->createNoCrontrolForm($list);
  510. } else {
  511. $formbuider = $this->createBindCrontrolForm($list, $relatedRule);
  512. }
  513. return $formbuider;
  514. }
  515. /**
  516. * 创建
  517. * @param array $list
  518. * @return array
  519. * @throws \FormBuilder\Exception\FormBuilderException
  520. * @throws \think\db\exception\DataNotFoundException
  521. * @throws \think\db\exception\DbException
  522. * @throws \think\db\exception\ModelNotFoundException
  523. */
  524. public function createForm(array $list)
  525. {
  526. if (!$list) return [];
  527. $list = array_combine(array_column($list, 'menu_name'), $list);
  528. $formbuider = [];
  529. $relateRule = $this->relatedRule;
  530. $sonConfig = $this->getSonConfig();
  531. foreach ($list as $key => $data) {
  532. if (in_array($key, $sonConfig)) {
  533. continue;
  534. }
  535. switch ($data['type']) {
  536. case 'text'://文本框
  537. $formbuider = array_merge($formbuider, $this->createTextForm($data['input_type'], $data));
  538. break;
  539. case 'radio'://单选框
  540. $builder = [];
  541. if (isset($relateRule[$key])) {
  542. $role = $relateRule[$key];
  543. $data['show_value'] = $role['show_value'];
  544. foreach ($role['son_type'] as $sk => $sv) {
  545. if (isset($list[$sk])) {
  546. $son_data = $list[$sk];
  547. $son_data['show_value'] = $role['show_value'];
  548. $son_build = [];
  549. if (isset($sv['son_type'])) {
  550. foreach ($sv['son_type'] as $ssk => $ssv) {
  551. $son_data['show_value'] = $sv['show_value'];
  552. $son_build[] = $this->formTypeShine($list[$ssk])[0];
  553. unset($list[$ssk]);
  554. }
  555. }
  556. $builder[] = $this->formTypeShine($son_data, $son_build)[0];
  557. unset($list[$sk]);
  558. }
  559. }
  560. $data['show_value'] = $role['show_value'];
  561. }
  562. $formbuider = array_merge($formbuider, $this->createRadioForm($data, $builder));
  563. break;
  564. case 'textarea'://多行文本框
  565. $formbuider = array_merge($formbuider, $this->createTextareaForm($data));
  566. break;
  567. case 'upload'://文件上传
  568. $formbuider = array_merge($formbuider, $this->createUpoadForm((int)$data['upload_type'], $data));
  569. break;
  570. case 'checkbox'://多选框
  571. $formbuider = array_merge($formbuider, $this->createCheckboxForm($data));
  572. break;
  573. case 'select'://多选框
  574. $formbuider = array_merge($formbuider, $this->createSelectForm($data));
  575. break;
  576. }
  577. }
  578. return $formbuider;
  579. }
  580. /**无组件绑定规则
  581. * @param array $list
  582. * @return array|bool
  583. * @throws \FormBuilder\Exception\FormBuilderException
  584. * @throws \think\db\exception\DataNotFoundException
  585. * @throws \think\db\exception\DbException
  586. * @throws \think\db\exception\ModelNotFoundException
  587. */
  588. public function createNoCrontrolForm(array $list)
  589. {
  590. if (!$list) return false;
  591. $formbuider = [];
  592. foreach ($list as $key => $data) {
  593. switch ($data['type']) {
  594. case 'text'://文本框
  595. $formbuider = array_merge($formbuider, $this->createTextForm($data['input_type'], $data));
  596. break;
  597. case 'radio'://单选框
  598. $formbuider = array_merge($formbuider, $this->createRadioForm($data));
  599. break;
  600. case 'textarea'://多行文本框
  601. $formbuider = array_merge($formbuider, $this->createTextareaForm($data));
  602. break;
  603. case 'upload'://文件上传
  604. $formbuider = array_merge($formbuider, $this->createUpoadForm((int)$data['upload_type'], $data));
  605. break;
  606. case 'checkbox'://多选框
  607. $formbuider = array_merge($formbuider, $this->createCheckboxForm($data));
  608. break;
  609. case 'select'://多选框
  610. $formbuider = array_merge($formbuider, $this->createSelectForm($data));
  611. break;
  612. }
  613. }
  614. return $formbuider;
  615. }
  616. /**有组件绑定规则
  617. * @param array $list
  618. * @param array $relatedRule
  619. * @return array|bool
  620. * @throws \FormBuilder\Exception\FormBuilderException
  621. * @throws \think\db\exception\DataNotFoundException
  622. * @throws \think\db\exception\DbException
  623. * @throws \think\db\exception\ModelNotFoundException
  624. */
  625. public function createBindCrontrolForm(array $list, array $relatedRule)
  626. {
  627. if (!$list || !$relatedRule) return false;
  628. $formbuider = [];
  629. $new_data = array();
  630. foreach ($list as $dk => $dv) {
  631. $new_data[$dv['menu_name']] = $dv;
  632. }
  633. foreach ($relatedRule as $rk => $rv) {
  634. if (isset($rv['son_type'])) {
  635. $data = $new_data[$rk];
  636. switch ($data['type']) {
  637. case 'text'://文本框
  638. $formbuider = array_merge($formbuider, $this->createTextForm($data['input_type'], $data));
  639. break;
  640. case 'radio'://单选框
  641. $son_builder = array();
  642. foreach ($rv['son_type'] as $sk => $sv) {
  643. if (isset($sv['son_type'])) {
  644. foreach ($sv['son_type'] as $ssk => $ssv) {
  645. $son_data = $new_data[$sk];
  646. $son_data['show_value'] = $sv['show_value'];
  647. $son_builder[] = $this->formTypeShine($son_data, $this->formTypeShine($new_data[$ssk])[0])[0];
  648. }
  649. } else {
  650. $son_data = $new_data[$sk];
  651. $son_data['show_value'] = $rv['show_value'];
  652. $son_builder[] = $this->formTypeShine($son_data)[0];
  653. }
  654. }
  655. $formbuider = array_merge($formbuider, $this->createRadioForm($data, $son_builder));
  656. break;
  657. case 'textarea'://多行文本框
  658. $formbuider = array_merge($formbuider, $this->createTextareaForm($data));
  659. break;
  660. case 'upload'://文件上传
  661. $formbuider = array_merge($formbuider, $this->createUpoadForm((int)$data['upload_type'], $data));
  662. break;
  663. case 'checkbox'://多选框
  664. $formbuider = array_merge($formbuider, $this->createCheckboxForm($data));
  665. break;
  666. case 'select'://多选框
  667. $formbuider = array_merge($formbuider, $this->createSelectForm($data));
  668. break;
  669. }
  670. }
  671. }
  672. return $formbuider;
  673. }
  674. /**
  675. * 系统配置form表单创建
  676. * @param int $tabId
  677. * @return array
  678. * @throws \FormBuilder\Exception\FormBuilderException
  679. * @throws \think\db\exception\DataNotFoundException
  680. * @throws \think\db\exception\DbException
  681. * @throws \think\db\exception\ModelNotFoundException
  682. */
  683. public function getConfigForm($url, int $tabId)
  684. {
  685. /** @var SystemConfigTabServices $service */
  686. $service = app()->make(SystemConfigTabServices::class);
  687. $title = $service->value(['id' => $tabId], 'title');
  688. $list = $this->dao->getConfigTabAllList($tabId);
  689. $formbuider = $this->createForm($list);
  690. $name = 'setting';
  691. if ($url) {
  692. $name = explode('/', $url)[2] ?? $name;
  693. }
  694. $postUrl = $this->postUrl[$name]['url'] ?? '/setting/config/save_basics';
  695. return create_form($title, $formbuider, $this->url($postUrl), 'POST');
  696. }
  697. /**
  698. * 新增路由增加设置项验证
  699. * @param $url
  700. * @param $post
  701. * @return bool
  702. */
  703. public function checkParam($url, $post)
  704. {
  705. $name = '';
  706. if ($url) {
  707. $name = explode('/', $url)[2] ?? $name;
  708. }
  709. $auth = $this->postUrl[$name]['auth'] ?? false;
  710. if ($auth === false) {
  711. throw new ValidateException('请求不被允许');
  712. }
  713. if ($auth) {
  714. /** @var SystemConfigTabServices $systemConfigTabServices */
  715. $systemConfigTabServices = app()->make(SystemConfigTabServices::class);
  716. foreach ($post as $key => $value) {
  717. $tab_ids = $systemConfigTabServices->getColumn([['eng_title', 'IN', $auth]], 'id');
  718. if (!$tab_ids || !in_array($key, $this->dao->getColumn([['config_tab_id', 'IN', $tab_ids]], 'menu_name'))) {
  719. throw new ValidateException('设置类目不被允许');
  720. }
  721. }
  722. }
  723. return true;
  724. }
  725. /**
  726. * 修改配置获取form表单
  727. * @param int $id
  728. * @return array
  729. * @throws \FormBuilder\Exception\FormBuilderException
  730. * @throws \think\db\exception\DataNotFoundException
  731. * @throws \think\db\exception\DbException
  732. * @throws \think\db\exception\ModelNotFoundException
  733. */
  734. public function editConfigForm(int $id)
  735. {
  736. $menu = $this->dao->get($id)->getData();
  737. if (!$menu) {
  738. throw new AdminException('修改数据不存在!');
  739. }
  740. /** @var SystemConfigTabServices $service */
  741. $service = app()->make(SystemConfigTabServices::class);
  742. $formbuider = [];
  743. $formbuider[] = $this->builder->input('menu_name', '字段变量', $menu['menu_name'])->disabled(1);
  744. $formbuider[] = $this->builder->hidden('type', $menu['type']);
  745. $formbuider[] = $this->builder->select('config_tab_id', '分类', (string)$menu['config_tab_id'])->setOptions($service->getSelectForm());
  746. $formbuider[] = $this->builder->input('info', '配置名称', $menu['info'])->autofocus(1);
  747. $formbuider[] = $this->builder->input('desc', '配置简介', $menu['desc']);
  748. switch ($menu['type']) {
  749. case 'text':
  750. $menu['value'] = json_decode($menu['value'], true);
  751. $formbuider[] = $this->builder->select('input_type', '类型', $menu['input_type'])->setOptions([
  752. ['value' => 'input', 'label' => '文本框']
  753. , ['value' => 'dateTime', 'label' => '时间']
  754. , ['value' => 'color', 'label' => '颜色']
  755. , ['value' => 'number', 'label' => '数字']
  756. ]);
  757. //输入框验证规则
  758. $formbuider[] = $this->builder->input('value', '默认值', $menu['value']);
  759. if (!empty($menu['required'])) {
  760. $formbuider[] = $this->builder->number('width', '文本框宽(%)', (int)$menu['width']);
  761. $formbuider[] = $this->builder->input('required', '验证规则', $menu['required'])->placeholder('多个请用,隔开例如:required:true,url:true');
  762. }
  763. break;
  764. case 'textarea':
  765. $menu['value'] = json_decode($menu['value'], true);
  766. //多行文本
  767. if (!empty($menu['high'])) {
  768. $formbuider[] = $this->builder->textarea('value', '默认值', $menu['value'])->rows(5);
  769. $formbuider[] = $this->builder->number('width', '文本框宽(%)', (int)$menu['width']);
  770. $formbuider[] = $this->builder->number('high', '多行文本框高(%)', (int)$menu['high']);
  771. } else {
  772. $formbuider[] = $this->builder->input('value', '默认值', $menu['value']);
  773. }
  774. break;
  775. case 'radio':
  776. $formbuider = array_merge($formbuider, $this->createRadioForm($menu));
  777. //单选和多选参数配置
  778. if (!empty($menu['parameter'])) {
  779. $formbuider[] = $this->builder->textarea('parameter', '配置参数', $menu['parameter'])->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  780. }
  781. break;
  782. case 'checkbox':
  783. $formbuider = array_merge($formbuider, $this->createCheckboxForm($menu));
  784. //单选和多选参数配置
  785. if (!empty($menu['parameter'])) {
  786. $formbuider[] = $this->builder->textarea('parameter', '配置参数', $menu['parameter'])->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  787. }
  788. break;
  789. case 'upload':
  790. $formbuider = array_merge($formbuider, $this->createUpoadForm(($menu['upload_type']), $menu));
  791. //上传类型选择
  792. if (!empty($menu['upload_type'])) {
  793. $formbuider[] = $this->builder->radio('upload_type', '上传类型', $menu['upload_type'])->options([['value' => 1, 'label' => '单图'], ['value' => 2, 'label' => '多图'], ['value' => 3, 'label' => '文件']]);
  794. }
  795. break;
  796. }
  797. $formbuider[] = $this->builder->number('sort', '排序', (int)$menu['sort']);
  798. $formbuider[] = $this->builder->radio('status', '状态', $menu['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  799. return create_form('编辑字段', $formbuider, $this->url('/setting/config/' . $id), 'PUT');
  800. }
  801. /**
  802. * 字段状态
  803. * @return array
  804. */
  805. public function formStatus(): array
  806. {
  807. return [['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']];
  808. }
  809. /**
  810. * 选择文文件类型
  811. * @return array
  812. */
  813. public function uploadType(): array
  814. {
  815. return [
  816. ['value' => 1, 'label' => '单图']
  817. , ['value' => 2, 'label' => '多图']
  818. , ['value' => 3, 'label' => '文件']
  819. ];
  820. }
  821. /**
  822. * 选择文本框类型
  823. * @return array
  824. */
  825. public function textType(): array
  826. {
  827. return [
  828. ['value' => 'input', 'label' => '文本框']
  829. , ['value' => 'dateTime', 'label' => '时间']
  830. , ['value' => 'color', 'label' => '颜色']
  831. , ['value' => 'number', 'label' => '数字']
  832. ];
  833. }
  834. /**
  835. * 获取创建配置规格表单
  836. * @param int $type
  837. * @param int $tab_id
  838. * @return array
  839. */
  840. public function createFormRule(int $type, int $tab_id): array
  841. {
  842. /** @var SystemConfigTabServices $service */
  843. $service = app()->make(SystemConfigTabServices::class);
  844. $formbuider = [];
  845. $form_type = '';
  846. $info_type = [];
  847. $parameter = [];
  848. switch ($type) {
  849. case 0://文本框
  850. $form_type = 'text';
  851. $info_type = $this->builder->select('input_type', '类型')->setOptions($this->textType());
  852. $parameter[] = $this->builder->input('value', '默认值');
  853. $parameter[] = $this->builder->number('width', '文本框宽(%)', 100);
  854. $parameter[] = $this->builder->input('required', '验证规则')->placeholder('多个请用,隔开例如:required:true,url:true');
  855. break;
  856. case 1://多行文本框
  857. $form_type = 'textarea';
  858. $parameter[] = $this->builder->textarea('value', '默认值');
  859. $parameter[] = $this->builder->number('width', '文本框宽(%)', 100);
  860. $parameter[] = $this->builder->number('high', '多行文本框高(%)', 5);
  861. break;
  862. case 2://单选框
  863. $form_type = 'radio';
  864. $parameter[] = $this->builder->textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>男\n2=>女\n3=>保密");
  865. $parameter[] = $this->builder->input('value', '默认值');
  866. break;
  867. case 3://文件上传
  868. $form_type = 'upload';
  869. $parameter[] = $this->builder->radio('upload_type', '上传类型', 1)->options($this->uploadType());
  870. break;
  871. case 4://多选框
  872. $form_type = 'checkbox';
  873. $parameter[] = $this->builder->textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  874. break;
  875. case 5://下拉框
  876. $form_type = 'select';
  877. $parameter[] = $this->builder->textarea('parameter', '配置参数')->placeholder("参数方式例如:\n1=>白色\n2=>红色\n3=>黑色");
  878. break;
  879. }
  880. if ($form_type) {
  881. $formbuider[] = $this->builder->hidden('type', $form_type);
  882. $formbuider[] = $this->builder->select('config_tab_id', '分类', $tab_id)->setOptions($service->getSelectForm());
  883. if ($info_type) {
  884. $formbuider[] = $info_type;
  885. }
  886. $formbuider[] = $this->builder->input('info', '配置名称')->autofocus(1);
  887. $formbuider[] = $this->builder->input('menu_name', '字段变量')->placeholder('例如:site_url');
  888. $formbuider[] = $this->builder->input('desc', '配置简介');
  889. $formbuider = array_merge($formbuider, $parameter);
  890. $formbuider[] = $this->builder->number('sort', '排序', 0);
  891. $formbuider[] = $this->builder->radio('status', '状态', 1)->options($this->formStatus());
  892. }
  893. return create_form('添加字段', $formbuider, $this->url('/setting/config'), 'POST');
  894. }
  895. /**
  896. * radio 和 checkbox规则的判断
  897. * @param $data
  898. * @return bool
  899. */
  900. public function valiDateRadioAndCheckbox($data)
  901. {
  902. $option = [];
  903. $option_new = [];
  904. $data['parameter'] = str_replace("\r\n", "\n", $data['parameter']);//防止不兼容
  905. $parameter = explode("\n", $data['parameter']);
  906. if (count($parameter) < 2) {
  907. throw new AdminException('请输入正确格式的配置参数');
  908. }
  909. foreach ($parameter as $k => $v) {
  910. if (isset($v) && !empty($v)) {
  911. $option[$k] = explode('=>', $v);
  912. }
  913. }
  914. if (count($option) < 2) {
  915. throw new AdminException('请输入正确格式的配置参数');
  916. }
  917. $bool = 1;
  918. foreach ($option as $k => $v) {
  919. $option_new[$k] = $option[$k][0];
  920. foreach ($v as $kk => $vv) {
  921. $vv_num = strlen($vv);
  922. if (!$vv_num) {
  923. $bool = 0;
  924. }
  925. }
  926. }
  927. if (!$bool) {
  928. throw new AdminException('请输入正确格式的配置参数');
  929. }
  930. $num1 = count($option_new);//提取该数组的数目
  931. $arr2 = array_unique($option_new);//合并相同的元素
  932. $num2 = count($arr2);//提取合并后数组个数
  933. if ($num1 > $num2) {
  934. throw new AdminException('请输入正确格式的配置参数');
  935. }
  936. return true;
  937. }
  938. /**
  939. * 验证参数
  940. * @param $data
  941. * @return bool
  942. */
  943. public function valiDateValue($data)
  944. {
  945. if (!$data || !isset($data['required']) || !$data['required']) {
  946. return true;
  947. }
  948. $valids = explode(',', $data['required']);
  949. foreach ($valids as $valid) {
  950. $valid = explode(':', $valid);
  951. if (isset($valid[0]) && isset($valid[1])) {
  952. $k = strtolower(trim($valid[0]));
  953. $v = strtolower(trim($valid[1]));
  954. switch ($k) {
  955. case 'required':
  956. if ($v == 'true' && $data['value'] === '') {
  957. throw new ValidateException(($data['info'] ?? '') . '请输入默认值');
  958. }
  959. break;
  960. case 'url':
  961. if ($v == 'true' && !check_link($data['value'])) {
  962. throw new ValidateException(($data['info'] ?? '') . '请输入正确url');
  963. }
  964. break;
  965. }
  966. }
  967. }
  968. }
  969. /**保存平台电子面单打印信息
  970. * @param array $data
  971. * @return bool
  972. */
  973. public function saveExpressInfo(array $data)
  974. {
  975. if (!is_array($data) || !$data) return false;
  976. // config_export_id 快递公司id
  977. // config_export_temp_id 快递公司模板id
  978. // config_export_com 快递公司编码
  979. // config_export_to_name 发货人姓名
  980. // config_export_to_tel 发货人电话
  981. // config_export_to_address 发货人详细地址
  982. // config_export_siid 电子面单打印机编号
  983. foreach ($data as $key => $value) {
  984. $this->dao->update(['menu_name' => 'config_export_' . $key], ['value' => json_encode($value)]);
  985. }
  986. }
  987. /**
  988. * 获取分享海报 兼容方法
  989. */
  990. public function getSpreadBanner()
  991. {
  992. //配置
  993. $banner = sys_config('spread_banner', []);
  994. if (!$banner) {
  995. //组合数据
  996. $banner = sys_data('routine_spread_banner');
  997. if ($banner) {
  998. $banner = array_column($banner, 'pic');
  999. $this->dao->update(['menu_name' => 'spread_banner'], ['value' => json_encode($banner)]);
  1000. \crmeb\services\CacheService::clear();
  1001. }
  1002. }
  1003. return $banner;
  1004. }
  1005. /**
  1006. * 保存wss配置
  1007. * @param int $wssOpen
  1008. * @param string $wssLocalpk
  1009. * @param string $wssLocalCert
  1010. */
  1011. public function saveSslFilePath(int $wssOpen, string $wssLocalpk, string $wssLocalCert)
  1012. {
  1013. $wssFile = root_path() . '.wss';
  1014. $content = <<<WSS
  1015. wssOpen = $wssOpen
  1016. wssLocalpk = $wssLocalpk
  1017. wssLocalCert = $wssLocalCert
  1018. WSS;
  1019. try {
  1020. file_put_contents($wssFile, $content);
  1021. } catch (\Throwable $e) {
  1022. throw new ValidateException('保存wss证书失败,失败原因:' . $e->getMessage());
  1023. }
  1024. }
  1025. /**
  1026. * 获取wss配置
  1027. * @param string $key
  1028. * @return array|false|mixed
  1029. */
  1030. public function getSslFilePath(string $key = '')
  1031. {
  1032. $wssFile = root_path() . '.wss';
  1033. try {
  1034. $content = parse_ini_file($wssFile);
  1035. } catch (\Throwable $e) {
  1036. $content = [];
  1037. }
  1038. if (isset($content[$key])) {
  1039. return $content[$key];
  1040. } else {
  1041. return $content;
  1042. }
  1043. }
  1044. }