2.获取openid这个参数在查询和设置设备状态的时候会用到,对应user参数名。获取openid需要三个参数,access_token已经有了,然后需要device_type和device_id
public const string GetOpenid ="https://api.weixin.qq.com/device/get_openid?access_token={0}&device_type={1}&device_id={2}";
而type和id是前端页面传过来的,当用户微信中打开控制面板的时候,微信会自动将这两个参数追加到url后面。而这个url的返回值结构是这样:
{"open_id":["oxa1otw5sk-Azgd8mx1bmBqoM2_E","oxa1ot8-j9j5bYUJJyAexe9d41_Y","oxa1ot5QTdxn0xNQ0DmYzoN0tUp1"],"resp_msg":{"ret_code":0,"error_info":"ok"}}
openid部分是一个数组,实际使用的是第三个(我目前也不知道前面两个id是干啥的),定义一个openidResult:
- public class OpenIdResult
- {
- private List<string> _openId;
- public List<string> open_id
- {
- get { return _openId??(_openId=new List<string>()); }
- set { _openId = value; }
- }
- public resp_msg resp_msg { get; set; }
- public string GetOpenId()
- {
- if (open_id!=null&&open_id.Count==3)
- {
- return open_id[2];
- }
- return "";
- }
- }
service中:- public string GetOpenId(string accessToken,string deviceType,string deviceId)
- {
- var url = string.Format(WxDeviceConfig.GetOpenid, accessToken, deviceType, deviceId);
- var res = SendHelp.Send<OpenIdResult>(accessToken, url, null, CommonJsonSendType.GET);
- return res.GetOpenId();
- }
|