Exemple #1
0
def get_qrcode_wechat(id):

    params = dict(appid=WECHAT_APPID,
                  secret=WECHAT_SECRET,
                  grant_type="client_credential")

    res = send_request_other(url="https://api.weixin.qq.com/cgi-bin/token",
                             params=params)

    token = res['access_token']

    data = dict(
        # access_token= token,
        scene=id,
        page="pages/details/index")

    res = request(method='POST',
                  json=data,
                  url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?" +
                  'access_token=%s' % token,
                  headers={"Content-Type": 'application/json'})

    path = os.path.join(IMAGE_PATH, 'goods')

    with open(os.path.join(path, "{}.jpg".format(id)), 'wb') as f:
        f.write(res.content)

    return "{}/statics/images/{}/{}".format(ServerUrl, "goods",
                                            "{}.jpg".format(id))
Exemple #2
0
    def wechatauth(self, request):
        params = dict(
            js_code=request.data_format.get("js_code"),
            appid=WECHAT_APPID,
            secret=WECHAT_SECRET,
            grant_type="authorization_code",
        )
        wechat_res = send_request_other(
            url="https://api.weixin.qq.com/sns/jscode2session",
            params=params)
        if not wechat_res.get("openid"):
            raise PubErrorCustom("获取用户错误,腾讯接口有误!")
        print(wechat_res)

        data={}
        token=False
        try:
            user=Users.objects.get(uuid=wechat_res.get('openid'))
            data = UsersSerializers(user,many=False).data

            token = get_token()
            res = UserModelSerializerToRedis(user, many=False).data
            RedisTokenHandler(key=token).redis_dict_set(res)

        except Users.DoesNotExist:
            pass

        return {"data":{
            "user" : data,
            "session_key":wechat_res.get("session_key"),
            "token":token
        }}