litian
9 小时以前 aff8d054df4a638f399dc8f15d98c19b9c9aa785
assets/js/login.js
@@ -1,10 +1,12 @@
import {
  setSessionGuid
} from "./userAction"
const app = getApp();
import MG from './middleGround/WebMiddleGroundApi';
import config from './config';
export const loginInfo = (app, callback) => {
  if (app.config.appId == 27) {
  if (config.appId == 27) {
    wx.navigateTo({
      url: "/pages/testLogin/index",
    });
@@ -13,9 +15,9 @@
      success: (res) => {
        wx.getUserInfo({
          success: (infoRes) => {
            app.MG.identity.checkWeChatAppAccount({
            MG.identity.checkWeChatAppAccount({
              code: res.code,
              appCode: app.config.appRefCode,
              appCode: config.appRefCode,
              encryptedData: infoRes.encryptedData,
              iv: infoRes.iv
            }).then(loginRes => {
@@ -42,20 +44,24 @@
              } else {
                wx.login({
                  success: (res) => {
                    app.MG.identity.loginByWeChatAppCode({
                    MG.identity.loginByWeChatAppCode({
                      code: res.code,
                      appRefCode: app.config.appRefCode,
                      appRefCode: config.appRefCode,
                      platform: "WeChatAppCustom",
                      encryptedData: infoRes.encryptedData,
                      iv: infoRes.iv
                    }).then(res => {
                      if (res && res.status == "Ok") {
                        // 储存token
                        wx.setStorageSync(app.config.tokenKey, res.token);
                        wx.setStorageSync(config.tokenKey, res.token);
                        // 获取用户信息
                        getUserInfo(app, callback, res.token)
                        // 记录登录统计
                        setSessionGuid()
                        //登录记录积分
                        MG.app.creatUserBehavior({
                          refCode: "sign"
                        })
                      } else {
                        console.log(res);
                        callback(false)
@@ -84,10 +90,9 @@
    })
  }
}
// 获取登录用户身份
const getUserInfo = (app, callback, token) => {
  app.MG.identity.getCurrentAppUser().then(res => {
  MG.identity.getCurrentAppUser().then(res => {
    // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认)
    if (res) {
      let defaultUser = {};
@@ -96,6 +101,7 @@
      let teacherRole = res.roleLinks.find((item) => item.role.refCode == 'teacher')
      let teacherInfos = res.infoList.find((item) => item.type == 'teacherInfo')
      let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone')
      let emailInfo = res.secretList.find((item) => item.type == 'EMail')
      if (teacherRole && teacherInfos) {
        defaultUser = {
          ...teacherInfos,
@@ -104,6 +110,7 @@
          userId: res.userId,
          role: 'Teacher',
          roleId: teacherRole.role.id,
          Email: emailInfo ? emailInfo.credential : JSON.parse(teacherInfos.data).email,
        }
      } else if (WeChatInfo) {
        defaultUser = {
@@ -116,7 +123,8 @@
      if (phoneNumber) {
        defaultUser.phoneNumber = phoneNumber.credential
      }
      wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser));
      wx.setStorageSync(config.userInfoKey, JSON.stringify(defaultUser));
    }
    callback(token);
  });