Smobiler上海石磨_.NET移动开发平台

标题: 微信注册时,获取到的用户省份和城市都是拼音... [打印本页]

作者: kell    时间: 2020-12-20 09:49
标题: 微信注册时,获取到的用户省份和城市都是拼音...
微信注册(绑定用户)时,获取到的用户省份和城市都是拼音,怎么才能获取到中文简体的省份和城市呢?尝试过在请求中加上lang=zh-CN也不行!头痛!
老师有没有更好的办法?获取官方有什么建议?

作者: Lula.Jin    时间: 2020-12-21 10:57
请问使用的是微信插件(https://www.smobiler.com/SmobilerDemo/weixin.aspx)还是微信api( https://www.smobiler.com/guide/weixinapi.aspx) 具体代码是怎么写的
作者: kell    时间: 2020-12-21 18: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;
                       }
           }
作者: peter.pan    时间: 2020-12-22 11:23
已查看你贴出的代码。
1.APP中的代码,lang参数中不为zh-CN,应该填写zh_CN,其中字符填写错误,可修改后测试
2.微信小程序中代码没有问题;可参看更新Smobiler微信小程序开发指南中的小程序模板,模板中同样有lang参数需要设置,最新模板已添加此参数,可直接更新使用 ;https://www.smobiler.com/guide/wxmini.aspx
作者: kell    时间: 2020-12-22 14:46
好的,谢谢老师!
作者: kell    时间: 2020-12-22 19:03
老师,https://www.smobiler.com/guide/wxmini.aspx 这里没有看到可以设置lang参数的地方哦
作者: peter.pan    时间: 2020-12-23 09:33
Step.5创建完成后,下载Smobiler官方的小程序模板文件,在创建的项目目录下,直接替换以下文件。

可直接下载小程序模板文件

作者: kell    时间: 2020-12-24 21:32
老师,这不就是小程序的制作流程么?我一早都做过了呀。可这和lang参数有什么关系么?
作者: kell    时间: 2020-12-24 21:32
难道你们之前的模板里没有考虑到lang参数,而现在的最新版模板中就加入了lang参数?对吗?
作者: liliala68    时间: 2021-1-12 09:06
微信注册(绑定用户)时, 北京快3获取到的用户省份和城市都是拼音,上海快3怎么才能获取到中文简体的省份和城市呢?尝试过在请求中加上lang=zh-CN也不行!头痛!
作者: kell    时间: 2021-1-12 22:04
老师说,lang=zh-CN要改为lang=zh_CN,但是我还没去验证过




欢迎光临 Smobiler上海石磨_.NET移动开发平台 (https://www.smobiler.com/) Powered by Discuz! X3.2