ServeServices.php 5.5 KB

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