Menu.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\Wechat\common\models;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use addons\Wechat\common\enums\MenuTypeEnum;
  6. /**
  7. * This is the model class for table "{{%addon_wechat_menu}}".
  8. *
  9. * @property int $id 公众号id
  10. * @property int|null $merchant_id 商户id
  11. * @property int|null $store_id 店铺ID
  12. * @property int|null $menu_id 微信菜单id
  13. * @property int|null $type 1:默认菜单;2个性化菜单
  14. * @property string|null $title 标题
  15. * @property int|null $tag_id 标签id
  16. * @property int|null $client_platform_type 手机系统
  17. * @property string|null $menu_data 微信菜单
  18. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int|null $created_at 创建时间
  20. * @property int|null $updated_at 修改时间
  21. */
  22. class Menu extends \common\models\base\BaseModel
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addon_wechat_menu}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['title'], 'required'],
  38. [['merchant_id', 'store_id', 'menu_id', 'type', 'tag_id', 'client_platform_type', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['menu_data'], 'safe'],
  40. [['title'], 'string', 'max' => 30],
  41. [['title'], 'verifyEmpty'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => '公众号id',
  51. 'merchant_id' => '商户id',
  52. 'store_id' => '店铺ID',
  53. 'menu_id' => '微信菜单id',
  54. 'type' => '1:默认菜单;2个性化菜单',
  55. 'title' => '标题',
  56. 'tag_id' => '标签id',
  57. 'client_platform_type' => '手机系统',
  58. 'menu_data' => '微信菜单',
  59. 'status' => '状态[-1:删除;0:禁用;1启用]',
  60. 'created_at' => '创建时间',
  61. 'updated_at' => '修改时间',
  62. ];
  63. }
  64. /**
  65. * 验证是否全部为空
  66. *
  67. * @return bool|void
  68. */
  69. public function verifyEmpty()
  70. {
  71. if(
  72. $this->type == MenuTypeEnum::INDIVIDUATION &&
  73. empty($this->tag_id) &&
  74. empty($this->client_platform_type)
  75. ) {
  76. $this->addError('sex', '菜单显示对象至少要有一个匹配信息是不为空的');
  77. }
  78. }
  79. /**
  80. * 修改默认菜单状态
  81. *
  82. * @param bool $insert
  83. * @return bool
  84. */
  85. public function beforeSave($insert)
  86. {
  87. $this->status = StatusEnum::ENABLED;
  88. return parent::beforeSave($insert);
  89. }
  90. /**
  91. * 修改其他菜单状态
  92. *
  93. * @param bool $insert
  94. * @param array $changedAttributes
  95. */
  96. public function afterSave($insert, $changedAttributes)
  97. {
  98. if ($this->type == MenuTypeEnum::CUSTOM) {
  99. self::updateAll(['status' => StatusEnum::DISABLED],
  100. [
  101. 'and',
  102. ['not in', 'id', [$this->id]],
  103. ['type' => MenuTypeEnum::CUSTOM],
  104. ['merchant_id' => Yii::$app->services->merchant->getNotNullId()]
  105. ]);
  106. }
  107. parent::afterSave($insert, $changedAttributes);
  108. }
  109. }
粤ICP备19079148号