ShareRedpack.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace addons\ShareRedpack\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "qimall_addons_share_redpack".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property string $name 海报名称
  12. * @property string|null $key_word 关键词
  13. * @property int|null $look_num 浏览次数
  14. * @property int $scan_code_num 扫码次数
  15. * @property string $poster_content 海报内容
  16. * @property string $status 状态【1启用 0禁用 -1删除】
  17. * @property string|null $share_url 分享图片
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class ShareRedpack extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return 'qimall_addons_share_redpack';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'name', 'poster_content'], 'required'],
  37. [['mall_id', 'look_num', 'scan_code_num', 'created_at', 'updated_at'], 'integer'],
  38. [['poster_content'], 'string'],
  39. [['name', 'key_word', 'status', 'share_url'], 'string', 'max' => 255],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => 'Mall ID',
  50. 'name' => 'Name',
  51. 'key_word' => 'Key Word',
  52. 'look_num' => 'Look Num',
  53. 'scan_code_num' => 'Scan Code Num',
  54. 'poster_content' => 'Poster Content',
  55. 'status' => 'Status',
  56. 'share_url' => 'Share Url',
  57. 'created_at' => 'Created At',
  58. 'updated_at' => 'Updated At',
  59. ];
  60. }
  61. /**
  62. * 保存数据
  63. * @Author lun
  64. * @DateTime 2021-09-16 14:16:24
  65. * @copyright: Copyright (c) 2020 广东七件事集团
  66. * @param array $params
  67. * @return object|boolean
  68. */
  69. public static function setData($mall_id, array $params){
  70. try{
  71. if(!empty($params['id'])){
  72. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status',StatusEnum::DELETE]]);
  73. if(empty($model)) throw new \Exception('海报不存在或已删除');
  74. }else{
  75. $model = new self();
  76. $model->loadDefaultValues();
  77. $model->mall_id = $mall_id;
  78. }
  79. // 将内容转为json字符串
  80. $params['poster_content'] = Json::encode($params['poster_content']);
  81. if(!$model->load($params,'') || !$model->save()) throw new \Exception($model->getErrorMessage());
  82. return $model;
  83. }catch(\Exception $e){
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. }