login.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <view class="login">
  3. <view class="login-top">
  4. <image class="back-img" src="../../static/login/login-back.png"></image>
  5. <view class="login-text">
  6. <view class="text">
  7. {{ $t("login.welcome") }}
  8. </view>
  9. <view class="text">
  10. {{ $t("app.appName") }}
  11. </view>
  12. </view>
  13. </view>
  14. <view class="login-form-wrap">
  15. <uni-forms
  16. class="login-form"
  17. ref="formRef"
  18. :modelValue="loginData"
  19. :rules="loginRules"
  20. >
  21. <uni-forms-item name="username" class="margin-bt">
  22. <!-- type="number" -->
  23. <uni-easyinput
  24. class="login-input"
  25. v-model="loginData.username"
  26. :placeholder="$t('login.enterUsername')"
  27. :placeholderStyle="placeholderStyle"
  28. :styles="inputStyles"
  29. />
  30. </uni-forms-item>
  31. <uni-forms-item name="password" class="margin-bt">
  32. <uni-easyinput
  33. type="password"
  34. v-model="loginData.password"
  35. :placeholder="$t('login.enterPassword')"
  36. :placeholderStyle="placeholderStyle"
  37. :styles="inputStyles"
  38. />
  39. </uni-forms-item>
  40. </uni-forms>
  41. <button type="primary" @click="formSubmit(formRef)">
  42. {{ $t("login.login") }}
  43. </button>
  44. <view class="flex-row align-center justify-between">
  45. <view class="btn-text" @click="loginWithDingTalk">
  46. {{ $t("login.loginWithDingTalk") }}
  47. </view>
  48. <view class="btn-text" @click="openLanguagePopup">
  49. {{ $t("login.languageChange") }}
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 引用语言选择组件 -->
  54. <language-popup ref="languagePopupRef" />
  55. <upgrade ref="upgradeRef" />
  56. </view>
  57. </template>
  58. <script setup>
  59. import { reactive, ref, onMounted, nextTick, getCurrentInstance } from "vue";
  60. import { onLoad } from "@dcloudio/uni-app";
  61. // 引入接口api
  62. import {
  63. appLogin,
  64. dingTalkLogin,
  65. dingTalkLoginH5,
  66. getInfo,
  67. getTokenByUserId,
  68. } from "@/api/login.js";
  69. // 引入配置文件
  70. import config from "@/utils/config";
  71. // 引入数据库操作
  72. import { saveUser } from "@/utils/appDb";
  73. // 引入本地存储操作
  74. import { setUserId, setToken, setDeptId, setUserInfo } from "@/utils/auth.js";
  75. // 引入组件
  76. import Upgrade from "@/components/upgrade.vue";
  77. import LanguagePopup from "@/components/language-popup.vue";
  78. // 引入钉钉JSAPI -- 仅在H5环境下使用
  79. let dd = null;
  80. // #ifdef H5
  81. import * as dingTalkJsApi from "dingtalk-jsapi";
  82. dd = dingTalkJsApi;
  83. // #endif
  84. const { appContext } = getCurrentInstance();
  85. const t = appContext.config.globalProperties.$t;
  86. const languagePopupRef = ref(null);
  87. const openLanguagePopup = () => {
  88. languagePopupRef.value.open();
  89. };
  90. // 判断当前环境是否在钉钉环境
  91. const isDingTalk = () => {
  92. const ua = window.navigator.userAgent.toLowerCase();
  93. console.log("🚀 ~ 当前环境 ~ ua:", ua);
  94. return ua.includes("dingtalk") || ua.includes("dingtalkwork");
  95. };
  96. const dingTalkAutoLogin = async () => {
  97. // 判断是否在钉钉环境
  98. if (!isDingTalk()) {
  99. console.log("当前环境不是钉钉环境,无法自动登录");
  100. return;
  101. }
  102. // 执行钉钉微应用免登逻辑
  103. loginWithDingTalkH5();
  104. }
  105. // 钉钉登录
  106. const loginWithDingTalk = async () => {
  107. // #ifdef APP
  108. const plugin = uni.requireNativePlugin("DingTalk");
  109. // 钉钉登录,这里无法使用async,否则java端会报参数错误
  110. plugin.login((res) => {
  111. console.log(res);
  112. if (res.success === 1) {
  113. dingTalkLogin({
  114. type: 20,
  115. code: res.code,
  116. state: res.state,
  117. }).then((res) => {
  118. console.log(res);
  119. handleLoginSuccess(res);
  120. });
  121. } else if (res.success === 2) {
  122. uni.showToast({ title: t("login.dingTalkError"), icon: "none" });
  123. console.error("APP端钉钉登录失败:", res);
  124. }
  125. });
  126. // #endif
  127. // #ifdef H5
  128. if (isDingTalk()) {
  129. if (!dd) {
  130. uni.showToast({ title: t("login.dingTalkJsapiMissing"), icon: "none" });
  131. return;
  132. }
  133. loginWithDingTalkH5();
  134. } else {
  135. console.log("当前是普通 H5 环境,无法使用钉钉登录");
  136. uni.showToast({ title: t("login.h5DingTalk"), icon: "none" });
  137. }
  138. // #endif
  139. };
  140. const loginWithDingTalkH5 = async () => {
  141. const corpId = config.default.corpId;
  142. console.log("🚀 ~ loginWithDingTalkH5 ~ corpId:", corpId);
  143. const clientId = config.default.clientId;
  144. console.log("🚀 ~ loginWithDingTalkH5 ~ clientId:", clientId);
  145. if (!corpId || !clientId) {
  146. console.error("缺少必要参数");
  147. return;
  148. }
  149. dd.requestAuthCode({
  150. corpId,
  151. clientId,
  152. success: async (result) => {
  153. console.log("🚀 ~ loginWithDingTalkH5 ~ result:", result);
  154. const { code } = result;
  155. dingTalkLoginH5({
  156. type: 10,
  157. state: new Date().getTime(),
  158. code: code,
  159. })
  160. .then((res) => {
  161. console.log("🚀 ~ loginWithDingTalkH5 ~ res:", res);
  162. handleLoginSuccess(res);
  163. })
  164. .catch((err) => {
  165. console.log("🚀 ~ loginWithDingTalkH5 ~ err:", err);
  166. });
  167. },
  168. fail: (err) => {
  169. console.log("🚀 ~ loginWithDingTalkH5 ~ err:", err);
  170. uni.showToast({
  171. title: "获取code失败:" + JSON.stringify(err),
  172. icon: "none",
  173. });
  174. },
  175. });
  176. };
  177. onLoad(async (options) => {
  178. console.log(
  179. "onLoad Login",
  180. uni.getLocale(),
  181. 11,
  182. uni.getStorageSync("language")
  183. );
  184. console.log(options);
  185. // 保存钉钉消息传递的参数
  186. if (options.userId) {
  187. uni.setStorageSync("dingTalkJson", JSON.stringify(options));
  188. const isLoggedIn = uni.getStorageSync("userId");
  189. if (!isLoggedIn) {
  190. const result = await getTokenByUserId(options.userId);
  191. await handleLoginSuccess(result);
  192. }
  193. }
  194. // #ifdef H5
  195. // 当前环境为H5时,判断是否是通过钉钉微应用打开的链接
  196. // 获取当前Url地址
  197. const url = window.location.href;
  198. console.log("当前环境为H5时 当前Url地址:", url);
  199. // 判断是否是通过钉钉微应用打开的链接
  200. if (url.includes("/deepoil")) {
  201. dingTalkAutoLogin();
  202. }
  203. // #endif
  204. });
  205. onMounted(() => {
  206. // console.log("onMounted");
  207. // 检查是否需要显示语言选择弹窗
  208. if (!uni.getStorageSync("language")) {
  209. nextTick(() => {
  210. openLanguagePopup();
  211. });
  212. }
  213. // 检查是否已登录
  214. const isLoggedIn = uni.getStorageSync("userId");
  215. // console.log("isLoggedIn", isLoggedIn);
  216. if (isLoggedIn) {
  217. uni.switchTab({
  218. url: "/pages/home/index",
  219. });
  220. }
  221. });
  222. const placeholderStyle = ref("color:#797979;font-weight:500;font-size:16px");
  223. const inputStyles = reactive({
  224. backgroundColor: "#F0F3FB",
  225. color: "#797979",
  226. });
  227. const loginData = reactive({
  228. username: "",
  229. password: "",
  230. });
  231. const loginRules = ref({
  232. username: {
  233. rules: [
  234. {
  235. required: true,
  236. errorMessage: t("login.enterUsername"),
  237. },
  238. ],
  239. },
  240. password: {
  241. rules: [
  242. {
  243. required: true,
  244. errorMessage: t("login.enterPassword"),
  245. },
  246. ],
  247. },
  248. });
  249. const formRef = ref();
  250. const formSubmit = async (formEl) => {
  251. if (!formEl) return;
  252. await formEl
  253. .validate()
  254. .then((res) => {
  255. appLogin({
  256. ...loginData,
  257. // rememberMe: ,
  258. // tenantName: ""
  259. })
  260. .then(async (result) => {
  261. console.log("result,", result.data);
  262. if (result) {
  263. await saveUser({
  264. name: loginData.username,
  265. pwd: loginData.password,
  266. });
  267. await handleLoginSuccess(result);
  268. }
  269. })
  270. .finally(() => {});
  271. })
  272. .catch((err) => {
  273. console.log("err", err);
  274. });
  275. };
  276. const handleLoginSuccess = async (result) => {
  277. if (result) {
  278. await setUserId(result.data.userId);
  279. await setToken(result.data);
  280. await getInfo().then(async (res) => {
  281. // console.log('useres', res)
  282. const data = JSON.stringify({
  283. user: res.data.user,
  284. roles: res.data.roles,
  285. });
  286. // console.log('data', data)
  287. await setUserInfo(data);
  288. await setDeptId(res.data.user.deptId);
  289. await uni.switchTab({
  290. url: "/pages/home/index",
  291. });
  292. });
  293. }
  294. };
  295. </script>
  296. <style lang="scss" scoped>
  297. .login-top {
  298. position: relative;
  299. width: 100%;
  300. height: 422rpx;
  301. }
  302. .back-img {
  303. width: 100%;
  304. height: 100%;
  305. }
  306. .login-text {
  307. width: 100%;
  308. height: 100%;
  309. box-sizing: border-box;
  310. position: absolute;
  311. top: 0;
  312. left: 0;
  313. color: #ffffff;
  314. font-size: 40rpx;
  315. font-family: "Negreta,PingFang SC";
  316. font-weight: 600;
  317. padding: 0 56rpx;
  318. display: flex;
  319. justify-content: center;
  320. flex-direction: column;
  321. .text {
  322. width: 100%;
  323. margin-bottom: 6rpx;
  324. }
  325. }
  326. .margin-bt {
  327. margin-bottom: 25px;
  328. }
  329. .login-form-wrap {
  330. padding: 60rpx;
  331. }
  332. :deep(.uni-easyinput__content-input) {
  333. height: 45px;
  334. }
  335. :deep(.uni-input-input) {
  336. color: #999999 !important;
  337. }
  338. uni-button[type="primary"] {
  339. background: #004098;
  340. }
  341. .btn-text {
  342. color: #004098;
  343. margin-top: 20px;
  344. font-size: 14px;
  345. font-weight: 500;
  346. }
  347. </style>