ConfigRepository.php 9.1 KB

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