Material.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace common\models\wechat;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%wechat_material}}".
  7. *
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property string $wechat_app_id 公众号appid
  12. * @property string $media_id media_id
  13. * @property string|null $url url
  14. * @property string $name
  15. * @property string|null $media_type 类型:image video voice text
  16. * @property string|null $material_desc 描述
  17. * @property int $status 状态【1启用 0禁用 -1删除】
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class Material extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%wechat_material}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'wechat_app_id', 'media_id', 'name'], 'required'],
  37. [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['wechat_app_id'], 'string', 'max' => 64],
  39. [['media_id'], 'string', 'max' => 128],
  40. [['url', 'material_desc'], 'string', 'max' => 255],
  41. [['name'], 'string', 'max' => 45],
  42. [['media_type'], 'string', 'max' => 12],
  43. [['material_desc'], 'safe'],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => 'Mall ID',
  54. 'wechat_app_id' => '公众号appid',
  55. 'media_id' => 'media_id',
  56. 'url' => 'url',
  57. 'name' => 'Name',
  58. 'media_type' => '类型:image video voice text',
  59. 'material_desc' => '描述',
  60. 'status' => '状态【1启用 0禁用 -1删除】',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. ];
  64. }
  65. /**
  66. * 保存数据
  67. * @Author lun
  68. * @DateTime 2021-09-17 14:16:24
  69. * @copyright: Copyright (c) 2020 广东七件事集团
  70. * @param array $params
  71. * @return object|boolean
  72. */
  73. public static function setData(int $mall_id, array $params){
  74. try{
  75. if(!empty($params['id'])){
  76. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status',StatusEnum::DELETE]]);
  77. if(empty($model)) throw new \Exception('素材不存在或已删除');
  78. }else{
  79. $model = new self();
  80. $model->loadDefaultValues();
  81. $model->mall_id = $mall_id;
  82. }
  83. if(!$model->load($params,'') || !$model->save()){
  84. throw new \Exception($model->getErrorMessage());
  85. }
  86. return $model;
  87. }catch(\Exception $e){
  88. self::$static_error = $e->getMessage();
  89. return false;
  90. }
  91. }
  92. }