MessageTemp.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/11/9
  5. * Time: 下午3:09
  6. */
  7. namespace app\common\models\notice;
  8. use app\common\models\BaseModel;
  9. use app\common\scopes\UniacidScope;
  10. use Illuminate\Database\Eloquent\Builder;
  11. class MessageTemp extends BaseModel
  12. {
  13. public $table = 'yz_message_template';
  14. protected $guarded = [''];
  15. protected $fillable = [];
  16. public $timestamps = true;
  17. public static $template_id = null;
  18. public static function boot()
  19. {
  20. parent::boot();
  21. static::addGlobalScope(new UniacidScope);
  22. }
  23. protected $casts = [
  24. 'data' => 'json'
  25. ];
  26. public static function getList()
  27. {
  28. return self::select('id', 'title')->where('is_default',0)->get();
  29. }
  30. public static function getDefaultList()
  31. {
  32. return self::where('is_default',1)->pluck('id')->toArray();
  33. }
  34. public function getTempIdByNoticeType($notice_type)
  35. {
  36. return self::where('notice_type',$notice_type)->first();
  37. }
  38. public static function delTempDataByTempId($temp_id)
  39. {
  40. return self::where('template_id',$temp_id)->delete();
  41. }
  42. public static function getIsDefaultById($temp_id)
  43. {
  44. return self::whereId($temp_id)->where('is_default',1)->first();
  45. }
  46. public static function getTempById($temp_id)
  47. {
  48. return self::select()->whereId($temp_id);
  49. }
  50. public static function fetchTempList($kwd)
  51. {
  52. return self::select()->where('is_default',0)->likeTitle($kwd);
  53. }
  54. public function scopeLikeTitle($query, $kwd)
  55. {
  56. return $query->where('title', 'like', '%' . $kwd . '%');
  57. }
  58. public static function handleArray($data)
  59. {
  60. $data['uniacid'] = \YunShop::app()->uniacid;
  61. $data['data'] = [];
  62. foreach ($data['tp_kw'] as $key => $val )
  63. {
  64. $data['data'][] = [
  65. 'keywords' => $data['tp_kw'][$key],
  66. 'value' => $data['tp_value'][$key],
  67. 'color' => $data['tp_color'][$key]
  68. ];
  69. }
  70. return array_except($data, ['tp_kw', 'tp_value', 'tp_color']);
  71. }
  72. public static function getSendMsg($temp_id, $params)
  73. {
  74. if (!intval($temp_id)) {
  75. return false;
  76. }
  77. $temp = self::withoutGlobalScopes(['uniacid'])->whereId($temp_id)->first();
  78. if (!$temp) {
  79. return false;
  80. }
  81. self::$template_id = $temp->template_id;
  82. $msg = [
  83. 'first' => [
  84. 'value' => self::replaceTemplate($temp->first, $params),
  85. 'color' => $temp->first_color
  86. ],
  87. 'remark' => [
  88. 'value' => self::replaceTemplate($temp->remark, $params),
  89. 'color' => $temp->remark_color
  90. ]
  91. ];
  92. foreach ($temp->data as $row) {
  93. $msg[$row['keywords']] = [
  94. 'value' => self::replaceTemplate($row['value'], $params),
  95. 'color' => $row['color']
  96. ];
  97. }
  98. return $msg;
  99. }
  100. private static function replaceTemplate($str, $datas = array())
  101. {
  102. foreach ($datas as $row ) {
  103. $str = str_replace('[' . $row['name'] . ']', $row['value'], $str);
  104. }
  105. return $str;
  106. }
  107. }