lib/blsm-mp-wx.rb in blsm-mp-wx-0.1.5 vs lib/blsm-mp-wx.rb in blsm-mp-wx-0.1.6

- old
+ new

@@ -5,10 +5,11 @@ require 'active_record' require 'active_support' require 'blsm-mp-wx/version' require 'blsm-mp-wx/model/active_record' require 'blsm-mp-wx/model/vd_mp_wx' +require 'blsm-mp-wx/model/vd_mp_msg' require 'json' require 'faraday' module BlsmMpWx ERROR_CODES = { @@ -202,9 +203,33 @@ json_obj = parse_json(response.body) return nil unless json_obj return nil unless json_obj.has_key?('short_url') json_obj['short_url'] + end + + #创建消息,存储到消息队列中 + #====Parameters + # * +app_id+ - 公众号的唯一标识 + # * +openid+ - 发送给用户的openid + # * +content+ - 消息内容 + # * +msg_name+ - 消息种类:new_order(新订单通知)、balance(结算通知)、score_change(积分变动通知)、custom(普通消息) + # new_order => content: {touser:'openid',order_id:''} 系统自动优先使用模板消息发送通知 + # balance => content: {touser:'openid',clearing_id} 系统自动优先使用模板消息发送通知 + # score_change => content: {touser:'',change:5,total:25,content:'邀请他人。。。。'} + def create_msg(app_id=nil, openid, content, msg_name) + app_id ||= self.APP_ID + return nil unless openid + return nil unless content[:touser]==openid + return nil unless %w(new_order balance score_change custom).include?(msg_name) + VdMpMsg.create({ + app_id: app_id, + openid: openid, + content: content, + msg_name: msg_name, + template: %w(new_order balance score_change).include?(msg_name), + status: 'none' + }) end private #解析json类型的数据,并将最后的数据转为hash或array # +json_data_str+ 要转换的json数据