StoreConfigServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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\services\store;
  12. use app\dao\store\StoreConfigDao;
  13. use app\services\BaseServices;
  14. use app\services\system\config\SystemConfigServices;
  15. use crmeb\form\Build;
  16. use crmeb\services\SystemConfigService;
  17. use think\exception\ValidateException;
  18. /**
  19. * Class StoreConfigServices
  20. * @package app\services\store
  21. * @mixin StoreConfigDao
  22. */
  23. class StoreConfigServices extends BaseServices
  24. {
  25. /**
  26. * 表单数据切割符号
  27. * @var string
  28. */
  29. protected $cuttingStr = '=>';
  30. //打印机配置
  31. const PRINTER_KEY = [
  32. 'store_printing_timing', 'store_terminal_number', 'store_printing_client_id',
  33. 'store_printing_api_key', 'store_develop_id', 'store_pay_success_printing_switch',
  34. 'store_print_type', 'store_fey_user', 'store_fey_ukey', 'store_fey_sn',
  35. ];
  36. //快递发货配置
  37. const EXPRESS_KEY = [
  38. 'store_config_export_id', 'store_config_export_temp_id', 'store_config_export_to_name',
  39. 'store_config_export_to_tel', 'store_config_export_to_address', 'store_config_export_siid', 'store_config_export_open'
  40. ];
  41. //桌码配置
  42. const TABLE_CODE = [
  43. 'store_code_switch', 'store_checkout_method', 'store_number_diners_window'
  44. ];
  45. const CONFIG_TYPE = [
  46. 'store_printing_deploy' => self::PRINTER_KEY,
  47. 'store_electronic_sheet' => self::EXPRESS_KEY,
  48. 'store_table_code' => self::TABLE_CODE
  49. ];
  50. /**
  51. * StoreConfigServices constructor.
  52. * @param StoreConfigDao $dao
  53. */
  54. public function __construct(StoreConfigDao $dao)
  55. {
  56. $this->dao = $dao;
  57. }
  58. /**
  59. * 保存或者更新门店配置
  60. * @param array $data
  61. * @param int $storeId
  62. */
  63. public function saveConfig(array $data, int $type = 1, int $relation_id = 0)
  64. {
  65. $config = [];
  66. foreach ($data as $key => $value) {
  67. if ($this->dao->count(['key_name' => $key, 'type' => $type, 'relation_id' => $relation_id])) {
  68. $this->dao->update(['key_name' => $key, 'type' => $type, 'relation_id' => $relation_id], ['value' => json_encode($value)]);
  69. } else {
  70. $config[] = [
  71. 'key_name' => $key,
  72. 'type' => $type,
  73. 'relation_id' => $relation_id,
  74. 'value' => json_encode($value)
  75. ];
  76. }
  77. }
  78. if ($config) {
  79. $this->dao->saveAll($config);
  80. }
  81. }
  82. /**
  83. * 获取配置
  84. * @param string $key
  85. * @param int $type
  86. * @param int $relation_id
  87. * @param $default
  88. * @return mixed|null
  89. */
  90. public function getConfig(string $key, int $type = 0, int $relation_id = 0, $default = null)
  91. {
  92. $value = $this->dao->value(['key_name' => $key, 'type' => $type, 'relation_id' => $relation_id], 'value');
  93. return is_null($value) ? $default : json_decode($value, true);
  94. }
  95. /**
  96. * 获取门店配置
  97. * @param int $storeId
  98. * @param array $keys
  99. */
  100. public function getConfigAll(array $keys, int $type = 0, int $relation_id = 0)
  101. {
  102. $confing = $this->dao->searchs($keys, $type, $relation_id)->column('value', 'key_name');
  103. return array_map(function ($item) {
  104. return json_decode($item, true);
  105. }, $confing);
  106. }
  107. public function getOptions(string $parameter)
  108. {
  109. $parameter = explode("\n", $parameter);
  110. $options = [];
  111. foreach ($parameter as $v) {
  112. if (strstr($v, $this->cuttingStr) !== false) {
  113. $pdata = explode($this->cuttingStr, $v);
  114. $options[] = ['label' => $pdata[1], 'value' => (int)$pdata[0]];
  115. }
  116. }
  117. return $options;
  118. }
  119. /**
  120. * @param array $configName
  121. * @param int $type
  122. * @param int $relation_id
  123. * @param int $group
  124. * @return array
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function getConfigAllField(array $configName = [], int $type = 0, int $relation_id = 0, int $group = 0)
  130. {
  131. /** @var SystemConfigServices $systemConfigServices */
  132. $systemConfigServices = app()->make(SystemConfigServices::class);
  133. $list = $systemConfigServices->getConfigAllListByWhere(['menu_name' => $configName], $type, $relation_id, ['menu_name', 'info', 'type', 'value', 'desc', 'parameter']);
  134. if ($list) {
  135. foreach ($list as &$item) {
  136. if ($relation_id != 0) {
  137. $item['value'] = $item['store_value'] ?? '';
  138. }
  139. $item['value'] = json_decode($item['value'], true);
  140. }
  141. $list = array_combine(array_column($list, 'menu_name'), $list);
  142. }
  143. $value = [];
  144. foreach ($configName as $key) {
  145. if ($group) {
  146. $value[$key] = $list[$key]['value'] ?? '';
  147. } else {
  148. $value[$key] = $list[$key] ?? ['info' => '', 'type' => 'text', 'value' => '', 'desc' => '', 'parameter' => ''];
  149. }
  150. }
  151. return $value;
  152. }
  153. /**
  154. * 获取表单
  155. * @param string $name
  156. * @param int $type
  157. * @param int $relation_id
  158. * @return array
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function getFormBuildRule(string $name, int $type = 0, int $relation_id = 0)
  164. {
  165. switch ($name) {
  166. case 'third'://第三方
  167. $data = $this->thirdPartyFormBuild($type, $relation_id);
  168. break;
  169. default:
  170. throw new ValidateException('类型错误');
  171. }
  172. return $data;
  173. }
  174. /**
  175. * 第三方配置
  176. * @return array
  177. */
  178. public function thirdPartyFormBuild(int $type = 0, int $relation_id = 0)
  179. {
  180. $build = new Build();
  181. $build->url('system/config');
  182. if ($type == 1) {
  183. $data = $this->getConfigAllField([
  184. 'store_pay_success_printing_switch', 'store_develop_id', 'store_printing_api_key', 'store_printing_client_id', 'store_terminal_number',
  185. 'store_print_type', 'store_fey_user', 'store_fey_ukey', 'store_fey_sn',
  186. 'store_printing_timing',
  187. 'store_config_export_open', 'store_config_export_siid', 'store_config_export_to_name', 'store_config_export_to_tel', 'store_config_export_to_address',
  188. ], $type, $relation_id);
  189. $build->rule([
  190. Build::tabs()->option('小票打印配置', [
  191. Build::switch('store_pay_success_printing_switch', $data['store_pay_success_printing_switch']['info'], (int)($data['store_pay_success_printing_switch']['value'] ?? 0))->control(1, [
  192. Build::radio('store_printing_timing', $data['store_printing_timing']['info'], $data['store_printing_timing']['value'])->options($this->getOptions($data['store_printing_timing']['parameter']))->info($data['store_printing_timing']['desc']),
  193. Build::radio('store_print_type', $data['store_print_type']['info'], in_array($data['store_print_type']['value'], [1, 2]) ? $data['store_print_type']['value'] : 1)->control(1, [
  194. Build::input('store_develop_id', $data['store_develop_id']['info'], $data['store_develop_id']['value'])->info($data['store_develop_id']['desc']),
  195. Build::input('store_printing_api_key', $data['store_printing_api_key']['info'], $data['store_printing_api_key']['value'])->info($data['store_printing_api_key']['desc']),
  196. Build::input('store_printing_client_id', $data['store_printing_client_id']['info'], $data['store_printing_client_id']['value'])->info($data['store_printing_client_id']['desc']),
  197. Build::input('store_terminal_number', $data['store_terminal_number']['info'], $data['store_terminal_number']['value'])->info($data['store_terminal_number']['desc']),
  198. ])->control(2, [
  199. Build::input('store_fey_user', $data['store_fey_user']['info'], $data['store_fey_user']['value'])->info($data['store_fey_user']['desc']),
  200. Build::input('store_fey_ukey', $data['store_fey_ukey']['info'], $data['store_fey_ukey']['value'])->info($data['store_fey_ukey']['desc']),
  201. Build::input('store_fey_sn', $data['store_fey_sn']['info'], $data['store_fey_sn']['value'])->info($data['store_fey_sn']['desc'])
  202. ])->options($this->getOptions($data['store_print_type']['parameter']))->info($data['store_print_type']['desc'])
  203. ])->trueValue('打开', 1)->falseValue('关闭', 0),
  204. ])->option('电子面单', [
  205. Build::radio('store_config_export_open', $data['store_config_export_open']['info'], (int)($data['store_config_export_open']['value'] ?? 0))->control(1, [
  206. Build::input('store_config_export_to_name', $data['store_config_export_to_name']['info'], $data['store_config_export_to_name']['value'])->info($data['store_config_export_to_name']['desc']),
  207. Build::input('store_config_export_to_tel', $data['store_config_export_to_tel']['info'], $data['store_config_export_to_tel']['value'])->info($data['store_config_export_to_tel']['desc']),
  208. Build::input('store_config_export_to_address', $data['store_config_export_to_address']['info'], $data['store_config_export_to_address']['value'])->info($data['store_config_export_to_address']['desc']),
  209. Build::input('store_config_export_siid', $data['store_config_export_siid']['info'], $data['store_config_export_siid']['value'])->info($data['store_config_export_siid']['desc']),
  210. ])->options($this->getOptions($data['store_config_export_open']['parameter']))->info($data['store_config_export_open']['desc'])
  211. ])
  212. ]);
  213. } elseif ($type = 2) {
  214. $data = $this->getConfigAllField([
  215. 'store_pay_success_printing_switch', 'store_develop_id', 'store_printing_api_key', 'store_printing_client_id', 'store_terminal_number',
  216. 'store_print_type', 'store_fey_user', 'store_fey_ukey', 'store_fey_sn',
  217. ], $type, $relation_id);
  218. $build->rule([
  219. Build::tabs()->option('小票打印配置', [
  220. Build::switch('store_pay_success_printing_switch', $data['store_pay_success_printing_switch']['info'], (int)($data['store_pay_success_printing_switch']['value'] ?? 0))->control(1, [
  221. Build::radio('store_print_type', $data['store_print_type']['info'], in_array($data['store_print_type']['value'], [1, 2]) ? $data['store_print_type']['value'] : 1)->control(1, [
  222. Build::input('store_develop_id', $data['store_develop_id']['info'], $data['store_develop_id']['value'])->info($data['store_develop_id']['desc']),
  223. Build::input('store_printing_api_key', $data['store_printing_api_key']['info'], $data['store_printing_api_key']['value'])->info($data['store_printing_api_key']['desc']),
  224. Build::input('store_printing_client_id', $data['store_printing_client_id']['info'], $data['store_printing_client_id']['value'])->info($data['store_printing_client_id']['desc']),
  225. Build::input('store_terminal_number', $data['store_terminal_number']['info'], $data['store_terminal_number']['value'])->info($data['store_terminal_number']['desc']),
  226. ])->control(2, [
  227. Build::input('store_fey_user', $data['store_fey_user']['info'], $data['store_fey_user']['value'])->info($data['store_fey_user']['desc']),
  228. Build::input('store_fey_ukey', $data['store_fey_ukey']['info'], $data['store_fey_ukey']['value'])->info($data['store_fey_ukey']['desc']),
  229. Build::input('store_fey_sn', $data['store_fey_sn']['info'], $data['store_fey_sn']['value'])->info($data['store_fey_sn']['desc'])
  230. ])->options($this->getOptions($data['store_print_type']['parameter']))->info($data['store_print_type']['desc'])
  231. ])->trueValue('打开', 1)->falseValue('关闭', 0),
  232. ])
  233. ]);
  234. }
  235. return $build->toArray();
  236. }
  237. }