123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- import store from '@/store';
- import $http from '@/config/requestConfig'
- import base from '@/config/baseUrl';
- // #ifdef H5
- import {
- h5Login
- } from '@/config/html5Utils';
- // #endif
- let code = "";
- let loginStart = true;
- let userInfo = {
- token: ""
- };
- let lastPageUrl = "";
- // 会员号密码一级手机号验证码登录
- async function login(options = {}) {
- // 请求登录
- let res = await $http.post(options.url, options.data);
- if (res.code != '200') {
- return uni.showToast({
- title: res.msg,
- icon: 'none'
- });
- }
- let loginStatus = await getLoginUserInfo(res.data, options.data.account);
- if (loginStatus) {
- // 跳转到首页
- uni.switchTab({
- url: "/pages/index/index"
- })
- setTimeout(() => {
- uni.showToast({
- title: '登录成功',
- icon: 'none'
- });
- }, 500);
- }
- }
- // 微信小程序登录
- function onLogin(type = "judge", callback) {
- //判断登录状态
- if (loginStart) {
- lastPageUrl = "";
- loginStart = false;
- const _this = this;
- let platform;
- // #ifdef MP-WEIXIN
- platform = 'weixin';
- // #endif
- // #ifdef MP-ALIPAY
- platform = 'alipay';
- // #endif
- // #ifdef MP-BAIDU
- platform = 'baidu';
- // #endif
- uni.login({
- provider: platform,
- success: function(loginRes) {
- if (loginRes.errMsg == 'login:ok') {
- code = loginRes.code;
- // 获取用户信息
- uni.getUserInfo({
- provider: platform,
- success: function(infoRes) {
- getUserInfo(infoRes, "", callback);
- },
- fail() {
- if (type != "try") {
- store.commit('setLoginPopupShow', true);
- Object.defineProperty(userInfo, "token", {
- get: function(val) {
- return {};
- },
- set: function(newVal) {
- callback && callback();
- }
- });
- setTimeout(() => {
- loginStart = true;
- }, 2000);
- } else {
- loginStart = true;
- }
- }
- });
- }
- }
- });
- }
- }
- //微信小程序获取用户信息
- function getUserInfo(info, type, callback) {
- let httpData = {
- wxSmallCode: code, //小程序code
- iv: info.iv, //小程序加密算法的初始向量
- encryptedData: info.encryptedData //包括敏感数据在内的完整用户信息的加密数据
- };
- // store.state.chatScenesInfo里面是小程序二维码附带的信息
- if (store.state.chatScenesInfo.recommendCode) {
- // 推荐码
- httpData.recommendUid = store.state.chatScenesInfo.recommendCode;
- }
- $http.post('api/open/v1/login', httpData).then(res => {
- loginStart = true;
- store.commit('setUserInfo', res);
- if (type == "authorized") {
- userInfo.token = res.token;
- store.commit('setLoginPopupShow', false);
- } else {
- callback && callback();
- }
- uni.showToast({
- title: "登录成功"
- });
- }, err => {
- loginStart = true;
- });
- }
- //判断是否登录(所有端)
- function judgeLogin(callback, type = "judge") {
- if (store.state.chatScenesInfo.scene == 1154) {
- uni.showToast({
- title: '请前往小程序使用完整服务',
- icon: "none"
- });
- } else {
- let storeToken = store.state.token;
- if (!storeToken) { // nvue页面读取不到vuex里面数据,将取缓存
- storeToken = uni.getStorageSync("token");
- }
- if (type != "force" && storeToken) {
- callback();
- } else {
- // #ifdef MP
- // onLogin(type, callback);
- // #endif
- // #ifdef APP-PLUS
- uni.showModal({
- title: "登录提示",
- content: "此时此刻需要您登录喔~",
- confirmText: "去登录",
- cancelText: "再逛会",
- success: (res) => {
- if (res.confirm) {
- uni.reLaunch({
- url: "/pages/login/login"
- });
- }
- }
- });
- // #endif
- // #ifdef H5
- h5Login(type, () => {
- callback();
- });
- // #endif
- }
- }
- }
- //获取登录用户信息
- async function getLoginUserInfo(token, userId) {
- let Author = "Bearer" + " " + token;
- let userInfoRes = await $http.get('/user/loginUser', {}, {
- header: {
- Authorization: Author,
- }
- }); //用户信息
- store.commit('setUserModules', {
- title: 'userInfo',
- data: {
- sysUser: {
- ...userInfoRes.data
- }
- }
- })
- store.commit('setUserModules', {
- title: 'userLoginId',
- data: userInfoRes.data.id
- })
- store.commit('setUserModules', {
- title: 'token',
- data: Author
- })
- if (userInfoRes.data.name == '游客') {
- uni.reLaunch({
- url: "/pages/user/certify"
- })
- return false;
- }
- // if (userInfoRes.data.sysUser.usertype == 'A') {
- // // 清除状态
- // uni.showToast({
- // title: '后线人员暂无登录权限',
- // icon: 'none',
- // duration: 2000
- // });
- // return false;
- // }
- // let userInfoCheckRes = await $http.get('/esmUserInternalcheck/findById?checkid=' +userId, {}, {
- // header: {
- // Authorization: Author
- // }
- // }); //用户临时信息
- // store.commit('setUserModules', {
- // title: 'userCheckInfo',
- // data: userInfoCheckRes.data
- // })
- let avatar;
- let avatars = await $http.get("/insTaskImage/findById?imgtype=avatar&taskid=" + userId, {}, {
- header: {
- Authorization: Author
- }
- })
- if ((avatars.code == '200') && (avatars.data.imgList.length > 0)) {
- avatar = avatars.data.imgList[0];
- } else {
- avatar = "/static/common/avatar.png";
- }
- store.commit('setUserModules', {
- title: 'avatar',
- data: avatar
- })
- let loginStatus = await getStaffStatus(userId);
- if (loginStatus) {
- return true;
- } else {
- return false;
- }
- }
- // 查询人员状态status,1为审核通过 2为新注册人员(新注册人员又分为5个状态 1:未认证 2:认证中 3:驳回认证 4:退回修改 5正常)
- async function getStaffStatus(userId) {
- //查询登录人的状态信息
- let res = await $http.get('/user/findByName?name=' + userId)
- //默认人员状态是未认证状态
- var status = '1';
- if (res.data.status == '1') {
- //正式人员
- status = '5'; //审核通过
- let userInfoRes = await $http.get('/user/loginUser'); //用户信息
- store.commit('setUserModules', {
- title: 'userInfo',
- data: {
- sysUser: {
- ...userInfoRes.data
- }
- }
- })
- } else {
- let userInfoRes = await $http.get('/user/loginUser'); //用户信息
- store.commit('setUserModules', {
- title: 'userInfo',
- data: {
- sysUser: {
- ...userInfoRes.data
- }
- }
- })
- //注册人员
- let res1 = await $http.get('/esmUserInternalcheck/findById?checkid=' + userId);
- store.commit('setUserModules', {
- title: 'userCheckInfo',
- data: res1.data
- })
- //判断人员审核状态
- // if ((!res1.data.esmUserInternal.identity) && (res1.data.esmUserInternal.status == '0')) {
- // console.log(1)
- // status = '1'; //未认证
- // } else if ((!!res1.data.esmUserInternal.identity) && (res1.data.esmUserInternal.status == '0')) {
- // console.log(1)
- // status = '2'; //认证中
- // } else if (res1.data.esmUserInternal.status == '2') {
- // console.log(1)
- // status = '3'; //审核驳回
- // } else if (res1.data.esmUserInternal.status == '3') {
- // console.log(1)
- // status = '4'; //退回修改
- // }
- }
- store.commit('setUserModules', {
- title: 'userStatus',
- data: status
- })
- return true;
- }
- export {
- login,
- onLogin,
- getUserInfo,
- judgeLogin,
- getStaffStatus
- }
|