ConfigRepository.php 9.0 KB

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