YM
2024-03-27 e4f61bb43673d0934b549fc865f228188aa03528
调整登录
5个文件已修改
163 ■■■■ 已修改文件
assets/js/config.js 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
assets/js/login.js 56 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
packageBookService/pages/bookServices/list/index.js 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/bindInfo/index.js 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/home/home.js 70 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
assets/js/config.js
@@ -1,8 +1,8 @@
export const requestCtx = "http://182.92.203.7:3001"; // 请求地址
export const appId = 27;
// export const requestCtx = "http://182.92.203.7:3001"; // 请求地址
// export const appId = 27;
// export const requestCtx = "http://jsysf.bnuic.com"; // 请求地址
// export const requestCtx = "https://jsek.bnuic.com"  // 请求地址
// export const appId = 3;
export const requestCtx = "https://jsek.bnuic.com"  // 请求地址
export const appId = 3;
// export const epubUrl = "http://182.92.203.7:3007/epubReadMobile/"; 
export const epubUrl = "http://jsysf.bnuic.com/epubReadMobile/#/";
export const requestTimeOut = 300000; // 请求超时时间
assets/js/login.js
@@ -1,9 +1,7 @@
import { setSessionGuid } from "./userAction"
export const checkLoginInfo = (app, callback) => {
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
export const loginInfo = (app, callback) => {
        wx.login({
            success: (res) => {
                wx.getUserInfo({
@@ -15,8 +13,25 @@
                            iv: infoRes.iv
                        }).then(loginRes => {
                            if (!loginRes) {
                            const pages = getCurrentPages();
                            const currentPage = pages[pages.length - 1];
                            let url = `/${currentPage.route}`;
                            if (Object.keys(currentPage.options).length) {
                                let option = "";
                                for (const key in currentPage.options) {
                                    if (currentPage.options[key]) {
                                        if (option) {
                                            option += "&"
                                        }
                                        option += key + "=" + currentPage.options[key]
                                    }
                                }
                                url += "?" + option
                            }
                            debugger
                            console.log(url);
                                wx.navigateTo({
                                    url: "/pages/bindInfo/index?code=" + res.code,
                                url: "/pages/bindInfo/index?page=" + encodeURIComponent(url),
                                });
                            } else {
                                wx.login({
@@ -24,14 +39,17 @@
                                        app.MG.identity.loginByWeChatAppCode({
                                            code: res.code,
                                            appRefCode: app.config.appRefCode,
                                            platform: "weChatAppCustom",
                                        platform: "WeChatAppCustom",
                                            encryptedData: infoRes.encryptedData,
                                            iv: infoRes.iv
                                        }).then(res => {
                                            if (res && res.status == "Ok") {
                                            // 储存token
                                                wx.setStorageSync(app.config.tokenKey, res.token);
                                            // 记录登录统计
                                                setSessionGuid()
                                                callback(res.token)
                                            // 获取用户信息
                                            getUserInfo(app, callback, res.token)
                                            } else {
                                                console.log(res);
                                                callback(false)
@@ -57,7 +75,29 @@
                callback(false)
            }
        })
    } else {
        callback(token)
    }
// 获取登录用户身份
const getUserInfo = (app, callback, token) => {
    app.MG.identity.getCurrentAppUser().then(res => {
        // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认)
        if (res) {
            let defaultUser = {};
            let WeChatInfo = res.infoList.find((item) => item.type === "WeChat");
            let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone')
            if (WeChatInfo) {
                defaultUser = {
                    nickName: WeChatInfo.name,
                    avatarUrl: WeChatInfo.icon,
                    weChatId: WeChatInfo.id
                }
            }
            if (phoneNumber) {
                defaultUser.phoneNumber = phoneNumber.credential
            }
            wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser));
        }
        callback(token);
    });
}
packageBookService/pages/bookServices/list/index.js
@@ -54,6 +54,7 @@
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
pages/bindInfo/index.js
@@ -1,4 +1,5 @@
// pages/index/bindInfo/index.js
import { setSessionGuid } from "../../assets/js/userAction"
const app = getApp();
Page({
@@ -8,7 +9,7 @@
  data: {
    avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    nickName: "",
    wxCode: null,
    redirectPage: ""
  },
  onChooseAvatar(res) {
@@ -46,15 +47,24 @@
            app.MG.identity.loginByWeChatAppCode({
              code: res.code,
              appRefCode: app.config.appRefCode,
              platform: "weChatApp",
              platform: "WeChatAppCustom",
              icon: this.data.avatarUrl,
              encryptedData: infoRes.encryptedData,
              iv: infoRes.iv,
              name: this.data.nickName,
            }).then(res => {
              if (res && res.status == "Ok") {
                // 储存token
                wx.setStorageSync(app.config.tokenKey, res.token);
                // 记录登录统计
                setSessionGuid()
                // 获取用户信息
                this.getUserInfo()
              } else {
                wx.showToast({
                  icon: "error",
                  title: res.message,
                });
              }
            })
          },
@@ -65,11 +75,9 @@
  // 获取登录用户身份
  getUserInfo() {
    app.MG.identity.getCurrentAppUser().then(res => {
      // console.log(res, "userInfo");
      // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认)
      if (res) {
        let defaultUser = {};
        // let secretData = res.secretList.find(i => i.type == 'LoginNameAndPassword')
        let WeChatInfo = res.infoList.find((item) => item.type === "WeChat");
        let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone')
@@ -80,18 +88,13 @@
            weChatId: WeChatInfo.id
          }
        }
        if (phoneNumber) {
          defaultUser.phoneNumber = phoneNumber.credential
        }
        this.setData({
          userInfo: defaultUser,
          loading: false
        })
        wx.setStorageSync(app.config.userInfoKey, JSON.stringify(this.data.userInfo));
        wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser));
      }
      wx.switchTab({
        url: '/pages/index/index'
        url: this.data.redirectPage ? this.data.redirectPage : '/pages/home/home'
      })
    });
  },
@@ -101,8 +104,9 @@
   */
  onLoad(options) {
    console.log(options, "options");
    console.log(decodeURIComponent(options.page));
    this.setData({
      wxCode: options.code
      redirectPage: decodeURIComponent(options.page)
    })
  },
pages/home/home.js
@@ -1,6 +1,6 @@
import { fetchHome } from '../../services/home/home';
import moment from 'moment'
import { checkLoginInfo } from '../../assets/js/login';
import { loginInfo } from '../../assets/js/login';
const app = getApp()
Page({
  data: {
@@ -8,7 +8,6 @@
    navBarHeight: '',
    searchVal: '',
    isWhite: false,
    backUrl: null,
    userInfo: {},
    tabList: [], //目录
    current: 1,//轮播图参数
@@ -39,7 +38,7 @@
  },
  onLoad(options) {
  onLoad() {
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
@@ -47,24 +46,22 @@
      barHeight: systInfo.statusBarHeight,
      navBarHeight: navBarHeight,
    });
    if (options.backUrl) {
      let backUrl = JSON.parse(decodeURIComponent(options.backUrl));
      if (backUrl.options) {
        for (let key in backUrl.options) {
          const value = backUrl.options[key]
          backUrl.backUrl += `${key}=${value}&`
        }
      }
      this.setData({
        backUrl: backUrl.backUrl
      })
    }
    // checkLoginInfo(app, (data) => {
    //   if (data) {
    //     this.getCurrentUserInfo();
    //   }
    // })
    // 首页测试登录功能,后续注释
    // 检查登录状态
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        // 如果不是第一次登录,会执行回调
        if (data) {
          // 登录成功,自动记录token和用户信息,并返回true
        } else {
          // 出现错误,返回false
        }
      })
    } else {
      // 如果是第一次登录,会跳转至绑定用户信息页面,填写完用户信息后进行登录并储存token和用户信息,结束后跳转回当前页面(携带页面参数)
    }
    this.init();
  },
@@ -91,39 +88,6 @@
    }, 500);
  },
  getCurrentUserInfo() {
    app.MG.identity.getCurrentAppUser().then(res => {
      // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认)
      if (res) {
        let defaultUser = {};
        let WeChatInfo = res.infoList.find((item) => item.type === "WeChat");
        let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone')
        if (WeChatInfo) {
          defaultUser = {
            nickName: WeChatInfo.name,
            avatarUrl: WeChatInfo.icon,
            weChatId: WeChatInfo.id
          }
        }
        if (phoneNumber) {
          defaultUser.phoneNumber = phoneNumber.credential
        }
        this.setData({
          userInfo: defaultUser,
        })
        wx.setStorageSync(app.config.userInfoKey, JSON.stringify(this.data.userInfo));
      }
      if (this.data.backUrl) {
        wx.navigateTo({
          url: this.data.backUrl,
        })
      } else {
        wx.switchTab({
          url: '/pages/home/home'
        })
      }
    });
  },
  getBannerList() {
    const list = []
    app.MG.resource.getItem({