DiyPage.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace common\models\diy;
  3. use common\enums\DiyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\link\Link;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%diy_page}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property string $title 名称
  14. * @property string $mall_template_id 商城模板ID,对应qimall_diy_mall_template.id
  15. * @property string $template_id 商城模板ID,对应qimall_diy_template.id
  16. * @property int $type 页面类型 1:普通页面 2:首页 3:特殊页面(不可设为首页)
  17. * @property int $is_home_page 是否首页 0:否 1:是
  18. * @property string $template 页面模板
  19. * @property string $content 内容
  20. * @property int $expire_time 过期时间
  21. * @property int $is_show_bottom_nav 过期时间
  22. * @property string|null $cover 预览图片
  23. * @property int|null $sort 排序,越小排越前
  24. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  25. * @property int|null $created_at 添加时间
  26. * @property int $updated_at
  27. */
  28. class DiyPage extends \common\models\base\BaseActiveRecord
  29. {
  30. const PAGE_PATH_DIY = '/pages/index/index';
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%diy_page}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'title'], 'required'],
  45. [['mall_id', 'mall_template_id', 'template_id', 'type', 'is_home_page', 'sort', 'expire_time', 'status', 'created_at', 'updated_at', 'is_show_bottom_nav'], 'integer'],
  46. [['content'], 'string'],
  47. [['title'], 'string', 'max' => 15],
  48. [['template'], 'string', 'max' => 20],
  49. [['cover'], 'string', 'max' => 255],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => 'Mall ID',
  60. 'title' => '名称',
  61. 'mall_template_id' => '我的模板ID,对应qimall_diy_mall_template.id',
  62. 'template_id' => '商城模板ID,对应qimall_diy_template.id',
  63. 'type' => '页面类型 1:普通页面 2:首页 3:特殊页面(不可设为首页)',
  64. 'is_home_page' => '是否首页 0:否 1:是',
  65. 'template' => '页面模板',
  66. 'content' => '内容',
  67. 'cover' => '预览图片',
  68. 'expire_time' => '过期时间',
  69. 'sort' => '排序,越小排越前',
  70. 'status' => '状态[-1:删除;0:禁用;1启用]',
  71. 'created_at' => '添加时间',
  72. 'updated_at' => 'Updated At',
  73. ];
  74. }
  75. /**
  76. * 保存数据
  77. * @param array $params
  78. * @return bool|array
  79. */
  80. public static function setData(array $params){
  81. try{
  82. if(!empty($params['id'])){
  83. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'],'status'=>[StatusEnum::ENABLED]]);
  84. if(empty($model)) throw new Exception('页面不存在或已删除');
  85. // 判断是否已过期,过期不允许修改
  86. if ($model->expire_time != -1 && $model->expire_time <= time()) throw new Exception('模板已过期请续费');
  87. }else{
  88. $model = new self();
  89. $model->loadDefaultValues();
  90. $model->template = $params['template'] ?? DiyEnum::CUSTOM_PAGE;
  91. }
  92. if(!$model->load($params,'') || !$model->save()){
  93. throw new Exception($model->getErrorMessage());
  94. }
  95. // 存入缓存
  96. return $model->toArray();
  97. }catch(Exception $e){
  98. self::setStaticError($e->getMessage());
  99. return false;
  100. }
  101. }
  102. /**
  103. * 获取装修
  104. * @Author: lun
  105. * @DateTime: 2021/10/13 0013 16:32
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @param array $cond
  108. * @param array $with
  109. * @param bool $is_array
  110. * @param string $select
  111. * @return array|\yii\db\ActiveRecord[]
  112. */
  113. public static function getDiyPage(array $cond=[], array $with=[],bool $is_array=true,string $select='*'){
  114. $default_cond = [['<>','status',StatusEnum::DELETE]];
  115. $cond = array_merge($default_cond,$cond);
  116. $query = self::find()->select($select);
  117. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  118. $data = $query->asArray($is_array)->all();
  119. return $data;
  120. }
  121. /**
  122. * 获取装修首页
  123. * @Author: lun
  124. * @DateTime: 2021/10/22 0022 14:16
  125. * @Copyright: copyright (c) 2021 广东七件事集团
  126. * @param $mall_id
  127. * @return array
  128. */
  129. public static function getHomePage($mall_id, &$navs = [])
  130. {
  131. $page = self::findOne(['mall_id' => $mall_id, 'is_home_page' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]);
  132. if (empty($page)) return [];
  133. $navs = [
  134. 'is_show'=>$navs['is_show'],
  135. 'selectedIconPath' => $navs['selectedIconPath'] ?? '',
  136. 'iconPath' => $navs['iconPath'] ?? '',
  137. 'text' => $navs['text'] ?? $page->title,
  138. 'url' => self::PAGE_PATH_DIY . '?id=' . $page->id,
  139. 'open_type' => Link::OPEN_TYPE_PAGE,
  140. 'params' => [
  141. 'title' => $page->title,
  142. 'route' => self::PAGE_PATH_DIY . '?id=' . $page->id,
  143. 'open_type' => Link::OPEN_TYPE_PAGE,
  144. 'navigate' => 'navigateTo',
  145. ],
  146. ];
  147. }
  148. }