menu.mod.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. function menu_languages() {
  8. $languages = array(
  9. array('ch'=>'简体中文', 'en'=>'zh_CN'),
  10. array('ch'=>'繁体中文TW', 'en'=>'zh_TW'),
  11. array('ch'=>'繁体中文HK', 'en'=>'zh_HK'),
  12. array('ch'=>'英文', 'en'=>'en'),
  13. array('ch'=>'印尼', 'en'=>'id'),
  14. array('ch'=>'马来', 'en'=>'ms'),
  15. array('ch'=>'西班牙', 'en'=>'es'),
  16. array('ch'=>'韩国', 'en'=>'ko'),
  17. array('ch'=>'意大利 ', 'en'=>'it'),
  18. array('ch'=>'日本', 'en'=>'ja'),
  19. array('ch'=>'波兰', 'en'=>'pl'),
  20. array('ch'=>'葡萄牙', 'en'=>'pt'),
  21. array('ch'=>'俄国', 'en'=>'ru'),
  22. array('ch'=>'泰文', 'en'=>'th'),
  23. array('ch'=>'越南', 'en'=>'vi'),
  24. array('ch'=>'阿拉伯语', 'en'=>'ar'),
  25. array('ch'=>'北印度', 'en'=>'hi'),
  26. array('ch'=>'希伯来', 'en'=>'he'),
  27. array('ch'=>'土耳其', 'en'=>'tr'),
  28. array('ch'=>'德语', 'en'=>'de'),
  29. array('ch'=>'法语', 'en'=>'fr')
  30. );
  31. return $languages;
  32. }
  33. function menu_get($id) {
  34. global $_W;
  35. $id = intval($id);
  36. if (empty($id)) {
  37. return array();
  38. }
  39. $menu_info = table('menu')->accountMenuInfo(array('id' => $id));
  40. if (!empty($menu_info)) {
  41. return $menu_info;
  42. } else {
  43. return array();
  44. }
  45. }
  46. function menu_default() {
  47. $result = array();
  48. $result = table('menu')->accountDefaultMenuInfo();
  49. return $result;
  50. }
  51. function menu_construct_createmenu_data($data_array, $is_conditional = false) {
  52. $menu = array();
  53. if (empty($data_array) || empty($data_array['button']) || !is_array($data_array)) {
  54. return $menu;
  55. }
  56. foreach ($data_array['button'] as $button) {
  57. $temp = array();
  58. $temp['name'] = preg_replace_callback('/\:\:([0-9a-zA-Z_-]+)\:\:/', create_function('$matches', 'return utf8_bytes(hexdec($matches[1]));'), $button['name']);
  59. $temp['name'] = urlencode($temp['name']);
  60. if (empty($button['sub_button'])) {
  61. $temp['type'] = $button['type'];
  62. if ($button['type'] == 'view') {
  63. $temp['url'] = urlencode($button['url']);
  64. } elseif ($button['type'] == 'click') {
  65. if (!empty($button['media_id']) && empty($button['key'])) {
  66. $temp['media_id'] = urlencode($button['media_id']);
  67. $temp['type'] = 'media_id';
  68. } elseif (empty($button['media_id']) && !empty($button['key'])) {
  69. $temp['type'] = 'click';
  70. $temp['key'] = urlencode($button['key']);
  71. }
  72. } elseif ($button['type'] == 'media_id' || $button['type'] == 'view_limited') {
  73. $temp['media_id'] = urlencode($button['media_id']);
  74. } elseif ($button['type'] == 'miniprogram') {
  75. $temp['appid'] = trim($button['appid']);
  76. $temp['pagepath'] = urlencode($button['pagepath']);
  77. $temp['url'] = urlencode($button['url']);
  78. } else {
  79. $temp['key'] = urlencode($button['key']);
  80. }
  81. } else {
  82. foreach ($button['sub_button'] as $sub_button) {
  83. $sub_temp = array();
  84. $sub_temp['name'] = preg_replace_callback('/\:\:([0-9a-zA-Z_-]+)\:\:/', create_function('$matches', 'return utf8_bytes(hexdec($matches[1]));'), $sub_button['name']);
  85. $sub_temp['name'] = urlencode($sub_temp['name']);
  86. $sub_temp['type'] = $sub_button['type'];
  87. if ($sub_button['type'] == 'view') {
  88. $sub_temp['url'] = urlencode($sub_button['url']);
  89. } elseif ($sub_button['type'] == 'click') {
  90. if (!empty($sub_button['media_id']) && empty($sub_button['key'])) {
  91. $sub_temp['media_id'] = urlencode($sub_button['media_id']);
  92. $sub_temp['type'] = 'media_id';
  93. } elseif (empty($sub_button['media_id']) && !empty($sub_button['key'])) {
  94. $sub_temp['type'] = 'click';
  95. $sub_temp['key'] = urlencode($sub_button['key']);
  96. }
  97. } elseif ($sub_button['type'] == 'media_id' || $sub_button['type'] == 'view_limited') {
  98. $sub_temp['media_id'] = urlencode($sub_button['media_id']);
  99. } elseif ($sub_button['type'] == 'miniprogram') {
  100. $sub_temp['appid'] = trim($sub_button['appid']);
  101. $sub_temp['pagepath'] = urlencode($sub_button['pagepath']);
  102. $sub_temp['url'] = urlencode($sub_button['url']);
  103. } else {
  104. $sub_temp['key'] = urlencode($sub_button['key']);
  105. }
  106. $temp['sub_button'][] = $sub_temp;
  107. }
  108. }
  109. $menu['button'][] = $temp;
  110. }
  111. if (empty($is_conditional) || empty($data_array['matchrule']) || !is_array($data_array['matchrule'])) {
  112. return $menu;
  113. }
  114. if($data_array['matchrule']['sex'] > 0) {
  115. $menu['matchrule']['sex'] = $data_array['matchrule']['sex'];
  116. }
  117. if($data_array['matchrule']['group_id'] != -1) {
  118. $menu['matchrule']['tag_id'] = $data_array['matchrule']['group_id'];
  119. }
  120. if($data_array['matchrule']['client_platform_type'] > 0) {
  121. $menu['matchrule']['client_platform_type'] = $data_array['matchrule']['client_platform_type'];
  122. }
  123. if(!empty($data_array['matchrule']['province'])) {
  124. $menu['matchrule']['country'] = urlencode('中国');
  125. $menu['matchrule']['province'] = urlencode($data_array['matchrule']['province']);
  126. if(!empty($data_array['matchrule']['city'])) {
  127. $menu['matchrule']['city'] = urlencode($data_array['matchrule']['city']);
  128. }
  129. }
  130. if(!empty($data_array['matchrule']['language'])) {
  131. $inarray = 0;
  132. $languages = menu_languages();
  133. foreach ($languages as $key => $value) {
  134. if(in_array($data_array['matchrule']['language'], $value, true)) {
  135. $inarray = 1;
  136. break;
  137. }
  138. }
  139. if($inarray === 1) {
  140. $menu['matchrule']['language'] = $data_array['matchrule']['language'];
  141. }
  142. }
  143. return $menu;
  144. }
  145. function menu_update_currentself() {
  146. global $_W;
  147. $account_api = WeAccount::create();
  148. $default_menu_info = $account_api->menuCurrentQuery();
  149. if (is_error($default_menu_info)) {
  150. return error(-1, $default_menu_info['message']);
  151. }
  152. if (empty($default_menu_info['is_menu_open']) || empty($default_menu_info['selfmenu_info'])) {
  153. return true;
  154. }
  155. $default_menu = $default_menu_info['selfmenu_info'];
  156. $default_sub_button = array();
  157. if (!empty($default_menu['button'])) {
  158. foreach ($default_menu['button'] as $key => &$button) {
  159. if (!empty($button['sub_button'])) {
  160. $default_sub_button[$key] = $button['sub_button'];
  161. }
  162. ksort($button);
  163. }
  164. unset($button);
  165. }
  166. ksort($default_menu);
  167. $wechat_menu_data = base64_encode(iserializer($default_menu));
  168. $all_default_menus = table('menu')->searchAccountMenuList(MENU_CURRENTSELF);
  169. if (!empty($all_default_menus)) {
  170. foreach ($all_default_menus as $menus_key => $menu_data) {
  171. if (empty($menu_data['data'])) {
  172. continue;
  173. }
  174. $single_menu_info = iunserializer(base64_decode($menu_data['data']));
  175. if (!is_array($single_menu_info) || empty($single_menu_info['button'])) {
  176. continue;
  177. }
  178. foreach ($single_menu_info['button'] as $key => &$single_button) {
  179. if (!empty($default_sub_button[$key])) {
  180. $single_button['sub_button'] = $default_sub_button[$key];
  181. } else {
  182. unset($single_button['sub_button']);
  183. }
  184. ksort($single_button);
  185. }
  186. unset($single_button);
  187. ksort($single_menu_info);
  188. $local_menu_data = base64_encode(iserializer($single_menu_info));
  189. if ($wechat_menu_data == $local_menu_data) {
  190. $default_menu_id = $menus_key;
  191. }
  192. }
  193. }
  194. if (!empty($default_menu_id)) {
  195. pdo_update('uni_account_menus', array('status' => STATUS_ON), array('id' => $default_menu_id));
  196. pdo_update('uni_account_menus', array('status' => STATUS_OFF), array('uniacid' => $_W['uniacid'], 'type' => MENU_CURRENTSELF, 'id !=' => $default_menu_id));
  197. } else {
  198. $insert_data = array(
  199. 'uniacid' => $_W['uniacid'],
  200. 'type' => MENU_CURRENTSELF,
  201. 'group_id' => -1,
  202. 'sex' => 0,
  203. 'data' => $wechat_menu_data,
  204. 'client_platform_type' => 0,
  205. 'area' => '',
  206. 'menuid' => 0,
  207. 'status' => STATUS_ON
  208. );
  209. pdo_insert('uni_account_menus', $insert_data);
  210. $insert_id = pdo_insertid();
  211. pdo_update('uni_account_menus', array('title' => '默认菜单_'.$insert_id), array('id' => $insert_id));
  212. pdo_update('uni_account_menus', array('status' => STATUS_OFF), array('uniacid' => $_W['uniacid'], 'type' => MENU_CURRENTSELF, 'id !=' => $insert_id));
  213. }
  214. return true;
  215. }
  216. function menu_update_conditional() {
  217. global $_W;
  218. $account_api = WeAccount::create();
  219. $conditional_menu_info = $account_api->menuQuery();
  220. if (is_error($conditional_menu_info)) {
  221. return error(-1, $conditional_menu_info['message']);
  222. }
  223. pdo_update('uni_account_menus', array('status' => STATUS_OFF), array('uniacid' => $_W['uniacid'], 'type' => MENU_CONDITIONAL));
  224. if (!empty($conditional_menu_info['conditionalmenu'])) {
  225. foreach ($conditional_menu_info['conditionalmenu'] as $menu) {
  226. $data = array(
  227. 'uniacid' => $_W['uniacid'],
  228. 'type' => MENU_CONDITIONAL,
  229. 'group_id' => isset($menu['matchrule']['tag_id']) ? $menu['matchrule']['tag_id'] : (isset($menu['matchrule']['group_id']) ? $menu['matchrule']['group_id'] : '-1'),
  230. 'sex' => $menu['matchrule']['sex'],
  231. 'client_platform_type' => $menu['matchrule']['client_platform_type'],
  232. 'area' => trim($menu['matchrule']['country']) . trim($menu['matchrule']['province']) . trim($menu['matchrule']['city']),
  233. 'data' => base64_encode(iserializer($menu)),
  234. 'menuid' => $menu['menuid'],
  235. 'status' => STATUS_ON,
  236. );
  237. if (!empty($menu['matchrule'])) {
  238. $menu_info = table('menu')->accountMenuInfo(array('uniacid' => $_W['uniacid'], 'menuid' => $menu['menuid'], 'type' => MENU_CONDITIONAL));
  239. $menu_id = $menu_info['id'];
  240. }
  241. if (!empty($menu_id)) {
  242. $data['title'] = !empty($menu_info['title']) ? $menu_info['title'] : '个性化菜单_' . $menu_id;
  243. pdo_update('uni_account_menus', $data, array('uniacid' => $_W['uniacid'], 'id' => $menu_id));
  244. } else {
  245. pdo_insert('uni_account_menus', $data);
  246. $insert_id = pdo_insertid();
  247. pdo_update('uni_account_menus', array('title' => '个性化菜单_'.$insert_id), array('id' => $insert_id));
  248. }
  249. }
  250. }
  251. return true;
  252. }
  253. function menu_delete($id) {
  254. global $_W;
  255. $menu_info = menu_get($id);
  256. if (empty($menu_info)) {
  257. return error(-1, '菜单不存在或已经删除');
  258. }
  259. if ($menu_info['status'] == STATUS_OFF) {
  260. pdo_delete('uni_account_menus', array('uniacid' => $_W['uniacid'], 'id' => $id));
  261. return error(0, '删除菜单成功!');
  262. }
  263. if ($menu_info['type'] == MENU_CONDITIONAL && $menu_info['menuid'] > 0 && $menu_info['status'] != STATUS_OFF) {
  264. $account_api = WeAccount::create($_W['acid']);
  265. $result = $account_api->menuDelete($menu_info['menuid']);
  266. if (is_error($result)) {
  267. return error(-1, $result['message']);
  268. }
  269. pdo_delete('uni_account_menus', array('uniacid' => $_W['uniacid'], 'id' => $id));
  270. }
  271. return true;
  272. }
  273. function menu_push($id) {
  274. global $_W;
  275. $menu_info = menu_get($id);
  276. if (empty($menu_info)) {
  277. return error(-1, '菜单不存在或已删除');
  278. }
  279. if ($menu_info['status'] == STATUS_OFF) {
  280. $post = iunserializer(base64_decode($menu_info['data']));
  281. if (empty($post)) {
  282. return error(-1, '菜单数据错误');
  283. }
  284. $is_conditional = (!empty($post['matchrule']) && $menu_info['type'] == MENU_CONDITIONAL) ? true : false;
  285. $menu = menu_construct_createmenu_data($post, $is_conditional);
  286. $account_api = WeAccount::create();
  287. $result = $account_api->menuCreate($menu);
  288. if (is_error($result)) {
  289. return error(-1, $result['message']);
  290. }
  291. if ($menu_info['type'] == MENU_CURRENTSELF) {
  292. pdo_update('uni_account_menus', array('status' => '1'), array('id' => $menu_info['id']));
  293. pdo_update('uni_account_menus', array('status' => '0'), array('id !=' => $menu_info['id'], 'uniacid' => $_W['uniacid'], 'type' => MENU_CURRENTSELF));
  294. } elseif ($menu_info['type'] == MENU_CONDITIONAL) {
  295. if ($post['matchrule']['group_id'] != -1) {
  296. $menu['matchrule']['groupid'] = $menu['matchrule']['tag_id'];
  297. unset($menu['matchrule']['tag_id']);
  298. }
  299. $status = pdo_update('uni_account_menus', array('status' => STATUS_ON, 'menuid' => $result), array('uniacid' => $_W['uniacid'], 'id' => $menu_info['id']));
  300. }
  301. return true;
  302. }
  303. if ($menu_info['status'] == STATUS_ON && $menu_info['type'] == MENU_CONDITIONAL && $menu_info['menuid'] > 0) {
  304. $account_api = WeAccount::create();
  305. $result = $account_api->menuDelete($menu_info['menuid']);
  306. if (is_error($result)) {
  307. return error(-1, $result['message']);
  308. } else {
  309. pdo_update('uni_account_menus', array('status' => STATUS_OFF), array('id' => $menu_info['id']));
  310. return true;
  311. }
  312. }
  313. }