hls_weixin_push.proc 7.71 KB
Newer Older
Spencer Chang's avatar
Spencer Chang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?xml version="1.0" encoding="UTF-8"?>
<p:procedure xmlns:a="http://www.leaf-framework.org/application" xmlns:ns1="uncertain.cache.action" xmlns:s="leaf.plugin.script" xmlns:p="uncertain.proc">
     <s:server-script><![CDATA[
     	    /**
             *read me
             *1.bm部分需改为相应系统的逻辑
             *2.sessioncookie部分不用修改,如果出现sessioncookie方面的问题,可检查此处的sessioncookie(如'/session/@session_id'是否有值)
             *3.调试函数:println(Object);会将Object打印在控制台(如应用服务器tomcat的控制台)。例如:println($session.toXML());会将session对象以xml格式打印出来。
             */
            try {
            
                var parameter = $ctx.parameter;
                var result; 
                
                //定义全局参数
                var list_id;
                var system_context = 'http://srmtest.haidilao.com/hdltrain';
            
                /*
                 *********** 测试推送代码
                 */
                 
                 var picurl = system_context + '/modules/weixin/client/img/msg_default.png';
                 
                 println('---------------------------');
                 println('picurl is :' + picurl);
                 
                 var pushStr = {
                 "touser": "1285624",
                 "msgtype": "news",
                 "agentid": "3",
                 "news": {
                 "articles": [{
                 "title": "test",
                 "description": "test",
                 "url": "",
                 "picurl": picurl
                 }]
                 }
                 };
                 //发送请求
                 result = Packages.leaf.plugin.weixin.QiyeWeixinNetworkUtil.sendNewsMessage("group1", JSON.stringify(pushStr), $ctx.sso.getObjectRegistry());
                 /*
                 *********** 测试推送代码
                 */
 /*     
                //获取控制器,判断状态
                var config_control_bm = new ModelService('weixin.interface.public.get_config_control');
                var config_control_records = config_control_bm.queryAsMap({
                    "config_code": "MESSAGE_PUSH_CONTROL"
                }).getChildren();
                if (config_control_records.length > 0) {
            
                    //获取控制器
                    var config_control_record = config_control_records[0];
            
                    //控制器为运行结束状态,才能开始本次同步
                    if (config_control_record.config_value == "FINISH") {
            
                        //开打控制器
                        var open_control_bm = new ModelService('weixin.interface.public.open_control');
                        open_control_bm.execute({
                            "config_code": "MESSAGE_PUSH_CONTROL"
                        });
            
                        //定义获取待发送消息列表
                        var message_list_bm = new ModelService('weixin.interface.message.pushing_message_list');
                        var message_list_records = message_list_bm.queryAsMap().getChildren();
                        for (var i = 0;i < message_list_records.length;i++) {
            
                            //定义全局的消息id            		
                            list_id = message_list_records[i].list_id;
            
                            //定义查看全文的URL
                            var url = system_context + '/modules/weixin/client/indexMessageDetailByURL.lview?state=purchase&bulletin_id=' + message_list_records[i].bulletin_id;
                            
                            //默认封面的图片URL
                            var picurl = system_context + '/modules/weixin/client/img/msg_default.png';
            
                            //拼接发送请求字符串
                            //发送多人,xxx(微信通讯录帐号)|xxx(微信通讯录帐号)
                            //发送所有人,@all
                            var pushStr = {
                                "touser": "@all",
                                "msgtype": "news",
                                "agentid": "3",
                                "news": {
                                    "articles": [{
                                        "title": message_list_records[i].title,
                                        "description": message_list_records[i].content,
                                        "url": url,
                                        "picurl": picurl
                                    }]
                                }
                            };
            
                            //发送请求
                            result = Packages.leaf.plugin.weixin.QiyeWeixinNetworkUtil.sendNewsMessage("group1", JSON.stringify(pushStr), $ctx.sso.getObjectRegistry());
            
                            //将结果的json字符转化成JOSN对象
                            var result_obj = JSON.parse(result);
            
                            //返回的错误代码和错误消息,判断是否推送成功
                            if (result_obj.errcode == "0" && result_obj.errmsg == "ok") {
                                //推送成功
                                var push_success_bm = new ModelService('weixin.interface.message.message_push_success');
                                push_success_bm.execute({
                                    "list_id": list_id,
                                    "err_message": result_obj.errmsg
                                });
            
                            } else {
                                //推送失败
                                var push_failed_bm = new ModelService('weixin.interface.message.message_push_failed');
                                push_failed_bm.execute({
                                    "list_id": list_id,
                                    "err_message": result_obj.errmsg
                                });
                            }
                        }
            
                        //关闭控制器
                        var close_control_bm = new ModelService('weixin.interface.public.close_control');
                        close_control_bm.execute({
                            "config_code": "MESSAGE_PUSH_CONTROL"
                        });
                    }
                }
            
                //没有同步
                if (!result) {
                    result = '自动同步程序正在运行或没有需要推送的信息!';
                }else{
                    result = '信息推送完成!';
                }
 */     
            } catch (e) {
                
                //关闭控制器
                var close_control_bm = new ModelService('weixin.interface.public.close_control');
                close_control_bm.execute({
                    "config_code": "MESSAGE_PUSH_CONTROL"
                });
                
                var serviceContext = Packages.leaf.service.ServiceContext.createServiceContext($ctx.getData());
                var ed = $instance('leaf.service.exception.IExceptionDescriptor');
                var map = new CompositeMap(ed.process(serviceContext, e.javaException));
                result = {
                    head: {
                        message: map.message,
                        code: 'failure'
                    },
                    body: {}
                };
            } finally {
                //执行finally代码,将result对象回写
                $ctx.parameter.json = String(result);
            }
      ]]></s:server-script>
</p:procedure>