From 923685cbb6f00d98f2ca58043eaff9d6ddf6e546 Mon Sep 17 00:00:00 2001 From: guoweilong Date: Mon, 20 Nov 2023 15:10:21 +0800 Subject: [PATCH] feat: 钉钉静默登录 --- api/auth.js | 6 +++++- common/config.js | 4 ++-- manifest.json | 2 +- pages/login/mobile.vue | 36 ++++++++++++++++++++++++++++-------- pages/market/index.vue | 29 +++++++++++++++++++++-------- store/mudules/user.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++------------- 6 files changed, 104 insertions(+), 33 deletions(-) diff --git a/api/auth.js b/api/auth.js index b6902c7..af91882 100644 --- a/api/auth.js +++ b/api/auth.js @@ -1,8 +1,12 @@ +import code from "../uni_modules/uview-ui/libs/config/props/code" + //请求工具参考https://ext.dcloud.net.cn/plugin?id=392 const { http } = uni.$u //使用手机 + 密码登录 export const passwordLogin = data => http.post('/auth/login', data) +//钉钉登录 +export const dingLogin = code => http.post('/auth/dingding/login?code='+code) //发送手机验证码 export const sendSmsCode = data => http.post('/member/auth/send-sms-code', data) //使用手机 + 验证码登录 @@ -10,6 +14,6 @@ export const smsLogin = data => http.post('/member/auth/sms-login', data) //微信小程序的一键登录 export const weixinMiniAppLogin = data => http.post('/member/auth/weixin-mini-app-login', data) //刷新令牌 -export const refreshToken = data => http.post('/member/auth/refresh-token', {data}) +export const refreshToken = data => http.post('/member/auth/refresh-token', {data}) //退出登录 export const logout = data => http.delete('/auth/logout', data) diff --git a/common/config.js b/common/config.js index c66d45b..24d25a6 100644 --- a/common/config.js +++ b/common/config.js @@ -1,9 +1,9 @@ module.exports = { //后端接口地址 // baseUrl: 'http://10.11.38.240:9001', //内网开发环境 - baseUrl: 'http://172.17.56.37:18082/api', //测试环境 + // baseUrl: 'http://172.17.56.37:18082/api', //测试环境 // baseUrl: 'http://172.17.56.16:18082/api', //ip正式环境 - // baseUrl: 'https://scdcapp.chinagasholdings.com/api', //域名正式环境 + baseUrl: 'https://scdcapp.chinagasholdings.com/api', //域名正式环境 // 超时 timeout: 30000, // 禁用 Cookie 等信息 diff --git a/manifest.json b/manifest.json index 973e1ee..86c5957 100644 --- a/manifest.json +++ b/manifest.json @@ -75,7 +75,7 @@ "base" : "./" }, "devServer" : { - "https" : false + "https" : true } } } diff --git a/pages/login/mobile.vue b/pages/login/mobile.vue index 67165f5..91342db 100644 --- a/pages/login/mobile.vue +++ b/pages/login/mobile.vue @@ -47,7 +47,7 @@ - @@ -97,24 +97,44 @@ }, methods: { detectionEnvironment() { + let that = this; console.log(this.$isDing); if (this.$isDing) { - // this.isDing = true; + this.isDing = true; dd.ready(() => { dd.runtime.permission.requestAuthCode({ - corpId: corpId, + corpId: "dinge6053c157358448735c2f4657eb6378f", onSuccess: (info) => { - callback(info.code) + that.$store.dispatch('Login', { + type: 2, + data: info.code + }).then(res => { + uni.$u.toast('登录成功'); + //获取全部字典信息 保存至store + that.$store.dispatch('loadDictDatas') + setTimeout(() => { + uni.switchTab({ + url: '/pages/market/index' + }) + }, 300) + }).catch(err => { + // alert(JSON.stringify(err)) + console.log(err); + // uni.$u.toast('非钉钉内部账号'); + setTimeout(()=>{ + that.isDing = false; + },500) + }) }, onFail: (err) => { - alert('fail') - alert(JSON.stringify(err)) + that.isDing = false + console.log(err); } }) }) } }, - + handleSubmit() { this.$refs.form.validate().then(res => { let data = this.formData @@ -137,7 +157,7 @@ }, mobileLogin(data) { this.$store.dispatch('Login', { - type: this.currentModeIndex, + type: 0, data: data }).then(res => { uni.$u.toast('登录成功'); diff --git a/pages/market/index.vue b/pages/market/index.vue index 7fb9c45..0f0afb6 100644 --- a/pages/market/index.vue +++ b/pages/market/index.vue @@ -37,6 +37,8 @@ import { listSimpleDictDatas } from '@/api/dict.js' + import * as dd from 'dingtalk-jsapi' + export default { components: { newsList @@ -168,13 +170,24 @@ }, loginOut() { // this.$modal.confirm('确定注销并退出系统吗?').then(() => { - this.$store.dispatch('Logout').then((res) => { - this.loginOutShow = false; - uni.reLaunch({ - url: '/pages/login/mobile' + //钉钉直接退出微应用 + if (this.$isDing) { + dd.biz.navigation.close({ + onSuccess: function(result) { + console.log(result, 'result') + }, + onFail: function(err) { + console.log(err, 'err') + } + }); + } else { + this.$store.dispatch('Logout').then((res) => { + this.loginOutShow = false; + uni.reLaunch({ + url: '/pages/login/mobile' + }) }) - }) - // }) + } } } } @@ -189,8 +202,8 @@ position: relative; min-height: 65vh; } - - .u-empty{ + + .u-empty { min-height: 65vh; } diff --git a/store/mudules/user.js b/store/mudules/user.js index f53dbf3..b2ad474 100644 --- a/store/mudules/user.js +++ b/store/mudules/user.js @@ -1,6 +1,16 @@ -import { getUserInfo } from '@/api/user' -import { passwordLogin, smsLogin, weixinMiniAppLogin, logout } from '@/api/auth' -import { listSimpleDictDatas } from '@/api/dict' +import { + getUserInfo +} from '@/api/user' +import { + passwordLogin, + smsLogin, + weixinMiniAppLogin, + logout, + dingLogin +} from '@/api/auth' +import { + listSimpleDictDatas +} from '@/api/dict' const AccessTokenKey = 'ACCESS_TOKEN' const RefreshTokenKey = 'REFRESH_TOKEN' @@ -9,9 +19,9 @@ const user = { state: { accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 - roleId:"", - entity:"", - entityName:"", + roleId: "", + entity: "", + entityName: "", userInfo: {} }, mutations: { @@ -28,8 +38,11 @@ const user = { // 更新令牌 SET_TOKEN(state, data) { // 设置令牌 - console.log(111,data); - const { access_token, refresh_token } = data + console.log(111, data); + const { + access_token, + refresh_token + } = data state.accessToken = access_token state.refreshToken = refresh_token const sysUser = data.sysUser; @@ -37,7 +50,7 @@ const user = { let roleIdString = sysUser.roles.map(role => role.roleId).join(',') || ""; let entityString = ysEntities.map(item => item.entity).join(',') || ""; let entityNameString = ysEntities.map(item => item.entityName).join(',') || ""; - console.log(roleIdString,entityString,entityNameString); + console.log(roleIdString, entityString, entityNameString); state.roleId = roleIdString; state.entity = entityString; state.entityName = entityNameString; @@ -61,13 +74,19 @@ const user = { state.refreshToken = '' state.userInfo = {} uni.reLaunch({ - url:'/pages/login/mobile' + url: '/pages/login/mobile' }) } }, actions: { //账号登录 - Login({ state, commit }, { type, data }) { + Login({ + state, + commit + }, { + type, + data + }) { if (type === 0) { return passwordLogin(data) .then(res => { @@ -86,6 +105,15 @@ const user = { .catch(err => { return Promise.reject(err) }) + } else if (type === 2) { + return dingLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) } else { return weixinMiniAppLogin(data) .then(res => { @@ -98,7 +126,10 @@ const user = { } }, // 退出登录 - Logout({ state, commit }) { + Logout({ + state, + commit + }) { return logout() .then(res => { return Promise.resolve(res) @@ -111,7 +142,10 @@ const user = { }) }, // 获得用户基本信息 - async ObtainUserInfo({ state, commit }) { + async ObtainUserInfo({ + state, + commit + }) { const res = await getUserInfo() commit('SET_USER_INFO', res.data) } -- libgit2 0.21.2