钉钉微应用【DG】--项目心得&&总结

3 篇文章 0 订阅
1 篇文章 0 订阅

钉钉免登步骤:

1、从后台获取corpId;

// 获取corpId(鉴权接口)
      const getCorpId = () => {
        return new Promise((resolve, reject) => {
          this.$post(
            `${this.apiHead}/baseServer/baseLoginService/getDingDingConfig`,
            {
              params: '{"redirectUrl":"www.abc.com","appId":"daogou"}',
            }
          )
            .then((res) => {
              let { data } = res;
              data = JSON.parse(data);
              resolve(data["corpId"]);
            })
            .catch((err) => {
              reject(err);
            });
        });
      };

2、以corpId为参数向钉钉请求用户authcode;

 const getCode = (corpId) => {
        // 获取authCode
        // alert(corpId);
        return new Promise((resolve, reject) => {
          this.$dd.runtime.permission.requestAuthCode({
            corpId: corpId, // 企业id
            onSuccess: (res) => {
              const code = res.code; // 通过该免登授权码可以获取用户身份
              resolve(code);
            },
            onFail: (err) => {
              this.logining = false;
              alert("获取code失败" + JSON.stringify(err));
              this.$toast("登录失败,请刷新");
              reject(err);
            },
          });
        });
      };

3、然后以authcode为参数,向后台请求用户数据(信息);

const getUser = (code) => {
        // 获取用户信息
        // alert(code);
        return new Promise((resolve, reject) => {
          const url = `${this.apiHead}/baseServer/baseLoginService/dingDingLogin`;
          this.$post(url, {
            params: `{"redirectUrl":"www.abc.com", "code": "${code}", "domain": "ssssss","appId":"daogou"}`,
          })
            .then((res) => {
              resolve(res);
            })
            .catch((err) => {
              reject(err);
            });
        });
      };
      this.$loading.open("登陆中");
      const corpId = await getCorpId();
      const code = await getCode(corpId);
      // alert(code);
      getUser(code)
        .then(async (res) => {
          // alert(JSON.stringify(res));
          this.$loading.close();
          if (res["status"] == "D") {
            this.$store.commit("set_variables", {
              logined: true,
              empName: res["data"]["userFullName"],
              empCode: res["data"]["userName"],
              userType: res["data"]["userType"],
              haveInfo: res["status"] == "D",
              unnormalTip: false,
              // userId: res["userId"],
              // photoUrl: res['data']['photoUrl'],
              // department: res['data']['department'],
              // job: res['data']['job'],
              authCode: code,
              status: res.data.status,
              isadmin: res.data.isadmin,
              positionList: res.data.positionList,
              menuResponsibilityList: res.data.menuResponsibilityList,
              userFullName: res.data.userFullName,
              userName: res.data.userName,
              userId: res.data.userId,
              operationOrgIds: res.data.operationOrgIds,
              domain: res.data.domain,
              useTrype: res.data.useTrype,
              platformCode: res.data.platformCode,
              responsibilityId: res.data.responsibilityId,

              // havePower: res["userType"] != "PUGUIDE",
            });
            this.$store.commit("cart/getShoppingCartNum");
          } else {
            const authCode = await getCode(corpId);
            if (
              res.msg.indexOf("没有找到对应的导购档案") > -1 ||
              res.msg.indexOf("该用户不存在") > -1
            ) {
              this.$store.commit("set_variables", {
                logined: true,
                haveInfo: false,
                authCode,
                unnormalTip: false,
              });
            } else {
              this.$store.commit("set_variables", {
                logined: true,
                haveInfo: false,
                authCode,
                unnormalTip: true,
              });
            }
          }
        })
        .catch((err) => {
          this.$loading.close();

          alert("获取用户资料失败" + JSON.stringify(err));

          this.$store.commit("set_variables", {
            logined: false,
          });
          if (isFirst) {
            this.getUser(false);
            return;
          }
          this.$toast("登录失败,请刷新");
        });
    },
    localLogin() {
      this.$loading.open("登陆中");
      this.$store.commit("set_variables", {
        logined: false,
        empName: "张三",
        empCode: "473436",
        department: "IT管理部",
        job: "软件开发工程师",
        userType: 3,
        haveInfo: false,
        unnormalTip: false,
      });
      setTimeout(() => {
        this.$loading.close();
        const obj = {
          isadmin: "N",
          platformCode: "PUGUIDE",
          responsibilityId: 390064,
          userName: "DG00045",
          operationOrgIds: 1865,
          userFullName: "帆帆",
          userType: "PUGUIDE",
          userId: 1865,
          unnormalTip: false,
        };
        this.$store.commit("set_variables", {
          ...obj,
          logined: true,
          empName: "张三",
          empCode: "473436",
          department: "IT管理部",
          job: "软件开发工程师",
          // userType: 3,
          // havePower: true,
          status: "n",
          haveInfo: true,
          pugCode: "DG00045",
          authCode: "09aa87054db5346ab8421d8ec15e0d54",
        });
        this.$store.commit("cart/getShoppingCartNum");
        // this.$store.commit("cart/getCartList", {
        //   params: `{"varIsadmin":"N","varPlatformCode":"PUGUIDE","operationRespId":390064,"varUserName":"DG00002","operatorUserId":1865,"varUserFullName":"帆帆","varUserType":"PUGUIDE","varUserId":1865,"userCode":"DG00002"}`,
        //   pageIndex: 1,
        //   pageRow: 5,
        // });
        const cartParams = this.isLocal
          ? this.localUser
          : { userCode: this.userName };
        this.$store.commit("cart/getCartList", {
          params: JSON.stringify(cartParams),
          pageIndex: 1,
          pageRow: 5,
        });
      });
    },
    login() {
      const env = this.env;
      if (!env) {
        this.localLogin();
      } else {
        this.getUser(true);
      }
    },
  },
  created() {
    // console.log(this.$store);
    this.login();
  },

关于实现功能时的注意事项:

一、 关于钉钉登录:

  1. 根据需要来选择是否鉴权,免登不需要鉴权;
  2. corpId尽量从后获取,尽量不要写在前端本地代码(前端代码可以从客户端下载,会造成corpId泄露);
  3. 关于公共数据:为了减少http请求数量,酌情在本地储存保存备份;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值