SystemConfigServices.php 47 KB

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