Skip to content

Commit

Permalink
🍉修复小程序登录的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Deali-Axy committed Aug 9, 2024
1 parent 75b2c27 commit 29d32b8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/apps/account/apis/oauth2/weapp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traceback
import requests
from django.contrib.auth import login
from django.contrib.auth import login as django_login
from django.contrib.auth.models import User
from django.conf import settings
from ninja import Router, Schema
Expand Down Expand Up @@ -34,12 +35,14 @@ def login(request, payload: WeappLoginSchema):
'grant_type': 'authorization_code'
}).json()
except Exception as e:
traceback.print_exc()
raise HttpError(400, f'请求企微登录接口出错:{e}')

if 'errcode' in login_info:
return responses.bad_request('小程序登录失败', {
'login_info': login_info
})
return responses.bad_request(
f'小程序登录失败。错误代码: {login_info["errcode"]}; {login_info["errmsg"]}',
{'login_info': login_info}
)

is_created_user = False

Expand All @@ -60,8 +63,8 @@ def login(request, payload: WeappLoginSchema):
UserClaim.objects.create(user=user, name='oauth2:weapp:session_key', value=session_key)

# 记录Django登录状态
login(request, user)
django_login(request, user)

token = generate_token({'username': user.username})

return responses.ok('登录成功', token)
return responses.ok('登录成功', token.dict())
18 changes: 18 additions & 0 deletions src/apps/account/migrations/0003_alter_userprofile_is_deleted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-08-09 15:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('account', '0002_alter_userprofile_options_alter_userprofile_table'),
]

operations = [
migrations.AlterField(
model_name='userprofile',
name='is_deleted',
field=models.BooleanField(default=False, verbose_name='软删除标志'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-08-09 15:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_starter_auth', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='userclaim',
name='value',
field=models.CharField(blank=True, max_length=255, null=True),
),
]
2 changes: 1 addition & 1 deletion src/django_starter/contrib/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Meta:
class UserClaim(models.Model):
user = models.ForeignKey(User, db_constraint=False, on_delete=models.DO_NOTHING)
name = models.CharField(max_length=255)
value = models.CharField(max_length=255)
value = models.CharField(max_length=255, null=True, blank=True)

class Meta:
db_table = table_name_wrapper('user_claims')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-08-09 15:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_starter_config', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='configitem',
name='is_deleted',
field=models.BooleanField(default=False, verbose_name='软删除标志'),
),
]
6 changes: 5 additions & 1 deletion src/django_starter/http/response/responses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Optional

from django.http import HttpResponse
from ninja.errors import HttpError


Expand All @@ -9,7 +11,9 @@ def _gen_resp(message, resp_data: Optional[dict], status_code):
data = {'detail': message}

if status_code >= 400:
raise HttpError(status_code, data['detail'])
# 不能使用 ninja 内置的 HttpError,因为这个 HttpError 只能附带 message,导致 resp_data 传不出去
# raise HttpError(status_code, data['detail'])
HttpResponse(data, status=status_code, content_type='application/json')

return data

Expand Down

0 comments on commit 29d32b8

Please sign in to comment.