Browse Source

Merge remote-tracking branch 'origin/master'

yu_ber 4 years ago
parent
commit
23f4bd0ece

+ 174 - 115
UI/src/pages/sellmanage/customMenu.js

@@ -3,7 +3,9 @@ import { Row, Col, Icon, message, Input, Button, Modal, Form, Tabs } from "antd"
 import header from './header.png';
 import keydown from './keydown.png';
 import hint from './hint.png';
-import { PlusOutlined } from '@ant-design/icons';
+import { connect } from "dva";
+import { config, request } from "utils";
+const { APIV4,WXMpAPI } = config;
 
 const { TabPane } = Tabs;
 const { TextArea } = Input;
@@ -17,82 +19,82 @@ class customMenu extends React.Component {
             editType: 1,
             dropText: "删除菜单",
             // 选中的菜单坐标,faIndex必须存在[0-2],subIndex可以为空,为空时删除父菜单
-            selectMenu: {faIndex: null, subIndex: null},
+            selectMenu: { faIndex: null, subIndex: null },
             testOption: {},
             menuData: {
                 "menu": {
                     "buttons": [
-                        {
-                            "type": "click",
-                            "name": "今日歌曲",
-                            "key": "V1001_TODAY_MUSIC",
-                            "url": null,
-                            "mediaId": null,
-                            "appId": null,
-                            "pagePath": null,
-                            "subButtons": []
-                        },
-                        {
-                            "type": null,
-                            "name": "菜单",
-                            "key": null,
-                            "url": null,
-                            "mediaId": null,
-                            "appId": null,
-                            "pagePath": null,
-                            "subButtons": [
-                                {
-                                    "type": "view",
-                                    "name": "搜索",
-                                    "key": null,
-                                    "url": "http://www.soso.com/",
-                                    "mediaId": null,
-                                    "appId": null,
-                                    "pagePath": null,
-                                    "subButtons": []
-                                },
-                                {
-                                    "type": "view",
-                                    "name": "视频",
-                                    "key": null,
-                                    "url": "http://v.qq.com/",
-                                    "mediaId": null,
-                                    "appId": null,
-                                    "pagePath": null,
-                                    "subButtons": []
-                                },
-                                {
-                                    "type": "click",
-                                    "name": "赞一下我们",
-                                    "key": "V1001_GOOD",
-                                    "url": null,
-                                    "mediaId": null,
-                                    "appId": null,
-                                    "pagePath": null,
-                                    "subButtons": []
-                                },
-                                {
-                                    "type": "view",
-                                    "name": "获取用户信息",
-                                    "key": null,
-                                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4f8c9fef3896ad71&redirect_uri=http%3A%2F%2Flocalhost%2Fwx%2Fredirect%2Fwx4f8c9fef3896ad71%2Fgreet&response_type=code&scope=snsapi_userinfo&state=&connect_redirect=1#wechat_redirect",
-                                    "mediaId": null,
-                                    "appId": null,
-                                    "pagePath": null,
-                                    "subButtons": []
-                                }
-                            ]
-                        },
-                        {
-                            "type": "click",
-                            "name": "戳我",
-                            "key": "V1001_TODAY_MUSIC",
-                            "url": null,
-                            "mediaId": null,
-                            "appId": null,
-                            "pagePath": null,
-                            "subButtons": []
-                        }
+                        // {
+                        //     "type": "click",
+                        //     "name": "今日歌曲",
+                        //     "key": "V1001_TODAY_MUSIC",
+                        //     "url": null,
+                        //     "mediaId": null,
+                        //     "appId": null,
+                        //     "pagePath": null,
+                        //     "subButtons": []
+                        // },
+                        // {
+                        //     "type": null,
+                        //     "name": "菜单",
+                        //     "key": null,
+                        //     "url": null,
+                        //     "mediaId": null,
+                        //     "appId": null,
+                        //     "pagePath": null,
+                        //     "subButtons": [
+                        //         {
+                        //             "type": "view",
+                        //             "name": "搜索",
+                        //             "key": null,
+                        //             "url": "http://www.soso.com/",
+                        //             "mediaId": null,
+                        //             "appId": null,
+                        //             "pagePath": null,
+                        //             "subButtons": []
+                        //         },
+                        //         {
+                        //             "type": "view",
+                        //             "name": "视频",
+                        //             "key": null,
+                        //             "url": "http://v.qq.com/",
+                        //             "mediaId": null,
+                        //             "appId": null,
+                        //             "pagePath": null,
+                        //             "subButtons": []
+                        //         },
+                        //         {
+                        //             "type": "click",
+                        //             "name": "赞一下我们",
+                        //             "key": "V1001_GOOD",
+                        //             "url": null,
+                        //             "mediaId": null,
+                        //             "appId": null,
+                        //             "pagePath": null,
+                        //             "subButtons": []
+                        //         },
+                        //         {
+                        //             "type": "view",
+                        //             "name": "获取用户信息",
+                        //             "key": null,
+                        //             "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4f8c9fef3896ad71&redirect_uri=http%3A%2F%2Flocalhost%2Fwx%2Fredirect%2Fwx4f8c9fef3896ad71%2Fgreet&response_type=code&scope=snsapi_userinfo&state=&connect_redirect=1#wechat_redirect",
+                        //             "mediaId": null,
+                        //             "appId": null,
+                        //             "pagePath": null,
+                        //             "subButtons": []
+                        //         }
+                        //     ]
+                        // },
+                        // {
+                        //     "type": "click",
+                        //     "name": "戳我",
+                        //     "key": "V1001_TODAY_MUSIC",
+                        //     "url": null,
+                        //     "mediaId": null,
+                        //     "appId": null,
+                        //     "pagePath": null,
+                        //     "subButtons": []
+                        // }
                     ],
                     "rule": null,
                     "menuId": null
@@ -103,6 +105,18 @@ class customMenu extends React.Component {
 
     };
 
+    componentWillMount() {
+        let self = this;
+        request({
+            // url: WXMpAPI + "/wx/menu/" + self.props.wxAppIdValue + "/get",
+            url: WXMpAPI + "/wx/menu/wx4f8c9fef3896ad71/get",
+            method: "get"
+        }).then(result => {
+            console.log(result);
+            
+        });
+    }
+
 
     // 点击菜单时
     /**
@@ -114,12 +128,12 @@ class customMenu extends React.Component {
     clickFaMenu(index) {
         // console.log(e);
         let faMenuArray = this.state.menuData.menu.buttons;
-        let editType, dropText="删除菜单";
-        let selectMenu = {faIndex: index, subIndex: null};
+        let editType, dropText = "删除菜单";
+        let selectMenu = { faIndex: index, subIndex: null };
         // console.log(faMenuArray[index]);
-        if(faMenuArray[index].subButtons.length>0){
+        if (faMenuArray[index].subButtons.length > 0) {
             editType = 1;
-        }else{
+        } else {
             editType = 2;
         }
         // console.log(editType);
@@ -132,7 +146,7 @@ class customMenu extends React.Component {
     clickSubMenu(index, i) {
         console.log(index);
         console.log(i);
-        let selectMenu = {faIndex: index, subIndex: i};
+        let selectMenu = { faIndex: index, subIndex: i };
         let editType = 2;
         this.setState({
             editType: editType,
@@ -143,8 +157,8 @@ class customMenu extends React.Component {
         let jsonData = this.state.menuData;
         let length = jsonData.menu.buttons.length;
         console.log(index)
-        // 暂时只添加子菜单的逻辑
-        if(index<length){
+        // 添加子菜单
+        if (index < length) {
             jsonData.menu.buttons[index].subButtons.push({
                 "type": "",
                 "name": "请输入",
@@ -155,6 +169,19 @@ class customMenu extends React.Component {
                 "pagePath": null,
             })
         }
+        // 添加父菜单
+        else {
+            jsonData.menu.buttons.push({
+                "type": "",
+                "name": "请输入",
+                "key": null,
+                "url": null,
+                "mediaId": null,
+                "appId": null,
+                "pagePath": null,
+                "subButtons": []
+            });
+        }
         console.log(jsonData);
         this.setState({
             menuData: jsonData
@@ -166,22 +193,22 @@ class customMenu extends React.Component {
         let jsonData = this.state.menuData;
         let faIndex = this.state.selectMenu.faIndex;
         let subIndex = this.state.selectMenu.subIndex;
-        if(faIndex==null){
+        if (faIndex == null) {
             message.warn("请选择需要删除的菜单!");
             return;
         }
-        if(faIndex!=null && subIndex==null){
+        if (faIndex != null && subIndex == null) {
             // TODO 添加确认的提示
             jsonData.menu.buttons.splice(faIndex, 1);
         }
         // console.log(jsonData.menu.buttons)
-        if(faIndex!=null && subIndex!=null){
+        if (faIndex != null && subIndex != null) {
             jsonData.menu.buttons[faIndex].subButtons.splice(subIndex, 1);
         }
         console.log(jsonData);
         this.setState({
             menuData: jsonData,
-            selectMenu: {faIndex: null, subIndex: null}
+            selectMenu: { faIndex: null, subIndex: null }
         })
     }
 
@@ -189,7 +216,9 @@ class customMenu extends React.Component {
 
     render() {
         let self = this;
+        let falg = 0;
 
+        console.log(123456)
         // 左边手机样式
         const phoneCss = {
             width: 322,
@@ -265,17 +294,24 @@ class customMenu extends React.Component {
                                 <img src={keydown} style={{ float: "left", width: 33 }} />
                                 {
                                     // 此处根据menuData数据进行渲染
-                                    [{name: "占位"}, {name: "占位"}, {name: "占位"}].map((abs, index) => {
+                                    [{ name: "占位" }, { name: "占位" }, { name: "占位" }].map((abs, index) => {
                                         let faMenu = self.state.menuData.menu.buttons[index];
-                                        if(faMenu==null){
-
-                                        }
-                                        let subMenuArray = faMenu.subButtons;
-                                        let subLength = subMenuArray.length ;
+                                        let subMenuArray = [];
+                                        let subLength = 0;
                                         let plus = [];
-                                        if (subMenuArray.length < 5) {
-                                            plus = [{ add: true }]
+                                        // 如果没有父菜单,则显示+号按钮
+                                        if (faMenu === undefined || faMenu === null) {
+                                            subLength = -1;
+                                            plus = [{ add: true }];
+                                            falg++;
+                                        } else {
+                                            subMenuArray = faMenu.subButtons;
+                                            subLength = subMenuArray.length;
+                                            if (subMenuArray.length < 5) {
+                                                plus = [{ add: true }]
+                                            }
                                         }
+
                                         return (
                                             <div style={{
                                                 float: "left",
@@ -285,44 +321,60 @@ class customMenu extends React.Component {
                                                 position: "relative",
                                                 left: index == 0 ? 1 : 0
                                             }}>
-                                                <Button id={"fa"+index} onClick={$ => self.clickFaMenu(index)} style={{
-                                                    width: "95.66px",
-                                                    height: "36px",
-                                                    borderRadius: 0,
-                                                    textAlign: "center",
-                                                    lineHeight: "36px",
-                                                    top: -1,
-                                                    position: "absolute",
-                                                    left: index == 0 ? -1 : 0// 不知道为什么总是少一个像素
-                                                }}>{faMenu.name}</Button>
                                                 {
-                                                    subMenuArray.map((subMenu, i) => {
+                                                    // 渲染父菜单
+                                                    (faMenu === undefined || faMenu === null) ? [] : [faMenu].map((item) => {
+
                                                         return (
-                                                            <Button id={"sub"+i} onClick={$ => self.clickSubMenu(index, i)} style={{
+                                                            <Button id={"fa" + index} onClick={$ => self.clickFaMenu(index)} style={{
                                                                 width: "95.66px",
                                                                 height: "36px",
                                                                 borderRadius: 0,
+                                                                textAlign: "center",
+                                                                lineHeight: "36px",
+                                                                top: -1,
                                                                 position: "absolute",
-                                                                top: -(36 * (1+i) + 1),
-                                                                left: index == 0 ? -1 : 0
-                                                            }}>{subMenu.name.length <= 4 ? subMenu.name : subMenu.name.substring(0, 4) + "..."}</Button>
+                                                                left: index == 0 ? -1 : 0// 不知道为什么总是少一个像素
+                                                            }}>{item.name}</Button>
                                                         );
+
                                                     })
                                                 }
                                                 {
-                                                    plus.map(() => {
+                                                    // 渲染子菜单
+                                                    subMenuArray.map((subMenu, i) => {
                                                         return (
-                                                            <Button id={"add"+index} onClick={$ => self.clickAdd(index)} style={{
+                                                            <Button id={"sub" + i} onClick={$ => self.clickSubMenu(index, i)} style={{
                                                                 width: "95.66px",
                                                                 height: "36px",
                                                                 borderRadius: 0,
                                                                 position: "absolute",
-                                                                top: -((subLength+1) * 36 + 1),
+                                                                top: -(36 * (1 + i) + 1),
                                                                 left: index == 0 ? -1 : 0
-                                                            }}><Icon type="plus" /></Button>
+                                                            }}>{subMenu.name.length <= 4 ? subMenu.name : subMenu.name.substring(0, 4) + "..."}</Button>
                                                         );
                                                     })
                                                 }
+                                                {
+                                                    // 渲染加号
+                                                    plus.map(() => {
+                                                        console.log(falg);
+                                                        if (falg == 0 || falg == 1) {
+                                                            return (
+                                                                <Button id={"add" + index} onClick={$ => self.clickAdd(index)} style={{
+                                                                    width: "95.66px",
+                                                                    height: "36px",
+                                                                    // display: lastFaMenu==0? "block": "none",
+                                                                    borderRadius: 0,
+                                                                    position: "absolute",
+                                                                    top: -((subLength + 1) * 36 + 1),
+                                                                    left: index == 0 ? -1 : 0
+                                                                }}><Icon type="plus" /></Button>
+                                                            );
+                                                        }
+                                                        return null;
+                                                    })
+                                                }
                                             </div>
                                         );
                                     })
@@ -336,7 +388,7 @@ class customMenu extends React.Component {
 
                         {/* 父菜单的表单 */}
                         <div style={{
-                            display: self.state.editType==1? "block": "none",
+                            display: self.state.editType == 1 ? "block" : "none",
                             height: "550px",
                             margin: "0 20px",
                             // padding: "10px",
@@ -357,7 +409,7 @@ class customMenu extends React.Component {
 
                         {/* 子菜单的表单 */}
                         <div style={{
-                            display: self.state.editType==2? "block": "none",
+                            display: self.state.editType == 2 ? "block" : "none",
                             height: "550px",
                             margin: "0 20px",
                         }}>
@@ -441,7 +493,7 @@ class customMenu extends React.Component {
                                         </TabPane>
                                     </Tabs>
                                 </div>
-                            </Form>                            
+                            </Form>
                         </div>
 
                     </Col>
@@ -451,4 +503,11 @@ class customMenu extends React.Component {
         );
     }
 }
-export default customMenu;
+
+//字面意思就是,把models的state变成组件的props
+function mapStateToProps (state) {
+    const { wxAppIdValue} = state.sellmanage // test就是models命名空间名字 
+    return { wxAppIdValue }; // 在这return,上面才能获取到
+}
+
+export default connect(mapStateToProps)(customMenu) ;

+ 17 - 15
UI/src/utils/config.js

@@ -1,11 +1,11 @@
 const APIV1 = '/api/v1'
-const APIV2 = '/api/v2'
+const APIV2 = '/api/v2' 
 const APIV3 = 'http://localhost:8004'
-const APIV4 = 'http://localhost:8005'
+const APIV4 = 'http://192.168.50.32:8006'
 
-const APIV3Auth = 'http://119.130.113.245:8030'
-const WXAPIV5= 'http://219.128.77.86:7000'
-const WXMpAPI= 'http://219.128.77.86:7001'
+const APIV3Auth = 'http://192.168.50.32:8031'
+const WXAPIV5= 'http://localhost:7000'
+const WXMpAPI= 'http://localhost:8081'
 module.exports =  {
   name: '亮健营销平台',
   prefix: 'bird',
@@ -13,15 +13,18 @@ module.exports =  {
   logo: '/logo.svg',
   iconFontCSS: '/iconfont.css',
   iconFontJS: '/iconfont.js',
-  CORS: [
-    'http://localhost:8000',
-    'http://localhost:8004',
-    'http://localhost:8005',
-    'http://119.130.113.245:7001',
-    'http://119.130.113.245:8030',
-    'http://219.128.77.86:7000',
-    'http://219.128.77.86:7001',
-   ],
+  CORS: ['http://localhost:8000',
+  'http://localhost:8030',
+  'http://192.168.50.32:8031',
+  'http://localhost:8080',
+  'http://192.168.50.32:8030',
+  'http://localhost:8004',
+  'http://192.168.50.32:8006',
+  'http://localhost:8030',
+  'http://192.168.50.32:8031',
+  'http://localhost:8081',
+  'http://192.168.50.32:8081',
+  'http://localhost:7000'],
   //YQL: ['http://localhost:57251'],
   openPages: ['/login','/'],
   apiPrefix: '/api/v1',
@@ -60,5 +63,4 @@ module.exports =  {
     // permissions:`${APIV3}/web/auth/permissions`
     permissions:`${APIV3Auth}/ums/oauth2/list/permission?types=2`,
   },
-  APIV4, 
 }

+ 15 - 17
UI/src/utils/config_dev.js

@@ -1,11 +1,11 @@
 const APIV1 = '/api/v1'
-const APIV2 = '/api/v2' 
+const APIV2 = '/api/v2'
 const APIV3 = 'http://localhost:8004'
-const APIV4 = 'http://192.168.50.32:8006'
+const APIV4 = 'http://localhost:8005'
 
-const APIV3Auth = 'http://192.168.50.32:8031'
-const WXAPIV5= 'http://localhost:7000'
-const WXMpAPI= 'http://localhost:8081'
+const APIV3Auth = 'http://119.130.113.245:8030'
+const WXAPIV5= 'http://219.128.77.86:7000'
+const WXMpAPI= 'http://219.128.77.86:7001'
 module.exports =  {
   name: '亮健营销平台',
   prefix: 'bird',
@@ -13,18 +13,15 @@ module.exports =  {
   logo: '/logo.svg',
   iconFontCSS: '/iconfont.css',
   iconFontJS: '/iconfont.js',
-  CORS: ['http://localhost:8000',
-  'http://localhost:8030',
-  'http://192.168.50.32:8031',
-  'http://localhost:8080',
-  'http://192.168.50.32:8030',
-  'http://localhost:8004',
-  'http://192.168.50.32:8006',
-  'http://localhost:8030',
-  'http://192.168.50.32:8031',
-  'http://localhost:8081',
-  'http://192.168.50.32:8081',
-  'http://localhost:7000'],
+  CORS: [
+    'http://localhost:8000',
+    'http://localhost:8004',
+    'http://localhost:8005',
+    'http://119.130.113.245:7001',
+    'http://119.130.113.245:8030',
+    'http://219.128.77.86:7000',
+    'http://219.128.77.86:7001',
+   ],
   //YQL: ['http://localhost:57251'],
   openPages: ['/login','/'],
   apiPrefix: '/api/v1',
@@ -63,4 +60,5 @@ module.exports =  {
     // permissions:`${APIV3}/web/auth/permissions`
     permissions:`${APIV3Auth}/ums/oauth2/list/permission?types=2`,
   },
+  APIV4, 
 }

+ 18 - 19
mpwechatApp/src/main/java/com/liangjian11/wx/mp/controller/WxMenuController.java

@@ -1,6 +1,8 @@
 package com.liangjian11.wx.mp.controller;
 
+import com.liangjian11.wx.mp.service.CustomMenuService;
 import com.liangjian11.wx.mp.utils.ResultInfo;
+import com.liangjian11.wx.mp.utils.ResultUtil;
 import lombok.AllArgsConstructor;
 import me.chanjar.weixin.common.api.WxConsts;
 import me.chanjar.weixin.common.bean.menu.WxMenu;
@@ -17,6 +19,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
 import javax.servlet.http.HttpServletRequest;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Map;
 
 import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
 
@@ -30,6 +33,9 @@ public class WxMenuController {
     @Autowired
     private WxMpService wxService;
 
+    @Autowired
+    private CustomMenuService customMenuService;
+
     /**
      * <pre>
      * 自定义菜单创建接口
@@ -146,28 +152,21 @@ public class WxMenuController {
         this.wxService.switchoverTo(appid).getMenuService().menuDelete(menuId);
     }
 
-    /**
-     * <pre>
-     * 自定义菜单查询接口
-     * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN
-     * </pre>
-     */
-    @GetMapping("/get")
-    public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
-        return this.wxService.switchoverTo(appid).getMenuService().menuGet();
-    }
 
     /**
-     * <pre>
-     * 测试个性化菜单匹配结果
-     * 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
-     * </pre>
-     *
-     * @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。
+     * 查询菜单
+     * @param appid
+     * @return
      */
-    @GetMapping("/menuTryMatch/{userid}")
-    public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String userid) throws WxErrorException {
-        return this.wxService.switchoverTo(appid).getMenuService().menuTryMatch(userid);
+    @GetMapping("get")
+    public ResultInfo query(@PathVariable String appid) {
+        try {
+            Map<String, Object> map = customMenuService.queryMenu(appid, wxService);
+            return new ResultInfo(ResultInfo.TYPE_RESULT_SUCCESS, 200, "查询成功!", map);
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+        }
+        return ResultUtil.createFail("服务器繁忙,请稍后再试!");
     }
 
     /**

+ 25 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/mapper/CustomMenuMapper.java

@@ -0,0 +1,25 @@
+package com.liangjian11.wx.mp.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.liangjian11.wx.mp.modle.CustomMenu;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+import java.util.Map;
+
+public interface CustomMenuMapper extends BaseMapper<CustomMenu> {
+
+    /**
+     * 查询自定义菜单
+     * @param appId
+     * @return
+     */
+    List<CustomMenu> queryMenuList(@Param("appId") String appId);
+
+    /**
+     * 简单查询自定义菜单
+     */
+    List<Map<String, Object>> simpleQueryMenuList(@Param("appId") String appId);
+
+}

+ 45 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/modle/CustomMenu.java

@@ -0,0 +1,45 @@
+package com.liangjian11.wx.mp.modle;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName("customMenu")
+public class CustomMenu {
+
+    @TableId(value = "menuId", type = IdType.AUTO)
+    private Integer menuId;
+
+    @TableField("appId")
+    private String appId;
+
+    @TableField("menuName")
+    private String menuName;
+
+    @TableField("menuSuperior")
+    private Integer menuSuperior;
+
+    @TableField("menuSort")
+    private Integer menuSort;
+
+    @TableField("menuType")
+    private String menuType;
+
+    @TableField("menuKey")
+    private String menuKey;
+
+    @TableField(exist = false)
+    private List<Message> messageList;
+
+    @TableField(exist = false)
+    private List<CustomMenu> subButtons;
+}

+ 3 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/modle/Message.java

@@ -71,6 +71,9 @@ public class Message extends Model<Message> {
     @TableField("template_id")
     private String templateId;
 
+    @TableField("menuId")
+    private Integer menuId;
+
     @TableField(exist = false)
     private Integer applicationId;
 

+ 11 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/CustomMenuService.java

@@ -0,0 +1,11 @@
+package com.liangjian11.wx.mp.service;
+
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpService;
+
+import java.util.Map;
+
+public interface CustomMenuService {
+
+    Map<String, Object> queryMenu(String appId, WxMpService wxService) throws WxErrorException;
+}

+ 148 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/impl/CustomMenuServiceImpl.java

@@ -0,0 +1,148 @@
+package com.liangjian11.wx.mp.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.liangjian11.wx.mp.mapper.CustomMenuMapper;
+import com.liangjian11.wx.mp.mapper.MessageMapper;
+import com.liangjian11.wx.mp.modle.CustomMenu;
+import com.liangjian11.wx.mp.modle.Message;
+import com.liangjian11.wx.mp.service.CustomMenuService;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpMenuService;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
+import me.chanjar.weixin.mp.bean.menu.WxMpSelfMenuInfo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+
+@Service
+public class CustomMenuServiceImpl implements CustomMenuService {
+
+    @Autowired
+    private CustomMenuMapper customMenuMapper;
+
+    @Autowired
+    private MessageMapper messageMapper;
+
+    @Override
+    public Map<String, Object> queryMenu(String appId, WxMpService wxService) throws WxErrorException {
+        // 1、查询微信端的自定义菜单
+        WxMpMenuService menuService = wxService.switchoverTo(appId).getMenuService();
+        WxMpGetSelfMenuInfoResult selfMenuInfo = menuService.getSelfMenuInfo();
+        List<WxMpSelfMenuInfo.WxMpSelfMenuButton> buttons = selfMenuInfo.getSelfMenuInfo().getButtons();
+
+//        UpdateWrapper<CustomMenu> updateWrapper = new UpdateWrapper<>();
+//        updateWrapper.eq("appId", appId);
+//        customMenuMapper.delete(updateWrapper);
+        // 2、将本地的同步成微信方的
+        for (WxMpSelfMenuInfo.WxMpSelfMenuButton faButton : buttons) {
+            Integer menuId = saveMenuAndMsg(faButton, appId, null);
+            if(faButton.getSubButtons()==null){
+                continue;
+            }
+            for (WxMpSelfMenuInfo.WxMpSelfMenuButton subButton : faButton.getSubButtons().getSubButtons()) {
+                saveMenuAndMsg(subButton, appId, menuId);
+            }
+        }
+
+        // 3、查询返回
+        List<CustomMenu> customMenuList = customMenuMapper.queryMenuList(appId);
+        Map<String, Object> map = new HashMap<>(2);
+        map.put("conditionalMenu", null);
+        map.put("menu", new HashMap<String, Object>(){
+            {
+                put("buttons", changeToJson(customMenuList));
+            }
+        });
+        return map;
+    }
+
+
+    /**
+     * 转换
+     * @param list
+     * @return
+     */
+    public List<CustomMenu> changeToJson(List<CustomMenu> list){
+        List<CustomMenu> faMenuList = new ArrayList<>(3);
+        for (CustomMenu menu : list) {
+            // 找到父菜单封装
+            if(menu.getMenuSuperior()==null){
+                menu.setSubButtons(new ArrayList<CustomMenu>(5));
+                faMenuList.add(menu);
+//                list.remove(menu);
+            }
+        }
+        // 将对应的子菜单找到,并封装
+        for (CustomMenu faMenu : faMenuList) {
+            Integer menuId = faMenu.getMenuId();
+            List<CustomMenu> subButtons = faMenu.getSubButtons();
+            for (CustomMenu subMenu : list) {
+                Integer menuSuperior = subMenu.getMenuSuperior();
+                if(menuId==menuSuperior){
+                    subButtons.add(subMenu);
+                }
+            }
+        }
+        return faMenuList;
+    }
+
+
+    /**
+     * 保存至库
+     * @param faButton
+     * @param appId
+     * @param menuId
+     * @return
+     */
+    public Integer saveMenuAndMsg(WxMpSelfMenuInfo.WxMpSelfMenuButton faButton, String appId, Integer menuId){
+        List<WxMpSelfMenuInfo.WxMpSelfMenuButton> subButtons = faButton.getSubButtons()==null ? null : faButton.getSubButtons().getSubButtons();
+
+
+
+
+
+
+
+        String key = faButton.getKey();
+        CustomMenu menu = new CustomMenu();
+        menu.setAppId(appId);
+        menu.setMenuSuperior(menuId);
+        menu.setMenuName(faButton.getName());
+        menu.setMenuType(faButton.getType());
+        menu.setMenuKey(key);
+        customMenuMapper.insert(menu);
+
+        if(subButtons==null || subButtons.size()==0){
+            QueryWrapper<Message> wrapper = new QueryWrapper<>();
+            wrapper.eq("menuKey", key);
+            wrapper.eq("msg_type", faButton.getType());
+            Integer count = messageMapper.selectCount(wrapper);
+
+            Message message = new Message();
+            message.setMenuId(menu.getMenuId());
+            message.setChannel(Short.parseShort("4"));
+            message.setMsgType(faButton.getType());// 类型
+            message.setUrl(faButton.getUrl());// view
+            message.setContent(faButton.getValue());// Text
+            message.setMediaId(faButton.getValue());// media
+            if(faButton.getType().equals("miniprogrampage")){
+                message.setMiniProgramAppId(faButton.getAppId());// 小程序
+                message.setPagepath(faButton.getPagePath());// 小程序
+                message.setUrl(faButton.getUrl());// 小程序
+            }
+
+            if(count>0){
+                messageMapper.update(message, wrapper);
+            }else{
+                messageMapper.insert(message);
+            }
+
+        }
+
+        return menu.getMenuId();
+    }
+
+}

+ 1 - 1
mpwechatApp/src/main/resources/application.yml

@@ -37,7 +37,7 @@ spring:
         username: test
         password: 360ljtest!
     application:
-        name: mpwechatT
+        name: mpwechatt1
         #配置面向服务调用
     cloud:
         config:

+ 58 - 0
mpwechatApp/src/main/resources/mapper/CustomMenuMapper.xml

@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.liangjian11.wx.mp.mapper.CustomMenuMapper">
+    <resultMap id="menuMap" type="com.liangjian11.wx.mp.modle.CustomMenu">
+        <id column="menuId" jdbcType="INTEGER" property="menuId" />
+        <result column="appId" jdbcType="VARCHAR" property="appId" />
+        <result column="menuName" jdbcType="VARCHAR" property="menuName" />
+        <result column="menuSuperior" jdbcType="VARCHAR" property="menuSuperior" />
+        <result column="menuSort" jdbcType="INTEGER" property="menuSort" />
+        <result column="menuType" jdbcType="VARCHAR" property="menuType" />
+        <result column="menuKey" jdbcType="VARCHAR" property="menuKey" />
+
+        <collection property="messageList" ofType="com.liangjian11.wx.mp.modle.Message">
+            <id column="msg_id" jdbcType="INTEGER" property="msgId"></id>
+            <result column="msg_type" jdbcType="INTEGER" property="msgType" />
+            <result column="content" jdbcType="VARCHAR" property="content" />
+            <result column="media_id" jdbcType="VARCHAR" property="mediaId" />
+            <result column="title" jdbcType="VARCHAR" property="title" />
+            <result column="description" jdbcType="VARCHAR" property="description" />
+            <result column="url" jdbcType="VARCHAR" property="url" />
+            <result column="thumb_url" jdbcType="VARCHAR" property="thumbUrl" />
+            <result column="pagepath" jdbcType="VARCHAR" property="pagepath" />
+            <result column="thumb_media_id" jdbcType="VARCHAR" property="thumbMediaId" />
+            <result column="miniProgramAppId" jdbcType="VARCHAR" property="miniProgramAppId" />
+            <result column="miniAppTitle" jdbcType="VARCHAR" property="miniAppTitle" />
+            <!-- 以下这两个是素材的本地url和微信端url -->
+            <result column="local_url" jdbcType="VARCHAR" property="localUrl" />
+            <result column="material_url" jdbcType="VARCHAR" property="materialUrl" />
+        </collection>
+    </resultMap>
+
+
+    <select id="queryMenuList" resultMap="menuMap">
+        SELECT A.*,
+            B.*,
+            C.local_url,
+            C.material_url
+        FROM customMenu A
+        LEFT JOIN message B on A.menuKey = B.menuKey
+        LEFT JOIN material C on (B.media_id = C.media_id AND B.msg_type = 'media_id')
+        WHERE A.appid = '${appId}'
+        ORDER BY menuSort
+    </select>
+
+
+    <select id="simpleQueryMenuList" resultType="map">
+        SELECT A.*,
+            B.msg_type, B.url, B.miniProgramAppId, B.pagepath, B.content, B.media_id,
+            C.local_url,
+            C.material_url
+        FROM customMenu A
+        LEFT JOIN message B on A.menuKey = B.menuKey
+        LEFT JOIN material C on (B.media_id = C.media_id AND B.msg_type = 'media_id')
+        WHERE A.appid = '${appId}'
+        ORDER BY menuSort
+    </select>
+
+</mapper>