Upgrade.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\Authority;
  3. use Yii;
  4. use yii\db\Exception;
  5. use common\enums\AppEnum;
  6. use common\components\Migration;
  7. use common\interfaces\AddonWidget;
  8. use common\helpers\StringHelper;
  9. use common\models\common\Menu;
  10. use common\models\common\MenuCate;
  11. use common\models\common\Provinces;
  12. use common\models\common\AttachmentCate;
  13. use common\models\common\ConfigCate;
  14. use common\models\rbac\AuthRole;
  15. use common\models\rbac\AuthItem;
  16. /**
  17. * 升级数据库
  18. *
  19. * Class Upgrade
  20. * @package addons\Authority
  21. */
  22. class Upgrade extends Migration implements AddonWidget
  23. {
  24. /**
  25. * @var array
  26. */
  27. public $versions = [
  28. '3.0.0', '3.0.3', '3.0.10', '3.0.12', '3.0.18', '3.0.25',
  29. '3.1.13', '3.1.16', '3.1.20', '3.1.21', '3.1.29', '3.1.30',
  30. '3.1.35', '3.1.38', '3.1.39', '3.1.49',
  31. ];
  32. /**
  33. * @param $addon
  34. * @return mixed|void
  35. * @throws Exception
  36. */
  37. public function run($addon)
  38. {
  39. switch ($addon->version) {
  40. case '3.1.49' :
  41. // 替换菜单
  42. $menus = Menu::find()->select(['id', 'tree'])->asArray()->all();
  43. foreach ($menus as $menu) {
  44. $this->updateTree($menu['id'], $menu['tree'], Menu::class);
  45. }
  46. // 替换菜单分类
  47. $menuCates = MenuCate::find()->select(['id', 'tree'])->asArray()->all();
  48. foreach ($menuCates as $menuCate) {
  49. $this->updateTree($menuCate['id'], $menuCate['tree'], MenuCate::class);
  50. }
  51. // 替换省市区
  52. $provinces = Provinces::find()->select(['id', 'tree'])->asArray()->all();
  53. foreach ($provinces as $province) {
  54. $this->updateTree($province['id'], $province['tree'], Provinces::class);
  55. }
  56. // 替换角色
  57. $authRoles = AuthRole::find()->select(['id', 'tree'])->asArray()->all();
  58. foreach ($authRoles as $role) {
  59. $this->updateTree($role['id'], $role['tree'], AuthRole::class);
  60. }
  61. // 替换权限
  62. $authItem = AuthItem::find()->select(['id', 'tree'])->asArray()->all();
  63. foreach ($authItem as $item) {
  64. $this->updateTree($item['id'], $item['tree'], AuthItem::class);
  65. }
  66. // 素材分类
  67. $attachmentCates = AttachmentCate::find()->select(['id', 'tree'])->asArray()->all();
  68. foreach ($attachmentCates as $attachmentCate) {
  69. $this->updateTree($attachmentCate['id'], $attachmentCate['tree'], AttachmentCate::class);
  70. }
  71. // 配置分类
  72. $configCates = ConfigCate::find()->select(['id', 'tree'])->asArray()->all();
  73. foreach ($configCates as $configCate) {
  74. $this->updateTree($configCate['id'], $configCate['tree'], ConfigCate::class);
  75. }
  76. break;
  77. case '3.0.25' :
  78. /* 创建表 */
  79. $this->createTable('{{%common_theme}}', [
  80. 'id' => "int(10) NOT NULL AUTO_INCREMENT COMMENT '主键'",
  81. 'merchant_id' => "int(10) NOT NULL DEFAULT '0' COMMENT '商户ID'",
  82. 'member_id' => "int(10) NULL DEFAULT '0' COMMENT '用户ID'",
  83. 'member_type' => "int(10) NULL DEFAULT '0' COMMENT '用户类型'",
  84. 'app_id' => "varchar(20) NOT NULL DEFAULT '' COMMENT '应用'",
  85. 'layout' => "varchar(50) NULL COMMENT '布局类型'",
  86. 'color' => "varchar(50) NULL DEFAULT 'black' COMMENT '主题颜色'",
  87. 'status' => "tinyint(4) NULL DEFAULT '1' COMMENT '状态[-1:删除;0:禁用;1启用]'",
  88. 'created_at' => "int(10) unsigned NULL DEFAULT '0' COMMENT '添加时间'",
  89. 'updated_at' => "int(10) unsigned NULL DEFAULT '0' COMMENT '修改时间'",
  90. 'PRIMARY KEY (`id`)',
  91. ], "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='公用_用户主题'");
  92. /* 索引设置 */
  93. $this->createIndex('member_id', '{{%common_theme}}', 'member_id', 0);
  94. break;
  95. case '3.0.18' :
  96. Yii::$app->services->config->findSaveByName('map_amap_code', AppEnum::BACKEND, [
  97. 'title' => 'Web端(Js Api)安全秘钥',
  98. 'name' => 'map_amap_code',
  99. 'app_id' => 'backend',
  100. 'type' => 'text',
  101. 'cate_id' => '32',
  102. 'extra' => '',
  103. 'remark' => '地图选择',
  104. 'is_hide_remark' => '0',
  105. 'default_value' => '',
  106. 'sort' => '1',
  107. 'status' => '1',
  108. ]);
  109. break;
  110. case '3.0.0' :
  111. break;
  112. }
  113. }
  114. /**
  115. * @param int $id
  116. * @param string $tree
  117. * @param $model
  118. * @return void
  119. */
  120. protected function updateTree($id, $tree, $model)
  121. {
  122. $tree = StringHelper::replace(' ', '', $tree);
  123. $endTree = substr($tree, strlen($tree) - 1);
  124. if ($endTree != '-') {
  125. $tree = $tree.'-';
  126. $model::updateAll(['tree' => $tree], ['id' => $id]);
  127. }
  128. }
  129. }
粤ICP备19079148号