SystemConfigServices.php 47 KB

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