小程序码的生成与获取码中的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);//解码
    }
   }
相关推荐
雪豹阿伟19 小时前
15.C# —— 多接口,泛型方法,泛型效率、泛型类和泛型接口、泛型约束
c#·上位机
影寂ldy19 小时前
C# const 常量 / readonly 只读 / static readonly
java·开发语言·c#
iCxhust20 小时前
c#多串口重量采集上位机程序
开发语言·汇编·c#·微机原理·8088单板机
雪豹阿伟20 小时前
14.C# —— 虚方法,new/override,密封类,索引器,接口
c#·上位机
糖果店的幽灵21 小时前
LangChain 1.3 完全教程:从入门到精通-Part 11: Tools(工具系统)
开发语言·langchain·c#
mykj15511 天前
AI旅拍小程序定制开发,解锁文旅变现新赛道
人工智能·小程序
专注VB编程开发20年1 天前
python翻译网页HTML的难题
python·c#·html
z落落1 天前
C# 抽象类(abstract)
java·开发语言·c#
biwenyunnet1 天前
【99做小程序只认餐宝盈】连锁餐饮小程序怎么做:从系统架构、技术选型到表结构与接口设计的完整实践
小程序·系统架构