生活随笔
收集整理的這篇文章主要介紹了
微信公众号开发流程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、首先注冊微信公眾號,要根據實際需求考慮清楚應該申請哪一種公眾號
以下是官方給出的建議,大家可以多參考參考
1)如果想簡單的發送消息,達到宣傳效果,建議可選擇訂閱號;
2)如果想用公眾號獲得更多的功能,例如開通微信支付,建議可以選擇服務號;
3)如果想用來管理內部企業員工、團隊,對內使用,可申請企業號;
4)訂閱號可通過微信認證資質審核通過后有一次升級為服務號的入口,升級成功后類型不可再變;
5)服務號不可變更成訂閱號。
2、自定義菜單設置
自定義菜單官方說明文檔地址:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
我個人使用的是view方式創建菜單,因為后面要獲取用戶基本信息,做單點登錄
1)通過APPID和appsecret獲取access_token(有效期是兩小時)
接口地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=appsecret
注意:獲取的時候一定要當前獲取的服務器ip添加相應的白名單,如下圖:
2)拿到access_token后,調用創建菜單地址
創建菜單地址:https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token
3)查詢自己創建的菜單
查詢菜單地址:https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=access_token
3、后臺對接微信用戶基本信息(java)
1)查看接口權限中網頁授權說明,可以清晰看到需要4步,如下:
2)上述4步中的第一步,獲取code:
前端第一步用下圖中的地址獲取code,其中的scope、redirect_uri最為重要,一個是獲取用戶基本信息的權限,一個是微信端跳轉到你登錄頁面,然后會帶入code和state字段給我們的頁面,如果地址沒問題,code會在返回的url中,通過截取可以獲得。
3)寫后端接口,通過code換取網頁授權access_token
Controller層
@PostMapping(value
= "/login", produces
= "application/json; charset=utf-8")@ApiOperation(value
= "登錄授權", notes
= "登錄授權", produces
= "application/json; charset=utf-8")@ApiImplicitParam(name
= "code", value
= "code作為換取access_token的票據", required
= true, dataType
= "string", paramType
= "query")public RestResponse login(@RequestParam(name
= "code", required
= true) String code
, HttpServletRequest request
, HttpServletResponse response
) {return ResultGenerator.genSuccessResult(sysWxUserService
.login(code
, request
, response
));}實現層代碼:
```
@Overridepublic WxUserVO login(String code
, HttpServletRequest request
, HttpServletResponse response
) {Map<String, Object> params
= new HashMap<>();String url
= WxCommonConstants.GET_ACCESS_TOKEN_URL
;String appId
= GlobalConfig.getConfig("wx.appid");String appSecret
= GlobalConfig.getConfig("wx.appsecret");params
.put("appid", appId
);params
.put("secret", appSecret
);params
.put("code", code
);params
.put("grant_type", "authorization_code");JSONObject jsonObject
= JSONObject.parseObject(HttpClientUtil.doHttpsGet(url
, params
));if (StringUtil.isNotEmpty(jsonObject
.get("errcode"))) {throw new SxException((Integer) jsonObject
.get("errcode"), (String) jsonObject
.get("errmsg"));}log
.info("通過code換取access_token、openID:{}", jsonObject
);String accessToken
= (String) jsonObject
.get("access_token");String openId
= (String) jsonObject
.get("openid");if (StringUtil.isEmpty(accessToken
) || StringUtil.isEmpty(openId
)) {throw new SxException(GovernExceptionEnum.WXUSER_OPENID_NOT_EXIST
);}String lang
= GlobalConfig.getConfig("wx.lang");url
= WxCommonConstants.GET_SNS_USER_INFO_URL
;params
= new HashMap<>();params
.put("access_token", accessToken
);params
.put("openid", jsonObject
.get("openid"));params
.put("lang", lang
);jsonObject
= JSONObject.parseObject(HttpClientUtil.doHttpsGet(url
, params
));log
.info("微信用戶信息:{}", jsonObject
);SysWxUser sysWxUser
= sysWxUserMapper
.selectByOpenId((String) jsonObject
.get("openid"));if (null == sysWxUser
) {sysWxUser
= new SysWxUser();sysWxUser
.setOpenId((String) jsonObject
.get("openid"));}saveOrUpdateWxUser(sysWxUser
, jsonObject
);String affiliationApp
= request
.getHeader(SystemCommonConstants.AFFILIATIONAPP_KEY
);String affiliationAppType
= request
.getHeader(SystemCommonConstants.AFFILIATIONAPPTYPE_KEY
);if (StringUtils.isEmpty(affiliationApp
) || StringUtils.isEmpty(affiliationAppType
)) {throw new SxException(SysExceptionEnum.AFFILIATIONAPP_OR_AFFILIATIONAPPTYPE_EMPTY
);}String currentTimeMillis
= String.valueOf(System.currentTimeMillis());StringBuilder key
= new StringBuilder();key
.append(CommonConstant.PREFIX_SHIRO_REFRESH_TOKEN
).append(sysWxUser
.getId()).append(SymbolConstants.COLON
).append(affiliationApp
).append(SymbolConstants.COLON
).append(affiliationAppType
);redisUtil
.setValue(key
.toString(), currentTimeMillis
, refreshTokenExpireTime
);redisUtil
.setValue(CommonConstant.PREFIX_SHIRO_WXUSER
+ sysWxUser
.getId(), sysWxUser
, refreshTokenExpireTime
);String token
= JwtUtil.sign(String.valueOf(sysWxUser
.getId()), currentTimeMillis
, affiliationApp
, affiliationAppType
);response
.setHeader("Authorization", token
);response
.setHeader("Access-Control-Expose-Headers", "Authorization");WxUserVO wxUserVO
= new WxUserVO();BeanUtils.copyProperties(sysWxUser
, wxUserVO
);wxUserVO
.setMobilePhone(sysWxUser
.getMobilPhone());if (StringUtil.isNotEmpty(sysWxUser
.getAreaId())) {SysArea sysArea
= sysAreaMapper
.selectById(sysWxUser
.getAreaId());if (null != sysArea
) {wxUserVO
.setAreaName(sysArea
.getName());}}sysWxUserLogService
.asyncSaveLog(sysWxUser
);Integer areaId
= StringUtils.isEmpty(sysWxUser
.getAreaId()) ? null : Integer.valueOf(sysWxUser
.getAreaId());hstLoginStatisService
.saveInfo(sysWxUser
.getId(), sysWxUser
.getUserName(), areaId
, CommonConstants.STRING_30
);return wxUserVO
;}
```java
4)刷新access_token(如果需要)
這步可以不需要,暫不做描述
5)拉取用戶信息(需scope為 snsapi_userinfo)
地址:http:GET(請使用https協議) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
本人在上述第三步中已經調用
可用微信開發者工具調試,實際調試結果如下:
最后實際調試的采坑點記錄:
1、后臺重定向地址以及設置菜單的appid是測試公眾號的appID,不是個人的,測試公眾號在"開發者工具"–>“公眾平臺測試賬號”–>里面含有appId和appsecret
2、設置JS接口安全域名 183.129.166.58:8999/
3、設置"網頁服務"–>“網頁賬號”–>“網頁授權獲取用戶基本信息”–>“授權回調頁面域名:”: 183.129.166.58:8999
總結
以上是生活随笔為你收集整理的微信公众号开发流程的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。