示例#1
0
 def post(self, request):
     user = json.loads(request.body)
     phone = user.get('phone')
     password = user.get('password')
     user = authenticate(request, username=phone, password=password)
     u = UserProfile.objects.filter(phone=phone)
     if not u:
         return self.write_json({'errno': 1, 'msg': '手机号尚未注册'})
     if user:
         login(request, user)
         try:
             up = UserProfile.objects.get(user=user)
         except UserProfile.DoesNotExist:
             return self.write_json({'errno': 1, 'msg': '用户不存在!'})
     else:
         return self.write_json({'errno': '1', 'msg': '密码错误,请重新输入'})
     data = {
         'token': generate_token(up.id),
         'userid': up.id,
         'username': up.username,
         'head_img': up.head_img,
         'phone': up.phone,
         'role': up.role
     }
     return self.write_json({'errno': 0, 'msg': 'success', 'data': data})
示例#2
0
    def post(self, request):
        user = json.loads(request.body)
        phone = user.get('phone')
        password = user.get('password')
        try:
            admin = UserProfile.objects.get(phone=phone)
        except:
            return self.write_json({'errno': 1, 'msg': '用户不存在'})
        if 1 != admin.role:
            return self.write_json({'errno': '1', 'msg': '该用户不是管理员'})
        user = authenticate(request, username=phone, password=password)
        if user:
            login(request, user)
            try:
                up = UserProfile.objects.get(user=user)
            except UserProfile.DoesNotExist:
                return self.write_json({'errno': 1, 'msg': '用户不存在'})
        else:
            return self.write_json({'errno': '1', 'msg': '密码错误,请重新输入'})

        request.session['username'] = up.username
        request.session.set_expiry(300)
        data = {
            'token': generate_token(up.id),
            'userid': up.id,
            'username': up.username,
            'head_img': up.head_img,
            'phone': up.phone,
            'role': up.role
        }
        return self.write_json({'errno': 0, 'msg': 'success', 'data': data})
示例#3
0
 def post(self, request):
     print(request.body)
     user_info = json.loads(request.body)
     password = user_info.get('password')
     new_password = user_info.get('new_password')
     up = UserProfile.objects.filter(username=request.user)
     user = request.user
     phone = user.phone
     user = authenticate(request, username=phone, password=password)
     if not user:
         return self.write_json({'errno': 1, 'msg': '旧密码错误!'})
     user.set_password(new_password)
     user.save()
     if user:
         login(request, user)
         try:
             ups = UserProfile.objects.get(user=user)
         except UserProfile.DoesNotExist:
             return self.write_json({'errno': 1, 'msg': '不存在用户信息'})
     try:
         ups = UserProfile.objects.get(user=user)
     except UserProfile.DoesNotExist:
         up = UserProfile()
         up.user = user
         up.username = user.username
         up.phone = user.username
         up.token = generate_token(up.id)
         up.save()
     return self.write_json({'errno': 0, 'msg': 'success'})
示例#4
0
        def is_login(self, *args, **kwargs):
            token = self.request.session.get('token', None)
            self.user_id = self.request.session.get('user_id', None)

            if not self.user_id or token != generate_token(self.user_id):
                url = LOGIN_URL
                if self.request.path.count("next") > 3:
                    return redirect(url)
                if "?" not in url:
                    get_method_param_keys = self.request.GET.keys()
                    if get_method_param_keys:
                        data = {}
                        #get GET params
                        for key in get_method_param_keys:
                            data[key] = self.request.GET.get('%s' % key)

                        _next = self.request.path + "?" + urllib.urlencode(
                            data)

                        url += "?" + urllib.urlencode(dict(next=_next))
                    else:
                        url += "?" + urllib.urlencode(
                            dict(next=self.request.path))
                return redirect(url)
            return view_func(self, *args, **kwargs)
