Commit 923685cbb6f00d98f2ca58043eaff9d6ddf6e546

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

feat: 钉钉静默登录

api/auth.js
  1 +import code from "../uni_modules/uview-ui/libs/config/props/code"
  2 +
1 3 //请求工具参考https://ext.dcloud.net.cn/plugin?id=392
2 4 const { http } = uni.$u
3 5  
4 6 //使用手机 + 密码登录
5 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 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 14 //微信小程序的一键登录
11 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 19 export const logout = data => http.delete('/auth/logout', data)
... ...
common/config.js
1 1 module.exports = {
2 2 //后端接口地址
3 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 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 8 timeout: 30000,
9 9 // 禁用 Cookie 等信息
... ...
manifest.json
... ... @@ -75,7 +75,7 @@
75 75 "base" : "./"
76 76 },
77 77 "devServer" : {
78   - "https" : false
  78 + "https" : true
79 79 }
80 80 }
81 81 }
... ...
pages/login/mobile.vue
... ... @@ -47,7 +47,7 @@
47 47 </view>
48 48 </u--form>
49 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 51 loadingText="检测为钉钉环境\n自动登录中..." bg-color="#e8e8e8"></u-loading-page>
52 52 </view>
53 53 </template>
... ... @@ -97,24 +97,44 @@
97 97 },
98 98 methods: {
99 99 detectionEnvironment() {
  100 + let that = this;
100 101 console.log(this.$isDing);
101 102 if (this.$isDing) {
102   - // this.isDing = true;
  103 + this.isDing = true;
103 104 dd.ready(() => {
104 105 dd.runtime.permission.requestAuthCode({
105   - corpId: corpId,
  106 + corpId: "dinge6053c157358448735c2f4657eb6378f",
106 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 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 138 handleSubmit() {
119 139 this.$refs.form.validate().then(res => {
120 140 let data = this.formData
... ... @@ -137,7 +157,7 @@
137 157 },
138 158 mobileLogin(data) {
139 159 this.$store.dispatch('Login', {
140   - type: this.currentModeIndex,
  160 + type: 0,
141 161 data: data
142 162 }).then(res => {
143 163 uni.$u.toast('登录成功');
... ...
pages/market/index.vue
... ... @@ -37,6 +37,8 @@
37 37 import {
38 38 listSimpleDictDatas
39 39 } from '@/api/dict.js'
  40 + import * as dd from 'dingtalk-jsapi'
  41 +
40 42 export default {
41 43 components: {
42 44 newsList
... ... @@ -168,13 +170,24 @@
168 170 },
169 171 loginOut() {
170 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 202 position: relative;
190 203 min-height: 65vh;
191 204 }
192   -
193   - .u-empty{
  205 +
  206 + .u-empty {
194 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 15 const AccessTokenKey = 'ACCESS_TOKEN'
6 16 const RefreshTokenKey = 'REFRESH_TOKEN'
... ... @@ -9,9 +19,9 @@ const user = {
9 19 state: {
10 20 accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌
11 21 refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌
12   - roleId:"",
13   - entity:"",
14   - entityName:"",
  22 + roleId: "",
  23 + entity: "",
  24 + entityName: "",
15 25 userInfo: {}
16 26 },
17 27 mutations: {
... ... @@ -28,8 +38,11 @@ const user = {
28 38 // 更新令牌
29 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 46 state.accessToken = access_token
34 47 state.refreshToken = refresh_token
35 48 const sysUser = data.sysUser;
... ... @@ -37,7 +50,7 @@ const user = {
37 50 let roleIdString = sysUser.roles.map(role => role.roleId).join(',') || "";
38 51 let entityString = ysEntities.map(item => item.entity).join(',') || "";
39 52 let entityNameString = ysEntities.map(item => item.entityName).join(',') || "";
40   - console.log(roleIdString,entityString,entityNameString);
  53 + console.log(roleIdString, entityString, entityNameString);
41 54 state.roleId = roleIdString;
42 55 state.entity = entityString;
43 56 state.entityName = entityNameString;
... ... @@ -61,13 +74,19 @@ const user = {
61 74 state.refreshToken = ''
62 75 state.userInfo = {}
63 76 uni.reLaunch({
64   - url:'/pages/login/mobile'
  77 + url: '/pages/login/mobile'
65 78 })
66 79 }
67 80 },
68 81 actions: {
69 82 //账号登录
70   - Login({ state, commit }, { type, data }) {
  83 + Login({
  84 + state,
  85 + commit
  86 + }, {
  87 + type,
  88 + data
  89 + }) {
71 90 if (type === 0) {
72 91 return passwordLogin(data)
73 92 .then(res => {
... ... @@ -86,6 +105,15 @@ const user = {
86 105 .catch(err => {
87 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 117 } else {
90 118 return weixinMiniAppLogin(data)
91 119 .then(res => {
... ... @@ -98,7 +126,10 @@ const user = {
98 126 }
99 127 },
100 128 // 退出登录
101   - Logout({ state, commit }) {
  129 + Logout({
  130 + state,
  131 + commit
  132 + }) {
102 133 return logout()
103 134 .then(res => {
104 135 return Promise.resolve(res)
... ... @@ -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 149 const res = await getUserInfo()
116 150 commit('SET_USER_INFO', res.data)
117 151 }
... ...