1. 代码:

1
2
3
4
5
6
7
8
9
10
# 获取钉钉二维码
class DingDing(APIView):

def get(self, request):
app_id = ''
redice_url = '' # 回调地址
app_secret = ''
return Response({"url": 'https://oapi.dingtalk.com/connect/qrconnect?appid=' + app_id +
'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + redice_url,
'code': 200})

2. 获取token:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class DingBind(APIView):

def get(self, request):
code = request.query_params.get('code')
print(code)
t = time.time()
# 时间戳
timestamp = str((int(round(t * 1000))))
# 替换成自己的appSecret
appSecret = 'Wrp4YkJ_Aay1lEclT4YUGqvMDHqf4gZJL-TzjOQOEZYpYXz3Ztw07Rq5Igr6KYyU'
# 构造签名
signature = base64.b64encode(
hmac.new(appSecret.encode('utf-8'), timestamp.encode('utf-8'), digestmod=sha256).digest())
# 请求接口,换取钉钉用户名
payload = {'tmp_auth_code': code}
headers = {'Content-Type': 'application/json'}
res = requests.post('https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature=' + parse.quote(
signature.decode("utf-8")) + "&timestamp=" + timestamp + "&accessKey=dingoalxhmajgqbhn4ennn",
data=json.dumps(payload), headers=headers) # accessKey替换成自己的appid
# 获取到一个随便的唯一id
res_dict = json.loads(res.text)
print(res_dict)
unionid = res_dict['user_info']['unionid']
user = User.objects.filter(ding=unionid).first()
if not user:
return Response({'code': 404, 'unioid': unionid})
else:
return Response({'code': 200, 'id': user.id, 'vip': user.id})
# 绑定已有用户
def post(self, request):
unionid = request.data.get('unionid', None)
created_at = request.data.get('created_at', None)
id = request.data.get('id')
wyx = request.data.get('wyx')
user = User.objects.filter(id=id).first()
if wyx & 1 == 1:
User.objects.filter(id=id).update(ding=unionid)
return Response({'msg': "绑定成功", 'code': 200, 'id': user.id, 'vip': user.id})
else:
User.objects.filter(id=id).update(git=created_at)
return Response({'msg': "绑定成功", 'code': 200, 'id': user.id, 'vip': user.id})

3.vue回调地址代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<template>
<div>
<h1>回调</h1>
</div>
</template>

<script>
import axios from 'axios'
export default {
name: "ding_url",
data(){
return{
code : this.$route.query.code,
}
},
created() {
if(this.code){
axios({
url:'http://127.0.0.1:8000/app2/ding_bind/?code='+this.code,
}).then(res =>{
if(res.data.code==404){
this.$router.push('/bind/?unionid='+res.data.unioid)
}else{
localStorage.setItem('vip',res.data.vip)
localStorage.setItem('id',res.data.id)
this.$router.push('/myback')
}
})
}
}
}
</script>

<style scoped>

</style>

4.vue绑定用户代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<template>
<div>
<p v-for="i in nr">
{{i.username}}---
<a-button @click="bind(i.id)">绑定</a-button>
</p>

</div>
</template>

<script>
import axios from 'axios'
export default {
name: "bind",
data() {
return {
unionid:this.$route.query.unionid,
created_at:this.$route.query.created_at,
nr:[],
}
},
methods:{
bind(id){
if (this.unionid){
axios({
url:'http://127.0.0.1:8000/app2/ding_bind/',
method:'post',
data:{'unionid':this.unionid,'id':id,'wyx':1}
}).then(res =>{
if(res.data.code == 200){
localStorage.setItem('vip',res.data.vip)
localStorage.setItem('id',res.data.id)
this.$router.push('/myback')
}else{
alert(res.data.msg)
}
})
}else{
axios({
url:'http://127.0.0.1:8000/app2/ding_bind/',
method:'post',
data:{'created_at':this.created_at,'id':id,'wyx':10}
}).then(res =>{
if(res.data.code == 200){
localStorage.setItem('vip',res.data.vip)
localStorage.setItem('id',res.data.id)
this.$router.push('/myback')
}else{
alert(res.data.msg)
}
})
}

}
},
created() {
axios({
url:'http://127.0.0.1:8000/app2/user/'
}).then(res =>{
this.nr = res.data
})
}

}
</script>

<style scoped>

</style>