common.uts 4.4 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2
import { state, mutations } from '@/uni_modules/uni-id-pages-x/store.uts';
import config from '@/uni_modules/uni-id-pages-x/config.uts';
3
export const loginSuccess = (_ : UTSJSONObject) => {
A
Anne_LXM 已提交
4 5
  // console.log('loginSuccess', e);
  // console.log("新用户uid", e["uid"]);
DCloud_JSON's avatar
DCloud_JSON 已提交
6
  // state.currentUserInfo = uniCloud.getCurrentUserInfo()
A
Anne_LXM 已提交
7 8 9 10
  mutations.updateUserInfo(null)
  // state.userInfo["_id"] = e["uid"]
  state.isLogin = true
  uni.$emit('uni-id-pages-x-login-success', '')
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
11

A
Anne_LXM 已提交
12
  // 登录后重定向设置
DCloud_JSON's avatar
DCloud_JSON 已提交
13
  function loginAfterToPage() {
14
    const pages = getCurrentPages()
A
Anne_LXM 已提交
15
    const currentPage = pages[pages.length - 1]
DCloud_JSON's avatar
DCloud_JSON 已提交
16 17 18 19 20
    let uniIdRedirectUrl = currentPage.options["uniIdRedirectUrl"];
    if (uniIdRedirectUrl != null) {
      // #ifndef APP
      uniIdRedirectUrl = decodeURIComponent(uniIdRedirectUrl)
      // #endif
A
Anne_LXM 已提交
21 22
      // console.log('uniIdRedirectUrl', uniIdRedirectUrl);
      uni.redirectTo({
23
        url: uniIdRedirectUrl as string,
A
Anne_LXM 已提交
24 25 26
        fail() {
          console.error("uniIdRouter redirectTo fail");
          uni.switchTab({
27
            "url": uniIdRedirectUrl as string
A
Anne_LXM 已提交
28 29 30 31 32 33
          })
        },
        success() {
          // console.log('uniIdRouter redirectTo success');
        }
      })
DCloud_JSON's avatar
DCloud_JSON 已提交
34
    } else if (currentPage.route.includes("uni_modules/uni-id-pages-x/pages/login/login") ){
A
Anne_LXM 已提交
35
      uni.navigateBack()
36 37 38 39 40 41 42 43 44 45 46 47
    } else {
      // 需要返回的页面数
      const needBackCount = pages.reduce((count, page) => {
        if (page.route.includes("uni_modules/uni-id-pages-x/pages")) {
          return count + 1
        }
        return count
      }, 0)
      // console.log('needBackCount', needBackCount);
      uni.navigateBack({
        delta: needBackCount
      })
A
Anne_LXM 已提交
48 49 50 51 52 53 54 55 56 57
    }
  };
  const toastDuration = 1500
  uni.showToast({
    title: '登录成功',
    duration: toastDuration,
    icon: 'none'
  });
  // 避免 showToast 还没结束就跳转引起的崩溃
  setTimeout(() => loginAfterToPage(), toastDuration);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
58 59 60

}

61
export const logout = async () => {
A
Anne_LXM 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  console.log("logout");
  // 1. 已经过期就不需要调用服务端的注销接口	2.即使调用注销接口失败,不能阻塞客户端
  if (uniCloud.getCurrentUserInfo().tokenExpired > Date.now()) {
    const uniIdCo = uniCloud.importObject("uni-id-co", { customUI: false })
    try {
      await uniIdCo.logout()
    } catch (err) {
      //TODO handle the exception
      console.log('err: ', err);
    }
  }
  uni.removeStorageSync('uni_id_token');
  uni.setStorageSync('uni_id_token_expired', 0)
  state.userInfo = {}
  state.isLogin = false
  uni.$emit('uni-id-pages-x-logout', '')
  // 调完注销接口 跳转至登录页面
  uni.redirectTo({
    url: "/uni_modules/uni-id-pages-x/pages/login/login"
  })
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
82 83 84

}

A
Anne_LXM 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
export const checkPassword = (password : string) : UTSJSONObject => {
  let res : UTSJSONObject = {
    "pass": true,
    "errMsg": null
  }
  // console.log("checkPassword", password);
  // 根据配置的密码强度校验
  let passwordStrength = config.getString("passwordStrength")
  if (passwordStrength == null) {
    return res
  }
  // 密码强度表达式
  const passwordRules : UTSJSONObject = {
    "super": {
      "rule": "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/])[0-9a-zA-Z~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/]{8,16}$",
      "errMsg": "密码必须包含大小写字母、数字和特殊符号,长度范围:8-16位之间"
    },
    "strong": {
      "rule": "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/])[0-9a-zA-Z~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/]{8,16}$",
      "errMsg": "密密码必须包含字母、数字和特殊符号,长度范围:8-16位之间"
    },
    "medium": {
      "rule": "^(?!.*[0-9]+$)(?!.*[a-zA-Z]+$)(?!.*[~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/]{8,16}$",
      "errMsg": "密码必须为字母、数字和特殊符号任意两种的组合,长度范围:8-16位之间"
    },
    "weak": {
      "rule": "^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&_\\-+=`|\\(){}\\[\\]:;\"'<>,.?/]{6,16}$",
      "errMsg": "密码必须包含字母和数字,长度范围:6-16位之间"
    }
  }
  let passwordRule = passwordRules.getString(passwordStrength + '.rule');
  let passwordRegExp = new RegExp(passwordRule as string);
  res["pass"] = passwordRegExp.test(password);
  res["errMsg"] = passwordRules.getString(passwordStrength + '.errMsg');
  return res
DCloud_JSON's avatar
DCloud_JSON 已提交
120
}