ConfigRepository.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\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\store\product\ProductRepository;
  16. use crmeb\jobs\CheckProductExtensionJob;
  17. use FormBuilder\Exception\FormBuilderException;
  18. use FormBuilder\Factory\Elm;
  19. use FormBuilder\Form;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\facade\Db;
  24. use think\facade\Queue;
  25. use think\facade\Route;
  26. /**
  27. * Class ConfigRepository
  28. * @package crmeb\repositories\system\config
  29. * @mixin SystemConfigDao
  30. */
  31. class ConfigRepository extends BaseRepository
  32. {
  33. const TYPES = ['input' => '文本框', 'number' => '数字框', 'textarea' => '多行文本框', 'radio' => '单选框', 'checkbox' => '多选框', 'select' => '下拉框', 'file' => '文件上传', 'image' => '图片上传', 'color' => '颜色选择框'];
  34. /**
  35. * ConfigRepository constructor.
  36. * @param SystemConfigDao $dao
  37. */
  38. public function __construct(SystemConfigDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * @param int $merId
  44. * @param SystemConfigClassify $configClassify
  45. * @param array $configs
  46. * @param array $formData
  47. * @return Form
  48. * @throws FormBuilderException
  49. * @author xaboy
  50. * @day 2020-04-23
  51. */
  52. public function formRule(int $merId, SystemConfigClassify $configClassify, array $configs, array $formData = [])
  53. {
  54. $components = $this->getRule($configs, $merId);
  55. $form = Elm::createForm(Route::buildUrl($merId ? 'merchantConfigSave' : 'configSave', ['key' => $configClassify->classify_key])->build(), $components);
  56. return $form->setTitle($configClassify->classify_name)->formData($formData);
  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. if ($config['config_type'] == 'image')
  70. $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]);
  71. else if ($config['config_type'] == 'file') {
  72. $component = Elm::uploadFile($config['config_key'], $config['config_name'], Route::buildUrl('configUpload', ['field' => 'file'])->build())->headers(['X-Token' => request()->token()]);
  73. } else if (in_array($config['config_type'], ['select', 'checkbox', 'radio'])) {
  74. $options = array_map(function ($val) {
  75. [$value, $label] = explode(':', $val, 2);
  76. return compact('value', 'label');
  77. }, explode("\n", $config['config_rule']));
  78. $component = Elm::{$config['config_type']}($config['config_key'], $config['config_name'])->options($options);
  79. } else
  80. $component = Elm::{$config['config_type']}($config['config_key'], $config['config_name']);
  81. if ($config['required']) $component->required();
  82. $component->info($config['info']);
  83. return $component;
  84. }
  85. /**
  86. * @param int $id
  87. * @param int $status
  88. * @return int
  89. * @throws DbException
  90. * @author xaboy
  91. * @day 2020-03-31
  92. */
  93. public function switchStatus(int $id, int $status)
  94. {
  95. return $this->dao->update($id, compact('status'));
  96. }
  97. /**
  98. * @param SystemConfigClassify $configClassify
  99. * @param int $merId
  100. * @return Form
  101. * @throws DataNotFoundException
  102. * @throws DbException
  103. * @throws FormBuilderException
  104. * @throws ModelNotFoundException
  105. * @author xaboy
  106. * @day 2020-04-22
  107. */
  108. public function cidByFormRule(SystemConfigClassify $configClassify, int $merId)
  109. {
  110. $config = $this->dao->cidByConfig($configClassify->config_classify_id, $merId == 0 ? 0 : 1);
  111. $keys = $config->column('config_key');
  112. return $this->formRule($merId, $configClassify, $config->toArray(), app()->make(ConfigValueRepository::class)->more($keys, $merId));
  113. }
  114. /**
  115. * @param int|null $id
  116. * @param array $formData
  117. * @return Form
  118. * @throws FormBuilderException
  119. * @author xaboy
  120. * @day 2020-03-31
  121. */
  122. public function form(?int $id = null, array $formData = []): Form
  123. {
  124. $form = Elm::createForm(is_null($id) ? Route::buildUrl('configSettingCreate')->build() : Route::buildUrl('configSettingUpdate', ['id' => $id])->build());
  125. $form->setRule([
  126. Elm::cascader('config_classify_id', '上级分类')->options(function () {
  127. $configClassifyRepository = app()->make(ConfigClassifyRepository::class);
  128. return array_merge([['value' => 0, 'label' => '请选择']], $configClassifyRepository->options());
  129. })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  130. Elm::select('user_type', '后台类型', 0)->options([
  131. ['label' => '总后台配置', 'value' => 0],
  132. ['label' => '商户后台配置', 'value' => 1],
  133. ])->requiredNum(),
  134. Elm::input('config_name', '配置名称')->required(),
  135. Elm::input('config_key', '配置key')->required(),
  136. Elm::input('info', '说明'),
  137. Elm::select('config_type', '配置类型')->options(function () {
  138. $options = [];
  139. foreach (self::TYPES as $value => $label) {
  140. $options[] = compact('value', 'label');
  141. }
  142. return $options;
  143. })->required(),
  144. Elm::textarea('config_rule', '规则'),
  145. Elm::number('sort', '排序', 0),
  146. Elm::switches('required', '必填', 0)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  147. Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  148. ]);
  149. return $form->setTitle(is_null($id) ? '添加配置' : '编辑配置')->formData($formData);
  150. }
  151. /**
  152. * @param int $id
  153. * @return Form
  154. * @throws DataNotFoundException
  155. * @throws DbException
  156. * @throws FormBuilderException
  157. * @throws ModelNotFoundException
  158. * @author xaboy
  159. * @day 2020-03-31
  160. */
  161. public function updateForm(int $id)
  162. {
  163. return $this->form($id, $this->dao->get($id)->toArray());
  164. }
  165. /**
  166. * @param array $where
  167. * @param int $page
  168. * @param int $limit
  169. * @return array
  170. * @throws DataNotFoundException
  171. * @throws DbException
  172. * @throws ModelNotFoundException
  173. * @author xaboy
  174. * @day 2020-03-31
  175. */
  176. public function lst(array $where, int $page, int $limit)
  177. {
  178. $query = $this->dao->search($where);
  179. $count = $query->count();
  180. $list = $query->page($page, $limit)->withAttr('typeName', function ($value, $data) {
  181. return self::TYPES[$data['config_type']];
  182. })->hidden(['config_classify_id'])->append(['typeName'])->select();
  183. return compact('count', 'list');
  184. }
  185. public function uploadForm()
  186. {
  187. $config = $this->getWhere(['config_key' => 'upload_type']);
  188. $rule = $this->getComponent($config, 0)->value(systemConfig('upload_type'));
  189. $make = app()->make(ConfigClassifyRepository::class);
  190. $rule->control([
  191. [
  192. 'value' => '2',
  193. 'rule' => $this->cidByFormRule($make->keyByData('qiniuyun'), 0)->formRule()
  194. ],
  195. [
  196. 'value' => '3',
  197. 'rule' => $this->cidByFormRule($make->keyByData('aliyun_oss'), 0)->formRule()
  198. ],
  199. [
  200. 'value' => '4',
  201. 'rule' => $this->cidByFormRule($make->keyByData('tengxun'), 0)->formRule()
  202. ],
  203. ]);
  204. return Elm::createForm(Route::buildUrl('systemSaveUploadConfig')->build(), [$rule])->setTitle('上传配置');
  205. }
  206. public function saveUpload($data)
  207. {
  208. $configValueRepository = app()->make(ConfigValueRepository::class);
  209. $uploadType = $data['upload_type'] ?? '1';
  210. $key = '';
  211. if ($uploadType == 2) {
  212. $key = 'qiniuyun';
  213. } else if ($uploadType == 3) {
  214. $key = 'aliyun_oss';
  215. } else if ($uploadType == 4) {
  216. $key = 'tengxun';
  217. }
  218. Db::transaction(function () use ($data, $key, $uploadType, $configValueRepository) {
  219. $configValueRepository->setFormData([
  220. 'upload_type' => $uploadType
  221. ], 0);
  222. if ($key) {
  223. $make = app()->make(ConfigClassifyRepository::class);
  224. if (!($cid = $make->keyById($key))) return app('json')->fail('保存失败');
  225. $configValueRepository->save($cid, $data, 0);
  226. }
  227. });
  228. }
  229. }