menu.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'sortable'], function ($, undefined, Backend, Table, Form, Sortable) {
  2. var Controller = {
  3. index: function () {
  4. var typeList = [
  5. {name: "click", title: "发送消息"},
  6. {name: "view", title: "跳转网页"},
  7. {name: "miniprogram", title: "跳转小程序"},
  8. {name: "scancode_push", title: "扫码推"},
  9. {name: "scancode_waitmsg", title: "扫码推提示框"},
  10. {name: "pic_sysphoto", title: "拍照发图"},
  11. {name: "pic_photo_or_album", title: "拍照相册发图"},
  12. {name: "pic_weixin", title: "相册发图"},
  13. {name: "location_select", title: "地理位置选择"},
  14. ];
  15. String.prototype.subByte = function (start, bytes) {
  16. for (var i = start; bytes > 0; i++) {
  17. var code = this.charCodeAt(i);
  18. bytes -= code < 256 ? 1 : 2;
  19. }
  20. return this.slice(start, i + bytes)
  21. };
  22. //初始化菜单
  23. $("#menu-list").prepend(Template("menutpl", {menu: menu}));
  24. //拖动排序
  25. new Sortable($("#menu-list")[0], {
  26. draggable: 'li.menu-item',
  27. onEnd: function () {
  28. updateChangeMenu();
  29. }
  30. });
  31. //子菜单拖动排序
  32. $(".sub-menu-list").each(function () {
  33. new Sortable(this, {
  34. draggable: 'li.sub-menu-item',
  35. onEnd: function () {
  36. updateChangeMenu();
  37. }
  38. });
  39. });
  40. //添加主菜单
  41. $(document).on('click', '#add-item', function () {
  42. var menu_item_total = $(".menu-item").size();
  43. if (menu_item_total < 3) {
  44. var item = '<li class="menu-item" data-type="click" data-key="" data-name="添加菜单" > <a href="javascript:;" class="menu-link"> <i class="icon-menu-dot"></i> <i class="weixin-icon sort-gray"></i> <span class="title">添加菜单</span> </a> <div class="sub-menu-box" style=""> <ul class="sub-menu-list"><li class=" add-sub-item"><a href="javascript:;" title="添加子菜单"><span class=" "><i class="weixin-icon add-gray"></i></span></a></li> </ul> <i class="arrow arrow-out"></i> <i class="arrow arrow-in"></i> </div></li>';
  45. var itemDom = $(item);
  46. itemDom.insertBefore(this);
  47. itemDom.trigger("click");
  48. $("#item_title").focus().select();
  49. $(".sub-menu-box", itemDom).show();
  50. updateChangeMenu();
  51. new Sortable($(".sub-menu-list", itemDom)[0], {draggable: 'li.sub-menu-item'});
  52. }
  53. });
  54. //切换类型
  55. $(document).on('change', 'input[name=type]', function () {
  56. $(".sub-menu-item.current,.menu-item.current").data("type", $(this).val()).trigger("click");
  57. });
  58. //删除菜单
  59. $(document).on('click', '#item_delete', function () {
  60. var current = $("#menu-list li.current");
  61. var prev = current.prev("li[data-type]");
  62. var next = current.next("li[data-type]");
  63. if (prev.size() == 0 && next.size() == 0 && $(".sub-menu-box", current).size() == 0) {
  64. last = current.closest(".menu-item");
  65. } else if (prev.size() > 0 || next.size() > 0) {
  66. last = prev.size() > 0 ? prev : next;
  67. } else {
  68. last = null;
  69. $(".weixin-content").hide();
  70. $(".no-weixin-content").show();
  71. }
  72. if (current.hasClass("sub-menu-item")) {
  73. $(".add-sub-item", current.parent()).removeClass("hidden");
  74. }
  75. current.remove();
  76. if (last) {
  77. last.trigger('click');
  78. } else {
  79. $("input[name='name']").val('');
  80. }
  81. updateChangeMenu();
  82. });
  83. //更新修改与变动
  84. var updateChangeMenu = function () {
  85. var item = $("#menu-list li.current");
  86. var values = $("#form-item").serializeArray();
  87. $.each(values, function (i, j) {
  88. if (j.name == 'name') {
  89. $(">a", item).html(j.value);
  90. }
  91. item.data(j.name, j.value);
  92. });
  93. menuUpdate();
  94. };
  95. //更新菜单数据
  96. var menuUpdate = function () {
  97. $.post("wechat/menu/edit", {menu: JSON.stringify(getMenuList())}, function (data) {
  98. if (data['code'] == 1) {
  99. } else {
  100. Toastr.error(__('Operation failed'));
  101. }
  102. }, 'json');
  103. };
  104. //获取菜单数据
  105. var getMenuList = function () {
  106. var menus = [];
  107. var sub_button = [];
  108. var menu_i = 0;
  109. var sub_menu_i = 0;
  110. var item;
  111. $("#menu-list li").each(function (i) {
  112. item = $(this);
  113. var name = item.data('name');
  114. if (name != null) {
  115. if (item.hasClass('menu-item')) {
  116. sub_menu_i = 0;
  117. if (item.find('.sub-menu-item').size() > 0) {
  118. menus[menu_i] = {"name": name, "sub_button": "sub_button"}
  119. } else {
  120. menus[menu_i] = $(this).data();
  121. }
  122. if (menu_i > 0) {
  123. menus[menu_i - 1]['sub_button'] = menus[menu_i - 1]['sub_button'] == "sub_button" ? sub_button : menus[menu_i - 1]['sub_button'];
  124. }
  125. sub_button = [];
  126. menu_i++;
  127. } else {
  128. sub_button[sub_menu_i++] = $(this).data();
  129. }
  130. }
  131. });
  132. if (sub_button.length > 0) {
  133. var len = menus.length;
  134. menus[len - 1]['sub_button'] = sub_button;
  135. }
  136. return menus;
  137. };
  138. //添加子菜单
  139. $(document).on('click', ".add-sub-item", function () {
  140. var sub_menu_item_total = $(this).parent().find(".sub-menu-item").size();
  141. if (sub_menu_item_total < 5) {
  142. var item = '<li class="sub-menu-item" data-type="click" data-key="" data-name="添加子菜单"><a href="javascript:;"><span class=" "><i class="weixin-icon sort-gray"></i><span class="sub-title">添加子菜单</span></span></a></li>';
  143. var itemDom = $(item);
  144. itemDom.insertBefore(this);
  145. itemDom.trigger("click");
  146. $("#item_title").focus().select();
  147. updateChangeMenu();
  148. if (sub_menu_item_total == 4) {
  149. $(this).addClass("hidden");
  150. }
  151. }
  152. return false;
  153. });
  154. //主菜单子菜单点击事件
  155. $(document).on('click', ".menu-item, .sub-menu-item", function () {
  156. var hasChild = $(".sub-menu-item", this).size() > 0 ? true : false;
  157. if ($(this).hasClass("sub-menu-item")) {
  158. $("#menu-list li").removeClass('current');
  159. } else {
  160. $("#menu-list li").removeClass('current');
  161. $("#menu-list > li").not(this).find(".sub-menu-box").hide();
  162. $(".sub-menu-box", this).toggle();
  163. }
  164. $(this).addClass('current');
  165. var data = $.extend({}, $(this).data());
  166. data.keytitle = data.key && typeof responselist[data.key] != 'undefined' ? responselist[data.key] : '';
  167. data.typeList = typeList;
  168. data.hasChild = hasChild;
  169. data.first = $(this).hasClass("menu-item") ? true : false;
  170. $(".weixin-content").show();
  171. $(".no-weixin-content").hide();
  172. $("#item-body").html(Template("itemtpl", data));
  173. return false;
  174. });
  175. //触发保存
  176. $("form#form-item").on('change', "input,textarea", function () {
  177. updateChangeMenu();
  178. });
  179. //加载远程菜单
  180. $(document).on('click', "#menuRemote", function () {
  181. Layer.confirm("确认加载远程微信服务端菜单?<br>本地菜单将会被覆盖!!!", {icon: 3}, function () {
  182. $.post("wechat/menu/remote", {}, function (ret) {
  183. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  184. if (ret.code == 1) {
  185. Layer.closeAll();
  186. Layer.alert('远程菜单覆盖成功!点击确定刷新页面!', {icon: 1}, function () {
  187. location.reload();
  188. });
  189. } else {
  190. Backend.api.toastr.error(msg ? msg : __('Operation failed'));
  191. }
  192. }, 'json');
  193. });
  194. return false;
  195. });
  196. //点击同步
  197. $(document).on('click', "#menuSyn", function () {
  198. $.post("wechat/menu/sync", {}, function (ret) {
  199. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  200. if (ret.code == 1) {
  201. Backend.api.toastr.success('菜单同步更新成功,生效时间请查看微信官网说明,或你重新关注公众号!');
  202. } else {
  203. Backend.api.toastr.error(msg ? msg : __('Operation failed'));
  204. }
  205. }, 'json');
  206. });
  207. //刷新资源
  208. var refreshkey = function (data) {
  209. responselist[data.eventkey] = data.title;
  210. $("input[name=key]").val(data.eventkey).trigger("change");
  211. $("#menu-list li.current").trigger("click");
  212. Layer.closeAll();
  213. };
  214. //选择资源
  215. $(document).on('click', "#select-resources", function () {
  216. var key = $("#key").val();
  217. Backend.api.open($(this).attr("href") + "?key=" + key, __('Select'), {
  218. callback: refreshkey
  219. });
  220. return false;
  221. });
  222. //添加资源
  223. $(document).on('click', "#add-resources", function () {
  224. Backend.api.open($(this).attr("href") + "?key=" + key, __('Add'), {
  225. callback: refreshkey
  226. });
  227. return false;
  228. });
  229. $("#menu-list li.menu-item:first").trigger("click");
  230. },
  231. add: function () {
  232. Form.api.bindevent($("form[role=form]"));
  233. },
  234. edit: function () {
  235. Form.api.bindevent($("form[role=form]"));
  236. }
  237. };
  238. return Controller;
  239. });