wechat - miaomiaoF/- GitHub Wiki

微信小程序

小程序登录

  1. 授权的操作

// 点击授权

bindGetUserInfo: function(e) { if (!e.detail.userInfo) { wx.showModal({ title: '警告通知', content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。', showCancel: false, success: function(res) { if (res.confirm) {} } }) return; } if (app.globalData.isConnected) { wx.setStorageSync('userInfo', e.detail.userInfo) let enn_openid = wx.getStorageSync('enn_openid'); if (enn_openid) { app.globalData.openId = wx.getStorageSync('enn_openid'); } else { this.getOpenid(); } app.globalData.userInfo = e.detail.userInfo; wx.setStorageSync('userInfo', e.detail.userInfo) } else { wx.showToast({ title: '当前无网络', icon: 'none', }) } },

  1. 登录

// 微信登录 wx.login({ success: res => { wx.request({ url: 'https://baoxian.enn.cn/xaejb/channel/api/wechar/login/' + res.code, data: {}, success: res => { wx.setStorageSync('openId', res.data.openId) //openId: "oz2kQ5fcTtzx4rwLvruFL_v9Q5-M" // session_key: "kCaVdHoEGZm62m5xYCgzHQ==" wx.request({ url: app.globalData.api + app.globalData.advisor + 'saveopenid', method: 'POST', data: { openId: wx.getStorageSync('openId') }, header: { 'Content-Type': 'application/json' }, success: res => { console.log(res.data.data) // 回到原来的地方放

          }
        })
        wx.navigateBack();
      }
    })
  },
})
  1. 返回上一页
// 回到原来的地方放
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面对象的setData()方法,把数据存到上一个页面中去
    prevPage.setData({
      userInfo: app.globalData.userInfo
    });
    wx.navigateBack({
      delta: 1
    })

小程序请求

小程序二维码的生成

  1. 获取二维码文档

  2. 后台java实现

    @ResponseBody @RequestMapping(value = "/wechar/qecode", method = RequestMethod.GET) public void wecharqr(HttpServletResponse responsex, @RequestParam(name="page", required = false,defaultValue = "pages/start/start") String page, @RequestParam(name="scene", required = false,defaultValue = "1") String scene) throws IOException {

     //请求参数
     String params = "appid=" + wxappid + "&secret=" + wxsecret  + "&grant_type=client_credential" ;
     //发送请求
     String sr = HttpUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
     log.info("微信返回:{}",sr);
     if(StringUtilities.isEmpty(sr)){
         log.info("微信返回为空");
     }
     JSONObject json = JSONObject.fromObject(sr);
     //用户的唯一标识(openid)
     String access_token = (String) json.get("access_token");
     //获取会话密钥(session_key)
     int expires_in =(Integer) json.get("expires_in");
      stringRedisTemplate.opsForValue().set("channel_access_token",access_token,expires_in-100);
     String redis_access_token=access_token;
    
     Map paramsc = new HashMap();
     paramsc.put("scene", scene);  //参数
     paramsc.put("page", page); //位置
     paramsc.put("width", 430);
     byte[] re= HttpUtil.doImgPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+redis_access_token,paramsc);
     HttpHeaders headers = new HttpHeaders();
     responsex.setContentType("image/jpeg");
     OutputStream stream;
     stream = responsex.getOutputStream();
     stream.write(re);
     stream.flush();
     stream.close();
    

    }

  3. 路由的选择 微信小程序路由