用户
 找回密码
 立即注册

28

主题

88

帖子

1178

积分

金牌会员

Rank: 6Rank: 6

积分
1178
发表于 2020-12-20 09:49:41
微信注册(绑定用户)时,获取到的用户省份和城市都是拼音,怎么才能获取到中文简体的省份和城市呢?尝试过在请求中加上lang=zh-CN也不行!头痛!
老师有没有更好的办法?获取官方有什么建议?
分享至 : QQ空间
0 人收藏
使用道具 举报 回复
发表于 2020-12-21 10:57:36
请问使用的是微信插件(https://www.smobiler.com/SmobilerDemo/weixin.aspx)还是微信api( https://www.smobiler.com/guide/weixinapi.aspx) 具体代码是怎么写的
使用道具 举报 回复 支持 反对
发表于 2020-12-21 18:50:50
两种都用到,前者用于app,后者用于非app.
微信插件代码如下:
public static WeXinAccessTokenResponseEntity GetAccessTokenByCode(string code)
        {
            //获取的格式为https://api.weixin.qq.com/sns/oa ... =authorization_code
            //appid为申请的应用号,secret是应用密钥,code是在调用登陆后获取的参数,grant_type固定为authorization_code
            string accessTokenParameterUrl = string.Format("{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code", accessTokenUrl, appid, secret, code);
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(accessTokenParameterUrl);
            myHttpWebRequest.Method = "GET";
            //获取结果
            HttpWebResponse result = (HttpWebResponse)myHttpWebRequest.GetResponse();
            string resultStr = "";
            if (result.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(result.GetResponseStream()))
                {
                    resultStr = reader.ReadToEnd();
                    //返回的是json格式,这里直接做序列化即可
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    return serializer.Deserialize<WeXinAccessTokenResponseEntity>(resultStr);
                }
            }
            else
            {
                return new WeXinAccessTokenResponseEntity() { errcode = -1, errmsg = "HTTP错误状态" + result.StatusCode };
            }
        }


        public static WeXinUserInfoResponseEntity GetUserInfoByOpenid(string accessToken, string openid)
        {
            //获取的格式为https://api.weixin.qq.com/sns/us ... N&openid=OPENID
            //access_token是前面获取的调用凭证,openid是用户标识
            string parameterUrl = string.Format("{0}?access_token={1}&openid={2}&lang=zh-CN", userinfoUrl, accessToken, openid);
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(parameterUrl);
            myHttpWebRequest.Method = "GET";
            //获取结果
            HttpWebResponse result = (HttpWebResponse)myHttpWebRequest.GetResponse();
            string resultStr = "";
            if (result.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(result.GetResponseStream()))
                {
                    resultStr = reader.ReadToEnd();
                    //返回的是json格式,这里直接做序列化即可
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    return serializer.Deserialize<WeXinUserInfoResponseEntity>(resultStr);
                }
            }
            else
            {
                return new WeXinUserInfoResponseEntity() { errcode = -1, errmsg = "HTTP错误状态" + result.StatusCode };
            }
        }
            weiXin1.registerApp(appid, (oo, aa) =>
            {
                if (aa.isError)
                {
                    Toast(aa.error);
                }
                else
                {
                    weiXin1.loginWithWeixin(LoginWeiXinScope, LoginWeiXinState, (ooo, aaa) =>
                    {
                        if (aaa.isError)
                        {
                            Toast(aaa.error);
                        }
                        else
                        {
                            //如果没有错误,会返回一个code,code在args参数中
                            string code = aaa.args.ToString();
                            //根据code去获取access_token
                            WeXinAccessTokenResponseEntity accessTokenEntity = GetAccessTokenByCode(code);
                            if (accessTokenEntity.errcode == 0)
                            {
                                //根据获取到的access token获取用户信息
                                var userEntity = GetUserInfoByOpenid(accessTokenEntity.access_token, accessTokenEntity.openid);
                                //这里要判断一下结果是否有错误
                                if (userEntity.errcode == 0)
                                {
                                    string unionid = userEntity.unionid;
                                    string avatar = userEntity.headimgurl;
                                    string province = userEntity.province;
                                    string city = userEntity.city;
                                    string nickname = userEntity.nickname;
                              }
                         }
                    }
              }
        }

微信api代码如下:

        private void WeiXinApi_Afterlogin(object sender, WeiXinApiLoginEventArgs e)
        {
                    WeiXinApiMiniLoginInfo weiXinApiMiniLogin = e.MiniLoginInfo;
                    if (weiXinApiMiniLogin.errcode == 0)
                    {
                        WeiXinApiMiniEncryptUserInfo apiMiniEncryptUserInfo = weiXinApiMiniLogin.GetMiniEncryptUserInfo(miniAppId, secret);
                        if (apiMiniEncryptUserInfo != null)
                        {
                            string unionid = apiMiniEncryptUserInfo.unionId;
                            string avatar = apiMiniEncryptUserInfo.avatarUrl;
                            string province = apiMiniEncryptUserInfo.province;
                            string city = apiMiniEncryptUserInfo.city;
                            string nickname = apiMiniEncryptUserInfo.nickName;
                       }
           }
使用道具 举报 回复 支持 反对
发表于 2020-12-22 11:23:32
已查看你贴出的代码。
1.APP中的代码,lang参数中不为zh-CN,应该填写zh_CN,其中字符填写错误,可修改后测试
2.微信小程序中代码没有问题;可参看更新Smobiler微信小程序开发指南中的小程序模板,模板中同样有lang参数需要设置,最新模板已添加此参数,可直接更新使用 ;https://www.smobiler.com/guide/wxmini.aspx
使用道具 举报 回复 支持 反对
发表于 2020-12-22 14:46:53
好的,谢谢老师!
使用道具 举报 回复 支持 反对
发表于 2020-12-22 19:03:48
老师,https://www.smobiler.com/guide/wxmini.aspx 这里没有看到可以设置lang参数的地方哦
使用道具 举报 回复 支持 反对
发表于 2020-12-23 09:33:40
Step.5创建完成后,下载Smobiler官方的小程序模板文件,在创建的项目目录下,直接替换以下文件。

可直接下载小程序模板文件
使用道具 举报 回复 支持 反对
发表于 2020-12-24 21:32:12
老师,这不就是小程序的制作流程么?我一早都做过了呀。可这和lang参数有什么关系么?
使用道具 举报 回复 支持 反对
发表于 2020-12-24 21:32:58
难道你们之前的模板里没有考虑到lang参数,而现在的最新版模板中就加入了lang参数?对吗?
使用道具 举报 回复 支持 反对
发表于 2021-1-12 09:06:57
微信注册(绑定用户)时, 北京快3获取到的用户省份和城市都是拼音,上海快3怎么才能获取到中文简体的省份和城市呢?尝试过在请求中加上lang=zh-CN也不行!头痛!
使用道具 举报 回复 支持 反对
12下一页
发新帖
您需要登录后才可以回帖 登录 | 立即注册