Commit 923685cbb6f00d98f2ca58043eaff9d6ddf6e546

Authored by 郭伟龙
1 parent fa27124f
Exists in develop and in 1 other branch charge

feat: 钉钉静默登录

  1 +import code from "../uni_modules/uview-ui/libs/config/props/code"
  2 +
1 //请求工具参考https://ext.dcloud.net.cn/plugin?id=392 3 //请求工具参考https://ext.dcloud.net.cn/plugin?id=392
2 const { http } = uni.$u 4 const { http } = uni.$u
3 5
4 //使用手机 + 密码登录 6 //使用手机 + 密码登录
5 export const passwordLogin = data => http.post('/auth/login', data) 7 export const passwordLogin = data => http.post('/auth/login', data)
  8 +//钉钉登录
  9 +export const dingLogin = code => http.post('/auth/dingding/login?code='+code)
6 //发送手机验证码 10 //发送手机验证码
7 export const sendSmsCode = data => http.post('/member/auth/send-sms-code', data) 11 export const sendSmsCode = data => http.post('/member/auth/send-sms-code', data)
8 //使用手机 + 验证码登录 12 //使用手机 + 验证码登录
@@ -10,6 +14,6 @@ export const smsLogin = data => http.post('/member/auth/sms-login', data) @@ -10,6 +14,6 @@ export const smsLogin = data => http.post('/member/auth/sms-login', data)
10 //微信小程序的一键登录 14 //微信小程序的一键登录
11 export const weixinMiniAppLogin = data => http.post('/member/auth/weixin-mini-app-login', data) 15 export const weixinMiniAppLogin = data => http.post('/member/auth/weixin-mini-app-login', data)
12 //刷新令牌 16 //刷新令牌
13 -export const refreshToken = data => http.post('/member/auth/refresh-token', {data}) 17 +export const refreshToken = data => http.post('/member/auth/refresh-token', {data})
14 //退出登录 18 //退出登录
15 export const logout = data => http.delete('/auth/logout', data) 19 export const logout = data => http.delete('/auth/logout', data)
common/config.js
1 module.exports = { 1 module.exports = {
2 //后端接口地址 2 //后端接口地址
3 // baseUrl: 'http://10.11.38.240:9001', //内网开发环境 3 // baseUrl: 'http://10.11.38.240:9001', //内网开发环境
4 - baseUrl: 'http://172.17.56.37:18082/api', //测试环境 4 + // baseUrl: 'http://172.17.56.37:18082/api', //测试环境
5 // baseUrl: 'http://172.17.56.16:18082/api', //ip正式环境 5 // baseUrl: 'http://172.17.56.16:18082/api', //ip正式环境
6 - // baseUrl: 'https://scdcapp.chinagasholdings.com/api', //域名正式环境 6 + baseUrl: 'https://scdcapp.chinagasholdings.com/api', //域名正式环境
7 // 超时 7 // 超时
8 timeout: 30000, 8 timeout: 30000,
9 // 禁用 Cookie 等信息 9 // 禁用 Cookie 等信息
@@ -75,7 +75,7 @@ @@ -75,7 +75,7 @@
75 "base" : "./" 75 "base" : "./"
76 }, 76 },
77 "devServer" : { 77 "devServer" : {
78 - "https" : false 78 + "https" : true
79 } 79 }
80 } 80 }
81 } 81 }
pages/login/mobile.vue
@@ -47,7 +47,7 @@ @@ -47,7 +47,7 @@
47 </view> 47 </view>
48 </u--form> 48 </u--form>
49 </view> 49 </view>
50 - <u-loading-page :loading="isDing" iconSize="50" loadingMode="spinner" image="/static/images/empty/loding-logo.gif" 50 + <u-loading-page v-if="isDing" :loading="isDing" iconSize="50" loadingMode="spinner" image="/static/images/empty/loding-logo.gif"
51 loadingText="检测为钉钉环境\n自动登录中..." bg-color="#e8e8e8"></u-loading-page> 51 loadingText="检测为钉钉环境\n自动登录中..." bg-color="#e8e8e8"></u-loading-page>
52 </view> 52 </view>
53 </template> 53 </template>
@@ -97,24 +97,44 @@ @@ -97,24 +97,44 @@
97 }, 97 },
98 methods: { 98 methods: {
99 detectionEnvironment() { 99 detectionEnvironment() {
  100 + let that = this;
100 console.log(this.$isDing); 101 console.log(this.$isDing);
101 if (this.$isDing) { 102 if (this.$isDing) {
102 - // this.isDing = true; 103 + this.isDing = true;
103 dd.ready(() => { 104 dd.ready(() => {
104 dd.runtime.permission.requestAuthCode({ 105 dd.runtime.permission.requestAuthCode({
105 - corpId: corpId, 106 + corpId: "dinge6053c157358448735c2f4657eb6378f",
106 onSuccess: (info) => { 107 onSuccess: (info) => {
107 - callback(info.code) 108 + that.$store.dispatch('Login', {
  109 + type: 2,
  110 + data: info.code
  111 + }).then(res => {
  112 + uni.$u.toast('登录成功');
  113 + //获取全部字典信息 保存至store
  114 + that.$store.dispatch('loadDictDatas')
  115 + setTimeout(() => {
  116 + uni.switchTab({
  117 + url: '/pages/market/index'
  118 + })
  119 + }, 300)
  120 + }).catch(err => {
  121 + // alert(JSON.stringify(err))
  122 + console.log(err);
  123 + // uni.$u.toast('非钉钉内部账号');
  124 + setTimeout(()=>{
  125 + that.isDing = false;
  126 + },500)
  127 + })
108 }, 128 },
109 onFail: (err) => { 129 onFail: (err) => {
110 - alert('fail')  
111 - alert(JSON.stringify(err)) 130 + that.isDing = false
  131 + console.log(err);
112 } 132 }
113 }) 133 })
114 }) 134 })
115 } 135 }
116 }, 136 },
117 - 137 +
118 handleSubmit() { 138 handleSubmit() {
119 this.$refs.form.validate().then(res => { 139 this.$refs.form.validate().then(res => {
120 let data = this.formData 140 let data = this.formData
@@ -137,7 +157,7 @@ @@ -137,7 +157,7 @@
137 }, 157 },
138 mobileLogin(data) { 158 mobileLogin(data) {
139 this.$store.dispatch('Login', { 159 this.$store.dispatch('Login', {
140 - type: this.currentModeIndex, 160 + type: 0,
141 data: data 161 data: data
142 }).then(res => { 162 }).then(res => {
143 uni.$u.toast('登录成功'); 163 uni.$u.toast('登录成功');
pages/market/index.vue
@@ -37,6 +37,8 @@ @@ -37,6 +37,8 @@
37 import { 37 import {
38 listSimpleDictDatas 38 listSimpleDictDatas
39 } from '@/api/dict.js' 39 } from '@/api/dict.js'
  40 + import * as dd from 'dingtalk-jsapi'
  41 +
40 export default { 42 export default {
41 components: { 43 components: {
42 newsList 44 newsList
@@ -168,13 +170,24 @@ @@ -168,13 +170,24 @@
168 }, 170 },
169 loginOut() { 171 loginOut() {
170 // this.$modal.confirm('确定注销并退出系统吗?').then(() => { 172 // this.$modal.confirm('确定注销并退出系统吗?').then(() => {
171 - this.$store.dispatch('Logout').then((res) => {  
172 - this.loginOutShow = false;  
173 - uni.reLaunch({  
174 - url: '/pages/login/mobile' 173 + //钉钉直接退出微应用
  174 + if (this.$isDing) {
  175 + dd.biz.navigation.close({
  176 + onSuccess: function(result) {
  177 + console.log(result, 'result')
  178 + },
  179 + onFail: function(err) {
  180 + console.log(err, 'err')
  181 + }
  182 + });
  183 + } else {
  184 + this.$store.dispatch('Logout').then((res) => {
  185 + this.loginOutShow = false;
  186 + uni.reLaunch({
  187 + url: '/pages/login/mobile'
  188 + })
175 }) 189 })
176 - })  
177 - // }) 190 + }
178 } 191 }
179 } 192 }
180 } 193 }
@@ -189,8 +202,8 @@ @@ -189,8 +202,8 @@
189 position: relative; 202 position: relative;
190 min-height: 65vh; 203 min-height: 65vh;
191 } 204 }
192 -  
193 - .u-empty{ 205 +
  206 + .u-empty {
194 min-height: 65vh; 207 min-height: 65vh;
195 } 208 }
196 209
store/mudules/user.js
1 -import { getUserInfo } from '@/api/user'  
2 -import { passwordLogin, smsLogin, weixinMiniAppLogin, logout } from '@/api/auth'  
3 -import { listSimpleDictDatas } from '@/api/dict' 1 +import {
  2 + getUserInfo
  3 +} from '@/api/user'
  4 +import {
  5 + passwordLogin,
  6 + smsLogin,
  7 + weixinMiniAppLogin,
  8 + logout,
  9 + dingLogin
  10 +} from '@/api/auth'
  11 +import {
  12 + listSimpleDictDatas
  13 +} from '@/api/dict'
4 14
5 const AccessTokenKey = 'ACCESS_TOKEN' 15 const AccessTokenKey = 'ACCESS_TOKEN'
6 const RefreshTokenKey = 'REFRESH_TOKEN' 16 const RefreshTokenKey = 'REFRESH_TOKEN'
@@ -9,9 +19,9 @@ const user = { @@ -9,9 +19,9 @@ const user = {
9 state: { 19 state: {
10 accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 20 accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌
11 refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 21 refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌
12 - roleId:"",  
13 - entity:"",  
14 - entityName:"", 22 + roleId: "",
  23 + entity: "",
  24 + entityName: "",
15 userInfo: {} 25 userInfo: {}
16 }, 26 },
17 mutations: { 27 mutations: {
@@ -28,8 +38,11 @@ const user = { @@ -28,8 +38,11 @@ const user = {
28 // 更新令牌 38 // 更新令牌
29 SET_TOKEN(state, data) { 39 SET_TOKEN(state, data) {
30 // 设置令牌 40 // 设置令牌
31 - console.log(111,data);  
32 - const { access_token, refresh_token } = data 41 + console.log(111, data);
  42 + const {
  43 + access_token,
  44 + refresh_token
  45 + } = data
33 state.accessToken = access_token 46 state.accessToken = access_token
34 state.refreshToken = refresh_token 47 state.refreshToken = refresh_token
35 const sysUser = data.sysUser; 48 const sysUser = data.sysUser;
@@ -37,7 +50,7 @@ const user = { @@ -37,7 +50,7 @@ const user = {
37 let roleIdString = sysUser.roles.map(role => role.roleId).join(',') || ""; 50 let roleIdString = sysUser.roles.map(role => role.roleId).join(',') || "";
38 let entityString = ysEntities.map(item => item.entity).join(',') || ""; 51 let entityString = ysEntities.map(item => item.entity).join(',') || "";
39 let entityNameString = ysEntities.map(item => item.entityName).join(',') || ""; 52 let entityNameString = ysEntities.map(item => item.entityName).join(',') || "";
40 - console.log(roleIdString,entityString,entityNameString); 53 + console.log(roleIdString, entityString, entityNameString);
41 state.roleId = roleIdString; 54 state.roleId = roleIdString;
42 state.entity = entityString; 55 state.entity = entityString;
43 state.entityName = entityNameString; 56 state.entityName = entityNameString;
@@ -61,13 +74,19 @@ const user = { @@ -61,13 +74,19 @@ const user = {
61 state.refreshToken = '' 74 state.refreshToken = ''
62 state.userInfo = {} 75 state.userInfo = {}
63 uni.reLaunch({ 76 uni.reLaunch({
64 - url:'/pages/login/mobile' 77 + url: '/pages/login/mobile'
65 }) 78 })
66 } 79 }
67 }, 80 },
68 actions: { 81 actions: {
69 //账号登录 82 //账号登录
70 - Login({ state, commit }, { type, data }) { 83 + Login({
  84 + state,
  85 + commit
  86 + }, {
  87 + type,
  88 + data
  89 + }) {
71 if (type === 0) { 90 if (type === 0) {
72 return passwordLogin(data) 91 return passwordLogin(data)
73 .then(res => { 92 .then(res => {
@@ -86,6 +105,15 @@ const user = { @@ -86,6 +105,15 @@ const user = {
86 .catch(err => { 105 .catch(err => {
87 return Promise.reject(err) 106 return Promise.reject(err)
88 }) 107 })
  108 + } else if (type === 2) {
  109 + return dingLogin(data)
  110 + .then(res => {
  111 + commit('SET_TOKEN', res.data)
  112 + return Promise.resolve(res)
  113 + })
  114 + .catch(err => {
  115 + return Promise.reject(err)
  116 + })
89 } else { 117 } else {
90 return weixinMiniAppLogin(data) 118 return weixinMiniAppLogin(data)
91 .then(res => { 119 .then(res => {
@@ -98,7 +126,10 @@ const user = { @@ -98,7 +126,10 @@ const user = {
98 } 126 }
99 }, 127 },
100 // 退出登录 128 // 退出登录
101 - Logout({ state, commit }) { 129 + Logout({
  130 + state,
  131 + commit
  132 + }) {
102 return logout() 133 return logout()
103 .then(res => { 134 .then(res => {
104 return Promise.resolve(res) 135 return Promise.resolve(res)
@@ -111,7 +142,10 @@ const user = { @@ -111,7 +142,10 @@ const user = {
111 }) 142 })
112 }, 143 },
113 // 获得用户基本信息 144 // 获得用户基本信息
114 - async ObtainUserInfo({ state, commit }) { 145 + async ObtainUserInfo({
  146 + state,
  147 + commit
  148 + }) {
115 const res = await getUserInfo() 149 const res = await getUserInfo()
116 commit('SET_USER_INFO', res.data) 150 commit('SET_USER_INFO', res.data)
117 } 151 }