Qrcode.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\Wechat\common\models;
  3. use Yii;
  4. use common\behaviors\MerchantStoreBehavior;
  5. use common\models\base\BaseModel;
  6. use addons\Wechat\common\enums\QrcodeModelTypeEnum;
  7. /**
  8. * This is the model class for table "{{%addon_wechat_qrcode}}".
  9. *
  10. * @property int $id
  11. * @property int|null $merchant_id 商户ID
  12. * @property int|null $store_id 店铺ID
  13. * @property string|null $name 场景名称
  14. * @property string|null $keyword 关联关键字
  15. * @property int|null $scene_id 场景ID
  16. * @property string|null $scene_str 场景值
  17. * @property int|null $model_type 类型
  18. * @property string|null $ticket ticket
  19. * @property int|null $expire_seconds 有效期
  20. * @property int|null $scan_num 扫描次数
  21. * @property string|null $type 二维码类型
  22. * @property string|null $url url
  23. * @property int|null $end_time 结束时间
  24. * @property int|null $is_addon 是否插件
  25. * @property string|null $addon_name 插件名称
  26. * @property string|null $extend 扩展
  27. * @property int|null $status 状态(-1:已删除,0:禁用,1:正常)
  28. * @property int|null $created_at 创建时间
  29. * @property int|null $updated_at 修改时间
  30. */
  31. class Qrcode extends BaseModel
  32. {
  33. use MerchantStoreBehavior;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%addon_wechat_qrcode}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['name', 'model_type'], 'required'],
  48. [
  49. [
  50. 'merchant_id',
  51. 'store_id',
  52. 'scene_id',
  53. 'model_type',
  54. 'expire_seconds',
  55. 'scan_num',
  56. 'end_time',
  57. 'is_addon',
  58. 'status',
  59. 'created_at',
  60. 'updated_at',
  61. ],
  62. 'integer',
  63. ],
  64. [['name'], 'string', 'max' => 50],
  65. [['keyword'], 'string', 'max' => 100],
  66. [['scene_str'], 'string', 'max' => 64],
  67. [['ticket', 'addon_name'], 'string', 'max' => 200],
  68. [['type'], 'string', 'max' => 10],
  69. [['url'], 'string', 'max' => 80],
  70. [['extend'], 'safe'],
  71. ];
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function attributeLabels()
  77. {
  78. return [
  79. 'id' => 'ID',
  80. 'merchant_id' => '商户ID',
  81. 'store_id' => '店铺ID',
  82. 'name' => '场景名称',
  83. 'keyword' => '关联关键字',
  84. 'scene_id' => '场景ID',
  85. 'scene_str' => '场景值',
  86. 'model_type' => '类型',
  87. 'ticket' => 'ticket',
  88. 'expire_seconds' => '有效期',
  89. 'scan_num' => '扫描次数',
  90. 'type' => '二维码类型',
  91. 'url' => 'url',
  92. 'end_time' => '结束时间',
  93. 'is_addon' => '是否插件',
  94. 'addon_name' => '插件名称',
  95. 'extend' => '扩展',
  96. 'status' => '状态',
  97. 'created_at' => '创建时间',
  98. 'updated_at' => '修改时间',
  99. ];
  100. }
  101. /**
  102. * 验证提交的类别
  103. */
  104. public function verifyModel()
  105. {
  106. if ($this->isNewRecord) {
  107. // 临时
  108. if ($this->model == QrcodeModelTypeEnum::TEM) {
  109. empty($this->expire_seconds) && $this->addError('expire_seconds', '临时二维码过期时间必填');
  110. } else {
  111. !$this->scene_str && $this->addError('scene_str', '永久二维码场景字符串必填');
  112. if (self::find()->where(['scene_str' => $this->scene_str, 'merchant_id' => $this->merchant_id])->one()) {
  113. $this->addError('scene_str', '场景值已经存在');
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * @param bool $insert
  120. * @return bool
  121. */
  122. public function beforeSave($insert)
  123. {
  124. if ($this->isNewRecord) {
  125. $this->end_time = time() + (int) $this->expire_seconds;
  126. $this->addon_name = Yii::$app->params['addon']['name'] ?? '';
  127. !empty($this->addon_name) && $this->is_addon = 1;
  128. }
  129. return parent::beforeSave($insert);
  130. }
  131. }
粤ICP备19079148号