menu.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. const {
  2. contains,
  3. cssNumber
  4. } = require("jquery");
  5. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  6. var Controller = {
  7. index: function () {
  8. var listsIndex = new Vue({
  9. el: "#listsIndex",
  10. data() {
  11. return {
  12. listData: [],
  13. currentMenu: [],
  14. currentPage: 1,
  15. limit: 10,
  16. offset: 0,
  17. totalPage: 0,
  18. }
  19. },
  20. mounted() {
  21. this.getList();
  22. },
  23. methods: {
  24. getList() {
  25. var that = this;
  26. Fast.api.ajax({
  27. url: 'shopro/wechat/menu/index',
  28. loading: true,
  29. type: 'GET',
  30. data: {
  31. limit: that.limit,
  32. offset: that.offset,
  33. }
  34. }, function (ret, res) {
  35. that.listData = res.data.rows;
  36. that.totalPage = res.data.total;
  37. that.currentMenu = res.data.currentMenu;
  38. return false;
  39. })
  40. },
  41. operation(opttype, id) {
  42. let that = this;
  43. switch (opttype) {
  44. case 'delete':
  45. that.$confirm('此操作将删除菜单, 是否继续?', '提示', {
  46. confirmButtonText: '确定',
  47. cancelButtonText: '取消',
  48. type: 'warning'
  49. }).then(() => {
  50. Fast.api.ajax({
  51. url: 'shopro/wechat/menu/del/ids/' + id,
  52. loading: true,
  53. type: 'POST',
  54. }, function (ret, res) {
  55. that.getList()
  56. })
  57. }).catch(() => {
  58. that.$message({
  59. type: 'info',
  60. message: '已取消删除'
  61. });
  62. });
  63. break;
  64. case 'status':
  65. that.$confirm('确认发布当前菜单?公众号底部菜单有延迟生效时间,您可稍等或重新关注查看', '提示', {
  66. confirmButtonText: '确定',
  67. cancelButtonText: '取消',
  68. type: 'warning'
  69. }).then(() => {
  70. Fast.api.ajax({
  71. url: `shopro/wechat/menu/publish?id=${id}`,
  72. loading: true,
  73. }, function (ret, res) {
  74. that.getList();
  75. })
  76. }).catch(() => {
  77. that.$message({
  78. type: 'info',
  79. message: '已取消'
  80. });
  81. });
  82. break;
  83. case 'create':
  84. Fast.api.open("shopro/wechat/menu/add", "新建", {
  85. callback(data) {
  86. that.getList();
  87. }
  88. });
  89. break;
  90. case 'copy':
  91. Fast.api.ajax({
  92. url: `shopro/wechat/menu/copy?id=${id}`,
  93. loading: true,
  94. }, function (ret, res) {
  95. that.getList();
  96. })
  97. break;
  98. case 'edit':
  99. Fast.api.open(`shopro/wechat/menu/edit?id=${id}`, "编辑", {
  100. callback(data) {
  101. that.getList();
  102. }
  103. });
  104. break;
  105. }
  106. },
  107. //分页
  108. pageSizeChange(val) {
  109. this.offset = 0;
  110. this.limit = val;
  111. this.getData();
  112. },
  113. pageCurrentChange(val) {
  114. this.offset = (val - 1) * 10;
  115. this.getData();
  116. },
  117. tableRowClassName({
  118. rowIndex
  119. }) {
  120. if (rowIndex % 2 == 1) {
  121. return 'bg-color';
  122. }
  123. return '';
  124. },
  125. tableCellClassName({
  126. columnIndex
  127. }) {
  128. if (columnIndex == 1 || columnIndex == 2) {
  129. return 'cell-left';
  130. }
  131. return '';
  132. },
  133. },
  134. })
  135. },
  136. add: function () {
  137. Controller.menu('add');
  138. },
  139. edit: function () {
  140. Controller.menu('edit');
  141. },
  142. menu: function (type) {
  143. var wechatMenu = new Vue({
  144. el: "#wechatMenu",
  145. data() {
  146. return {
  147. menuData: [],
  148. rightData: {},
  149. selectedIndex1: null,
  150. selectedIndex2: null,
  151. selectLevel: null,
  152. rightShow: false,
  153. menuTitle: '',
  154. edit_id: null,
  155. optType: type,
  156. viewUrl: Config.shopro.domain,
  157. wxMiniProgramapp_id: Config.wxMiniProgram.app_id,
  158. options: [],
  159. selectLimit: 6,
  160. selectOffset: 0,
  161. selectCurrentPage: 1,
  162. selectTotalPage: 0,
  163. detailForm: {
  164. type: '',
  165. content_id: {},
  166. content: {},
  167. content_title: ''
  168. }
  169. }
  170. },
  171. mounted() {
  172. if (this.optType == 'edit') {
  173. this.getmenuData();
  174. }
  175. },
  176. methods: {
  177. typeChange() {
  178. this.getoptions()
  179. this.rightData.media_id = "";
  180. },
  181. selectChange(val) {
  182. if (this.rightData.media_type=='news') {
  183. let num = 0
  184. this.options.forEach(i => {
  185. if (i.media_id == val) {
  186. num++
  187. }
  188. })
  189. if(num>1){
  190. this.rightData.media_id=''
  191. this.$notify({
  192. title: '警告',
  193. message: '不支持选择多条图文消息',
  194. type: 'warning'
  195. });
  196. return false;
  197. }
  198. }
  199. },
  200. getoptions() {
  201. let that = this;
  202. Fast.api.ajax({
  203. url: 'shopro/wechat/material/select',
  204. loading: true,
  205. type: 'GET',
  206. data: {
  207. limit: that.selectLimit,
  208. offset: that.selectOffset,
  209. type: that.rightData.media_type
  210. }
  211. }, function (ret, res) {
  212. that.options = [];
  213. if (that.rightData.media_type == 'news') {
  214. res.data.rows.forEach(i => {
  215. i.content.news_item.forEach(e => {
  216. that.options.push({
  217. media_id: i.media_id,
  218. title: e.title,
  219. thumb_url: e.thumb_url
  220. })
  221. })
  222. })
  223. } else if (that.rightData.media_type == 'image') {
  224. res.data.rows.forEach(i => {
  225. that.options.push({
  226. media_id: i.media_id,
  227. title: i.name,
  228. thumb_url: i.url
  229. })
  230. })
  231. } else if (that.rightData.media_type == 'video') {
  232. res.data.rows.forEach(i => {
  233. that.options.push({
  234. media_id: i.media_id,
  235. title: i.name,
  236. thumb_url: i.cover_url
  237. })
  238. })
  239. } else if (that.rightData.media_type == 'voice') {
  240. res.data.rows.forEach(i => {
  241. that.options.push({
  242. media_id: i.media_id,
  243. title: i.name,
  244. thumb_url: ''
  245. })
  246. })
  247. } else if (that.rightData.media_type == 'text') {
  248. res.data.rows.forEach(i => {
  249. that.options.push({
  250. media_id: i.id,
  251. title: i.name,
  252. thumb_url: JSON.parse(i.content)
  253. })
  254. })
  255. } else if (that.rightData.media_type == 'link') {
  256. res.data.rows.forEach(i => {
  257. that.options.push({
  258. media_id: i.id,
  259. title: i.name,
  260. thumb_url: JSON.parse(i.content).url,
  261. image: JSON.parse(i.content).image,
  262. description: JSON.parse(i.content).description,
  263. })
  264. })
  265. }
  266. that.selectTotalPage = res.data.total;
  267. return false;
  268. })
  269. },
  270. selectSizeChange(val) {
  271. this.selectOffset = 0;
  272. this.selectLimit = val;
  273. this.selectCurrentPage = 1;
  274. this.getoptions();
  275. },
  276. selectCurrentChange(val) {
  277. this.selectOffset = (val - 1) * 6;
  278. this.selectLimit = 6;
  279. this.selectCurrentPage = 1;
  280. this.getoptions();
  281. },
  282. createTemplate() {
  283. let that=this;
  284. Fast.api.open(`shopro/wechat/material/add?type=${that.rightData.media_type}`, '创建', {
  285. callback(data) {
  286. that.getoptions();
  287. }
  288. })
  289. },
  290. getmenuData() {
  291. var that = this;
  292. if (Config.row.content) {
  293. that.menuData = JSON.parse(Config.row.content);
  294. that.menuData.forEach(i => {
  295. i.selected = false;
  296. i.show = false;
  297. if (!i.appid) {
  298. i.appid = '';
  299. i.pagepath = '';
  300. }
  301. this.$set(i, 'media_type', i.key ? i.key.split('|')[0] : '')
  302. this.$set(i, 'media_id', i.key ? i.key.split('|')[1] : '')
  303. if (i.sub_button) {
  304. i.sub_button.forEach(j => {
  305. j.selected = false;
  306. if (!j.appid) {
  307. j.appid = '';
  308. j.pagepath = '';
  309. }
  310. this.$set(j, 'media_type', j.key ? j.key.split('|')[0] : '')
  311. this.$set(j, 'media_id', j.key ? j.key.split('|')[1] : '')
  312. })
  313. } else {
  314. i.sub_button = []
  315. }
  316. })
  317. } else {
  318. that.menuData = []
  319. }
  320. that.edit_id = Config.row.id;
  321. that.menuTitle = Config.row.name;
  322. },
  323. changeRadio(e) {
  324. if(e=='click'){
  325. this.rightData.url = "";
  326. this.rightData.appid = "";
  327. this.rightData.pagepath = "";
  328. }else{
  329. this.rightData.key=''
  330. }
  331. this.$forceUpdate();
  332. },
  333. menuSelect(index1, index2) {
  334. this.selectedIndex1 = index1;
  335. this.selectedIndex2 = index2;
  336. this.rightShow = true;
  337. this.menuData.forEach(i => {
  338. i.selected = false;
  339. i.show = false;
  340. if (i.sub_button) {
  341. i.sub_button.forEach(j => {
  342. j.selected = false;
  343. })
  344. }
  345. });
  346. this.menuData[index1].show = true;
  347. //选择1
  348. if (index2 == null) {
  349. this.selectLevel = 1;
  350. this.menuData[index1].selected = true;
  351. this.menuData[index1].show = true;
  352. this.rightData = this.menuData[index1];
  353. } else {
  354. this.selectLevel = 2;
  355. this.menuData[index1].sub_button[index2].selected = true;
  356. this.rightData = this.menuData[index1].sub_button[index2];
  357. }
  358. if (this.rightData.media_type) {
  359. this.getoptions();
  360. }
  361. },
  362. addMenu(index, level) {
  363. //右侧显示
  364. this.rightShow = true;
  365. this.selectLevel = level;
  366. if (index != null) {
  367. this.selectedIndex1 = index;
  368. this.menuData.forEach(i => {
  369. i.selected = false;
  370. if (i.sub_button) {
  371. i.sub_button.forEach(j => {
  372. j.selected = false;
  373. })
  374. }
  375. });
  376. this.menuData[index].sub_button.push({
  377. name: '',
  378. type: 'view',
  379. selected: true,
  380. url: '',
  381. appid: '',
  382. pagepath: '',
  383. media_type: '',
  384. media_id: '',
  385. })
  386. this.rightData = this.menuData[index].sub_button[this.menuData[index].sub_button.length - 1];
  387. this.selectedIndex2 = this.menuData[index].sub_button.length - 1;
  388. } else {
  389. this.menuData.forEach(i => {
  390. i.selected = false;
  391. i.show = false;
  392. });
  393. this.menuData.push({
  394. name: '',
  395. selected: true,
  396. show: true,
  397. type: 'view',
  398. url: '',
  399. appid: '',
  400. pagepath: '',
  401. sub_button: [],
  402. media_type: '',
  403. media_id: '',
  404. })
  405. this.selectedIndex1 = this.menuData.length - 1;
  406. this.rightData = this.menuData[this.menuData.length - 1];
  407. }
  408. },
  409. delMenu() {
  410. if (this.selectedIndex2 != null) {
  411. this.menuData[this.selectedIndex1].sub_button.splice(this.selectedIndex2, 1);
  412. if (this.menuData[this.selectedIndex1].sub_button.length > 0) {
  413. if (this.selectedIndex2 == 0) {
  414. this.menuData[this.selectedIndex1].sub_button[0].selected = true;
  415. this.rightData = this.menuData[this.selectedIndex1].sub_button[0];
  416. } else {
  417. this.menuData[this.selectedIndex1].sub_button[this.selectedIndex2 - 1].selected = true;
  418. this.rightData = this.menuData[this.selectedIndex1].sub_button[this.selectedIndex2 - 1];
  419. this.selectedIndex2--
  420. }
  421. } else {
  422. this.rightData = {};
  423. this.rightShow = false;
  424. }
  425. } else {
  426. this.menuData.splice(this.selectedIndex1, 1);
  427. if (this.menuData.length > 0) {
  428. if (this.selectedIndex1 == 0) {
  429. this.menuData[0].selected = true;
  430. this.menuData[0].show = true;
  431. this.rightData = this.menuData[0];
  432. } else {
  433. this.menuData[this.selectedIndex1 - 1].selected = true;
  434. this.menuData[this.selectedIndex1 - 1].show = true;
  435. this.rightData = this.menuData[this.selectedIndex1 - 1];
  436. this.selectedIndex1--
  437. }
  438. } else {
  439. this.rightData = {};
  440. this.rightShow = false;
  441. }
  442. }
  443. },
  444. choosePath() {
  445. let that = this;
  446. let multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  447. parent.Fast.api.open("shopro/link/select?multiple=" + multiple, "选择路径", {
  448. callback: function (data) {
  449. let link_path=''
  450. if(data.data.path!='/pages/index/index'){
  451. link_path=data.data.path.substring(1)
  452. }
  453. if (that.selectedIndex2 != null) {
  454. if (that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].type == 'view') {
  455. that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].url = that.viewUrl + link_path;
  456. } else {
  457. that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].pagepath = '/pages/index/index?page='+encodeURIComponent(link_path);
  458. that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].url = that.viewUrl + link_path;
  459. that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].appid = that.wxMiniProgramapp_id;
  460. that.rightData.appid = that.wxMiniProgramapp_id;
  461. }
  462. that.rightData.url = that.menuData[that.selectedIndex1].sub_button[that.selectedIndex2].url;
  463. } else {
  464. if (that.menuData[that.selectedIndex1].type == 'view') {
  465. that.menuData[that.selectedIndex1].url = that.viewUrl + link_path;
  466. } else {
  467. that.menuData[that.selectedIndex1].url = that.viewUrl + link_path;
  468. that.menuData[that.selectedIndex1].pagepath = link_path?'/pages/index/index?page='+encodeURIComponent(link_path):'/pages/index/index';
  469. that.menuData[that.selectedIndex1].appid = that.wxMiniProgramapp_id;
  470. that.rightData.appid = that.wxMiniProgramapp_id;
  471. }
  472. that.rightData.url = that.menuData[that.selectedIndex1].url;
  473. that.rightData.pagepath = that.menuData[that.selectedIndex1].pagepath;
  474. }
  475. }
  476. });
  477. },
  478. menuHide() {
  479. this.selectedIndex1 = null;
  480. this.selectedIndex2 = null;
  481. this.menuData.forEach(i => {
  482. i.selected = false;
  483. i.show = false;
  484. if (i.sub_button.length > 0) {
  485. i.sub_button.forEach(j => {
  486. j.selected = false;
  487. })
  488. }
  489. });
  490. this.rightShow = false;
  491. },
  492. menuShow() {
  493. this.rightShow = true;
  494. },
  495. subData(type) {
  496. let that = this;
  497. if (that.menuTitle == '') {
  498. that.$notify({
  499. title: '警告',
  500. message: '请输入标题',
  501. type: 'warning'
  502. });
  503. return false;
  504. }
  505. if (that.menuData.length == 0) {
  506. that.$notify({
  507. title: '警告',
  508. message: '请输入菜单内容',
  509. type: 'warning'
  510. });
  511. return false;
  512. }
  513. let savemenuData = JSON.parse(JSON.stringify(that.menuData))
  514. savemenuData.forEach(i => {
  515. delete i.show;
  516. delete i.selected;
  517. if (i.sub_button.length > 0) {
  518. delete i.url;
  519. delete i.appid;
  520. delete i.pagepath;
  521. delete i.type;
  522. delete i.key;
  523. i.sub_button.forEach(j => {
  524. delete j.selected;
  525. if (j.type == 'view') {
  526. delete j.appid;
  527. delete j.pagepath;
  528. } else if (j.type == 'click') {
  529. delete j.appid;
  530. delete j.pagepath;
  531. delete j.url;
  532. let type = j.media_type;
  533. let id = j.media_id
  534. this.$set(j, 'key', type + '|' + id)
  535. }
  536. delete j.media_type;
  537. delete j.media_id;
  538. })
  539. } else {
  540. delete i.sub_button;
  541. if (i.type) {
  542. if (i.type == 'view') {
  543. delete i.appid;
  544. delete i.pagepath;
  545. } else if (i.type == 'click') {
  546. delete i.appid;
  547. delete i.pagepath;
  548. delete i.url;
  549. let type = i.media_type
  550. let id = i.media_id
  551. this.$set(i, 'key', type + '|' + id)
  552. }
  553. }
  554. }
  555. delete i.media_type;
  556. delete i.media_id;
  557. })
  558. var urlsub = ''
  559. if (that.optType == 'edit') {
  560. if (type == 'publish') {
  561. urlsub = 'shopro/wechat/menu/edit?id=' + that.edit_id + '&act=' + type
  562. } else {
  563. urlsub = 'shopro/wechat/menu/edit?id=' + that.edit_id
  564. }
  565. } else {
  566. if (type == 'publish') {
  567. urlsub = 'shopro/wechat/menu/add?act=' + type
  568. } else {
  569. urlsub = 'shopro/wechat/menu/add'
  570. }
  571. }
  572. // return false;
  573. Fast.api.ajax({
  574. url: urlsub,
  575. loading: true,
  576. type: 'POST',
  577. data: {
  578. data: JSON.stringify({
  579. name: that.menuTitle,
  580. type: 'menu',
  581. content: savemenuData
  582. })
  583. }
  584. }, function (ret, res) {
  585. Fast.api.close()
  586. })
  587. },
  588. },
  589. watch: {
  590. rightData: {
  591. handler: function (newVal) {
  592. if (this.selectLevel == 1) {
  593. let name = newVal.name;
  594. let num = 0;
  595. for (var i = 0; i < name.length; i++) {
  596. if (name[i].charCodeAt() >= 20 && name[i].charCodeAt() <= 127) {
  597. num++
  598. } else {
  599. num += 2
  600. }
  601. if (num > 8) {
  602. newVal.name = newVal.name.substr(0, i)
  603. }
  604. }
  605. } else if (this.selectLevel == 2) {
  606. let name = newVal.name;
  607. let num = 0;
  608. for (var i = 0; i < name.length; i++) {
  609. if (name[i].charCodeAt() >= 20 && name[i].charCodeAt() <= 127) {
  610. num++
  611. } else {
  612. num += 2
  613. }
  614. if (num > 16) {
  615. newVal.name = newVal.name.substr(0, i)
  616. }
  617. }
  618. }
  619. },
  620. deep: true
  621. }
  622. }
  623. })
  624. },
  625. api: {
  626. bindevent: function () {
  627. Form.api.bindevent($("form[role=form]"));
  628. }
  629. }
  630. };
  631. return Controller;
  632. });