小程序码的生成与获取码中的scene

前端生成码

javascript 复制代码
request('GET', 'Spread/GetProgramCode', {
          appId: wx.getAccountInfoSync().miniProgram.appId,
          appSecret: '76fcc9af10b30bc662eedb0eadec3908',
          scene: '&cid=0&tp=1'//tp=1表示是推荐奖励 cid是优惠券id
        })
          .then(res => {
            res = res.data
            if (res.statusCode == 200) {
                 this.setData({visible:true,imagecode:getApp().globalData.url+res.data});
            } else{
              wx.showToast({ title: res.data, icon: 'error' });
              console.log(res.data);
            } 
          });

后端生成码

cs 复制代码
[HttpGet, Route("GetProgramCode")]
public object GetProgramCode(string appId, string appSecret, string scene)
{
    try
    {
        string dic = AppDomain.CurrentDomain.BaseDirectory;
        string rootPath =dic+ "UpLoadFiles\\" + DateTime.Now.ToString("yyyyMMdd");
        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
        string filePath = rootPath + "\\"+fileName;
        if (!Directory.Exists(rootPath)){
            Directory.CreateDirectory(rootPath);
        }
        scene = "uId=" + UserId + scene;
        var bytes = GetWxacodeUnlimit(appId, appSecret, scene);
        System.IO.File.WriteAllBytes(filePath, bytes);
        return Ok("/UpLoadFiles/"+ DateTime.Now.ToString("yyyyMMdd") +"/"+ fileName);
    }
    catch (Exception ex)
    {
        return NotFound(ex.Message);
    }
}



// 返回图片字节数组
public byte[] GetWxacodeUnlimit(string appId, string appSecret, string scene, string page = null, int width = 430)
{
    if (string.IsNullOrEmpty(scene)) throw new ArgumentException("scene 必须提供");
    var token = GetAccessToken(appId, appSecret);
    var client = new RestClient($"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={token}");
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    var body = new
    {
        scene = scene,
        page = "pages/my/my",   // 可为空
        env_version="trial",
        width = width
    };
    request.AddParameter("application/json", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
    var resp = client.Execute(request);

    // 如果返回 JSON 则是错误信息
    if (!string.IsNullOrEmpty(resp.ContentType) && resp.ContentType.Contains("application/json"))
    {
        var err = JsonConvert.DeserializeObject<WxJsonError>(resp.Content);
        throw new Exception($"微信API错误: {err.errcode} {err.errmsg}");
    }

    if (resp.RawBytes == null || resp.RawBytes.Length == 0) throw new Exception("未返回图片数据");
    return resp.RawBytes;
}

前端获取码的scene信息

javascript 复制代码
onLoad: function (options) {
    console.log(options);
    if(options.scene!=undefined){//扫描二维码获取推荐人id
      const decoded = decodeURIComponent(options.scene);//解码
    }
   }
相关推荐
CHU7290352 小时前
宠物寄养小程序功能版块设计解析:安全、便捷、透明的寄养服务生态
安全·小程序·宠物
无风听海2 小时前
.NET10之C# Target-typed new expression深入解析
windows·c#·.net
网易独家音乐人Mike Zhou2 小时前
【Python】TXT、BIN文件的十六进制相互转换小程序
python·单片机·mcu·小程序·嵌入式·ti毫米波雷达
AI前端老薛2 小时前
Taro 小程序如何优雅地分包
小程序·taro
蜡台2 小时前
浙政钉(浙里办小程序) H5 二次回退问题修复方案
前端·小程序·浙政钉·浙里办
这辈子谁会真的心疼你2 小时前
怎么修改pdf文档属性?介绍三个方法
数据库·pdf·c#
CHU7290353 小时前
美护便捷预约,解锁精致生活——美业服务商城小程序前端功能解析
前端·小程序·生活
初九之潜龙勿用18 小时前
C# 解决“因为算法不同,客户端和服务器无法通信”的问题
服务器·开发语言·网络协议·网络安全·c#
net3m3320 小时前
C#插件化架构(Plugin Architecture)或 可插拔架构,根据产品类型编码的不同自动路由到目标函数,而无需为每个产品都编码相应的代码!!
重构·c#