ConfigRepository.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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\common\repositories\system\config;
  12. use app\common\dao\system\config\SystemConfigDao;
  13. use app\common\model\system\config\SystemConfigClassify;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\system\CacheRepository;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use FormBuilder\Factory\Elm;
  18. use FormBuilder\Form;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Db;
  23. use think\facade\Route;
  24. /**
  25. * Class ConfigRepository
  26. * @package crmeb\repositories\system\config
  27. * @mixin SystemConfigDao
  28. */
  29. class ConfigRepository extends BaseRepository
  30. {
  31. const TYPES = ['input' => '文本框', 'number' => '数字框', 'textarea' => '多行文本框', 'radio' => '单选框', 'switches' => '开关', 'checkbox' => '多选框', 'select' => '下拉框', 'file' => '文件上传', 'image' => '图片上传', 'images' => '多图片上传', 'color' => '颜色选择框'];
  32. /**
  33. * ConfigRepository constructor.
  34. * @param SystemConfigDao $dao
  35. */
  36. public function __construct(SystemConfigDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * @param int $merId
  42. * @param SystemConfigClassify $configClassify
  43. * @param array $configs
  44. * @param array $formData
  45. * @return Form
  46. * @throws FormBuilderException
  47. * @author xaboy
  48. * @day 2020-04-23
  49. */
  50. public function formRule(int $merId, SystemConfigClassify $configClassify, array $configs, array $formData = [])
  51. {
  52. $components = $this->getRule($configs, $merId);
  53. $form = Elm::createForm(Route::buildUrl($merId ? 'merchantConfigSave' : 'configSave', ['key' => $configClassify->classify_key])->build(), $components);
  54. return $form->setTitle($configClassify->classify_name)->formData(array_filter($formData, function ($item) {
  55. return $item !== '' && !is_null($item);
  56. }));
  57. }
  58. public function getRule(array $configs, $merId)
  59. {
  60. $components = [];
  61. foreach ($configs as $config) {
  62. $component = $this->getComponent($config, $merId);
  63. $components[] = $component;
  64. }
  65. return $components;
  66. }
  67. public function getComponent($config, $merId)
  68. {
  69. switch ($config['config_type']) {
  70. case 'image':
  71. $component = Elm::frameImage($config['config_key'], $config['config_name'], '/' . config('admin.' . ($merId ? 'merchant' : 'admin') . '_prefix') . '/setting/uploadPicture?field=' . $config['config_key'] . '&type=1')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]);
  72. break;
  73. case 'images':
  74. $component = Elm::frameImage($config['config_key'], $config['config_name'], '/' . config('admin.' . ($merId ? 'merchant' : 'admin') . '_prefix') . '/setting/uploadPicture?field=' . $config['config_key'] . '&type=2')->maxLength(5)->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]);
  75. break;
  76. case 'file':
  77. $component = Elm::uploadFile($config['config_key'], $config['config_name'], rtrim(systemConfig('site_url'), '/') . Route::buildUrl('configUpload', ['field' => 'file'])->build())->headers(['X-Token' => request()->token()]);
  78. break;
  79. case 'select':
  80. //notbreak
  81. case 'checkbox':
  82. //notbreak
  83. case 'radio':
  84. $options = array_map(function ($val) {
  85. [$value, $label] = explode(':', $val, 2);
  86. return compact('value', 'label');
  87. }, explode("\n", $config['config_rule']));
  88. $component = Elm::{$config['config_type']}($config['config_key'], $config['config_name'])->options($options);
  89. break;
  90. case 'switches':
  91. $component = Elm::{$config['config_type']}($config['config_key'], $config['config_name'])->activeText('开')->inactiveText('关');
  92. break;
  93. default:
  94. $component = Elm::{$config['config_type']}($config['config_key'], $config['config_name']);
  95. break;
  96. }
  97. if ($config['required']) $component->required();
  98. $component->appendRule('suffix', [
  99. 'type' => 'div',
  100. 'style' => ['color' => '#999999'],
  101. 'domProps' => [
  102. 'innerHTML' => $config['info'],
  103. ]
  104. ]);
  105. if ($config['config_props'] ?? '') {
  106. $props = @parse_ini_string($config['config_props'], false, INI_SCANNER_TYPED);
  107. if (is_array($props)) {
  108. $component->props($props);
  109. if (isset($props['required']) && $props['required']) {
  110. $component->required();
  111. }
  112. if (isset($props['defaultValue'])) {
  113. $component->value($props['defaultValue']);
  114. }
  115. }
  116. }
  117. return $component;
  118. }
  119. /**
  120. * @param int $id
  121. * @param int $status
  122. * @return int
  123. * @throws DbException
  124. * @author xaboy
  125. * @day 2020-03-31
  126. */
  127. public function switchStatus(int $id, int $status)
  128. {
  129. return $this->dao->update($id, compact('status'));
  130. }
  131. /**
  132. * @param SystemConfigClassify $configClassify
  133. * @param int $merId
  134. * @return Form
  135. * @throws DataNotFoundException
  136. * @throws DbException
  137. * @throws FormBuilderException
  138. * @throws ModelNotFoundException
  139. * @author xaboy
  140. * @day 2020-04-22
  141. */
  142. public function cidByFormRule(SystemConfigClassify $configClassify, int $merId)
  143. {
  144. $config = $this->dao->cidByConfig($configClassify->config_classify_id, $merId == 0 ? 0 : 1);
  145. $keys = $config->column('config_key');
  146. return $this->formRule($merId, $configClassify, $config->toArray(), app()->make(ConfigValueRepository::class)->more($keys, $merId));
  147. }
  148. /**
  149. * @param int|null $id
  150. * @param array $formData
  151. * @return Form
  152. * @throws FormBuilderException
  153. * @author xaboy
  154. * @day 2020-03-31
  155. */
  156. public function form(?int $id = null, array $formData = []): Form
  157. {
  158. $form = Elm::createForm(is_null($id) ? Route::buildUrl('configSettingCreate')->build() : Route::buildUrl('configSettingUpdate', ['id' => $id])->build());
  159. $form->setRule([
  160. Elm::cascader('config_classify_id', '上级分类')->options(function () {
  161. $configClassifyRepository = app()->make(ConfigClassifyRepository::class);
  162. return array_merge([['value' => 0, 'label' => '请选择']], $configClassifyRepository->options());
  163. })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  164. Elm::select('user_type', '后台类型', 0)->options([
  165. ['label' => '总后台配置', 'value' => 0],
  166. ['label' => '商户后台配置', 'value' => 1],
  167. ])->requiredNum(),
  168. Elm::input('config_name', '配置名称')->required(),
  169. Elm::input('config_key', '配置key')->required(),
  170. Elm::textarea('info', '说明'),
  171. Elm::select('config_type', '配置类型')->options(function () {
  172. $options = [];
  173. foreach (self::TYPES as $value => $label) {
  174. $options[] = compact('value', 'label');
  175. }
  176. return $options;
  177. })->required(),
  178. Elm::textarea('config_rule', '选择项'),
  179. Elm::textarea('config_props', '配置'),
  180. Elm::number('sort', '排序', 0)->precision(0)->max(99999),
  181. Elm::switches('required', '必填', 0)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  182. Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  183. ]);
  184. return $form->setTitle(is_null($id) ? '添加配置' : '编辑配置')->formData($formData);
  185. }
  186. /**
  187. * @param int $id
  188. * @return Form
  189. * @throws DataNotFoundException
  190. * @throws DbException
  191. * @throws FormBuilderException
  192. * @throws ModelNotFoundException
  193. * @author xaboy
  194. * @day 2020-03-31
  195. */
  196. public function updateForm(int $id)
  197. {
  198. return $this->form($id, $this->dao->get($id)->toArray());
  199. }
  200. /**
  201. * @param array $where
  202. * @param int $page
  203. * @param int $limit
  204. * @return array
  205. * @throws DataNotFoundException
  206. * @throws DbException
  207. * @throws ModelNotFoundException
  208. * @author xaboy
  209. * @day 2020-03-31
  210. */
  211. public function lst(array $where, int $page, int $limit)
  212. {
  213. $query = $this->dao->search($where);
  214. $count = $query->count();
  215. $list = $query->page($page, $limit)->withAttr('typeName', function ($value, $data) {
  216. return self::TYPES[$data['config_type']];
  217. })->hidden(['config_classify_id'])->append(['typeName'])->select();
  218. return compact('count', 'list');
  219. }
  220. public function tabForm($group, $merId)
  221. {
  222. $make = app()->make(ConfigClassifyRepository::class);
  223. $list = $make->children($group->config_classify_id, 'config_classify_id,classify_key,classify_name,info');
  224. $children = [];
  225. foreach ($list as $item) {
  226. $_children = $this->cidByFormRule($make->keyByData($item['classify_key']), $merId)->formRule();
  227. if ($item['info']) {
  228. array_unshift($_children, [
  229. 'type' => 'el-alert',
  230. 'props' => [
  231. 'type' => 'warning',
  232. 'closable' => false,
  233. 'title' => $item['info']
  234. ]
  235. ], ['type' => 'div', 'style' => ['height' => '20px', 'width' => '100%']]);
  236. }
  237. $children[] = [
  238. 'type' => 'el-tab-pane',
  239. 'props' => [
  240. 'label' => $item['classify_name'],
  241. 'name' => $item['classify_key']
  242. ],
  243. 'children' => $_children
  244. ];
  245. }
  246. if ($group['classify_key'] === 'distribution_tabs') {
  247. $action = Route::buildUrl('configOthersSettingUpdate')->build();
  248. } else {
  249. $action = Route::buildUrl($merId ? 'merchantConfigSave' : 'configSave', ['key' => $group['classify_key']])->build();
  250. }
  251. return Elm::createForm($action, [
  252. [
  253. 'type' => 'el-tabs',
  254. 'native' => true,
  255. 'props' => [
  256. 'value' => $list[0]['classify_key'] ?? ''
  257. ],
  258. 'children' => $children
  259. ]
  260. ])->setTitle($group['classify_name']);
  261. }
  262. public function uploadForm()
  263. {
  264. $config = $this->getWhere(['config_key' => 'upload_type']);
  265. $rule = $this->getComponent($config, 0)->value(systemConfig('upload_type'));
  266. $make = app()->make(ConfigClassifyRepository::class);
  267. $rule->control([
  268. [
  269. 'value' => '1',
  270. 'rule' => $this->cidByFormRule($make->keyByData('local'), 0)->formRule()
  271. ],
  272. [
  273. 'value' => '2',
  274. 'rule' => $this->cidByFormRule($make->keyByData('qiniuyun'), 0)->formRule()
  275. ],
  276. [
  277. 'value' => '3',
  278. 'rule' => $this->cidByFormRule($make->keyByData('aliyun_oss'), 0)->formRule()
  279. ],
  280. [
  281. 'value' => '4',
  282. 'rule' => $this->cidByFormRule($make->keyByData('tengxun'), 0)->formRule()
  283. ],
  284. [
  285. 'value' => '5',
  286. 'rule' => $this->cidByFormRule($make->keyByData('huawei_obs'), 0)->formRule()
  287. ],
  288. [
  289. 'value' => '6',
  290. 'rule' => $this->cidByFormRule($make->keyByData('ucloud'), 0)->formRule()
  291. ],
  292. ]);
  293. return Elm::createForm(Route::buildUrl('systemSaveUploadConfig')->build(), [$rule])->setTitle('上传配置');
  294. }
  295. public function saveUpload($data)
  296. {
  297. $configValueRepository = app()->make(ConfigValueRepository::class);
  298. $uploadType = $data['upload_type'] ?? '1';
  299. $key = '';
  300. switch ($uploadType) {
  301. case 1:
  302. $key = 'local';
  303. break;
  304. case 2:
  305. $key = 'qiniuyun';
  306. break;
  307. case 3:
  308. $key = 'aliyun_oss';
  309. break;
  310. case 4:
  311. $key = 'tengxun';
  312. break;
  313. case 5:
  314. $key = 'huawei_obs';
  315. break;
  316. case 6:
  317. $key = 'ucloud';
  318. break;
  319. }
  320. Db::transaction(function () use ($data, $key, $uploadType, $configValueRepository) {
  321. $configValueRepository->setFormData([
  322. 'upload_type' => $uploadType
  323. ], 0);
  324. if ($key) {
  325. $make = app()->make(ConfigClassifyRepository::class);
  326. if (!($cid = $make->keyById($key))) return app('json')->fail('保存失败');
  327. $configValueRepository->save($cid, $data, 0);
  328. }
  329. });
  330. }
  331. public function wechatForm()
  332. {
  333. $formData['wechat_chekc_file'] = app()->make(CacheRepository::class)->getWhere(['key' => 'wechat_chekc_file']);
  334. if ($formData['wechat_chekc_file'] && !is_file($formData['wechat_chekc_file'])) $formData['wechat_chekc_file'] = '';
  335. $form = Elm::createForm(Route::buildUrl('configWechatUploadSet')->build());
  336. $form->setRule([
  337. Elm::uploadFile('wechat_chekc_file', '上传校验文件', rtrim(systemConfig('site_url'), '/') . Route::buildUrl('configUploadName', ['field' => 'file'])->build())->headers(['X-Token' => request()->token()]),
  338. ]);
  339. return $form->setTitle('上传校验文件')->formData($formData);
  340. }
  341. /**
  342. * 替换appid
  343. * @param string $appid
  344. * @param string $projectanme
  345. */
  346. public function updateConfigJson($appId = '', $projectName = '', $path = '')
  347. {
  348. $fileUrl = $path . "/download/project.config.json";
  349. $string = file_get_contents($fileUrl); //加载配置文件
  350. // 替换appid
  351. $appIdOld = '/"appid"(.*?),/';
  352. $appIdNew = '"appid"' . ': ' . '"' . $appId . '",';
  353. $string = preg_replace($appIdOld, $appIdNew, $string); // 正则查找然后替换
  354. // 替换小程序名称
  355. $projectNameOld = '/"projectname"(.*?),/';
  356. $projectNameNew = '"projectname"' . ': ' . '"' . $projectName . '",';
  357. $string = preg_replace($projectNameOld, $projectNameNew, $string); // 正则查找然后替换
  358. $newFileUrl = $path . "/download/project.config.json";
  359. @file_put_contents($newFileUrl, $string); // 写入配置文件
  360. }
  361. /**
  362. * 替换url
  363. * @param $url
  364. */
  365. public function updateUrl($url, $path)
  366. {
  367. $fileUrl = $path . "/download/common/vendor.js";
  368. $string = file_get_contents($fileUrl); //加载配置文件
  369. $string = str_replace('https://mer.crmeb.net', $url, $string); // 正则查找然后替换
  370. $ws = str_replace('https', 'wss', $url);
  371. $string = str_replace('wss://mer.crmeb.net', $ws, $string); // 正则查找然后替换
  372. $newFileUrl = $path . "/download/common/vendor.js";
  373. @file_put_contents($newFileUrl, $string); // 写入配置文件
  374. }
  375. /**
  376. * 关闭直播
  377. * @param int $iszhibo
  378. */
  379. public function updateAppJson($path)
  380. {
  381. $fileUrl = $path . "/download/app.json";
  382. $string = file_get_contents($fileUrl); //加载配置文件
  383. $pats = '/,
  384. "plugins": \{
  385. "live-player-plugin": \{
  386. "version": "(.*?)",
  387. "provider": "(.*?)"
  388. }
  389. }/';
  390. $string = preg_replace($pats, '', $string); // 正则查找然后替换
  391. $newFileUrl = $path . "/download/app.json";
  392. @file_put_contents($newFileUrl, $string); // 写入配置文件
  393. }
  394. /**
  395. * 去掉菜单
  396. * @param int $iszhibo
  397. */
  398. public function updateRouteJson($path)
  399. {
  400. $fileUrl = $path . "/download/app.json";
  401. $string = file_get_contents($fileUrl); //加载配置文件
  402. $pats = '/
  403. {
  404. "pagePath": "pages\/plant_grass\/index",
  405. "iconPath": "static\/images\/5-001.png",
  406. "selectedIconPath": "static\/images\/5-002.png",
  407. "text": "逛逛"
  408. },/';
  409. $string = preg_replace($pats, '', $string); // 正则查找然后替换
  410. $newFileUrl = $path . "/download/app.json";
  411. @file_put_contents($newFileUrl, $string); // 写入配置文件
  412. }
  413. /**
  414. * TODO 请求方式
  415. * @param $path
  416. * @param bool $plant
  417. * @author Qinii
  418. * @day 1/4/22
  419. */
  420. public function updatePlantJson(string $path, int $plant)
  421. {
  422. $fileUrl = $path . "/download/common/vendor.js";
  423. $string = file_get_contents($fileUrl); //加载配置文件
  424. $string = str_replace('"-openPlantGrass-"', $plant ? 'true' : 'false', $string); // 正则查找然后替换
  425. $newFileUrl = $path . "/download/common/vendor.js";
  426. @file_put_contents($newFileUrl, $string); // 写入配置文件
  427. }
  428. }