微信礼品的官方文档,请戳微信礼品卡。
官方咨询邮箱:weixincard@tencent.com。
本文仅记录微信礼品卡后端开发相关:
1.设计礼品卡数据库表结构
请仔细阅读微信礼品卡官方文档,再进行数据库表结构设计。
确保调用微信接口返回成功之后,然后再向数据库表中插入或者修改数据。
创建礼品卡接口
根据API,我只设计了一张表存储礼品卡的相关信息。建议给API的每个属性值都创建一个字段。
贴一下官方API参数示例:
{"card":{"card_type":"GENERAL_CARD","general_card":{"sub_card_type":"GIFT_CARD","background_pic_url":"https://mmbiz.qlogo.cn/mmbiz/","base_info":{"max_give_friend_times":1,"giftcard_info":{"price":1},"logo_url":"https://mmbiz.qlogo.cn/mmbiz/p98FjXy8LaeMq67mEpDmkj05EtiaVcGOibVaVux3Agib1ibcHFkCoic7HuQWFawx9XGCNWIO085drjdxTib2nBHlYGAA/0?wx_fmt=gif","brand_name":"微信咖啡厅","code_type":"CODE_TYPE_QRCODE","title":"心意卡","color":"Color020","notice":"使用时向服务员出示","service_phone":"020-88888888","description":"不可与其他优惠同享","date_info":{"type":"DATE_TYPE_FIX_TIME_RANGE","begin_timestamp":1397577600,"end_timestamp":1472724261},"sku":{"quantity":50000000},"get_limit":0,"use_custom_code":false,"can_give_friend":true,"location_id_list":[213059884],"center_title":"顶部居中按钮","center_sub_title":"按钮下方的wording","center_url":"www.qq.com","center_app_brand_user_name":"gh_86a091e50ad4@app","center_app_brand_pass":"pages/index/index","custom_url_name":"新品推荐","custom_url":"https://www.starbucks.com.cn/","custom_app_brand_user_name":"gh_86a091e50ad4@app","custom_app_brand_pass":"pages/index/index","need_push_on_view":true},"advanced_info":{"text_image_list":[{"image_url":"http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0","text":"此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味"},{"image_url":"http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sj piby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0","text":"此菜品迎合大众口味,老少皆宜,营养均衡"}]},"supply_balance":true,"prerogative":"礼品卡享受更多优惠","auto_activate":true,"init_balance":10000,"custom_field1":{"name":"优惠券","url":"","app_brand_user_name":"","app_brand_pass":""},"custom_field2":{"name":"兑换券","url":"","app_brand_user_name":"","app_brand_pass":""}}}
}
创建-礼品卡货架接口 根据API,我设计了5张表。先贴一下官方API参数示例:
{"page": {"page_title": "礼品卡","support_multi": true,"banner_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","theme_list": [{"theme_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","title": "title","title_color": "#FB966E","show_sku_title_first": true,"item_list": [{"card_id": "pbLatjiSj_yVRH5XTb2Zsln7DNQg","title": "焦糖拿铁"},{"card_id": "pbLatjlq75CPBR_tYCRdPlxSGlOs","title": "焦糖拿铁"}],"pic_item_list": [{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语1"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语2"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语3"}],"category_index": 0},{"theme_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","title": "title_lalala","title_color": "#FB966E","item_list": [{"card_id": "pbLatjiSj_yVRH5XTb2Zsln7DNQg","title": "焦糖拿铁"},{"card_id": "pbLatjlq75CPBR_tYCRdPlxSGlOs","title": "蛋糕"}],"pic_item_list": [{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语1","outer_img_id": "outer_img_id_1"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语2","outer_img_id": "outer_img_id_2"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语3","outer_img_id": "outer_img_id_3"}],"category_index": 1},{"theme_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","title": "title_lalala","title_color": "#FB966E","item_list": [{"card_id": "pbLatjiSj_yVRH5XTb2Zsln7DNQg"},{"card_id": "pbLatjlq75CPBR_tYCRdPlxSGlOs"}],"pic_item_list": [{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语1","outer_img_id": "outer_img_id_1"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语2","outer_img_id": "outer_img_id_2"},{"background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/p98FjXy8LafBWIJsGFe7tlPvtBFxXXTPdx5cEuFMcWWsiaR1DyrN5ML3jiaVYZibovA8OrwOylUia6ywvVU6Aqboibw/0","default_gifting_msg": "祝福语3","outer_img_id": "outer_img_id_3"}],"is_banner": true}],"category_list": [{"title": "主题分类一"},{"title": "主题分类二"}],"address": "广州市海珠区222号","service_phone": "020-12345678","biz_description": "退款指引","cell_1": {"title": "申请发票","url": "https://open.weixin.qq.com"},"cell_2": {"title": "申请退款","url": "https://mp.weixin.qq.com"}}
}
- 礼品卡货架货架主表
主要记录除了category_list和theme_list参数之外的属性信息
- 礼品卡货架主题分类列表
主要记录了 category_list参数信息,关联货架表主键ID或者微信回传的page_id,视情况而定,我关联的是page_id
- 礼品卡货架货架主题表
主要记录了在theme_list内除了item_list和pic_item_list参数之外的属性信息,关联货架表主键ID或者微信回传的page_id,视情况而定,我关联的是page_id,关联了礼品卡货架主题分类列表主键ID
- 礼品卡货架主题卡片关联表
主要记录了item_list参数的信息,关联了礼品卡货架货架主题表主键ID。
- 礼品卡货架祝福语表
主要记录了pic_item_list参数的信息,关联了礼品卡货架货架主题表主键ID
脑图示例:
礼品卡卡片创建和修改相对简单,货架的新建和修改相对复杂。这里我在写逻辑的时候,设定每一个货架主题必须绑定一个主题分类,这样在提交给微信的时候,theme_list内category_index属性对应到category_list都可找到对应的index值。
新建相对简单,前端封装好数据结构,接口接收到参数做处理即可。
编辑这里建议前端封装好数据结构,传到后端。除了礼品卡货架货架主表做update外,先移除之前的和货架关联的主题分类列表、主题表、主题卡片关联表、祝福语表,然后再新建上面4张表数据。
2.获取access_token
建议存储到redis,并设置有效时常
3.礼品卡接口调用
数据验证、数据封装,做好日志打印、事务处理
4.卡券事件推送
卡券事件推送URL在微信公众号里的"开发-基本配置-服务器配置"内配置
这里我写了一个servlet,get用于服务器验证,post用于业务处理。但目前我只用到了对账。
开发过程中,客户的公众号授权给了第三方平台来运营,导致配置了服务器域名也无法回调到我们的服务器上,经反复确认之后,取消了原来友商的授权。消息成功的推送到了我们的服务器上。
一旦启用服务器配置,自定义菜单配置失效并停用,提供两种方案参考:
- 效仿友商创建第三方平台(推荐,可以和客户商量增加人天)
- 在“添加功能插件”里找到自定义菜单并启用,原配置则生效,但不可修改。(折中省事)
关于微信重复回调问题处理,提供两种方案参考:
- 数据库的code字段设置为unique
- 微信回调的FromUserName+CreateTime作为回调的值存储在redis等no-sql数据库中,每次回调时判断此值存在与否,存在则直接返回success,不存在则继续处理。