Browse Source

feat: update few code

xutongzee 1 year ago
parent
commit
cd891fefc7
7 changed files with 195 additions and 35 deletions
  1. 9 1
      src/App.vue
  2. 19 0
      src/api/login.js
  3. 26 0
      src/api/member.js
  4. 18 18
      src/store/modules/enum.js
  5. 73 2
      src/store/modules/user.js
  6. 50 6
      src/utils/dingtalk.js
  7. 0 8
      src/views/approve/detail.vue

+ 9 - 1
src/App.vue

@@ -10,6 +10,8 @@
 import TabBar from '@/components/TabBar'
 // import store from './store';
 // import NavBar from '@/components/NavBar'
+
+
 export default {
   components: {
     TabBar,
@@ -52,7 +54,13 @@ export default {
     if (!list.length) {
       this.$store.dispatch('enum/getTypeList')
     }
-  }
 
+    try {
+      // NOTE: 静默获取用户token
+      this.$store.dispatch('user/login')
+      
+    } catch (e) {} /* eslint-disable-line */
+
+  }
 }
 </script>

+ 19 - 0
src/api/login.js

@@ -0,0 +1,19 @@
+/**
+ * @description api/login/xx
+ */
+
+
+import request from '@/utils/request'
+
+/**
+ * 获取用户token
+ * @param {String} code 前端获取的免授权code
+ * @returns {Object} {data: userToken}
+ */
+export const login = code => (request({
+    data: {
+        code
+    },
+    method: 'POST',
+    url: 'login/login'
+}))

+ 26 - 0
src/api/member.js

@@ -0,0 +1,26 @@
+/**
+ * @description api/member/xxx
+ */
+
+import request from '@/utils/request'
+
+/**
+ * 获取用户信息
+ * @param {String} code 前端获取的免授权code
+ * @returns {Object} {data: userToken}
+ */
+export const getUserinfo = () => (request({
+    method: 'POST',
+    url: 'member/user_info'
+}))
+
+// /api/member/edit
+// type: 修改的类型(1:昵称,2:签名,3:手机号)  
+// nickname: 昵称  
+// signature: 签名  
+// phone: 手机号  
+export const putUserInfo = (data) => (request({
+    data,
+    method: 'POST',
+    url: '/member/edit'
+}))

+ 18 - 18
src/store/modules/enum.js

