Service.php 3.8 KB

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