人人商城

menu.ctrl.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('mc');
  8. load()->model('menu');
  9. load()->model('material');
  10. $dos = array('display', 'delete', 'refresh', 'post', 'push', 'copy', 'current_menu');
  11. $do = in_array($do, $dos) ? $do : 'display';
  12. $_W['page']['title'] = $_W['account']['type_name'] . ' - 自定义菜单';
  13. if($_W['isajax']) {
  14. if(!empty($_GPC['method'])) {
  15. $do = $_GPC['method'];
  16. }
  17. }
  18. if($do == 'display') {
  19. permission_check_account_user('platform_menu_conditional');
  20. set_time_limit(0);
  21. $type = !empty($_GPC['type']) ? intval($_GPC['type']) : MENU_CURRENTSELF;
  22. if ($type == MENU_CONDITIONAL) {
  23. $update_conditional_menu = menu_update_conditional();
  24. if(is_error($update_conditional_menu)) {
  25. itoast($update_conditional_menu['message'], '', 'error');
  26. }
  27. }
  28. $pindex = max(1, intval($_GPC['page']));
  29. $psize = 15;
  30. $condition = " WHERE uniacid = :uniacid";
  31. $params[':uniacid'] = $_W['uniacid'];
  32. if (isset($_GPC['keyword'])) {
  33. $condition .= " AND title LIKE :keyword";
  34. $params[':keyword'] = "%{$_GPC['keyword']}%";
  35. }
  36. if (!empty($type)) {
  37. $condition .= " AND type = :type";
  38. $params[':type'] = $type;
  39. }
  40. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('uni_account_menus') . $condition, $params);
  41. $data = pdo_fetchall("SELECT * FROM " . tablename('uni_account_menus') . $condition . " ORDER BY type ASC, status DESC,id DESC LIMIT " . ($pindex - 1) * $psize . "," . $psize, $params);
  42. $pager = pagination($total, $pindex, $psize);
  43. if ($type == MENU_CONDITIONAL) {
  44. $names = array(
  45. 'sex' => array('不限', '男', '女'),
  46. 'client_platform_type' => array('不限', '苹果', '安卓', '其他')
  47. );
  48. $groups = mc_fans_groups(true);
  49. }
  50. template('platform/menu');
  51. }
  52. if ($do == 'push') {
  53. $id = intval($_GPC['id']);
  54. $result = menu_push($id);
  55. if (is_error($result)) {
  56. iajax(-1, $result['message']);
  57. } else {
  58. iajax(0, '修改成功!', referer());
  59. }
  60. }
  61. if ($do == 'copy') {
  62. $id = intval($_GPC['id']);
  63. $menu = menu_get($id);
  64. if (empty($menu)) {
  65. itoast('菜单不存在或已经删除', url('platform/menu/display'), 'error');
  66. }
  67. if ($menu['type'] != MENU_CONDITIONAL) {
  68. itoast('该菜单不能复制', url('platform/menu/display'), 'error');
  69. }
  70. unset($menu['id'], $menu['menuid']);
  71. $menu['status'] = STATUS_OFF;
  72. $menu['title'] = $menu['title'] . '- 复本';
  73. pdo_insert('uni_account_menus', $menu);
  74. $id = pdo_insertid();
  75. itoast('', url('platform/menu/post', array('id' => $id, 'copy' => 1, 'type' => MENU_CONDITIONAL)));
  76. }
  77. if ($do == 'post') {
  78. permission_check_account_user('platform_menu_default');
  79. $type = intval($_GPC['type']);
  80. $id = intval($_GPC['id']);
  81. $copy = intval($_GPC['copy']);
  82. if (empty($type)) {
  83. if (!$_W['isajax']) {
  84. $update_self_menu = menu_update_currentself();
  85. if (is_error($update_self_menu)) {
  86. itoast($update_self_menu['message'], '', 'info');
  87. }
  88. }
  89. $type = MENU_CURRENTSELF;
  90. $default_menu = menu_default();
  91. $id = intval($default_menu['id']);
  92. }
  93. $params = array();
  94. if ($id > 0) {
  95. $menu = menu_get($id);
  96. if (empty($menu)) {
  97. itoast('菜单不存在或已经删除', url('platform/menu/display'), 'error');
  98. }
  99. if (!empty($menu['data'])) {
  100. $menu['data'] = iunserializer(base64_decode($menu['data']));
  101. if (!empty($menu['data']['button'])) {
  102. foreach ($menu['data']['button'] as &$button) {
  103. if (!empty($button['url'])) {
  104. $button['url'] = preg_replace('/(.*)redirect_uri=(.*)&response_type(.*)wechat_redirect/', '$2', $button['url']);
  105. $button['url'] = urldecode($button['url']);
  106. }
  107. if (empty($button['sub_button'])) {
  108. if ($button['type'] == 'media_id') {
  109. $button['type'] = 'click';
  110. }
  111. $button['sub_button'] = array();
  112. } else {
  113. $button['sub_button'] = !empty($button['sub_button']['list']) ? $button['sub_button']['list'] : $button['sub_button'];
  114. foreach ($button['sub_button'] as &$subbutton) {
  115. if (!empty($subbutton['url'])) {
  116. $subbutton['url'] = preg_replace('/(.*)redirect_uri=(.*)&response_type(.*)wechat_redirect/', '$2', $subbutton['url']);
  117. $subbutton['url'] = urldecode($subbutton['url']);
  118. }
  119. if ($subbutton['type'] == 'media_id') {
  120. $subbutton['type'] = 'click';
  121. }
  122. }
  123. unset($subbutton);
  124. }
  125. }
  126. unset($button);
  127. }
  128. if (!empty($menu['data']['matchrule']['province'])) {
  129. $menu['data']['matchrule']['province'] .= '省';
  130. }
  131. if (!empty($menu['data']['matchrule']['city'])) {
  132. $menu['data']['matchrule']['city'] .= '市';
  133. }
  134. if (empty($menu['data']['matchrule']['sex'])) {
  135. $menu['data']['matchrule']['sex'] = 0;
  136. }
  137. if (empty($menu['data']['matchrule']['group_id'])) {
  138. $menu['data']['matchrule']['group_id'] = -1;
  139. }
  140. if (empty($menu['data']['matchrule']['client_platform_type'])) {
  141. $menu['data']['matchrule']['client_platform_type'] = 0;
  142. }
  143. if (empty($menu['data']['matchrule']['language'])) {
  144. $menu['data']['matchrule']['language'] = '';
  145. }
  146. $params = $menu['data'];
  147. $params['title'] = $menu['title'];
  148. $params['type'] = $menu['type'];
  149. $params['id'] = $menu['id'];
  150. $params['status'] = $menu['status'];
  151. }
  152. $type = $menu['type'];
  153. }
  154. $status = $params['status'];
  155. $groups = mc_fans_groups();
  156. $languages = menu_languages();
  157. if ($_W['isajax'] && $_W['ispost']) {
  158. set_time_limit(0);
  159. $_GPC['group']['title'] = trim($_GPC['group']['title']);
  160. $_GPC['group']['type'] = intval($_GPC['group']['type']) == 0 ? 1 : intval($_GPC['group']['type']);
  161. $post = $_GPC['group'];
  162. if (empty($post['title'])) {
  163. iajax(-1, '请填写菜单组名称!', '');
  164. }
  165. $check_title_exist_condition = array(
  166. 'title' => $post['title'],
  167. 'type' => $type,
  168. );
  169. if (!empty($id)) {
  170. $check_title_exist_condition['id <>'] = $id;
  171. }
  172. $check_title_exist = pdo_getcolumn('uni_account_menus', $check_title_exist_condition, 'id');
  173. if (!empty($check_title_exist)) {
  174. iajax(-1, '菜单组名称已存在,请重新命名!', '');
  175. }
  176. if ($post['type'] == MENU_CONDITIONAL && empty($post['matchrule'])) {
  177. iajax(-1, '请选择菜单显示对象', '');
  178. }
  179. if (!empty($post['button'])) {
  180. foreach ($post['button'] as $key => &$button) {
  181. $keyword_exist = strexists($button['key'], 'keyword:');
  182. if ($keyword_exist) {
  183. $button['key'] = substr($button['key'], 8);
  184. }
  185. if (!empty($button['sub_button'])) {
  186. foreach ($button['sub_button'] as &$subbutton) {
  187. $sub_keyword_exist = strexists($subbutton['key'], 'keyword:');
  188. if ($sub_keyword_exist) {
  189. $subbutton['key'] = substr($subbutton['key'], 8);
  190. }
  191. }
  192. unset($subbutton);
  193. }
  194. }
  195. unset($button);
  196. }
  197. $is_conditional = $post['type'] == MENU_CONDITIONAL ? true : false;
  198. $account_api = WeAccount::createByUniacid();
  199. $menu = $account_api->menuBuild($post, $is_conditional);
  200. if ($_GPC['submit_type'] == 'publish' || $is_conditional) {
  201. $result = $account_api->menuCreate($menu);
  202. } else {
  203. $result = true;
  204. }
  205. if (is_error($result)) {
  206. iajax($result['errno'], $result['message']);
  207. } else {
  208. if ($post['matchrule']['group_id'] != -1) {
  209. $menu['matchrule']['groupid'] = $menu['matchrule']['tag_id'];
  210. unset($menu['matchrule']['tag_id']);
  211. }
  212. $menu = json_decode(urldecode(json_encode($menu)), true);
  213. $insert = array(
  214. 'uniacid' => $_W['uniacid'],
  215. 'menuid' => $result,
  216. 'title' => $post['title'],
  217. 'type' => $post['type'],
  218. 'sex' => intval($menu['matchrule']['sex']),
  219. 'group_id' => isset($menu['matchrule']['group_id']) ? $menu['matchrule']['group_id'] : -1,
  220. 'client_platform_type' => intval($menu['matchrule']['client_platform_type']),
  221. 'area' => trim($menus['matchrule']['country']) . trim($menu['matchrule']['province']) . trim($menu['matchrule']['city']),
  222. 'data' => base64_encode(iserializer($menu)),
  223. 'status' => STATUS_ON,
  224. 'createtime' => TIMESTAMP,
  225. );
  226. if ($post['type'] == MENU_CURRENTSELF) {
  227. if (!empty($id)) {
  228. pdo_update('uni_account_menus', $insert, array('uniacid' => $_W['uniacid'], 'type' => MENU_CURRENTSELF, 'id' => $id));
  229. } else {
  230. pdo_insert('uni_account_menus', $insert);
  231. }
  232. iajax(0, '创建菜单成功', url('platform/menu/display'));
  233. } elseif ($post['type'] == MENU_CONDITIONAL) {
  234. if ($post['status'] == STATUS_OFF && $post['id'] > 0) {
  235. pdo_update('uni_account_menus', $insert, array('uniacid' => $_W['uniacid'], 'type' => MENU_CONDITIONAL, 'id' => $post['id']));
  236. } else {
  237. pdo_insert('uni_account_menus', $insert);
  238. }
  239. iajax(0, '创建菜单成功', url('platform/menu/display', array('type' => MENU_CONDITIONAL)));
  240. }
  241. }
  242. }
  243. template('platform/menu');
  244. }
  245. if ($do == 'delete') {
  246. $id = intval($_GPC['id']);
  247. $result = menu_delete($id);
  248. if (is_error($result)) {
  249. itoast($result['message'], referer(), 'error');
  250. }
  251. itoast('删除菜单成功', referer(), 'success');
  252. }
  253. if ($do == 'current_menu') {
  254. $current_menu = $_GPC['current_menu'];
  255. if ($current_menu['type'] == 'click') {
  256. if (!empty($current_menu['media_id']) && empty($current_menu['key'])) {
  257. $wechat_attachment = pdo_get('wechat_attachment', array('media_id' => $current_menu['media_id']));
  258. if ($wechat_attachment['type'] == 'news') {
  259. $material = pdo_get('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $wechat_attachment['id']));
  260. $material['items'][0]['thumb_url'] = tomedia($material['thumb_url']);
  261. $material['items'][0]['title'] = $material['title'];
  262. $material['items'][0]['digest'] = $material['digest'];
  263. $material['type'] = 'news';
  264. } elseif ($wechat_attachment['type'] == 'video') {
  265. $material['tag'] = iunserializer($wechat_attachment['tag']);
  266. $material['attach'] = tomedia($wechat_attachment['attachment'], true);
  267. $material['type'] = 'video';
  268. } elseif ($wechat_attachment['type'] == 'voice') {
  269. $material['attach'] = tomedia($wechat_attachment['attachment'], true);
  270. $material['type'] = 'voice';
  271. $material['filename'] = $wechat_attachment['filename'];
  272. } elseif ($wechat_attachment['type'] == 'image') {
  273. $material['attach'] = tomedia($wechat_attachment['attachment'], true);
  274. $material['url'] = "url({$material['attach']})";
  275. $material['type'] = 'image';
  276. }
  277. } else {
  278. $keyword_info = explode(':', $current_menu['key']);
  279. if ($keyword_info[0] == 'keyword') {
  280. $rule_info = pdo_get('rule', array('name' => $keyword_info[1]), array('id'));
  281. $material['child_items'][0] = pdo_get('rule_keyword', array('rid' => $rule_info['id']), array('content'));
  282. $material['name'] = $keyword_info[1];
  283. $material['type'] = 'keyword';
  284. }
  285. }
  286. }
  287. if ($current_menu['type'] != 'click' && $current_menu['type'] != 'view') {
  288. $material = array();
  289. if ($current_menu['etype'] == 'module') {
  290. $module_name = explode(':', $current_menu['key']);
  291. load()->model('module');
  292. $material = module_fetch($module_name[1]);
  293. if ($material['issystem']) {
  294. $path = '/framework/builtin/' . $material['name'];
  295. } else {
  296. $path = '../addons/' . $material['name'];
  297. }
  298. $cion = $path . '/icon-custom.jpg';
  299. if (!file_exists($cion)) {
  300. $cion = $path . '/icon.jpg';
  301. if (!file_exists($cion)) {
  302. $cion = './resource/images/nopic-small.jpg';
  303. }
  304. }
  305. $material['icon'] = $cion;
  306. $material['type'] = $current_menu['type'];
  307. $material['etype'] = 'module';
  308. } elseif ($current_menu['etype'] == 'click') {
  309. $keyword_info = explode(':', $current_menu['key']);
  310. if ($keyword_info[0] == 'keyword') {
  311. $rule_info = pdo_get('rule', array('name' => $keyword_info[1]), array('id'));
  312. $material['child_items'][0] = pdo_get('rule_keyword', array('rid' => $rule_info['id']), array('content'));
  313. $material['name'] = $keyword_info[1];
  314. $material['type'] = $current_menu['type'];
  315. $material['etype'] = 'click';
  316. }
  317. }
  318. }
  319. iajax(0, $material);
  320. }