ServeServices.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\serve;
  12. use app\services\BaseServices;
  13. use crmeb\services\copyproduct\CopyProduct;
  14. use crmeb\services\express\Express;
  15. use crmeb\services\FormBuilder;
  16. use crmeb\services\printer\Printer;
  17. use crmeb\services\serve\Serve;
  18. use crmeb\services\sms\Sms;
  19. use think\facade\Config;
  20. /**
  21. * 平台服务入口
  22. * Class ServeServices
  23. * @package crmeb\services
  24. */
  25. class ServeServices extends BaseServices
  26. {
  27. /**
  28. * @var FormBuilder
  29. */
  30. protected $builder;
  31. /**
  32. * SmsTemplateApplyServices constructor.
  33. * @param FormBuilder $builder
  34. */
  35. public function __construct(FormBuilder $builder)
  36. {
  37. $this->builder = $builder;
  38. }
  39. /**
  40. * 获取配置
  41. * @param array $config
  42. * @return array
  43. */
  44. public function getConfig(array $config = [])
  45. {
  46. return array_merge([
  47. 'account' => sys_config('sms_account'),
  48. 'secret' => sys_config('sms_token')
  49. ], $config);
  50. }
  51. /**
  52. * 根据类型获取短信发送配置
  53. * @param $type
  54. * @param array $configDefault
  55. * @return array
  56. */
  57. protected function getTypeConfig($type, array $configDefault = [])
  58. {
  59. if (!$type) {
  60. $type = Config::get('sms.default', '');
  61. }
  62. $config = Config::get('sms.stores.' . $type);
  63. foreach ($config as $key => &$item) {
  64. if (empty($item)) {
  65. $item = sys_config($key);
  66. }
  67. }
  68. if ($configDefault) {
  69. $config = array_merge($config, $configDefault);
  70. }
  71. return $config;
  72. }
  73. /**
  74. * 短信
  75. * @param string|null $type
  76. * @param array $config
  77. * @return Sms
  78. */
  79. public function sms(string $type = null, array $config = [])
  80. {
  81. return app()->make(Sms::class, [$type, $this->getTypeConfig($type, $config)]);
  82. }
  83. /**
  84. * 复制商品
  85. * @param string|null $type
  86. * @param array $config
  87. * @return CopyProduct
  88. */
  89. public function copy(string $type = null, array $config = [])
  90. {
  91. return app()->make(CopyProduct::class, [$type, $this->getConfig($config)]);
  92. }
  93. /**
  94. * 电子面单
  95. * @param array $config
  96. * @return Express
  97. */
  98. public function express(array $config = [])
  99. {
  100. return app()->make(Express::class, [$this->getConfig($config)]);
  101. }
  102. /**
  103. * 小票打印
  104. * @param array $config
  105. * @return Express
  106. */
  107. public function orderPrint(array $config = [])
  108. {
  109. return app()->make(Printer::class, [$this->getConfig($config)]);
  110. }
  111. /**
  112. * 用户
  113. * @param array $config
  114. * @return Serve
  115. */
  116. public function user(array $config = [])
  117. {
  118. return app()->make(Serve::class, [$this->getConfig($config)]);
  119. }
  120. /**
  121. * 获取短信模板
  122. * @param int $page
  123. * @param int $limit
  124. * @param int $type
  125. * @return array
  126. */
  127. public function getSmsTempsList(int $page, int $limit, int $type)
  128. {
  129. $list = $this->sms()->temps($page, $limit, $type);
  130. foreach ($list['data'] as &$item) {
  131. $item['templateid'] = $item['temp_id'];
  132. switch ((int)$item['temp_type']) {
  133. case 1:
  134. $item['type'] = '验证码';
  135. break;
  136. case 2:
  137. $item['type'] = '通知';
  138. break;
  139. case 30:
  140. $item['type'] = '营销短信';
  141. break;
  142. }
  143. }
  144. return $list;
  145. }
  146. /**
  147. * 创建短信模板表单
  148. * @return array
  149. * @throws \FormBuilder\Exception\FormBuilderException
  150. */
  151. public function createSmsTemplateForm()
  152. {
  153. $field = [
  154. $this->builder->input('title', '模板名称')->placeholder('模板名称,如:订单支付成功'),
  155. $this->builder->input('content', '模板内容')->type('textarea')->placeholder('模板内容,如:您购买的商品已支付成功,支付金额{$pay_price}元,订单号{$order_id},感谢您的光临!(注:模板内容不要添加短信签名)'),
  156. $this->builder->radio('type', '模板类型', 1)->options([['label' => '验证码', 'value' => 1], ['label' => '通知', 'value' => 2], ['label' => '营销', 'value' => 3]])
  157. ];
  158. return $field;
  159. }
  160. /**
  161. * 获取短信申请模板
  162. * @return array
  163. * @throws \FormBuilder\Exception\FormBuilderException
  164. */
  165. public function getSmsTemplateForm()
  166. {
  167. return create_form('申请短信模板', $this->createSmsTemplateForm(), $this->url('/notify/sms/temp'), 'POST');
  168. }
  169. }