Service.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\extra\basic;
  3. use app\model\system\SystemConfig;
  4. use Overtrue\EasySms\Strategies\OrderStrategy;
  5. use support\Request;
  6. use think\facade\Db;
  7. abstract class Service
  8. {
  9. /**
  10. * @var null
  11. */
  12. protected $mode = null;
  13. protected $dao;
  14. /**
  15. * 短信配置
  16. * @param int $type 1 系统配置 2 代理配置
  17. * @return array
  18. */
  19. protected function smsConfig(int $type = 1): array
  20. {
  21. $sms = (new SystemConfig)->where("type","sms")->column("value","name");
  22. return [
  23. "config" => [
  24. 'enable' => true,
  25. 'timeout' => 5.0,
  26. "default" => [
  27. "strategy" => OrderStrategy::class,
  28. "gateways" => [$sms['sms_type']]
  29. ],
  30. "gateways" => [
  31. 'errorlog' => [
  32. 'file' => runtime_path().'/tmp/easy-sms.log',
  33. ],
  34. 'aliyun' => [
  35. 'access_key_id' => $sms['AccessKeyId'],
  36. 'access_key_secret' => $sms['AccessKeySecret'],
  37. 'sign_name' => trim($sms['sign']),
  38. ],
  39. ]
  40. ],
  41. "template" => $sms['login']
  42. ];
  43. }
  44. /**
  45. * 默认排序筛选
  46. * @param array $param
  47. * @param string $prefix
  48. * @param array $filter
  49. * @return string[]
  50. */
  51. public function defaultSort(array $param = [],string $prefix = "",array $filter = []): array
  52. {
  53. if (!empty($prefix)) $prefix = $prefix.".";
  54. if (isset($param['order']) && $param['order'] == 'descending'){
  55. $orderBy = ["{$prefix}{$param['field']}" => "desc"];
  56. } else if (isset($param['order']) && $param['order'] == 'ascending'){
  57. $orderBy = ["{$prefix}{$param['field']}" => "asc"];
  58. } else {
  59. $orderBy = ["{$prefix}create_at" => "desc"];
  60. }
  61. return $orderBy;
  62. }
  63. /**
  64. * @param array $param 参数
  65. * @param array $filter 过滤器
  66. * @param string $prefix 链表前缀
  67. * @param string $join 链表表名
  68. * @param string $field 字段
  69. * @param string $forKey 链表首字母
  70. * @param string $localKey 被链表首字母
  71. */
  72. protected function searchVal(array $param = [],array $filter = [],string $prefix = "",string $join = "",string $field = "",string $forKey = "",string $localKey = "")
  73. {
  74. $orderBy = $this->defaultSort($param,$prefix);
  75. $commonFilter = [];
  76. // 起止时间
  77. if (!empty($param['create'])) {
  78. $times = between_time($param['create']);
  79. $start = date('Y-m-d',$times['start_time']);
  80. $end = date('Y-m-d',($times['end_time'] + 86400));
  81. $commonFilter[] = ['create_at', '>=', $start ];
  82. $commonFilter[] = ['create_at', '<', $end ];
  83. }
  84. $filter = array_merge($filter,$commonFilter);
  85. if (!empty($join)) {
  86. $this->mode = $this->mode->alias('a')->join("{$join} {$prefix}","a.{$forKey} = {$prefix}.{$localKey}")->field($field);
  87. }
  88. return $this->mode->order($orderBy)->where($filter);
  89. }
  90. /**
  91. * 事务执行
  92. */
  93. protected function transaction(callable $callback)
  94. {
  95. try {
  96. Db::startTrans();
  97. $result = $callback();
  98. Db::commit();
  99. return $result;
  100. } catch (\Exception $e) {
  101. Db::rollBack();
  102. throw $e;
  103. }
  104. }
  105. /**
  106. * @param $name
  107. * @param $arguments
  108. * @return mixed
  109. */
  110. public function __call($name, $arguments)
  111. {
  112. // TODO: Implement __call() method.
  113. return call_user_func_array([$this->dao, $name], $arguments);
  114. }
  115. /**
  116. * 获取分页配置
  117. * @param bool $isPage
  118. * @param bool $isRelieve
  119. * @return int[]
  120. */
  121. public function getPageValue()
  122. {
  123. $request = Request();
  124. $page = $request->get('page',0);
  125. $limit = $request->get('limit',0);
  126. return [(int)$page, (int)$limit];
  127. }
  128. }