Link.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace common\models\link;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%link}}".
  8. *
  9. * @property int $id 主键
  10. * @property string|null $name 英文名
  11. * @property string $title 中文名
  12. * @property int $cat_id 分类id
  13. * @property string $open_type 打开类型:navigate,redirect,app,web,contact,call
  14. * @property string|null $route 路由
  15. * @property string|null $icon 图标
  16. * @property string|null $remark 备注
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 修改时间
  20. */
  21. class Link extends BaseActiveRecord
  22. {
  23. // 打开类型
  24. const OPEN_TYPE_PAGE = 'page';
  25. const OPEN_TYPE_CALL = 'call';
  26. const OPEN_TYPE_WEB = 'web';
  27. const OPEN_TYPE_APPLET = 'applet';
  28. const OPEN_TYPE = [
  29. self::OPEN_TYPE_PAGE => '页面',
  30. self::OPEN_TYPE_WEB => '网页',
  31. self::OPEN_TYPE_CALL => '电话联系',
  32. self::OPEN_TYPE_APPLET => '小程序',
  33. ];
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%link}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['cat_id', 'status', 'created_at', 'updated_at'], 'integer'],
  48. [['remark'], 'string'],
  49. [['name', 'title', 'open_type'], 'string', 'max' => 20],
  50. [['route'], 'string', 'max' => 200],
  51. [['icon'], 'string', 'max' => 255],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => '主键',
  61. 'name' => '英文名',
  62. 'title' => '中文名',
  63. 'cat_id' => '分类id',
  64. 'open_type' => '打开类型:navigate,redirect,app,web,contact,call',
  65. 'route' => '路由',
  66. 'icon' => 'Icon',
  67. 'remark' => '备注',
  68. 'status' => '状态[-1:删除;0:禁用;1启用]',
  69. 'created_at' => '创建时间',
  70. 'updated_at' => '修改时间',
  71. ];
  72. }
  73. public function getCategory()
  74. {
  75. return $this->hasMany(LinkCategory::class, ['id' => 'cat_id'])->select("id,title,is_link");
  76. }
  77. /**
  78. * 根据cat_id获取链接列表
  79. * @param $mall_id
  80. * @param $cat_id
  81. * @return array|\yii\db\ActiveRecord[]
  82. */
  83. public static function getLinkByCatId($mall_id, $cat_id)
  84. {
  85. $field = "id,title as name,cat_id,open_type,route as value,remark";
  86. return self::find()->select($field)->where(['mall_id' => $mall_id, 'cat_id' => $cat_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
  87. }
  88. /**
  89. * 获取所有链接,将cat_id作为键
  90. * @Author: lun
  91. * @DateTime: 2021/10/13 0013 12:20
  92. * @Copyright: copyright (c) 2021 广东七件事集团
  93. * @param array $cond
  94. * @param bool $is_array
  95. * @param string $select
  96. * @param string $indexBy
  97. * @return array|\yii\db\ActiveRecord[]
  98. */
  99. public static function getLink(array $cond=[], array $with=[],bool $is_array=true,string $select='*'){
  100. $default_cond = [['<>','status',StatusEnum::DELETE]];
  101. $cond = array_merge($default_cond,$cond);
  102. $query = self::find()->select($select);
  103. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  104. $data = $query->asArray($is_array)->all();
  105. return $data;
  106. }
  107. }