@@ -46,40 +46,40 @@ const getters = {
 
 const mutations = {
     // NOTE: 出差类型
-    SET_EVECTION_TYPE_LIST(state, paylod) {
-        state.evectionTypeList = [...paylod.list]
+    SET_EVECTION_TYPE_LIST(state, payload) {
+        state.evectionTypeList = [...payload.list]
     },
     // NOTE: 模块列表
-    SET_MODULE_LIST(state, paylod) {
-        state.moduleList = [...paylod.list]
+    SET_MODULE_LIST(state, payload) {
+        state.moduleList = [...payload.list]
     },
     // NOTE: 缓急程度
-    SET_DEGREE_LIST(state, paylod) {
-        state.degreeList = [...paylod.list]
+    SET_DEGREE_LIST(state, payload) {
+        state.degreeList = [...payload.list]
     },
     // NOTE: 采购类型
-    SET_APPLY_TYPE_LIST(state, paylod) {
-        state.applyTypeList = [...paylod.list]
+    SET_APPLY_TYPE_LIST(state, payload) {
+        state.applyTypeList = [...payload.list]
     },
     // NOTE: 采购支付类型
-    SET_APPLY_PAY_TYPE_LIST(state, paylod) {
-        state.applyPayTypeList = [...paylod.list]
+    SET_APPLY_PAY_TYPE_LIST(state, payload) {
+        state.applyPayTypeList = [...payload.list]
     },
     // NOTE: 呈批类型
-    SET_OFFER_TYPE_LIST(state, paylod) {
-        state.offerTypeList = [...paylod.list]
+    SET_OFFER_TYPE_LIST(state, payload) {
+        state.offerTypeList = [...payload.list]
     },
     // NOTE: 请假类型
-    SET_LEAVE_TYPE_LIST(state, paylod) {
-        state.leaveTypeList = [...paylod.list]
+    SET_LEAVE_TYPE_LIST(state, payload) {
+        state.leaveTypeList = [...payload.list]
     },
     // NOTE: 维修类型
-    SET_MAINTAIN_TYPE_LIST(state, paylod) {
-        state.maintainTypeList = [...paylod.list]
+    SET_MAINTAIN_TYPE_LIST(state, payload) {
+        state.maintainTypeList = [...payload.list]
     },
     // NOTE: 合同类型
-    SET_CONTRACT_TYPE_LIST(state, paylod) {
-        state.contractTypeList = [...paylod.list]
+    SET_CONTRACT_TYPE_LIST(state, payload) {
+        state.contractTypeList = [...payload.list]
     }
 }
 

+ 73 - 2
src/store/modules/user.js

@@ -1,11 +1,82 @@
+import { getRequestAuthCode, platform } from '@/utils/dingtalk'
+import { login } from '@/api/login'
+import { getUserinfo } from '@/api/member'
+import { Toast } from 'vant'
+
+
+// import { init } from 'dingtalk-mock-sdk'
+
+// init({
+//   token: 'U3if31VghziIj3VCnSwgeHO0CVlKs7Z4',
+//   jsapiMock: true,
+//   httpMock: false,
+// })
 
 const state = {
     name: '刘壹手',
     token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI4NSIsImlzcyI6Imh0dHBzOlwvXC96YWluLmNvbSIsImF1ZCI6Imh0dHBzOlwvXC96YWluLmNvbSIsImlhdCI6MTY5ODMxMjI4NSwibmJmIjoxNjk4MzEyMjg1LCJleHAiOjE3MjQyMzIyODV9.ziAXs6DiZKGGjtPuaCZ6Vfpv6Ki_deZhFPnDDLjAJUg',
 }
 
-const mutations = {}
-const actions = {}
+const mutations = {
+    UPDATE_USER_DATA: (state, payload) => {
+        const { phone, name } = payload.data
+        state.phone = phone
+        state.name = name
+        state.userinfo = payload.data
+    },
+    // 更新用户Token
+    UPDATE_USER_TOKEN: (state, payload) => (state.token = payload.token),
+}
+const actions = {
+    async getUserInfo ({ commit }) {
+        try {
+            const result = await getUserinfo()
+            if (result.code === 1) {
+                const data = result.data
+
+                commit({
+                    type: 'UPDATE_USER_DATA',
+                    data
+                })
+
+                // phone	int			手机号  
+                // name	string			姓名  
+                // nickname	string		昵称  
+                // headimg	string			头像地址  
+                // title	string			职称  
+                // department	string		部门(多个部门以逗号分隔)  
+                // signature	string		个人签名  
+            }
+        } catch (error) {
+            Toast({
+                icon: 'error',
+                message: error.message
+            })
+        }
+    },
+
+    // 登录获取token
+    async login ({ commit }) {
+        try {
+            if (platform === 'notInDingTalk') {
+                Toast({
+                    message: '当前应用不在钉钉使用无法登录'
+                })
+                return
+            }
+            const code = await getRequestAuthCode()
+            const result = await login(code)
+            if (result.code === 1) {
+                commit({
+                    type: 'UPDATE_USER_TOKEN',
+                    token: result.data
+                })
+            }
+        } catch (error) {
+            login
+        }
+    },
+}
 
 export default {
     namespaced: true,

+ 50 - 6
src/utils/dingtalk.js

@@ -3,16 +3,19 @@ import complexPicker from 'dingtalk-jsapi/api/biz/contact/complexPicker'
 import setRight from 'dingtalk-jsapi/api/biz/navigation/setRight'
 import setTitle from 'dingtalk-jsapi/api/biz/navigation/setTitle';
 
+import requestAuthCode from 'dingtalk-jsapi/api/runtime/permission/requestAuthCode'
+import requestOperateAuthCode from 'dingtalk-jsapi/api/runtime/permission/requestOperateAuthCode'
+
 import { getENV } from "dingtalk-jsapi/lib/env";
 import { compareVersion } from "dingtalk-jsapi/lib/sdk";
 
-import { init } from 'dingtalk-mock-sdk'
+// import { init } from 'dingtalk-mock-sdk'
 
-init({
-  token: 'U3if31VghziIj3VCnSwgeHO0CVlKs7Z4',
-  jsapiMock: true,
-  httpMock: false,
-})
+// init({
+//   token: 'U3if31VghziIj3VCnSwgeHO0CVlKs7Z4',
+//   jsapiMock: true,
+//   httpMock: false,
+// })
 
 // 公司CorpId
 const CorpId = 'dingf1b2e9ddf9d214e224f2f5cc6abecb85' /* eslint-disable-line */
@@ -96,6 +99,7 @@ export function settingNavigationRight ({
 /**
  * @description 设置导航栏标题
  */
+// FIXME: 需要修改入参
 export function settingNavigationTitle ({
     title = '默认标题',
     callback
@@ -106,4 +110,44 @@ export function settingNavigationTitle ({
             callback && callback(result)
         }
     })
+}
+
+/**
+ * @description 获取微登录免授权码-无感操作 (only 5min life)
+ */
+export function getRequestAuthCode () {
+    return new Promise((resolve, reject) => {
+        requestAuthCode({
+            corpId: CorpId,
+            onSuccess: function(result) {
+                resolve(result.code)
+            },
+            onFail : function(err) {
+                console.log(err);
+                reject(err)
+            }
+        })
+    })
+}
+
+/**
+ * @description 获取登录授权-有交互方式
+ */
+export function getRequestOperateAuthCode () {
+    return new Promise((resolve, reject) => {
+        requestOperateAuthCode({
+            corpId: CorpId,
+            agentId: AppId,
+            onSuccess: function(result) {
+                /*{
+                    code: 'hYLK98jkf0m' //string authCode
+                }*/
+                resolve(result)
+            },
+            onFail : function(err) {
+                reject(err)
+            }
+         
+        })
+    })
 }

+ 0 - 8
src/views/approve/detail.vue

@@ -56,14 +56,6 @@
 
 <script>
 import * as dd from 'dingtalk-jsapi'
-// dingtalk mock
-import { init } from 'dingtalk-mock-sdk'
-
-init({
-  token: 'U3if31VghziIj3VCnSwgeHO0CVlKs7Z4',
-  jsapiMock: true,
-  httpMock: false,
-})
 
 import setLeft from 'dingtalk-jsapi/api/biz/navigation/setLeft'
 import setTitle from 'dingtalk-jsapi/api/biz/navigation/setTitle'