RuleKeyword.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/6/6
  5. * Time: 下午9:09
  6. */
  7. namespace app\common\modules\wechat\models;
  8. use app\common\models\BaseModel;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. class RuleKeyword extends BaseModel
  11. {
  12. //public $table = 'rule_keyword';
  13. //public $timestamps = false;
  14. public $table = 'yz_wechat_rule_keyword';
  15. protected $guarded = [''];
  16. use SoftDeletes;
  17. // module字段从微擎兼容下来,新框架中它的意义在于,海报插件使用了该字段,仅仅是为了兼容微擎
  18. // 由于海报使用了module字段,海报会检查module字段是否为yun_shop,才进行处理微信消息
  19. // 沿用这个想法,则公众号插件也对该字段赋值为wechat,以此判断是否公众号插件进行处理
  20. // 为兼容海报插件,该字段在新框架也是需要的
  21. // 目前只有两种方式对该字段赋值,1.海报插件对其赋值为yun_shop 2.公众号插件对其赋值为wechat
  22. protected static $module = 'yun_shop';//海报需要的字段,海报插件进行处理
  23. /**
  24. * 字段规则
  25. *
  26. * @return array
  27. */
  28. public function rules()
  29. {
  30. return [
  31. 'rid' => 'required|numeric',
  32. 'uniacid' => 'required|numeric',
  33. 'module' => 'required',
  34. 'content' => 'required',
  35. 'type' => 'numeric|required',
  36. 'displayorder' => 'numeric',
  37. 'status' => 'numeric',
  38. ];
  39. }
  40. /**
  41. * 定义字段名
  42. *
  43. * @return array
  44. */
  45. public function atributeNames()
  46. {
  47. return [
  48. 'rid' => '规则id',
  49. 'uniacid' => '公众号id',
  50. 'module' => '模块',
  51. 'content' => '关键字内容',
  52. 'type' => '触发类型',
  53. 'displayorder' => '回复优先级',
  54. 'status' => '是否开启',
  55. ];
  56. }
  57. public static function destroyKeywordByRuleId($roleId)
  58. {
  59. return static::uniacid()
  60. ->where('rid', $roleId)
  61. ->where('module', static::$module)
  62. ->delete();
  63. }
  64. public static function updateKeywordByRoleId($roleId, $keyword)
  65. {
  66. return static::uniacid()
  67. ->where('rid', $roleId)
  68. ->where('module', static::$module)
  69. ->update(['content' => trim($keyword)]);
  70. }
  71. public function hasOneRule()
  72. {
  73. return $this->hasOne(Rule::class,'id','rid')->select('id','name');
  74. }
  75. // 通过关键字获取规则
  76. public static function getRuleKeywordByKeywords($keywords)
  77. {
  78. // 先找精准触发
  79. $accurate = static::uniacid()->where('status','=',1)
  80. ->where('content','=',$keywords)
  81. ->where('type','=',1)
  82. ->orderBy('displayorder','desc')
  83. ->first();
  84. // 再找模糊查询,正则匹配先不考虑
  85. if (empty($accurate)) {
  86. return static::uniacid()->where('status','=',1)
  87. ->where('content','like',$keywords.'%')
  88. ->where('type','!=',1)
  89. ->orderBy('displayorder','desc')
  90. ->first();
  91. } else {
  92. return $accurate;
  93. }
  94. }
  95. public static function hasKeyword($keyword)
  96. {
  97. $id = self::uniacid()->where('module', static::$module)->where('content', $keyword)->value('id');
  98. return empty($id) ? false : $id;
  99. }
  100. public static function delKeyword($keyword)
  101. {
  102. return self::uniacid()
  103. ->where('module', static::$module)
  104. ->where('content', $keyword)
  105. ->delete();
  106. }
  107. }