uniapp中微信小程序取手机号(后端net7+盛派SDK) 2023年08月09日测试成功,不过看微信的小程序文档8月26日起微信小程序取手机号就要收费了,取一次3分钱。 原理:前端取到code后传到后端,后端再把code传给微信的某一接口取手机号 前端uniapp中的vue代码:
<template> <view> <view> <button type="default" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">获取手机号</button> <text>{{phone}}</text> </view> </view> </template> <script> export default { data() { return { phone: '', } }, methods: { decryptPhoneNumber(e) { var code = e.detail.code; var url = "http://localhost:5049/wx/getphone?code="+code; uni.request({ url:url, method:'GET', success: (res) => { console.log(res); this.phone = res.data; } }) }, } } </script> <style> button, input { border: 1px solid darkgray; } </style>
后端net7代码,用盛派SDK,按官网文档弄(https://sdk.weixin.senparc.com/Docs/WxOpen#title_install),之前还要引用一大堆nuget,然后在program里注册,然后才能直接用 nuget: Senparc.CO2NET, Senparc.Weixin, Senparc.Weixin.AspNet, Senparc.Weixin.WxOpen, appsetting:
"SenparcWeixinSetting": { "IsDebug": true, //小程序 "WxOpenAppId": "wxd64fd8b6c1111111", "WxOpenAppSecret": "78eae7ca46111eb825044083fee88c6c", "WxOpenToken": "#{WxOpenToken}#", "WxOpenEncodingAESKey": "#{WxOpenEncodingAESKey}#" }
program:
using Senparc.Weixin.RegisterServices; using Senparc.Weixin; using Senparc.Weixin.AspNet; using Senparc.Weixin.WxOpen; #region 盛派SDK注册 builder.Services.AddMemoryCache(); builder.Services.AddSenparcWeixinServices(builder.Configuration); #endregion var app = builder.Build(); //盛派SDK, 启用微信配置 var registerService = app.UseSenparcWeixin(app.Environment, null, null, register => { }, (register, weixinSetting) => { register.RegisterWxOpenAccount(weixinSetting, "jct小程序"); });
根据code取手机号的后端代码,直接调用方法即可:
using JCT.DAL; using JCT.Model; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp; using SqlSugar.Extensions; namespace JCT.Web.ApiController { [Route("api/[controller]")] [ApiController] public class WXController : ControllerBase { [HttpGet("GetPhone")] public string GetPhone(string code) { try { string appid = "wxd64fd8b6ce111111"; var result = BusinessApi.GetUserPhoneNumber(appid, code); return result.phone_info.phoneNumber; } catch (Exception ex) { return ex.Message; } } } }
标签:uniapp,code,手机号,Weixin,Senparc,盛派,net7,using,SDK From: https://www.cnblogs.com/niunan/p/17616308.html