事件常量

同一事件常量定义事件常量,方便维护和提高代码阅读性;定义格式:名称使用全部大写中间用下划线链接,前缀为模块名便于区分,值为“store_event:”加要实现的常量名称;

例:

// 订单事件类型
const (
    STORE_EVENT_ORDER_STATUS_CHANGE             = "store_event:order_status_change"              // 商城订单状态变更事件
    STORE_EVENT_ORDER_DETAIL_DELIVERED          = "store_event:order_detail_delivered"           // 订单发货事件
    STORE_EVENT_ORDER_DETAIL_RECEIVED           = "store_event:order_detail_received"            // 订单收货事件
    STORE_EVENT_ORDER_CREATED                   = "store_event_order_created"                    // 订单创建成功
    STORE_EVENT_ORDER_CANCELED                  = "store_event_order_canceled"                   // 取消订单
    STORE_EVENT_ORDER_CLOSED                    = "store_event_order_closed"                     // 关闭订单
    STORE_EVENT_ORDER_COMPLETED                 = "store_event:order_completed"                  // 订单完成事件
    STORE_EVENT_ORDER_PASSORD_CARD_SEND_MESSAGE = "store_event:order_password_card_send_message" //卡密商品订单发送卡密信息事件
)

事件触发和事件接收

触发使用 commonService.EventBus().Publish(“事件常量”, “对应方法参数”)
例:

commonService.EventBus().Publish(storeEventConsts.STORE_EVENT_ORDER_PASSORD_CARD_SEND_MESSAGE, ctx, goodsAttrId, quantity, orderId, uid) //卡密商品订单发送卡密信息事件

对应的消息事件接收:

// 商城用户事件
func subscribe() {    
//监听卡密商品发货成功发送消息给用户
    _ = commonService.EventBus().Subscribe(storeEventConsts.STORE_EVENT_ORDER_PASSORD_CARD_SEND_MESSAGE,
        func(ctx context.Context, goodsAttrId uint64, quantity uint, orderId uint64, uid uint64) {
            err := g.Try(ctx, func(ctx context.Context) {
                //查询订单相关信息
                //orderInfo, err := storeOrderService.StoreOrder().GetByOrderId(ctx, orderId)
                //获取相关属性,确定是否是固定卡密还是一次性卡密
                goodsAttrValueInfo, err := storeGoodsService.StoreGoodsAttrValue().GetById(ctx, goodsAttrId)

                if goodsAttrValueInfo.VirtualType == storeGoods.STORE_GOODS_VIRTUAL_FIXED_CARD_PASSWORD {
                    _, err = dao.StoreMemberMessage.Ctx(ctx).Insert(do.StoreMemberMessage{
                        Uid: uid,
                        Content: `<div style="font-size: 12px">卡密商品:<p  style="color: #2e8bea" >` + goodsAttrValueInfo.Goods.Name + `-` + goodsAttrValueInfo.Suk + `</p>已发货成功<br/>` +
                            `数量:` + strconv.Itoa(int(quantity)) + `<br/>` +
                            `卡密内容:<p  style="font-weight:bold" >` + goodsAttrValueInfo.VirtualInfo + `</p></div>`,
                    })
                } else if goodsAttrValueInfo.VirtualType == storeGoods.STORE_GOODS_VIRTUAL_SINGLE_USE_CARD_PASSWORD {
                    storeGoodsVirtualList := make([]*goodsModel.StoreGoodsVirtualListRes, 0)
                    // 获取需要发货的卡密
                    err = goodsDao.StoreGoodsVirtual.Ctx(ctx).Where(g.Map{
                        goodsDao.StoreGoodsVirtual.Columns().GoodsAttrValueId: goodsAttrId,
                        goodsDao.StoreGoodsVirtual.Columns().Status:           0,
                    }).Order("created_at asc").Limit(int(quantity)).Scan(&storeGoodsVirtualList)
                    for _, value := range storeGoodsVirtualList {
                        _, err = dao.StoreMemberMessage.Ctx(ctx).Insert(do.StoreMemberMessage{
                            Uid: uid,
                            Content: `卡密商品:<p  style="color: #2e8bea" >` + goodsAttrValueInfo.Goods.Name + `-` + goodsAttrValueInfo.Suk + `已发货成功</p><br/>` +
                                `数量:` + strconv.Itoa(int(quantity)) + `<br/>` +
                                `卡密内容:<p  style="font-weight:bold" >` + `卡号:` + value.CardNumber + ` 密码:` + value.CardPassword + `</p><br/>`,
                        })
                    }

                }

                liberr.ErrIsNil(ctx, err)
            })
            if err != nil {
                g.Log().Error(ctx, err)
            }
        })
}
作者:huang  创建时间:2024-11-19 17:00
最后编辑:huang  更新时间:2024-11-20 10:01