NotifyConfigTrait.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\base\InvalidConfigException;
  6. use common\enums\StatusEnum;
  7. use common\helpers\ArrayHelper;
  8. use common\enums\NotifyConfigTypeEnum;
  9. /**
  10. * Trait NotifyConfigTrait
  11. * @package common\traits
  12. */
  13. trait NotifyConfigTrait
  14. {
  15. /**
  16. * @throws InvalidConfigException
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->modelClass === null) {
  22. throw new InvalidConfigException('"modelClass" 属性必须设置.');
  23. }
  24. if ($this->viewPrefix === null) {
  25. throw new InvalidConfigException('"viewPrefix" 属性必须设置.');
  26. }
  27. }
  28. /**
  29. * 首页
  30. *
  31. * @return mixed
  32. */
  33. public function actionIndex()
  34. {
  35. $nameMap = $this->getNameMap();
  36. $typeMap = $this->getTypeMap();
  37. $models = $this->modelClass::find()
  38. ->where(['>=', 'status', StatusEnum::DISABLED])
  39. ->andWhere(['in', 'name', array_keys($nameMap)])
  40. ->andFilterWhere(['addon_name' => Yii::$app->params['addon']['name'] ?? ''])
  41. ->andWhere(['app_id' => $this->getAppId()])
  42. ->andWhere(['merchant_id' => $this->getMerchantId()])
  43. ->asArray()
  44. ->all();
  45. $default = new $this->modelClass;
  46. $default = ArrayHelper::toArray($default->loadDefaultValues());
  47. $data = [];
  48. foreach ($nameMap as $name => $value) {
  49. if (!isset($data[$name])) {
  50. $data[$name] = [
  51. 'name' => $name,
  52. 'value' => $value,
  53. 'params' => []
  54. ];
  55. }
  56. foreach ($typeMap as $key => $item) {
  57. $default['status'] = StatusEnum::DISABLED;
  58. if (in_array($key, [NotifyConfigTypeEnum::SYS])) {
  59. $default['status'] = StatusEnum::ENABLED;
  60. }
  61. $data[$name]['params'][$key] = $default;
  62. }
  63. foreach ($models as $model) {
  64. if ($name == $model['name'] && in_array($model['type'], array_keys($typeMap))) {
  65. $data[$name]['params'][$model['type']] = $model;
  66. }
  67. }
  68. }
  69. return $this->render($this->viewPrefix . $this->action->id, [
  70. 'data' => $data,
  71. 'nameMap' => $nameMap,
  72. 'typeMap' => $typeMap,
  73. ]);
  74. }
  75. /**
  76. * ajax编辑/创建
  77. *
  78. * @return mixed|string|\yii\web\Response
  79. * @throws \yii\base\ExitException
  80. */
  81. public function actionAjaxEdit()
  82. {
  83. $name = Yii::$app->request->get('name');
  84. $type = Yii::$app->request->get('type');
  85. $model = $this->findModel($name, $type);
  86. try {
  87. if (!empty($model->content)) {
  88. $content = Json::decode($model->content);
  89. $model->content = $content;
  90. }
  91. } catch (\Exception $e) {
  92. }
  93. // ajax 校验
  94. $this->activeFormValidate($model);
  95. if ($model->load(Yii::$app->request->post())) {
  96. $model->content = is_array($model->content) ? Json::encode($model->content) : $model->content;
  97. return $model->save()
  98. ? $this->redirect(Yii::$app->request->referrer)
  99. : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
  100. }
  101. return $this->renderAjax($this->viewPrefix . $type, [
  102. 'model' => $model,
  103. 'typeMap' => $this->getTypeMap(),
  104. 'nameMap' => $this->getNameMap(),
  105. ]);
  106. }
  107. /**
  108. * 辅助表格信息
  109. *
  110. * @return mixed|string|\yii\web\Response
  111. * @throws \yii\base\ExitException
  112. */
  113. public function actionHelpTable()
  114. {
  115. $name = Yii::$app->request->get('name');
  116. $default = $this->getNameDefaultData($name);
  117. $tables = $default['tables'];
  118. $data = [];
  119. foreach ($tables as $table) {
  120. $tableSchema = Yii::$app->db->getTableSchema($table['tableName']);
  121. $table['fields'] = [];
  122. foreach ($tableSchema->columns as $column) {
  123. // 过滤不显示的字段
  124. if (isset($table['filterFields']) && in_array($column->name, $table['filterFields'])) {
  125. continue;
  126. }
  127. $table['fields'][] = [
  128. 'name' => '{' . $table['prefix'] . '.' . $column->name . '}',
  129. 'comment' => $column->comment,
  130. ];
  131. }
  132. $data[] = $table;
  133. }
  134. return $this->renderAjax($this->viewPrefix . $this->action->id, [
  135. 'data' => $data,
  136. 'name' => $name,
  137. 'nameMap' => $this->getNameMap(),
  138. ]);
  139. }
  140. /**
  141. * 返回模型
  142. *
  143. * @param $id
  144. * @return \yii\db\ActiveRecord
  145. */
  146. protected function findModel($name, $type)
  147. {
  148. /* @var $model \yii\db\ActiveRecord */
  149. if (empty($model = $this->modelClass::find()->where([
  150. 'name' => $name,
  151. 'type' => $type,
  152. 'merchant_id' => $this->getMerchantId(),
  153. ])
  154. ->one())) {
  155. $model = new $this->modelClass;
  156. $model = $model->loadDefaultValues();
  157. $model->name = $name;
  158. $model->type = $type;
  159. if (in_array($type, [NotifyConfigTypeEnum::APP_PUSH, NotifyConfigTypeEnum::SYS])) {
  160. $model->attributes = $this->getNameDefaultData($name);
  161. }
  162. if (in_array($type, [NotifyConfigTypeEnum::SYS])) {
  163. $model->status = StatusEnum::ENABLED;
  164. }
  165. }
  166. $model->app_id = $this->getAppId();
  167. $model->addon_name = Yii::$app->params['addon']['name'] ?? '';
  168. !empty($model->addon_name) && $model->is_addon = StatusEnum::ENABLED;
  169. empty($model->params) && $model->params = [];
  170. return $model;
  171. }
  172. /**
  173. * @return string
  174. */
  175. protected function getAppId()
  176. {
  177. return Yii::$app->id;
  178. }
  179. /**
  180. * @return array|string[]
  181. */
  182. protected function getTypeMap()
  183. {
  184. return NotifyConfigTypeEnum::getMap();
  185. }
  186. }
粤ICP备19079148号