EditablePageModel.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace app\common\logic;
  3. /**
  4. * ============================================================================
  5. * DSMall多用户商城
  6. * ============================================================================
  7. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  8. * 网站地址: http://www.csdeshang.com
  9. * ----------------------------------------------------------------------------
  10. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  11. * 不允许对程序代码以任何形式任何目的的再发布。
  12. * ============================================================================
  13. * 逻辑层模型
  14. */
  15. class EditablePageModel {
  16. public function modelAdd($page_id, $type, $model_id, $config_id, $store_id = 0) {
  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. $editable_page_config_model = model('editable_page_config');
  64. $editable_page_config_info = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $config_id));
  65. if (!$editable_page_config_info) {
  66. return ds_callback(false, lang('param_error'));
  67. }
  68. $condition = array();
  69. if ($direction) {
  70. $sort = array('editable_page_config_sort_order', '>', $editable_page_config_info['editable_page_config_sort_order']);
  71. $order = 'editable_page_config_sort_order asc';
  72. } else {
  73. $sort = array('editable_page_config_sort_order', '<', $editable_page_config_info['editable_page_config_sort_order']);
  74. $order = 'editable_page_config_sort_order desc';
  75. }
  76. $condition[] = array('editable_page_id', '=', $editable_page_config_info['editable_page_id']);
  77. $editable_page_config_list = $editable_page_config_model->getEditablePageConfigList($condition, 1, $order);
  78. if (empty($editable_page_config_list)) {
  79. return ds_callback(false, lang('param_error'));
  80. } else {
  81. $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']));
  82. $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']));
  83. }
  84. return ds_callback(true);
  85. }
  86. public function modelSort($direction, $config_id, $o_config_id) {
  87. $editable_page_config_model = model('editable_page_config');
  88. $editable_page_config_info1 = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $direction ? $config_id : $o_config_id));
  89. if (!$editable_page_config_info1) {
  90. return ds_callback(false, lang('param_error'));
  91. }
  92. $editable_page_config_info2 = $editable_page_config_model->getOneEditablePageConfig(array('editable_page_config_id' => $direction ? $o_config_id : $config_id));
  93. if (!$editable_page_config_info2) {
  94. return ds_callback(false, lang('param_error'));
  95. }
  96. //重新排序
  97. $condition = array();
  98. $condition[] = array('editable_page_id', '=', $editable_page_config_info1['editable_page_id']);
  99. $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']]);
  100. $editable_page_config_list = $editable_page_config_model->getEditablePageConfigList($condition);
  101. if (!empty($editable_page_config_list)) {
  102. foreach ($editable_page_config_list as $val) {
  103. if ($val['editable_page_config_id'] == $config_id) {
  104. if ($direction) {
  105. $sort = $editable_page_config_list[count($editable_page_config_list) - 1]['editable_page_config_sort_order'];
  106. } else {
  107. $sort = $editable_page_config_list[0]['editable_page_config_sort_order'];
  108. }
  109. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $val['editable_page_config_id']), array('editable_page_config_sort_order' => $sort));
  110. continue;
  111. }
  112. if ($direction) {
  113. $sort = $val['editable_page_config_sort_order'] - 1;
  114. } else {
  115. $sort = $val['editable_page_config_sort_order'] + 1;
  116. }
  117. $editable_page_config_model->editEditablePageConfig(array('editable_page_config_id' => $val['editable_page_config_id']), array('editable_page_config_sort_order' => $sort));
  118. }
  119. }
  120. return ds_callback(true);
  121. }
  122. public function modelEdit($editable_page_config_info, $post, $store_id = 0) {
  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. $upload_model = model('upload');
  139. /**
  140. * 删除图片
  141. */
  142. $file_array = $upload_model->getOneUpload($file_id);
  143. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_EDITABLE_PAGE . DIRECTORY_SEPARATOR . $file_array['file_name']);
  144. /**
  145. * 删除信息
  146. */
  147. $condition = array();
  148. $condition[] = array('upload_id', '=', $file_id);
  149. $upload_model->delUpload($condition);
  150. return ds_callback(true);
  151. }
  152. /**
  153. * 图片上传
  154. */
  155. public function imageUpload($name, $config_id) {
  156. $file_name = '';
  157. $file_object = request()->file($name);
  158. if ($file_object) {
  159. $res = ds_upload_pic(ATTACH_EDITABLE_PAGE, $name);
  160. if ($res['code']) {
  161. $file_name = $res['data']['file_name'];
  162. } else {
  163. return ds_callback(false, $res['msg']);
  164. }
  165. } else {
  166. return ds_callback(false, lang('param_error'));
  167. }
  168. /**
  169. * 模型实例化
  170. */
  171. $upload_model = model('upload');
  172. /**
  173. * 图片数据入库
  174. */
  175. $insert_array = array();
  176. $insert_array['file_name'] = $file_name;
  177. $insert_array['upload_type'] = '7';
  178. $insert_array['file_size'] = $_FILES[$name]['size'];
  179. $insert_array['item_id'] = intval($config_id);
  180. $insert_array['upload_time'] = TIMESTAMP;
  181. $result = $upload_model->addUpload($insert_array);
  182. if ($result) {
  183. $data = array();
  184. $data['file_id'] = $result;
  185. $data['file_name'] = $file_name;
  186. $data['file_path'] = ds_get_pic(ATTACH_EDITABLE_PAGE , $file_name);
  187. /**
  188. * 整理为json格式
  189. */
  190. return ds_callback(true, '', $data);
  191. } else {
  192. return ds_callback(false, lang('ds_common_op_fail'));
  193. }
  194. }
  195. private function getBaseConfig($model_id, $config_info, $post) {
  196. $config_info['back_color'] = $post['back_color'];
  197. $config_info['margin_top'] = $post['margin_top'];
  198. $config_info['margin_bottom'] = $post['margin_bottom'];
  199. switch ($model_id) {
  200. case 1:
  201. $config_info['width'] = $post['width'];
  202. $config_info['height'] = $post['height'];
  203. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = intval($post['image_count']);
  204. if ($config_info['image'][0]['count'] < 1) {
  205. return ds_callback(false, lang('param_error'));
  206. }
  207. break;
  208. case 2:
  209. $config_info['width'] = $post['width'];
  210. $config_info['height'] = $post['height'];
  211. break;
  212. case 3:
  213. case 5:
  214. case 6:
  215. $config_info['goods'][0]['count'] = intval($post['goods_count']);
  216. if ($config_info['goods'][0]['count'] < 1) {
  217. return ds_callback(false, lang('param_error'));
  218. }
  219. break;
  220. case 11:
  221. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = intval($post['image_count']);
  222. $config_info['goods'][0]['count'] = intval($post['goods_count']);
  223. if ($config_info['image'][0]['count'] < 1 || $config_info['goods'][0]['count'] < 1) {
  224. return ds_callback(false, lang('param_error'));
  225. }
  226. for ($i = 0; $i < $config_info['image'][0]['count']; $i++) {
  227. if (!isset($config_info['goods'][$i])) {
  228. $config_info['goods'][$i] = $config_info['goods'][0];
  229. }
  230. $config_info['goods'][$i]['count'] = $config_info['goods'][0]['count'];
  231. }
  232. $config_info['goods'] = array_slice($config_info['goods'], 0, $config_info['image'][0]['count']);
  233. break;
  234. case 12:
  235. $config_info['image'][0]['count'] = $config_info['link'][0]['count'] = $config_info['text'][0]['count'] = intval($post['image_count']);
  236. if ($config_info['image'][0]['count'] < 1) {
  237. return ds_callback(false, lang('param_error'));
  238. }
  239. break;
  240. case 13:
  241. $config_info['text'][1]['count'] = $config_info['link'][1]['count'] = intval($post['text_count']);
  242. if ($config_info['text'][1]['count'] < 1) {
  243. return ds_callback(false, lang('param_error'));
  244. }
  245. break;
  246. case 14:
  247. $config_info['image'][0]['count'] = intval($post['image_count']);
  248. if ($config_info['image'][0]['count'] < 1) {
  249. return ds_callback(false, lang('param_error'));
  250. }
  251. for ($i = 0; $i < $config_info['image'][0]['count']; $i++) {
  252. if (!isset($config_info['image'][$i + 1])) {
  253. $config_info['image'][$i + 1] = $config_info['image'][1];
  254. }
  255. }
  256. $config_info['image'] = array_slice($config_info['goods'], 0, $config_info['image'][0]['count'] + 1);
  257. break;
  258. }
  259. return ds_callback(true, '',$config_info);
  260. }
  261. public function updatePage($data, $store_id = 0) {
  262. $editable_page_config_model = model('editable_page_config');
  263. $editable_page_config_model->store_id = $store_id;
  264. //更新页面编辑时间
  265. model('editable_page')->editEditablePage(array('editable_page_id' => $data['editable_page_id']), array('editable_page_edit_time' => TIMESTAMP));
  266. if (isset($data['editable_page_config_content']['goods'])) {
  267. $data['goods_list'] = array();
  268. foreach ($data['editable_page_config_content']['goods'] as $key => $val) {
  269. $data['goods_list'][$key] = $editable_page_config_model->getEditablePageConfigGoods($val);
  270. foreach ($data['goods_list'][$key] as $i => $goods) {
  271. $data['goods_list'][$key][$i]['goods_image_url'] = goods_thumb($goods, 240);
  272. }
  273. }
  274. }
  275. if (isset($data['editable_page_config_content']['cate'])) {
  276. $data['cate_list'] = array();
  277. foreach ($data['editable_page_config_content']['cate'] as $key => $val) {
  278. $data['cate_list'][$key] = $editable_page_config_model->getEditablePageConfigCate($val, $data['editable_page_model_id']);
  279. }
  280. }
  281. if (isset($data['editable_page_config_content']['brand'])) {
  282. $data['brand_list'] = array();
  283. foreach ($data['editable_page_config_content']['brand'] as $key => $val) {
  284. $data['brand_list'][$key] = $editable_page_config_model->getEditablePageConfigBrand($val);
  285. }
  286. }
  287. return $data;
  288. }
  289. }