Ver código fonte

切换账号及校区

zhangshuling 4 anos atrás
pai
commit
f762ac3d33
4 arquivos alterados com 81 adições e 63 exclusões
  1. 73 52
      pages/index/index.js
  2. 4 8
      pages/index/index.wxml
  3. 3 2
      pages/login/login.js
  4. 1 1
      utils/urls.js

+ 73 - 52
pages/index/index.js

@@ -9,34 +9,12 @@ Page({
    */
   data: {
     array: ['艾克斯郎校区1', '艾克斯郎校区2', '艾克斯郎校区3', '艾克斯郎校区4'],
-    objectArray: [
-      {
-        id: 0,
-        name: '艾克斯郎校区1'
-      },
-      {
-        id: 1,
-        name: '艾克斯郎校区2'
-      },
-      {
-        id: 2,
-        name: '艾克斯郎校区3'
-      },
-      {
-        id: 3,
-        name: '艾克斯郎校区4'
-      }
-    ],
     index: 0,
-    stuList: [
-      { id: 0 },
-      { id: 1 }
-    ],
-    curStu: 0,
+    curStu: {},
     classList: [
-      { img: '/images/kc.png', title: '今日课程', num: '1' },
-      { img: '/images/jtzy.png', title: '今日作业', num: '6' },
-      { img: '/images/cq.png', title: '出勤率', num: '100%' },
+      { img: '/images/kc.png', title: '今日课程', num: '0' },
+      { img: '/images/jtzy.png', title: '今日作业', num: '0' },
+      { img: '/images/cq.png', title: '出勤率', num: '0%' },
     ],
     appList: [
       { img: '/images/kb.png', title: '我的课表' },
@@ -50,25 +28,61 @@ Page({
     ],
     bannerList: [],
     userList: [],
+    userItems: [],
+    orgItems: [],
   },
 
   //切换身份
   changeStu: function (e) {
     let sid = e.currentTarget.dataset.id;
-    console.log('切换用户 : ' + sid);
-    if (this.data.curStu == sid) {
+    this.doChangeUser(sid);
+  },
+  doChangeUser: function (sid) {
+    if (this.data.curStu.studentId == sid) {
+      console.log('无须切换')
       return;
     }
-    this.setData({
-      curStu: sid
+    wx.showLoading({
+      title: '正在切换用户',
+      mask: true
     })
+    console.log('切换用户 : ' + sid);
+    const urls = urlDef.urls;
+    util.apiPost(urls.change_user, { targetId: sid }).then(rs => {
+      wx.hideLoading();
+      if (rs === 1) {
+        console.log('切换成功');
+        this.loadIndexData();
+      } else {
+        wx.showToast({
+          title: '切换失败,请稍后重试',
+          icon: 'none'
+        })
+      }
+    });
   },
 
   //切换校区
   bindPickerChange: function (e) {
-    this.setData({
-      index: e.detail.value
-    })
+    let i = e.detail.value;
+    if (i == this.data.index) {
+      console.log('无须切换...');
+      return;
+    }
+
+    // 取该校区 第1位学生 作为默认登录用户
+    let targetId = this.data.orgItems[i].orgId;
+    console.log('切换校区 ' + targetId);
+    this.data.userItems.some(u => {
+      console.log('用户: ' + JSON.stringify(u));
+      if (u.orgId === targetId) {
+        this.doChangeUser(u.studentId);
+        this.setData({
+          index: i
+        });
+        return true;
+      }
+    });
   },
 
   /**
@@ -126,33 +140,40 @@ Page({
   onShareAppMessage: function () {
 
   },
-
+  strLen(s) {
+    return (s ? s.length : 0);
+  },
   loadIndexData: function () {
     const urls = urlDef.urls;
     //获取登录用户数据, 并加载首页数据
     util.apiPost(urls.get_cur_user, {}).then(rs => {
-      console.log(JSON.stringify(rs));
-
-      let curUser = rs.attr;
-      curUser.studentId = curUser.personId; // 新增studentId属性
-      let otherUsers = rs.attr.others.filter(o => { return o.studentId != curUser.studentId });
-      curUser.others = [];// 清除 others 属性
-      console.log('其他用户 : ' + JSON.stringify(otherUsers));
-
-
-      this.setData({ 'curStu': curUser.studentId });
-      let userList = [curUser].concat(otherUsers);
-      userList.map(o => {
-        if (o.imageUrl && o.imageUrl.length > 0) {
-          o.imageUrl = urls.oss_file + 'image/'+ o.imageUrl;
+      // console.log(JSON.stringify(rs));
+      let userItems = rs.attr.others;
+      let orgItems = [];
+      userItems.forEach(u => {
+        let exists = false;
+        orgItems.some(o => {
+          if (o.orgId === u.orgId) {
+            exists = true;
+            return true;
+          }
+        });
+        if (!exists) {
+          orgItems.push({ orgId: u.orgId, orgName: u.orgName });
+        }
+        if (u.id === rs.id) { // 当前用户
+          this.setData({ 'curStu': { studentId: u.studentId, orgId: u.orgId } });
+        }
+        if (this.strLen(u.imageUrl) === 0) {
+          u.imageUrl = '/images/head.png';
         } else {
-          o.imageUrl = '/images/head.png';
+          u.imageUrl = urls.oss_file + 'image/' + u.imageUrl;
         }
-      })
-      this.setData({ 'userList': userList });
+      });
+
+      this.setData({ 'userItems': userItems, 'orgItems': orgItems });
 
 
-      console.log('userList ' + JSON.stringify(this.data.userList));
       let personId = rs.attr.personId;
       let orgId = rs.attr.orgId;
       let today = util.curTime().substring(0, 10).replace(/\//g, '-');

+ 4 - 8
pages/index/index.wxml

@@ -2,19 +2,15 @@
   <!-- 顶部学生信息 -->
   <view class="head-view">
     <view class="head-left">
-      <view class="head-img-view" wx:for="{{userList}}" wx:key="index" data-id="{{item.studentId}}" bindtap="changeStu">
-      <!--
-        <image class="{{curStu == item.studentId ? 'curStu' : 'stuHead'}}" wx:if="{{index == 0}}" src="/images/head.png"></image>
-        <image class="{{curStu == item.studentId ? 'curStu' : 'stuHead'}}" wx:if="{{index != 0}}" src="/images/head2.png"></image>
-        -->
-        <image class="{{curStu == item.studentId ? 'curStu' : 'stuHead'}}" src="{{item.imageUrl}}"></image>
+      <view class="head-img-view" wx:for="{{userItems}}" wx:key="index" data-id="{{item.studentId}}" bindtap="changeStu" wx:if="{{curStu.orgId == item.orgId}}">
+        <image class="{{curStu.studentId == item.studentId ? 'curStu' : 'stuHead'}}" src="{{item.imageUrl}}"></image>
       </view>
     </view>
     <view class="head-right">
       <view class="section">
-        <picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">
+        <picker bindchange="bindPickerChange" value="{{index}}" range="{{orgItems}}" range-key="orgName">
           <view class="picker">
-            {{array[index]}}
+            {{orgItems[index].orgName}}
           </view>
         </picker>
       </view>

+ 3 - 2
pages/login/login.js

@@ -89,6 +89,7 @@ Page({
       openId: wx.getStorageSync('openId'),
     }
     ).then(rs => {
+      wx.hideLoading()
       if (rs.success > 0) {
         wx.setStorageSync('sso-token', rs.data.token);
         wx.showToast({
@@ -99,10 +100,10 @@ Page({
         })
       } else {
         wx.showToast({
-          title: rs.errMsg
+          title: rs.errMsg,
+          icon: 'none'
         })
       }
-      wx.hideLoading({ })
     })
 
   },

+ 1 - 1
utils/urls.js

@@ -1,7 +1,7 @@
 // const baseUrl = 'https://demo.schoolwisdoms.com';
 // const baseUrl = 'https://app.schoolwisdoms.com';
 
-const baseUrl = 'http://192.168.2.104';
+const baseUrl = 'http://192.168.2.103';
 
 const ssoBase = baseUrl + '/sso';
 const platformBase = baseUrl + '/admin';