BusinessCardMaterials.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_business_card_material".
  9. *
  10. * @property int $id 名片素材
  11. * @property int|null $mall_id 商城ID
  12. * @property int|null $user_id 用户ID
  13. * @property string|null $title 标题
  14. * @property string|null $intro 简介
  15. * @property string|null $ext_type 素材格式:pdf=PDF,video=视频,pic_text=图文,link=链接
  16. * @property int|null $cate_id 分类ID
  17. * @property int|null $is_show 是否展示
  18. * @property string|null $share_title 分享标题
  19. * @property string|null $share_desc 分享描述
  20. * @property string|null $cover_img 封面图片
  21. * @property string|null $material_link 素材链接,如link链接、video链接和pdf文件链接
  22. * @property string|null $material_cont 素材内容,例如图文
  23. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int|null $created_at 创建时间
  25. * @property int|null $updated_at 更新时间
  26. */
  27. class BusinessCardMaterials extends \common\models\base\BaseActiveRecord
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return 'qimall_addons_business_card_material';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'user_id', 'cate_id', 'is_show', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['material_cont'], 'string'],
  44. [['title', 'share_title'], 'string', 'max' => 128],
  45. [['intro', 'share_desc', 'cover_img', 'material_link'], 'string', 'max' => 255],
  46. [['ext_type'], 'string', 'max' => 16],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => 'Mall ID',
  57. 'user_id' => 'User ID',
  58. 'title' => 'Title',
  59. 'intro' => 'Intro',
  60. 'ext_type' => 'Ext Type',
  61. 'cate_id' => 'Cate ID',
  62. 'is_show' => 'Is Show',
  63. 'share_title' => 'Share Title',
  64. 'share_desc' => 'Share Desc',
  65. 'cover_img' => 'Cover Img',
  66. 'material_link' => 'Material Link',
  67. 'material_cont' => 'Material Cont',
  68. 'status' => 'Status',
  69. 'created_at' => 'Created At',
  70. 'updated_at' => 'Updated At',
  71. ];
  72. }
  73. // 分类
  74. public function getCate()
  75. {
  76. return $this->hasOne(BusinessCardMaterialsCate::class, ['id' => 'cate_id'])->select('id,name');
  77. }
  78. public function getUser()
  79. {
  80. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  81. }
  82. /**
  83. * 数据设置
  84. */
  85. public static function setData(array $params)
  86. {
  87. try{
  88. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  89. if (isset($params['id']) && !empty($params['id'])) {
  90. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  91. if(empty($model)) throw new Exception('素材不存在或已删除');
  92. } else {
  93. $model = new self();
  94. $model->loadDefaultValues();
  95. $model->mall_id = $params['mall_id'];
  96. }
  97. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  98. return $model;
  99. }catch(Exception $e){
  100. self::setStaticError($e->getMessage());
  101. return false;
  102. }
  103. }
  104. }