ODOO微信用户信息同步问题
Python使用http高级requests库来对接微信开放平台和公众平台非常干净利落,主要两个小问题需要注意一下:
1、用户昵称乱码
正常拉取用户信息所返回的内容编码不太友好,读取时乱码,需要显式设定lang和encoding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
''' author: i@renjie.me ''' def get_sns_user_info(self, sns_token, openid): url = self.get_api_url("api") + "/sns/userinfo" params = dict( access_token = sns_token, openid = openid, lang = "en", ) res = requests.get(url, params=params) res.encoding = 'utf-8' ret = res.json() log = "get_sns_user_info: sns_token(%s) openid(%s) ret(%s)" % (sns_token, openid, ret) if "nickname" in ret: _logger.info(log) return ret else: _logger.error(log) |
2、用户头像时效
如只保存头像链接,若用户更换头像,原有链接将失效,需要用Binary字段存储一份图片二进制base64编码的副本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
''' author: i@renjie.me headimgurl = fields.Char(u'头像地址', readonly=True) head = fields.Binary(u"头像", attachment=True, readonly=True) ''' @api.multi def write(self, vals): if "headimgurl" in vals: if vals.get("headimgurl"): res = requests.get(vals["headimgurl"]) vals["head"] = res.content.encode('base64') else: vals["head"] = "" return super(UsersWechat, self).write(vals) |