小程序码的生成与获取码中的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);//解码
    }
   }
相关推荐
2501_916008896 小时前
深入解析iOS应用启动性能优化策略与实践
android·ios·性能优化·小程序·uni-app·cocoa·iphone
齐鲁大虾7 小时前
新人编程语言选择指南
javascript·c++·python·c#
加号38 小时前
【C#】 WebAPI 接口设计与实现指南
开发语言·c#
unicrom_深圳市由你创科技8 小时前
上位机开发常用的语言 / 框架有哪些?
c++·python·c#
xiaoshuaishuai811 小时前
C# ZLibrary数字资源分发
开发语言·windows·c#
Eiceblue12 小时前
C# 实现 XLS 与 XLSX 格式双向互转(无需依赖 Office)
开发语言·c#·visual studio
TuCoder13 小时前
制作景区导览小程序时,现有手绘图是否可以复用?
小程序·智慧景区·手绘地图·景区导览·电子导览·地图制作·ebmap
aini_lovee13 小时前
基于C#的三菱PLC串口通信实现方案
服务器·网络·c#
光泽雨14 小时前
c#MVVM中的消息通知机制
服务器·c#
江沉晚呤时14 小时前
C# 整型溢出处理机制:checked 与 unchecked 上下文解析
c#·.net