Bladeren bron

fix:小程序授权页面

chenzhen2 1 jaar geleden
bovenliggende
commit
b00cc50897

+ 6 - 1
project.config.json

@@ -44,7 +44,12 @@
     "tabSize": 2
   },
   "packOptions": {
-    "ignore": [],
+    "ignore": [
+      {
+        "value": "static",
+        "type": "folder"
+      }
+    ],
     "include": []
   },
   "appid": "wxda2ef261ac3cca32"

+ 21 - 1
src/api/user.js

@@ -1,4 +1,5 @@
 import $http from '@/common/request.js';
+import config from '@/config';
 function getCompmayUsers() {
   return $http({
     url: `/user/companyUsers`,
@@ -6,6 +7,7 @@ function getCompmayUsers() {
   });
 }
 
+// 获取用户信息
 function getUserInfo(params) {
   return $http({
     url: '/user/info',
@@ -23,11 +25,29 @@ function setWxAuthUserInfo(params) {
   // return Promise.resolve()
 }
 
+function login(params) {
+  return $http({
+    url: `${config.duoduoenvService}userNew/wechat/login`,
+    method: 'POST',
+    data: JSON.stringify(params)
+  })
+}
+
+// duoduo-service/duoduoenv-service/userNew/wechat/register
+// 注册
+function register(params) {
+  return $http({
+    url: `${config.duoduoenvService}userNew/wechat/register`,
+    method: 'POST',
+    data: JSON.stringify(params)
+  })
+}
 
 
 
 
 export {
   getCompmayUsers,
-  getUserInfo
+  getUserInfo,
+  login
 }

+ 7 - 6
src/app.wpy

@@ -10,11 +10,11 @@
 </style>
 
 <script>
-import wepy from '@wepy/core'
-import eventHub from './common/eventHub'
-import vuex from '@wepy/x'
+import wepy from '@wepy/core';
+import eventHub from './common/eventHub';
+import vuex from '@wepy/x';
 
-wepy.use(vuex)
+wepy.use(vuex);
 
 wepy.app({
   hooks: {
@@ -22,7 +22,7 @@ wepy.app({
     // 同时存在 Page hook 和 App hook 时,优先执行 Page hook,返回值再交由 App hook 处
     'before-setData': function(dirty) {
       //   console.log('setData dirty: ', dirty);
-      return dirty
+      return dirty;
     }
   },
   globalData: {
@@ -38,12 +38,13 @@ wepy.app({
   },
 
   methods: {}
-})
+});
 </script>
 <config>
 {
     pages: [
       'pages/index',
+      'pages/auth/index',
       'pages/bindTenant/index',
     ],
     "subpackages":[

+ 149 - 0
src/pages/auth/index.wpy

@@ -0,0 +1,149 @@
+<style lang="less">
+.login-box {
+  padding-top: 40px;
+  .avatar-wrapper {
+    padding: 0px;
+    width: 196rpx;
+    height: 196rpx;
+    border-radius: 50%;
+    image {
+      width: 196rpx;
+      height: 196rpx;
+      border-radius: 50%;
+    }
+  }
+  .avatar-tip {
+    text-align: center;
+    font-family: PingFang SC;
+    font-size: 28rpx;
+    font-weight: 400;
+    line-height: 44rpx;
+    text-align: center;
+  }
+
+  .bind-btn-box {
+    position: fixed;
+    bottom: 228rpx;
+    left: 50%;
+    transform: translateX(-50%);
+    .bind-btn {
+      width: 548rpx;
+      height: 100rpx;
+      line-height: 100rpx;
+      border-radius: 56rpx;
+      background: rgba(61, 203, 204, 1);
+      border: none;
+      font-family: PingFang SC;
+      font-size: 32rpx;
+      font-weight: 400;
+      letter-spacing: 0px;
+      text-align: center;
+      color: #fff;
+    }
+    .bind-btn-tip {
+      //styleName: 14/常规;
+      font-family: PingFang SC;
+      padding-top: 32rpx;
+      font-size: 28rpx;
+      font-weight: 400;
+      line-height: 44rpx;
+      text-align: center;
+      color: rgba(139, 148, 158, 1);
+    }
+  }
+
+  .home-btn {
+    margin-top: 240rpx;
+    width: 548rpx;
+    height: 100rpx;
+    line-height: 100rpx;
+    border-radius: 56rpx;
+    border: 1px solid rgba(61, 203, 204, 1);
+  }
+}
+</style>
+<template>
+ <div class="login-box">
+  <button class="avatar-wrapper"
+    open-type="chooseAvatar" 
+    bind:chooseavatar="onChooseAvatar">
+    <image class="avatar" src="{{avatarUrl}}"/>
+  </button>  
+  <div class="avatar-tip">可点击获取头像</div>
+  <div class="bind-btn-box">
+   <button class="bind-btn"
+   open-type="getPhoneNumber"
+   bindgetphonenumber="phonenumberAuth">微信手机号认证</button>
+   <!-- bindgetphonenumber="phonenumberAuth" -->
+   <div class="bind-btn-tip">确保是入职公司使用的手机号</div>
+  </div>
+  <button class="home-btn" @click="goHome">首页</button>
+ </div>
+</template>
+
+<script>
+import wepy from '@wepy/core';
+import { mapState } from '@wepy/x';
+import store from '@/store';
+import config from '@/config';
+import { setAvatar, setUserInfoByAuth } from '@/service/user';
+let defaultAvatarUrl =
+  config.h5StaticPath + 'page-bind-tenant/default_avatar.svg';
+wepy.page({
+  store,
+  config: {
+    navigationBarTitleText: 'test'
+  },
+  data: {
+    nicknameValue: '',
+    avatarUrl: defaultAvatarUrl,
+    msg: '测试数据',
+    userInformation: wx.getStorageSync('userInformation')
+  },
+  onLoad() {
+    if (wx.getUserProfile) {
+      this.canIUseGetUserProfile = true;
+    }
+  },
+  onShow() {},
+  methods: {
+    // 绑定租户
+    bindTenant() {},
+    onChooseAvatar(e) {
+      this.avatarUrl = e.$wx.detail.avatarUrl;
+      let avatarUrl = this.avatarUrl;
+      let that = this;
+      wx.getFileSystemManager().readFile({
+        filePath: avatarUrl,
+        encoding: 'base64',
+        success: function(res) {
+          console.log(res.data);
+          that.avatarUrl = 'data:image/png;base64,' + res.data;
+          setAvatar(that.avatarUrl);
+        }
+      });
+    },
+    goHome() {
+      wx.navigateTo({
+        url: '/packagesEnv/pages/home/index'
+      });
+    },
+    // 手机号认证
+    phonenumberAuth(e) {
+      console.log(e)
+    },
+    goBindTenant() {
+      console.log('被点击了');
+      wx.navigateTo({
+        url: '/pages/bindTenant/index'
+      });
+    }
+  },
+  created() {}
+});
+</script>
+<config>
+{
+navigationBarTitleText: '首页',
+}
+</config>

+ 58 - 106
src/pages/index.wpy

@@ -1,34 +1,30 @@
 <style lang="less">
+page {
+  width: 100%;
+  height: 100%;
+}
 .login-box {
-  padding-top: 40px;
-  .avatar-wrapper {
-    padding: 0px;
-    width: 160rpx;
-    height: 160rpx;
-    border-radius: 50%;
-    image {
-      width: 160rpx;
-      width: 160rpx;
-      border-radius: 50%;
-    }
-  }
-  .avatar {
-    width: 160rpx;
-    height: 160rpx;
-  }
-
-  .weui-input {
-    margin: 0 auto;
-    margin-top: 40rpx;
-    padding: 0 20rpx;
-    width: 548rpx;
-    height: 100rpx;
-    line-height: 100rpx;
-    border-radius: 5px;
-    border: 1px solid rgba(61, 203, 204, 1);
+  box-sizing: border-box;
+  width: 100%;
+  height: 100%;
+}
+.login {
+  width: 100%;
+  height: 100%;
+  background: rgba(0, 0, 0.2);
+  image {
+    position: relative;
+    width: 384rpx;
+    height: 144rpx;
+    top: 336rpx;
+    left: 50%;
+    transform: translateX(-50%);
   }
   .bind-btn {
-    margin-top: 100rpx;
+    position: fixed;
+    bottom: 200rpx;
+    left: 50%;
+    transform: translateX(-50%);
     width: 548rpx;
     height: 100rpx;
     line-height: 100rpx;
@@ -42,108 +38,60 @@
     text-align: center;
     color: #fff;
   }
-  .phone-tip {
-    padding-top: 32rpx;
-    font-family: PingFang SC;
-    font-size: 28rpx;
-    font-weight: 400;
-    line-height: 44rpx;
-    letter-spacing: 0px;
-    text-align: center;
-    color: rgba(139, 148, 158, 1);
-    text-align: center;
-  }
-  .home-btn {
-    margin-top: 240rpx;
-    width: 548rpx;
-    height: 100rpx;
-    line-height: 100rpx;
-    border-radius: 56rpx;
-    border: 1px solid rgba(61, 203, 204, 1);
-  }
 }
 </style>
 <template>
- <div class="login-box">
-  <button class="avatar-wrapper"
-    open-type="chooseAvatar" 
-    bind:chooseavatar="onChooseAvatar">
-    <image class="avatar" src="{{avatarUrl}}"/>
-  </button> 
-  <form bindsubmit="submit">
-    <input type="nickname" 
-    value="{{nicknameValue}}"
-     class="weui-input" 
-    placeholder="请输入昵称"/>
-  </form>  
-  <button class="bind-btn"
-  open-type="getPhoneNumber"
-  @click="goBindTenant">微信手机号认证</button>
-
-   <!-- bindgetphonenumber="phonenumberAuth" -->
-  <div class="phone-tip">确保是入职公司使用的手机号</div>
-  <button class="home-btn" @click="goHome">首页</button>
- </div>
+  <div class="login-box">
+    <page-top-bar title="" 
+  titleColor="#1B2129"></page-top-bar>
+   <div class="login">
+       <image src="{{h5StaticPath +'/page-bind-tenant/logo_title.png'}}" alt=""/>
+       <button class="bind-btn"
+        @click="goBindTenant">登录</button>
+   </div>
+  </div>
 </template>
 
 <script>
 import wepy from '@wepy/core';
 import { mapState } from '@wepy/x';
 import store from '@/store';
-import { setUserInfoByAuth } from '@/service/user';
-let defaultAvatarUrl = '';
+import config from '@/config';
+import { checkRegist } from '@/service/user';
 wepy.page({
   store,
-  config: {
-    navigationBarTitleText: 'test'
-  },
   data: {
-    nicknameValue: '',
-    avatarUrl: defaultAvatarUrl,
-    msg: '测试数据',
-    userInformation: wx.getStorageSync('userInformation')
-  },
-  onLoad() {
-    if (wx.getUserProfile) {
-      this.canIUseGetUserProfile = true;
-    }
+    h5StaticPath: config.h5StaticPath,
+    isActivated: 0
   },
+  onLoad() {},
   onShow() {
-    // this.checkAuthCamra();
+    this.checkTenantRegist();
   },
   methods: {
-    // 绑定租户
-    bindTenant() {},
-    onChooseAvatar(e) {
-      // console.log('被点击了');
-      // console.log('被点击了',e)
-      console.log('avatarUrl', e.$wx.detail.avatarUrl);
-      console.log(e.$wx);
-      this.avatarUrl = e.$wx.detail.avatarUrl;
-      let avatarUrl = this.avatarUrl;
-      let baseImg = [];
-      let key = e.$wx.timeStamp;
-      let that = this;
-      wx.getFileSystemManager().readFile({
-        filePath: avatarUrl,
-        encoding: 'base64',
-        success: function(res) {
-          console.log(res.data);
-          that.avatarUrl = 'data:image/png;base64,' + res.data;
-        }
-      });
+    checkTenantRegist() {
+      checkRegist()
+        .then(res => {
+          this.isActivated = res.isActivated;
+          if (this.isActivated && this.isActivated == 2) {
+            // 已经激活
+            this.goHome();
+          } else if (this.isActivated == 0) {
+            // 未激活
+            this.goAuth();
+          }
+        })
+        .catch(error => {});
     },
     goHome() {
       wx.navigateTo({
         url: '/packagesEnv/pages/home/index'
       });
     },
-    // 手机号认证
-    phonenumberAuth(e) {},
-    goBindTenant() {
+    goAuth() {
       console.log('被点击了');
       wx.navigateTo({
-        url: '/pages/bindTenant/index'
+        url: '/pages/auth/index'
       });
     }
   },
@@ -152,6 +100,10 @@ wepy.page({
 </script>
 <config>
 {
-navigationBarTitleText: '首页',
+navigationBarTitleText: '登录',
+navigationStyle:"custom",
+usingComponents: {
+    'page-top-bar': '~@/components/common/page-top-bar',      
+  },
 }
 </config>

+ 187 - 98
src/service/user.js

@@ -1,123 +1,212 @@
 import { getUserInfo, setWxAuthUserInfo, getThirdInfo, createAccount, changePhone } from '@/api/user.js';
 import store from '@/store';
 import config from '@/config';
+import { login } from '@/api/user.js';
 
 // 获取用户数据
 function getUserData() {
-    return new Promise((resolve, reject) => {
-        Promise.all([getUserInfo(), getThirdInfo()]).then(([userInfoRes, thirdInfores]) => {
-            let userInfo = userInfoRes.data || {};
-            let thirdInfo = thirdInfores.data || {};
-            let cachedInfo = wx.getStorageSync('UserInfo');
-            userInfo = { ...cachedInfo, ...userInfo, ...thirdInfo };
-            userInfo.userName = userInfo.userName || '';
-            // test 用户身份
-            // userInfo.roles = ['tenant.admin', 'operator', 'owner'];
-            // userInfo.phone = '';wxLogin
-            // userInfo.nickName = '';
-            // userInfo.userName = ''
-            userInfo = addRolesInfoToUserInfo(userInfo);
-            if (!userInfo.userName) {
-                userInfo.defaultUserName = 'Hello';
-            }
-            // todo 模拟银泰配置 接口增加buildingLogo
-            if (config.projectName === 'yintai') {
-                userInfo.buildingLogo = `${config.h5StaticPath}/page-yintai/project-logo.png`;
-            }
-            //   userInfo.companyId = 'c68dcccd57984277ab7736f2d257cd0c'
-            store.commit('setUserInfo', userInfo);
-            resolve(userInfo);
-        }).catch((res) => {
-            reject(res);
-        })
+  return new Promise((resolve, reject) => {
+    Promise.all([getUserInfo(), getThirdInfo()]).then(([userInfoRes, thirdInfores]) => {
+      let userInfo = userInfoRes.data || {};
+      let thirdInfo = thirdInfores.data || {};
+      let cachedInfo = wx.getStorageSync('UserInfo');
+      userInfo = { ...cachedInfo, ...userInfo, ...thirdInfo };
+      userInfo.userName = userInfo.userName || '';
+      // test 用户身份
+      // userInfo.roles = ['tenant.admin', 'operator', 'owner'];
+      // userInfo.phone = '';wxLogin
+      // userInfo.nickName = '';
+      // userInfo.userName = ''
+      userInfo = addRolesInfoToUserInfo(userInfo);
+      if (!userInfo.userName) {
+        userInfo.defaultUserName = 'Hello';
+      }
+      // todo 模拟银泰配置 接口增加buildingLogo
+      if (config.projectName === 'yintai') {
+        userInfo.buildingLogo = `${config.h5StaticPath}/page-yintai/project-logo.png`;
+      }
+      //   userInfo.companyId = 'c68dcccd57984277ab7736f2d257cd0c'
+      store.commit('setUserInfo', userInfo);
+      resolve(userInfo);
+    }).catch((res) => {
+      reject(res);
     })
+  })
 }
 
 // 检查登录
 function checkLogin(needGetetUserInfo = true) {
-    let token = store.state.user.token;
-    if (!token) {
-        return wxLogin(needGetetUserInfo);
-    } else {
-        return Promise.resolve();
-    }
+  let token = store.state.user.token;
+  if (!token) {
+    return wxLogin(needGetetUserInfo);
+  } else {
+    return Promise.resolve();
+  }
 }
 
 //  检查用户信息
 function checkHasUserInfo() {
-    let userInfo = store.state.user.userInfo;
-    if (userInfo && JSON.stringify(userInfo) !== '{}') {
-        return Promise.resolve(userInfo);
-    } else {
-        return checkLogin(false).then(() => {
-            return getUserData();
-        })
-    }
+  let userInfo = store.state.user.userInfo;
+  if (userInfo && JSON.stringify(userInfo) !== '{}') {
+    return Promise.resolve(userInfo);
+  } else {
+    return checkLogin(false).then(() => {
+      return getUserData();
+    })
+  }
 }
 
 function setUserInfoByAuth() {
-    return new Promise((resolve, reject) => {
-      // if (ing) {
-      //   return;
-      // }
-      // ing = true;
-      // let { userInfo = {} } = store.state.user;
-      // // 如果已经保存过了
-      // let nickName = userInfo.nickName;
-      // if (nickName) {
-      //   ing = false;
-      //   resolve();
-      //   return;
-      // }
-      wx.getUserProfile({
-        desc: '用于完善用户信息',
-        success: res => {
-          let detail = res;
-          wx.login({
-            success: loginRes => {
-              // ing = false;
-              // if (loginRes.code) {
-              //   let params = {
-              //     encryptedData: detail.encryptedData,
-              //     iv: detail.iv,
-              //     jsCode: loginRes.code
-              //   };
-              //   setWxAuthUserInfo(params).then(res => {
-              //     let userInfo = JSON.parse(detail.rawData);
-              //     let cachedInfo = wx.getStorageSync('UserInfo');
-              //     userInfo = {
-              //       ...cachedInfo,
-              //       ...userInfo,
-              //       headImgUrl: userInfo.avatarUrl
-              //     };
-              //     delete userInfo.avatarUrl;
-              //     store.commit('setUserInfo', {
-              //       ...userInfo
-              //     });
-              //     wx.setStorageSync('UserInfo', { ...userInfo });
-              //     resolve(res.data);
-              //   });
-              // }
-            },
-            fail: res => {
-              // ing = false;
-              // reject(res);
+  return new Promise((resolve, reject) => {
+    // if (ing) {
+    //   return;
+    // }
+    // ing = true;
+    // let { userInfo = {} } = store.state.user;
+    // // 如果已经保存过了
+    // let nickName = userInfo.nickName;
+    // if (nickName) {
+    //   ing = false;
+    //   resolve();
+    //   return;
+    // }
+    wx.getUserProfile({
+      desc: '用于完善用户信息',
+      success: res => {
+        let detail = res;
+        wx.login({
+          success: loginRes => {
+            // ing = false;
+            // if (loginRes.code) {
+            //   let params = {
+            //     encryptedData: detail.encryptedData,
+            //     iv: detail.iv,
+            //     jsCode: loginRes.code
+            //   };
+            //   setWxAuthUserInfo(params).then(res => {
+            //     let userInfo = JSON.parse(detail.rawData);
+            //     let cachedInfo = wx.getStorageSync('UserInfo');
+            //     userInfo = {
+            //       ...cachedInfo,
+            //       ...userInfo,
+            //       headImgUrl: userInfo.avatarUrl
+            //     };
+            //     delete userInfo.avatarUrl;
+            //     store.commit('setUserInfo', {
+            //       ...userInfo
+            //     });
+            //     wx.setStorageSync('UserInfo', { ...userInfo });
+            //     resolve(res.data);
+            //   });
+            // }
+          },
+          fail: res => {
+            // ing = false;
+            // reject(res);
+          }
+        });
+      },
+      fail: res => {
+        // ing = false;
+        // wx.showToast({
+        //   icon: 'none',
+        //   title: '授权用户公开信息后方可进入下一步操作'
+        // });
+        // reject();
+      }
+    });
+  });
+}
+
+// 检查用户是否注册过小程序及绑定过租户
+function checkRegist() {
+  return new Promise((resolve, reject) => {
+    wx.login({
+      success(wxLoginRes) {
+        const jsCode = wxLoginRes.code;
+        const appId = wx.getAccountInfoSync().miniProgram.appId;
+        if (jsCode) {
+          let params = {
+            "appId": appId,
+            "wxCode": jsCode
+          }
+          login(params).then(res => {
+            if (res.result == 'success') {
+              let data = res.data || []
+              resolve(data)
+            } else {
+              reject('login接口返回fail');
             }
+          })
+        } else {
+          wx.showToast({
+            title: `网络异常, 请稍后重试`,
+            icon: 'none',
+            duration: 5e3
           });
-        },
-        fail: res => {
-          // ing = false;
-          // wx.showToast({
-          //   icon: 'none',
-          //   title: '授权用户公开信息后方可进入下一步操作'
-          // });
-          // reject();
+          reject('wx.login接口返回code为空');
         }
-      });
+      },
+      fail(res) {
+        reject(res);
+        wx.showToast({
+          title: `网络异常, 请稍后重试`,
+          icon: 'none',
+          duration: 5e3
+        });
+      }
     });
-  }
+  });
+}
+
+// 登录
+function wxLogin() {
+  store.commit('setTokenIsValid', false);
+  store.commit('setToken', '');
+  return new Promise((resolve, reject) => {
+    wx.login({
+      success(wxLoginRes) {
+        const jsCode = wxLoginRes.code;
+        const appId = wx.getAccountInfoSync().miniProgram.appId;
+        if (jsCode) {
+          let params = {
+            "appId": appId,
+            "wxCode": jsCode
+          }
+        } else {
+          wx.hideLoading();
+          wx.showToast({
+            title: `网络异常, 请稍后重试`,
+            icon: 'none',
+            duration: 5e3
+          });
+          reject('wx.login接口返回code为空');
+        }
+      },
+      fail(res) {
+        reject(res);
+        wx.showToast({
+          title: `网络异常, 请稍后重试`,
+          icon: 'none',
+          duration: 5e3
+        });
+      }
+    });
+  });
+}
+
+// 本地保存用户头像
+function setAvatar(avatar) {
+  wx.setStorageSync('avatarBase64', avatar);
+}
+function getAvatar() {
+  wx.getStorageSync('avatarBase64');
+}
 
 export {
-    checkHasUserInfo,
-    setUserInfoByAuth
+  checkHasUserInfo,
+  checkRegist,
+  setAvatar,
+  getAvatar,
+  setUserInfoByAuth
 }

File diff suppressed because it is too large
+ 9 - 0
static/page-bind-tenant/default_avatar.svg


BIN
static/page-bind-tenant/logo_title.png