日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信公众号开发流程

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信公众号开发流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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層

/*** @Description 微信的單點登錄* @author xkh* @date 2021-9-31 10:27* @param code 票據* @return com.sx.common.result.RestResponse*/@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));}實現層代碼: ```/*** @param code 票據* @return com.sx.system.vo.WxUserVO* @Description 微信的單點登錄* @author xkh* @date 2021-9-31 10:46*/@Overridepublic WxUserVO login(String code, HttpServletRequest request, HttpServletResponse response) {//TODO 1、先通過code獲取access_token和openidMap<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"));}/*** {* "access_token":"ACCESS_TOKEN",* "expires_in":7200,* "refresh_token":"REFRESH_TOKEN",* "openid":"OPENID",* "scope":"SCOPE"* }*/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);}//TODO 2、通過access_token拉取用戶信息(需scope為 snsapi_userinfo)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);//TODO 3、查詢數據庫微信用戶信息、保存或者更新用戶信息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);}// 設置RefreshToken,時間戳為當前時間戳,直接設置即可(不用先刪后設,會覆蓋已有的RefreshToken)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);//把用戶設置到redis中,不需要重復查詢redisUtil.setValue(CommonConstant.PREFIX_SHIRO_WXUSER + sysWxUser.getId(), sysWxUser, refreshTokenExpireTime);// 從Header中Authorization返回AccessToken,時間戳為當前時間戳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())) {//TODO 用戶選擇的地區名稱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

總結

以上是生活随笔為你收集整理的微信公众号开发流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。