小程序码的生成与获取码中的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);//解码
    }
   }
相关推荐
唐青枫20 小时前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫2 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6252 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902112 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠3 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫5 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech5 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf7 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6257 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech7 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf