EditablePageModel.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace app\common\logic;
  3. /**
  4. *
  5. *
  6. * ----------------------------------------------------------------------------
  7. *
  8. * 逻辑层模型
  9. */
  10. class EditablePageModel
  11. {
  12. public function modelAdd($page_id, $type, $model_id, $config_id, $store_id = 0)
  13. {
  14. $editable_page_config_model = model('editable_page_config');
  15. $editable_page_model_model = model('editable_page_model');
  16. $editable_page_model_info = $editable_page_model_model->getOneEditablePageModel(array('editable_page_model_id' => $model_id));
  17. if (!$editable_page_model_info) {
  18. return ds_callback(false, lang('editable_page_model_not_exist'));
  19. }
  20. $sort = 0;
  21. $condition = array();
  22. $condition[] = array('editable_page_id', '=', $page_id);
  23. if ($config_id) {
  24. $editable_page_config_info = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $config_id));
  25. if ($editable_page_config_info) {
  26. $sort = $editable_page_config_info['editable_page_config_sort_order'] + 1;
  27. $condition[] = array('editable_page_config_sort_order', '>', $editable_page_config_info['editable_page_config_sort_order']);
  28. }
  29. }
  30. //重新排序
  31. $editable_page_config_list = $editable_page_config_model->getEditablePageConfigList($condition);
  32. if (!empty($editable_page_config_list)) {
  33. foreach ($editable_page_config_list as $val) {
  34. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $val['editable_page_config_id']), array('editable_page_config_sort_order' => $val['editable_page_config_sort_order'] + 1));
  35. }
  36. }
  37. if ($type == 'h5') {
  38. //初始数据
  39. if ($model_id == 2) {
  40. $editable_page_model_info['editable_page_model_content'] = '{"width":"100%","height":"300px","back_color":"unset","margin_top":"0","margin_bottom":"0","editor":["&lt;table&gt;&lt;tbody&gt;&lt;tr class=&quot;firstRow&quot;&gt;&lt;td width=&quot;237&quot; valign=&quot;middle&quot; rowspan=&quot;2&quot; colspan=&quot;1&quot; align=&quot;center&quot;&gt;&lt;img src=&quot;\/uploads\/home\/common\/page-model-h5-2-1.png&quot;\/&gt;&lt;\/td&gt;&lt;td width=&quot;237&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;img src=&quot;\/uploads\/home\/common\/page-model-h5-2-2.png&quot;\/&gt;&lt;\/td&gt;&lt;\/tr&gt;&lt;tr&gt;&lt;td width=&quot;237&quot; valign=&quot;top&quot; align=&quot;left&quot;&gt;&lt;img src=&quot;\/uploads\/home\/common\/page-model-h5-2-2.png&quot;\/&gt;&lt;\/td&gt;&lt;\/tr&gt;&lt;\/tbody&gt;&lt;\/table&gt;&lt;p&gt;&lt;br\/&gt;&lt;\/p&gt;"]}';
  41. }
  42. if ($model_id == 1) {
  43. $editable_page_model_info['editable_page_model_content'] = '{"width":"100%","height":"188px","back_color":"unset","margin_top":"0","margin_bottom":"0","image":[{"count":2,"list":{"1":{"path":"\/uploads\/home\/common\/page-model-h5-1-1.png","sort":"9"},"0":{"path":"\/uploads\/home\/common\/page-model-h5-1-1.png","sort":"9"}}}],"link":[{"count":2,"list":[]}]}';
  44. }
  45. $editable_page_model_info['editable_page_model_content'] = str_replace('1200px', '100%', $editable_page_model_info['editable_page_model_content']);
  46. }
  47. $data = array(
  48. 'editable_page_id' => $page_id,
  49. 'editable_page_config_sort_order' => $sort,
  50. 'editable_page_model_id' => $model_id,
  51. 'editable_page_config_content' => $editable_page_model_info['editable_page_model_content'],
  52. );
  53. $new_config_id = $editable_page_config_model->addEditablePageConfig($data);
  54. $data['editable_page_config_id'] = $new_config_id;
  55. $data['editable_page_config_content'] = json_decode($data['editable_page_config_content'], true);
  56. $data = $this->updatePage($data, $store_id);
  57. return ds_callback(true, '', $data);
  58. }
  59. public function modelMove($direction, $config_id)
  60. {
  61. $editable_page_config_model = model('editable_page_config');
  62. $editable_page_config_info = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $config_id));
  63. if (!$editable_page_config_info) {
  64. return ds_callback(false, lang('param_error'));
  65. }
  66. $condition = array();
  67. if ($direction) {
  68. $sort = array('editable_page_config_sort_order', '>', $editable_page_config_info['editable_page_config_sort_order']);
  69. $order = 'editable_page_config_sort_order asc';
  70. } else {
  71. $sort = array('editable_page_config_sort_order', '<', $editable_page_config_info['editable_page_config_sort_order']);
  72. $order = 'editable_page_config_sort_order desc';
  73. }
  74. $condition[] = array('editable_page_id', '=', $editable_page_config_info['editable_page_id']);
  75. $editable_page_config_list = $editable_page_config_model->getEditablePageConfigList($condition, 1, $order);
  76. if (empty($editable_page_config_list)) {
  77. return ds_callback(false, lang('param_error'));
  78. } else {
  79. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $editable_page_config_info['editable_page_config_id']), array('editable_page_config_sort_order' => $editable_page_config_list[0]['editable_page_config_sort_order']));
  80. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $editable_page_config_list[0]['editable_page_config_id']), array('editable_page_config_sort_order' => $editable_page_config_info['editable_page_config_sort_order']));
  81. }
  82. return ds_callback(true);
  83. }
  84. public function modelSort($direction, $config_id, $o_config_id)
  85. {
  86. $editable_page_config_model = model('editable_page_config');
  87. $editable_page_config_info1 = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $direction ? $config_id : $o_config_id));
  88. if (!$editable_page_config_info1) {
  89. return ds_callback(false, lang('param_error'));
  90. }
  91. $editable_page_config_info2 = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $direction ? $o_config_id : $config_id));
  92. if (!$editable_page_config_info2) {
  93. return ds_callback(false, lang('param_error'));
  94. }
  95. //重新排序
  96. $condition = array();
  97. $condition[] = array('editable_page_id', '=', $editable_page_config_info1['editable_page_id']);
  98. $condition[] = array('editable_page_config_sort_order', 'between', [$editable_page_config_info1['editable_page_config_sort_order'], $editable_page_config_info2['editable_page_config_sort_order']]);
  99. $editable_page_config_list = $editable_page_config_model->getEditablePageConfigList($condition);
  100. if (!empty($editable_page_config_list)) {
  101. foreach ($editable_page_config_list as $val) {
  102. if ($val['editable_page_config_id'] == $config_id) {
  103. if ($direction) {
  104. $sort = $editable_page_config_list[count($editable_page_config_list) - 1]['editable_page_config_sort_order'];
  105. } else {
  106. $sort = $editable_page_config_list[0]['editable_page_config_sort_order'];
  107. }
  108. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $val['editable_page_config_id']), array('editable_page_config_sort_order' => $sort));
  109. continue;
  110. }
  111. if ($direction) {
  112. $sort = $val['editable_page_config_sort_order'] - 1;
  113. } else {
  114. $sort = $val['editable_page_config_sort_order'] + 1;
  115. }
  116. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $val['editable_page_config_id']), array('editable_page_config_sort_order' => $sort));
  117. }
  118. }
  119. return ds_callback(true);
  120. }
  121. public function modelEdit($editable_page_config_info, $post, $store_id = 0)
  122. {
  123. $editable_page_config_model = model('editable_page_config');
  124. $config_info = json_decode($editable_page_config_info['editable_page_config_content'], true);
  125. $res = $this->getBaseConfig($editable_page_config_info['editable_page_model_id'], $config_info, $post);
  126. if (!$res['code']) {
  127. return $res;
  128. }
  129. $config_info = $res['data'];
  130. if (!$editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $editable_page_config_info['editable_page_config_id']), array('editable_page_config_content' => json_encode($config_info)))) {
  131. return ds_callback(false, lang('ds_common_op_fail'));
  132. }
  133. $editable_page_config_info['editable_page_config_content'] = $config_info;
  134. $editable_page_config_info = $this->updatePage($editable_page_config_info, $store_id);
  135. return ds_callback(true, '', $editable_page_config_info);
  136. }
  137. public function imageDel($file_id)
  138. {
  139. $upload_model = model('upload');
  140. /**
  141. * 删除图片
  142. */
  143. $file_array = $upload_model->getOneUpload($file_id);
  144. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_EDITABLE_PAGE . DIRECTORY_SEPARATOR . $file_array['file_name']);
  145. /**
  146. * 删除信息
  147. */
  148. $condition = array();
  149. $condition[] = array('upload_id', '=', $file_id);
  150. $upload_model->delUpload($condition);
  151. return ds_callback(true);
  152. }
  153. /**
  154. * 图片上传
  155. */
  156. public function imageUpload($name, $config_id)
  157. {
  158. $file_name = '';
  159. $file_object = request()->file($name);
  160. if ($file_object) {
  161. $res = ds_upload_pic(ATTACH_EDITABLE_PAGE, $name);
  162. if ($res['code']) {
  163. $file_name = $res['data']['file_name'];
  164. } else {
  165. return ds_callback(false, $res['msg']);
  166. }
  167. } else {
  168. return ds_callback(false, lang('param_error'));
  169. }
  170. /**
  171. * 模型实例化
  172. */
  173. $upload_model = model('upload');
  174. /**
  175. * 图片数据入库
  176. */
  177. $insert_array = array();
  178. $insert_array['file_name'] = $file_name;
  179. $insert_array['upload_type'] = '7';
  180. $insert_array['file_size'] = $_FILES[$name]['size'];
  181. $insert_array['item_id'] = intval($config_id);
  182. $insert_array['upload_time'] = TIMESTAMP;
  183. $result = $upload_model->addUpload($insert_array);
  184. if ($result) {
  185. $data = array();
  186. $data['file_id'] = $result;
  187. $data['file_name'] = $file_name;
  188. $data['file_path'] = ds_get_pic(ATTACH_EDITABLE_PAGE, $file_name);
  189. /**
  190. * 整理为json格式
  191. */
  192. return ds_callback(true, '', $data);
  193. } else {
  194. return ds_callback(false, lang('ds_common_op_fail'));
  195. }
  196. }
  197. private function getBaseConfig($model_id, $config_info, $post)
  198. {
  199. $config_info['back_color'] = $post['back_color'];
  200. $config_info['margin_top'] = $post['margin_top'];
  201. $config_info['margin_bottom'] = $post['margin_bottom'];
  202. switch ($model_id) {
  203. case 1:
  204. $config_info['width'] = $post['width'];
  205. $config_info['height'] = $post['height'];
  206. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = intval($post['image_count']);
  207. if ($config_info['image'][0]['count'] < 1) {
  208. return ds_callback(false, lang('param_error'));
  209. }
  210. break;
  211. case 2:
  212. $config_info['width'] = $post['width'];
  213. $config_info['height'] = $post['height'];
  214. break;
  215. case 3:
  216. case 5:
  217. case 6:
  218. $config_info['goods'][0]['count'] = intval($post['goods_count']);
  219. if ($config_info['goods'][0]['count'] < 1) {
  220. return ds_callback(false, lang('param_error'));
  221. }
  222. break;
  223. case 11:
  224. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = intval($post['image_count']);
  225. $config_info['goods'][0]['count'] = intval($post['goods_count']);
  226. if ($config_info['image'][0]['count'] < 1 || $config_info['goods'][0]['count'] < 1) {
  227. return ds_callback(false, lang('param_error'));
  228. }
  229. for ($i = 0; $i < $config_info['image'][0]['count']; $i++) {
  230. if (!isset($config_info['goods'][$i])) {
  231. $config_info['goods'][$i] = $config_info['goods'][0];
  232. }
  233. $config_info['goods'][$i]['count'] = $config_info['goods'][0]['count'];
  234. }
  235. $config_info['goods'] = array_slice($config_info['goods'], 0, $config_info['image'][0]['count']);
  236. break;
  237. case 12:
  238. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = $config_info['text'][0]['count'] = intval($post['image_count']);
  239. if ($config_info['image'][0]['count'] < 1) {
  240. return ds_callback(false, lang('param_error'));
  241. }
  242. break;
  243. case 13:
  244. $config_info['text'][1]['count'] = $config_info['link'][1]['count'] = intval($post['text_count']);
  245. if ($config_info['text'][1]['count'] < 1) {
  246. return ds_callback(false, lang('param_error'));
  247. }
  248. break;
  249. case 14:
  250. $config_info['image'][0]['count'] = intval($post['image_count']);
  251. if ($config_info['image'][0]['count'] < 1) {
  252. return ds_callback(false, lang('param_error'));
  253. }
  254. for ($i = 0; $i < $config_info['image'][0]['count']; $i++) {
  255. if (!isset($config_info['image'][$i + 1])) {
  256. $config_info['image'][$i + 1] = $config_info['image'][1];
  257. }
  258. }
  259. $config_info['image'] = array_slice($config_info['goods'], 0, $config_info['image'][0]['count'] + 1);
  260. break;
  261. }
  262. return ds_callback(true, '', $config_info);
  263. }
  264. public function updatePage($data, $store_id = 0)
  265. {
  266. $editable_page_config_model = model('editable_page_config');
  267. $editable_page_config_model->store_id = $store_id;
  268. //更新页面编辑时间
  269. model('editable_page')->editEditablePage(array('editable_page_id' => $data['editable_page_id']), array('editable_page_edit_time' => TIMESTAMP));
  270. if (isset($data['editable_page_config_content']['goods'])) {
  271. $data['goods_list'] = array();
  272. foreach ($data['editable_page_config_content']['goods'] as $key => $val) {
  273. $data['goods_list'][$key] = $editable_page_config_model->getEditablePageConfigGoods($val);
  274. foreach ($data['goods_list'][$key] as $i => $goods) {
  275. $data['goods_list'][$key][$i]['goods_image_url'] = goods_thumb($goods, 240);
  276. }
  277. }
  278. }
  279. if (isset($data['editable_page_config_content']['cate'])) {
  280. $data['cate_list'] = array();
  281. foreach ($data['editable_page_config_content']['cate'] as $key => $val) {
  282. $data['cate_list'][$key] = $editable_page_config_model->getEditablePageConfigCate($val, $data['editable_page_model_id']);
  283. }
  284. }
  285. if (isset($data['editable_page_config_content']['brand'])) {
  286. $data['brand_list'] = array();
  287. foreach ($data['editable_page_config_content']['brand'] as $key => $val) {
  288. $data['brand_list'][$key] = $editable_page_config_model->getEditablePageConfigBrand($val);
  289. }
  290. }
  291. return $data;
  292. }
  293. }