Helper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace modules\sms\library;
  3. use ba\Random;
  4. use app\admin\model\sms\Variable;
  5. use think\db\exception\PDOException;
  6. use think\facade\Db;
  7. class Helper
  8. {
  9. public static function calcVar($id)
  10. {
  11. $varData = Variable::where('id', $id)->find()->toArray();
  12. if (!$varData) return '';
  13. if ($varData['value_source'] == 'literal') {
  14. return $varData['value'];
  15. } elseif ($varData['value_source'] == 'func') {
  16. $class = '\\' . $varData['namespace'] . '\\' . $varData['class'];
  17. if (!class_exists($class)) return '';
  18. if (!method_exists($class, $varData['func'])) return '';
  19. $handle = new $class();
  20. return $handle->{$varData['func']}($varData['param']);
  21. } elseif ($varData['value_source'] == 'sql') {
  22. $sql = str_replace('__PREFIX__', config('database.connections.mysql.prefix'), $varData['sql']);
  23. try {
  24. $res = Db::query($sql);
  25. } catch (PDOException $e) {
  26. // return $e->getMessage();
  27. return '';
  28. }
  29. if ($res) {
  30. if (is_array($res[0])) {
  31. foreach ($res[0] as $value) {
  32. return $value;
  33. }
  34. } else {
  35. return $res[0];
  36. }
  37. }
  38. }
  39. return '';
  40. }
  41. public static function numeric(int $len = 6)
  42. {
  43. return Random::build('numeric', $len);
  44. }
  45. public static function alnum(int $len = 6)
  46. {
  47. return Random::build('alnum', $len);
  48. }
  49. /**
  50. * 分析字符串中的自定义变量
  51. * @param string $content 要分析的字符串
  52. * @param string $variableIds 要分析的变量ID数组
  53. * @param array $tplVar 动态传递新的变量值
  54. * @return array 解析过变量的模板以及用到的变量数组
  55. */
  56. public static function analysisVariable(string $content, string $variableIds, array $tplVar = []): array
  57. {
  58. if (!$content && !$variableIds) {
  59. return [
  60. 'content' => '',
  61. 'variables' => [],
  62. ];
  63. }
  64. // 读取数据库中的模板变量
  65. $allVariable = []; // 全部变量
  66. $useVariable = []; // 使用到的变量
  67. $variableTmp = Db::name('sms_variable')->where('status', 1)->select();
  68. $variables = explode(',', $variableIds); // 要分析的变量数组
  69. foreach ($variableTmp as $value) {
  70. $varName = $value['name'];
  71. // 是需要分析的变量
  72. if (in_array($value['id'], $variables)) {
  73. $value['type'] = 'definition_now';// 标记为现在定义
  74. if (array_key_exists($varName, $tplVar)) {
  75. $value['value'] = $tplVar[$varName];
  76. } else {
  77. $value['value'] = self::calcVar($value['id']);
  78. }
  79. $useVariable[$varName] = $value['value'];
  80. $allVariable[$varName] = $value;
  81. continue;
  82. }
  83. $value['type'] = 'predefined';// 标记为预定义
  84. $allVariable[$varName] = $value;
  85. }
  86. unset($variableTmp);
  87. // 准备传递过来的模板变量
  88. foreach ($tplVar as $key => $value) {
  89. $allVariable[$key] = [
  90. 'type' => 'definition_now',// 标记为现在定义
  91. 'value' => $value,
  92. ];
  93. }
  94. $match = [];
  95. preg_match_all('/\${(.*?)}/', $content, $match);// 匹配到所有变量
  96. foreach ($match[1] as $key => $value) {
  97. if (array_key_exists($value, $allVariable)) {
  98. if ($allVariable[$value]['type'] == 'definition_now') {
  99. $content = str_replace($match[0][$key], $allVariable[$value]['value'], $content);
  100. $useVariable[$value] = $allVariable[$value]['value'];
  101. } else if ($allVariable[$value]['type'] == 'predefined') {
  102. $variableValue = self::calcVar($allVariable[$value]['id']);
  103. $content = str_replace($match[0][$key], $variableValue, $content);
  104. $useVariable[$value] = $variableValue;
  105. }
  106. }
  107. }
  108. return [
  109. 'content' => $content,
  110. 'variables' => $useVariable,
  111. ];
  112. }
  113. }