EditablePageModel.php 16 KB

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