示例#5
0
 def post(self, request):
     msg = json.loads(request.body)
     if 'TRADE' in msg.get('type'):
         print(msg.get('id'))
         sign = auth.Sign(app_id='${app_id}', app_secret='${app_secret}')
         yzclient = YZClient(sign)
         params = {}
         params['tid'] = msg.get('id')
         files = {}
         result = yzclient.invoke('youzan.trade.get',
                                  '4.0.0',
                                  'GET',
                                  params=params,
                                  files=files)
         r = json.loads(result.decode('utf-8'))
         s = (((r.get('response')).get('full_order_info')).get('order_info')
              ).get('status_str')
         phone = (((r.get('response')).get('full_order_info')
                   ).get('address_info')).get('receiver_tel')
         good = (((r.get('response')).get('full_order_info')).get('orders'))
         price = (((r.get('response')).get('full_order_info')
                   ).get('pay_info')).get('total_fee')
         print(r)
         print(s)
         print(phone)
         print(price)
         try:
             for goods in good:
                 course = Course.objects.get(title=goods.get('title'))
                 if '已完成' == s:
                     ups = UserProfile.objects.filter(phone=phone)
                     if not ups:
                         user = User.objects.create_user(username=phone,
                                                         password='******')
                         up = UserProfile()
                         up.user = user
                         up.head_img = 'http://static.openmindclub.com/image/default-avatar/avatar2.png'
                         up.phone = phone
                         up.username = phone
                         up.token = generate_token(up.id)
                         up.save()
                         user_buy = User_Buy_Record()
                         user_buy.user = up
                         user_buy.price = price
                         user_buy.course = course
                         user_buy.status = 1
                         user_buy.save()
                     if ups:
                         for us in ups:
                             user_buy = User_Buy_Record()
                             user_buy.user = us
                             user_buy.course = course
                             user_buy.status = 1
                             user_buy.save()
             return self.write_json({'code': 0, 'msg': 'success'})
         except Course.DoesNotExist:
             return self.write_json({'code': 0, 'msg': 'success'})
     return self.write_json({'code': 0, 'msg': 'success'})
示例#6
0
 def post(self, request):
     user_info = json.loads(request.body)
     new_password = user_info.get('new_password')
     phone = user_info.get('phone')
     print(phone)
     try:
         user = User.objects.get(username=phone)
         user.set_password(new_password)
         user.save()
         try:
             up = UserProfile.objects.get(user=user)
         except UserProfile.DoesNotExist:
             up = UserProfile()
             up.phone = user.username
             up.token = generate_token(up.id)
             up.save()
         up.user = user
         up.save()
         return self.write_json({'errno': 0, 'msg': 'success'})
     except User.DoesNotExist:
         return self.write_json({'errno': 1, 'msg': '不存在用户'})
示例#7
0
 def post(self, request):
     user_info = json.loads(request.body)
     print(user_info)
     username = user_info.get('username')
     password = user_info.get('password')
     phone = user_info.get('phone')
     auth_code = user_info.get('auth_code')
     #      ac = AuthCode.objects.filter(phone=phone,auth_code=auth_code).first()
     #      if not ac:
     #          return self.write_json({'errno':1,'msg':'手机验证码不存在!'})
     #      ac.delete()
     ups = UserProfile.objects.filter(username=username)
     if ups:
         return self.write_json({'errno': 1, 'msg': '昵称已存在,请换一个昵称'})
     try:
         User.objects.get(username=phone)
         return self.write_json({'errno': 1, 'msg': '该手机号的用户已存在'})
     except User.DoesNotExist:
         #            user = User.objects.create_user(username=phone,password=password)
         user = User(username=phone)
         user.password = make_password(password=password, salt=None)
         user.save()
         up = UserProfile()
         up.user = user
         up.head_img = 'http://static.openmindclub.com/image/default-avatar/avatar2.png'
         up.username = username
         up.phone = phone
         up.token = generate_token(up.id)
         up.save()
     data = {
         'user': user.username,
         'username': up.username,
         'id': str(up.id),
         'token': up.token,
         'head_img': up.head_img
     }
     return self.write_json({'errno': 0, 'msg': 'success', 'data': data})