Service.php 3.2 KB

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