Config.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\config;
  12. use app\common\repositories\system\CacheRepository;
  13. use app\common\repositories\system\config\ConfigClassifyRepository;
  14. use app\common\repositories\system\config\ConfigRepository;
  15. use app\validate\admin\ConfigValidate;
  16. use crmeb\basic\BaseController;
  17. use crmeb\services\FileService;
  18. use crmeb\services\UploadService;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use think\App;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\exception\ValidateException;
  25. use think\facade\Log;
  26. /**
  27. * 系统配置
  28. */
  29. class Config extends BaseController
  30. {
  31. /**
  32. * @var ConfigRepository
  33. */
  34. protected $repository;
  35. /**
  36. * Config constructor.
  37. * @param App $app
  38. * @param ConfigRepository $repository
  39. */
  40. public function __construct(App $app, ConfigRepository $repository)
  41. {
  42. parent::__construct($app);
  43. $this->repository = $repository;
  44. }
  45. /**
  46. * @return mixed
  47. * @throws DataNotFoundException
  48. * @throws DbException
  49. * @throws ModelNotFoundException
  50. * @author xaboy
  51. * @day 2020-03-31
  52. */
  53. public function lst()
  54. {
  55. $where = $this->request->params(['keyword', 'config_classify_id', 'user_type']);
  56. [$page, $limit] = $this->getPage();
  57. $lst = $this->repository->lst($where, $page, $limit);
  58. return app('json')->success($lst);
  59. }
  60. /**
  61. * @return mixed
  62. * @throws FormBuilderException
  63. * @author xaboy
  64. * @day 2020-03-31
  65. */
  66. public function createTable()
  67. {
  68. $cid = $this->request->param('config_classify_id',0);
  69. $form = $this->repository->form($cid);
  70. return app('json')->success(formToData($form));
  71. }
  72. /**
  73. * @param int $id
  74. * @return mixed
  75. * @throws DataNotFoundException
  76. * @throws DbException
  77. * @throws ModelNotFoundException
  78. * @throws FormBuilderException
  79. * @author xaboy
  80. * @day 2020-03-31
  81. */
  82. public function updateTable($id)
  83. {
  84. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  85. $form = $this->repository->updateForm($id);
  86. return app('json')->success(formToData($form));
  87. }
  88. /**
  89. * @param int $id
  90. * @return mixed
  91. * @throws DbException
  92. * @author xaboy
  93. * @day 2020-03-31
  94. */
  95. public function switchStatus($id)
  96. {
  97. $status = $this->request->param('status', 0);
  98. if (!$this->repository->exists($id))
  99. return app('json')->fail('分类不存在');
  100. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  101. return app('json')->success('修改成功');
  102. }
  103. /**
  104. * @param int $id
  105. * @return mixed
  106. * @throws DataNotFoundException
  107. * @throws DbException
  108. * @throws ModelNotFoundException
  109. * @author xaboy
  110. * @day 2020-03-27
  111. */
  112. public function get($id)
  113. {
  114. $data = $this->repository->get($id);
  115. if (!$data)
  116. return app('json')->fail('配置不存在');
  117. else
  118. return app('json')->success($data->hidden(['mer_id', 'value']));
  119. }
  120. /**
  121. * @param ConfigValidate $validate
  122. * @param ConfigClassifyRepository $configClassifyRepository
  123. * @return mixed
  124. * @author xaboy
  125. * @day 2020-03-27
  126. */
  127. public function create()
  128. {
  129. $data = $this->getParams();
  130. if ($this->repository->keyExists($data['config_key']))
  131. return app('json')->fail('配置key已存在');
  132. if ($data['linked_status'] && !$data['linked_id']) return app('json')->fail('请选择关联显示上级');
  133. $this->repository->create($data);
  134. return app('json')->success('添加成功');
  135. }
  136. /**
  137. * @param int $id
  138. * @param ConfigValidate $validate
  139. * @param ConfigClassifyRepository $configClassifyRepository
  140. * @return mixed
  141. * @throws DbException
  142. * @author xaboy
  143. * @day 2020-03-27
  144. */
  145. public function update($id)
  146. {
  147. $data = $this->getParams();
  148. if (!$this->repository->exists($id))
  149. return app('json')->fail('分类不存在');
  150. if ($this->repository->keyExists($data['config_key'], $id))
  151. return app('json')->fail('配置key已存在');
  152. $this->repository->update($id, $data);
  153. return app('json')->success('修改成功');
  154. }
  155. /**
  156. * @param int $id
  157. * @return mixed
  158. * @throws DbException
  159. * @author xaboy
  160. * @day 2020-03-27
  161. */
  162. public function delete($id)
  163. {
  164. $this->repository->delete($id);
  165. return app('json')->success('删除成功');
  166. }
  167. /**
  168. * @param string $key
  169. * @param ConfigClassifyRepository $configClassifyRepository
  170. * @return mixed
  171. * @throws DataNotFoundException
  172. * @throws DbException
  173. * @throws FormBuilderException
  174. * @throws ModelNotFoundException
  175. * @author xaboy
  176. * @day 2020-04-22
  177. */
  178. public function form($key, ConfigClassifyRepository $configClassifyRepository)
  179. {
  180. $tab_id = $this->request->param('tab_id', 0);
  181. if (!$configClassifyRepository->keyExists($key) || !($configClassfiy = $configClassifyRepository->keyByData($key)))
  182. return app('json')->fail('配置分类不存在');
  183. if ($configClassifyRepository->existsWhere(['pid' => $configClassfiy->config_classify_id]))
  184. $form = $this->repository->tabForm($configClassfiy, $this->request->merId(),$tab_id);
  185. else
  186. $form = $this->repository->cidByFormRule($configClassfiy, $this->request->merId());
  187. return app('json')->success(formToData($form));
  188. }
  189. /**
  190. * 文件上传到本地
  191. * @param $field
  192. * @return \think\response\Json
  193. * @author Qinii
  194. * @day 2023/4/25
  195. */
  196. public function upload($field)
  197. {
  198. $file = $this->request->file($field);
  199. if (!$file) return app('json')->fail('请上传附件');
  200. //ico 图标处理
  201. if ($file->getOriginalExtension() == 'ico') {
  202. $file->move('public', 'favicon.ico');
  203. $res = tidy_url('public/favicon.ico');
  204. return app('json')->success(['src' => $res]);
  205. }
  206. $upload = UploadService::create(1);
  207. $data = $upload->to('attach')->validate()->move($field);
  208. if ($data === false) {
  209. return app('json')->fail($upload->getError());
  210. }
  211. return app('json')->success(['src' => path_to_url($upload->getFileInfo()->filePath)]);
  212. }
  213. public function uploadWechatForm()
  214. {
  215. return app('json')->success(formToData($this->repository->wechatForm()));
  216. }
  217. public function uploadAsName()
  218. {
  219. $file = $this->request->file('file');
  220. validate(["file|文件" => ['fileExt' => 'txt']])->check(['file' => $file]);
  221. if (!$file) return app('json')->fail('请上传附件');
  222. $res = \think\facade\Filesystem::putFileAs('', $file, $file->getOriginalName());
  223. if (!$res) return app('json')->fail('上传失败');
  224. return app('json')->success(['src' => $res]);
  225. }
  226. public function uploadWechatSet()
  227. {
  228. $name = $this->request->param('wechat_chekc_file');
  229. if (!$name || !is_file(public_path() . 'uploads/' . $name)) return app('json')->fail('文件不存在');
  230. try {
  231. rename(public_path() . 'uploads/' . $name, public_path() . $name);
  232. app()->make(CacheRepository::class)->save('wechat_chekc_file', public_path() . $name, 0);
  233. } catch (\Exception $exception) {
  234. return app('json')->fail('修改失败');
  235. }
  236. return app('json')->success('提交成功');
  237. }
  238. /**
  239. * 下载小程序
  240. * @return mixed
  241. */
  242. public function downloadTemp()
  243. {
  244. $is_live = $this->request->param('is_live', 0);
  245. $is_menu = $this->request->param('is_menu', 0);
  246. if (systemConfig('routine_appId') == '') throw new ValidateException('请先配置小程序appId');
  247. $code = get_crmeb_version_code();
  248. $name = md5(time());
  249. $make = app()->make(CacheRepository::class);
  250. $routine_zip = $make->getResult('routine_zip');
  251. $path = root_path() . 'extend';
  252. if (!is_dir($path . '/download')) {
  253. @mkdir($path . '/download', 0777);
  254. }
  255. if (!is_dir(public_path() . 'static/download')) {
  256. @mkdir(public_path() . 'static/download', 0777);
  257. }
  258. if (!is_dir($path . '/mp-weixin/' . $code)) {
  259. return app('json')->fail('缺少小程序源文件');
  260. }
  261. try {
  262. @unlink(public_path() . 'static/download/' . $routine_zip . '.zip');
  263. //拷贝源文件
  264. /** @var FileService $fileService */
  265. $fileService = app(FileService::class);
  266. $fileService->copyDir($path . '/mp-weixin/' . $code, $path . '/download');
  267. //替换appid和名称
  268. $this->repository->updateConfigJson(systemConfig('routine_appId'), systemConfig('routine_name'), $path);
  269. //是否开启直播
  270. if ($is_live == 0) {
  271. $this->repository->updateAppJson($path);
  272. }
  273. $this->repository->updatePlantJson($path, $is_menu);
  274. //是否显示菜单
  275. if ($is_menu == 0) {
  276. $this->repository->updateRouteJson($path);
  277. }
  278. //替换url
  279. $this->repository->updateUrl(systemConfig('site_url'), $path);
  280. //压缩文件
  281. $fileService->addZip(
  282. $path . '/download',
  283. public_path() . 'static/download/' . $name . '.zip',
  284. $path . '/download'
  285. );
  286. $data['url'] = rtrim(systemConfig('site_url'), '/') . '/static/download/' . $name . '.zip';
  287. app()->make(CacheRepository::class)->save('routine_zip', $name);
  288. return app('json')->success($data);
  289. } catch (\Throwable $throwable) {
  290. Log::info($throwable->getMessage());
  291. return app('json')->fail('生成失败:' . $throwable->getMessage());
  292. }
  293. }
  294. public function getRoutineConfig()
  295. {
  296. $data['routine_name'] = systemConfig('routine_name');
  297. $data['routine_appId'] = systemConfig('routine_appId');
  298. $data['url'] = 'https://doc.crmeb.com/mer/mer2/4491';
  299. $data['site_url'] = rtrim(systemConfig('site_url'), '/') . '/pages/index/index';
  300. return app('json')->success($data);
  301. }
  302. /**
  303. * 根据配置分类的key获取配置项key
  304. * @param $key
  305. * @return \think\response\Json
  306. * @author Qinii
  307. */
  308. public function getConfig($key)
  309. {
  310. $data = $this->repository->getConfigKey($key);
  311. return app('json')->success($data);
  312. }
  313. public function getParams()
  314. {
  315. $data = $this->request->params(['user_type', 'config_classify_id', 'config_name', 'config_props', 'config_key', 'config_type', 'config_rule', 'required', 'info', 'sort', 'status','linked_status']);
  316. app()->make(ConfigValidate::class)->check($data);
  317. $configClassifyRepository = app()->make(ConfigClassifyRepository::class);
  318. if (!$configClassifyRepository->exists($data['config_classify_id']))
  319. throw new ValidateException('配置分类不已存在');
  320. if ($data['linked_status']){
  321. $linked_data = $this->request->param('linked_data',[]);
  322. if (!$linked_data) throw new ValidateException('请选择关联显示上级');
  323. $data['linked_id'] = $linked_data[0];
  324. $data['linked_value'] = $linked_data[1];
  325. }
  326. return $data;
  327. }
  328. }