EditablePageModel.php 16 KB

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