Exemplo n.º 1
0
    def login_by_wx_ticket(self):
        """微信扫描二维码登录"""
        scene_id = self.args['scene_id']

        wx_userinfo = pf_redis.get('pf_scene_openid:%s' % scene_id)
        if not wx_userinfo:
            return self.send_success(done=False)
        wx_userinfo = json.loads(wx_userinfo.decode())

        if len(str(scene_id)) == 9:
            success, user_or_msg = AuthFunc.login_by_wx(
                self.session, wx_userinfo)
            if not success:
                return self.send_fail(user_or_msg)

            user = user_or_msg
            self.set_current_user(user)
        else:
            return self.send_fail("scene_id 无效")

        pf_redis.delete('pf_scene_openid:%s' % scene_id)

        # 更新微信信息
        AuthFunc.update_through_wx(self.session,
                                   wx_userinfo,
                                   user,
                                   action="bind")
        # 手机绑定状态
        phone_bind = bool(user.phone)
        return self.send_success(done=True, phone_bind=phone_bind)
Exemplo n.º 2
0
 def make_scene_id_len8():
     """8 位随机数"""
     while 1:
         scene_id = random.randint(10000000, 99999999)
         if not pf_redis.get('pf_scene_openid:%s' % scene_id):
             break
     return scene_id
Exemplo n.º 3
0
    def get_client_access_token():
        access_token = pf_redis.get('pf_access_token')
        if access_token:
            return access_token.decode('utf-8')

        data = json.loads(
            urllib.request.urlopen(
                WxOauth2.client_access_token_url).read().decode("utf-8"))
        if 'access_token' in data:
            access_token = data['access_token']
            pf_redis.set("pf_access_token", access_token, 3600)
            return access_token
        else:
            return None
Exemplo n.º 4
0
    def bind_wx(self):
        """绑定微信"""
        scene_id = self.args['scene_id']

        wx_userinfo = pf_redis.get('pf_scene_openid:%s' % scene_id)
        if not wx_userinfo:
            return self.send_success(done=False)
        wx_userinfo = json.loads(wx_userinfo.decode())
        wx_unionid = wx_userinfo["unionid"]

        # 绑定微信
        if len(str(scene_id)) == 8:
            bind_key = "ph_scene_bind_account:%s" % scene_id
            bind_user_id = redis.get(bind_key) or -1
            user = models.AccountInfo.get_by_id(self.session, bind_user_id)

            # scene_id 不对或者 Redis 的 key 过期了
            if not user or not user.passport_id:
                return self.send_fail("请刷新页面后重试")

            success, errmsg = AuthFunc.update_passportinfo(
                user.passport_id, "wx_unionid", wx_unionid)
            if not success:
                if errmsg == "NOT EXIST":
                    return self.send_fail("账户不存在,请联系森果客服 400-027-0135")
                elif errmsg == "SAME VALUE":
                    # 重复绑定是为了取微信信息
                    pass
                elif errmsg == "ALREADY BIND":
                    return self.send_fail("该微信已被其他账户绑定")
                else:
                    return self.send_fail("绑定失败,请联系森果客服 400-027-0135")

            redis.delete(bind_key)
        else:
            return self.send_fail("scene_id 无效")

        pf_redis.delete('pf_scene_openid:%s' % scene_id)
        redis.delete("ph_scene_bind_account:%s" % scene_id)

        # 更新微信信息
        AuthFunc.update_through_wx(self.session,
                                   wx_userinfo,
                                   user,
                                   action="bind")
        return self.send_success(done=True)