DiyForm.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\common\diy\DiyConfig;
  9. use app\models\DiyPage;
  10. use app\models\DiyTemplate;
  11. use yii\base\BaseObject;
  12. use yii\base\Model;
  13. use yii\helpers\Json;
  14. class DiyForm extends Model
  15. {
  16. /**
  17. * @return array
  18. */
  19. public function rules()
  20. {
  21. return [
  22. [[]]
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. ];
  29. }
  30. public function save()
  31. {
  32. if(!$this->validate()) {
  33. return [
  34. 'code' => 1,
  35. 'msg' => $this->getErrorSummary(false)[0],
  36. ];
  37. }
  38. }
  39. public static function getTemplateEdit()
  40. {
  41. $id = get_params('id', 0);
  42. $template = DiyConfig::getSetting($id);
  43. return [
  44. 'code' => 0,
  45. 'msg' => '加载成功',
  46. 'data' => [
  47. //'modules_list' => DiyConfig::getConfig(),
  48. 'template' => $template ? Json::decode($template->template) : []
  49. ]
  50. ];
  51. }
  52. public static function getTemplateSave()
  53. {
  54. $template = post_params('template');
  55. $controls = post_params('controls');
  56. $id = post_params('id', 0);
  57. $name = post_params('name', "DIY");
  58. $type = post_params('type', 'index');
  59. $form = DiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $id]) ?: new DiyTemplate();
  60. $form->store_id = get_store_id();
  61. $form->name = $name;
  62. $form->template = $template;
  63. $form->is_delete = 0;
  64. $form->addtime = time();
  65. $form->type = $type;
  66. if ($form->save()) {
  67. // 保存控件
  68. if ($controls) {
  69. self::saveControls($controls);
  70. }
  71. return [
  72. 'code' => 0,
  73. 'msg' => '保存成功',
  74. 'data' => [
  75. 'template' => $form,
  76. 'controls' => DiyTemplate::find()
  77. ->where([
  78. 'store_id' => get_store_id(),
  79. 'is_delete' => 0,
  80. 'type' => 'controls'
  81. ])->asArray()->one()
  82. ]
  83. ];
  84. } else {
  85. return [
  86. 'code' => 0,
  87. 'msg' => '保存失败'
  88. ];
  89. }
  90. }
  91. public static function saveControls($controls)
  92. {
  93. $controlsDiy = DiyTemplate::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'type' => 'controls']) ?: new DiyTemplate();
  94. $controlsDiy->store_id = get_store_id();
  95. $controlsDiy->name = 'controls';
  96. $controlsDiy->template = $controls;
  97. $controlsDiy->is_delete = 0;
  98. $controlsDiy->addtime = time();
  99. $controlsDiy->type = 'controls';
  100. return $controlsDiy->save();
  101. }
  102. public static function getTemplateList()
  103. {
  104. $list = DiyTemplate::find()
  105. ->where([
  106. 'store_id' => get_store_id(),
  107. 'is_delete' => 0,
  108. 'type' => ['index', 'user', 'goods', 'cat', 'sign']
  109. ])->orderBy(['id' => SORT_ASC])->asArray()->all();
  110. $template_list = [];
  111. $index_list = [];
  112. foreach($list as $val) {
  113. if (in_array($val['type'],['user', 'goods', 'cat', 'sign'])) {
  114. $template_list[] = $val;
  115. } else {
  116. $index_list[] = $val;
  117. }
  118. }
  119. $new_list = array_merge($template_list, $index_list);
  120. return [
  121. 'code' => 0,
  122. 'msg' => '加载成功',
  123. 'data' => [
  124. 'list' => $new_list,
  125. 'controls' => DiyTemplate::find()
  126. ->where([
  127. 'store_id' => get_store_id(),
  128. 'is_delete' => 0,
  129. 'type' => 'controls'
  130. ])->asArray()->one()
  131. ]
  132. ];
  133. }
  134. public static function getPageList()
  135. {
  136. $query = DiyPage::find()
  137. ->with(['template'])
  138. ->where([
  139. 'store_id' => get_store_id(),
  140. 'is_delete' => 0
  141. ])->orderBy(['id' => SORT_ASC])->asArray();
  142. $pagination = pagination_make($query);
  143. $pagination['data'] = $pagination['list'];
  144. unset($pagination['list']);
  145. return [
  146. 'code' => 0,
  147. 'msg' => '加载成功',
  148. 'template_list' => DiyTemplate::find()->select(['id', 'name'])
  149. ->where(['store_id' => get_store_id(),'is_delete' => 0])->asArray()->all(),
  150. 'data' => $pagination
  151. ];
  152. }
  153. public static function getPageSave()
  154. {
  155. $form = DiyPage::findOne(post_params('id')) ?: new DiyPage();
  156. $form->template_id = post_params('template_id');
  157. $form->title = post_params('title');
  158. $form->store_id = get_store_id();
  159. $form->addtime = time();
  160. if ($form->save()) {
  161. $res = ['code' => 0, 'msg' => '保存成功'];
  162. }else {
  163. $res = ['code' => 1, 'msg' => '保存失败'];
  164. }
  165. return $res;
  166. }
  167. }