Config.php 12 KB

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