微信扫码关注公众号

一、扫码开发步骤

2、根据用户id生成ticket

5、取消关联,根据用户id删除对应的openID

二、准备工作:

2、填写接口配置信息

三、API文档查看

2、

在该用户下方选择生成带参数的二维码API

四、对应代码

(二)实现类

import com.alibaba.fastjson.JSONObject;
import com.cyb.base.BaseApiService;
import com.cyb.base.BaseResponse;
import com.cyb.weixin.api.service.WeiXinQrCodeService;
import com.cyb.weixin.constant.WeiXinConstant;
import com.cyb.weixin.mp.config.WxMpConfiguration;
import com.cyb.weixin.mp.config.WxMpProperties;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;

@RestController
public class WeiXinQrCodeServiceImpl extends BaseApiService implements WeiXinQrCodeService {

    @Autowired
    private WxMpProperties wxMpProperties;

    @Override
    public BaseResponse<JSONObject> getQrUrl(Long userId) {
        if (userId == null) {
            return setResultError("userId不能为空!");
        }
        //获取配置第一个appId,因为配置文件中可以配置多个APPID
        String appId = wxMpProperties.getConfigs().get(0).getAppId();
        // 根据appid 获取对应的  WxMpQrcodeService
        WxMpQrcodeService qrcodeService = WxMpConfiguration.getMpServices().get(appId).getQrcodeService();
        try {
            WxMpQrCodeTicket wxMpQrCodeTicket = qrcodeService.qrCodeCreateTmpTicket(userId + "",
                    WeiXinConstant.QR_CODE_EXPIRE_SECONDS);
            if (wxMpQrCodeTicket == null) {
                return setResultError("生成二维码链接失败!");
            }
            String ticket = wxMpQrCodeTicket.getTicket();
            return setResultSuccess(URLEncoder.encode(ticket,"UTF-8"));
        } catch (Exception e) {
            return setResultError("生成二维码链接失败!");
        }
    }
}

(三)获取配置文件信息工具类 ,以下是获取配置文件中前缀为:wx.mp开头配置文件

(四)配置文件bootstrap.yml 增加以下配置

wx:
  mp:
    configs:
      - appId: wx6abbebaf087d74c6
        secret: ef98ffacbf90aa0e859a21a84a925
        token: cybtoken

(五)WxMpConfiguration配置类

(七)拼接二维码链接

(八)扫码handler

package com.cyb.weixin.mp.handler;

import com.cyb.weixin.impl.manager.WxMpServiceManage;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class ScanHandler extends AbstractHandler {

    @Autowired
    private WxMpServiceManage wxMpServiceManage;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        // 扫码事件处理
        String eventKey = wxMpXmlMessage.getEventKey();
        if (!StringUtils.isEmpty(eventKey)) {
            Long userId = Long.parseLong(eventKey);
            String openId = wxMpXmlMessage.getFromUser();
            wxMpServiceManage.handler(userId, openId);
        }
        return null;
    }
}

(十)会员服务feign客户端

@FeignClient("cyb-member")
public interface MemberInfoServiceFeign extends MemberInfoService {
}

以上代码本人亲测有效,如有以为可以留言。

本人该篇博客主要用于本人以后查看开发思路以及帮助对该资料有需求的同志,并该博客有以下参考相关人员:,请大家在转载的时候也说明出处。

经验分享 程序员 微信小程序 职场和发展