<?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.session、cookie部分不用修改，如果出现session、cookie方面的问题，可检查此处的session、cookie(如'/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>
