Example #1
0
    def wechatlogin(self, request):

        session_key = request.data_format.get("session_key")
        appId = WECHAT_APPID
        sessionKey = session_key
        encryptedData = request.data_format.get("encryptedData")
        iv = request.data_format.get("iv")
        pc = WXBizDataCrypt(appId, sessionKey)
        res = pc.decrypt(encryptedData, iv)

        try:
            user = Users.objects.get(uuid=res.get('openid'))
        except Users.DoesNotExist:
            user = Users.objects.create(
                **{
                    "userid":
                    idGenerator.userid('4001'),
                    "uuid":
                    res.get('openId') if 'unionId' not in
                    res else res['unionId'],
                    "rolecode":
                    '4001',
                    "mobile":
                    res.get('openId') if 'unionId' not in
                    res else res['unionId'],
                    "name":
                    res.get("nickName"),
                    "sex":
                    res.get("sex"),
                    "addr":
                    "{}-{}-{}".format(res.get("country"), res.get("city"),
                                      res.get("province")),
                    "pic":
                    res.get("avatarUrl"),
                    "appid":
                    res.get("watermark")['appid']
                })

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

        res = Address.objects.filter(
            userid=user.userid).order_by('-createtime')
        address = AddressModelSerializer(
            res, many=True).data[0] if res.count() else {}

        return {
            "data": {
                "user": UsersSerializers(user, many=False).data,
                "token": token,
                "address": address
            }
        }
Example #2
0
File: api.py Project: gzshop/server
    def login1(self, request):

        try:
            user = Users.objects.get(uuid=request.data_format.get("mobile",""))
        except Users.DoesNotExist:
            user = Users.objects.create(**{
                "userid": idGenerator.userid('4001'),
                "uuid": request.data_format.get("mobile",""),
                "rolecode": '4001',
                "mobile": request.data_format.get("mobile",""),
                "name": request.data_format.get("mobile",""),
                "sex": "",
                "addr": "",
                "pic": "",
                "appid": ""
            })

        if not request.data_format.get('vercode',None):
            raise PubErrorCustom("验证码不能为空!")

        rRes = RedisVercodeHandler().get(user.uuid)
        if rRes and not len(rRes) or not rRes:
            raise PubErrorCustom("请先获取验证码!")

        if request.data_format.get('vercode',None) != rRes:
            raise PubErrorCustom("验证码错误!")

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

        res=Address.objects.filter(userid=user.userid,moren='0')
        address = AddressModelSerializer(res, many=True).data[0] if res.count() else {}

        return {"data": {
            "token": token,
            "userinfo":UsersSerializers(user, many=False).data,
            "address":address
        }}