login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. <!-- <view class="uni-padding-wrap">
  54. <view>
  55. <checkbox-group @change="handleChange">
  56. <label>
  57. <checkbox :value="true" :checked="isChecked" style="transform: scale(0.6)" />
  58. </label>
  59. </checkbox-group>
  60. </view>
  61. <view class="uni-title">
  62. 已阅读并同意
  63. <text style="text-decoration: underline" @click="goPrivacy">《隐私政策》</text>
  64. <text style="text-decoration: underline" @click="goAgreement">《用户服务协议》</text>
  65. </view>
  66. <uni-popup ref="privacyRef">
  67. <scroll-view scroll-y="true" class="privacy" style="height: 500px; border-radius: 20rpx">
  68. <view style="background-color: #fff; padding: 0 50rpx; border-radius: 20rpx">
  69. <Privacy />
  70. </view>
  71. </scroll-view>
  72. </uni-popup>
  73. <uni-popup ref="aggRef">
  74. <scroll-view scroll-y="true" class="privacy" style="height: 500px; border-radius: 20rpx">
  75. <view style="background-color: #fff; padding: 0 50rpx; border-radius: 20rpx">
  76. <Agg />
  77. </view>
  78. </scroll-view>
  79. </uni-popup>
  80. </view> -->
  81. <!-- 引用语言选择组件 -->
  82. <language-popup ref="languagePopupRef" />
  83. <upgrade ref="upgradeRef" />
  84. </view>
  85. </template>
  86. <script setup>
  87. import { reactive, ref, onMounted, nextTick, getCurrentInstance } from "vue";
  88. import { onLoad } from "@dcloudio/uni-app";
  89. // 引入接口api
  90. import {
  91. appLogin,
  92. dingTalkLogin,
  93. dingTalkLoginH5,
  94. getInfo,
  95. getTokenByUserId,
  96. } from "@/api/login.js";
  97. // 引入配置文件
  98. import config from "@/utils/config";
  99. // 引入数据库操作
  100. import { saveUser } from "@/utils/appDb";
  101. // 引入本地存储操作
  102. import { setUserId, setToken, setDeptId, setUserInfo } from "@/utils/auth.js";
  103. // 引入组件
  104. import Upgrade from "@/components/upgrade.vue";
  105. import LanguagePopup from "@/components/language-popup.vue";
  106. // 引入钉钉JSAPI -- 仅在H5环境下使用
  107. let dd = null;
  108. // #ifdef H5
  109. import * as dingTalkJsApi from "dingtalk-jsapi";
  110. dd = dingTalkJsApi;
  111. // #endif
  112. const { appContext } = getCurrentInstance();
  113. const t = appContext.config.globalProperties.$t;
  114. const languagePopupRef = ref(null);
  115. const openLanguagePopup = () => {
  116. languagePopupRef.value.open();
  117. };
  118. let isChecked = ref(false);
  119. let my_value = ref(false);
  120. const handleChange = (val) => {
  121. my_value.value = val.detail.value[0];
  122. };
  123. // let privacyRef = ref(null);
  124. // let aggRef = ref(null);
  125. // const goPrivacy = () => {
  126. // privacyRef.value.open();
  127. // };
  128. // const goAgreement = () => {
  129. // aggRef.value.open();
  130. // };
  131. // 判断当前环境是否在钉钉环境
  132. const isDingTalk = () => {
  133. const ua = window.navigator.userAgent.toLowerCase();
  134. console.log("🚀 ~ 当前环境 ~ ua:", ua);
  135. return ua.includes("dingtalk") || ua.includes("dingtalkwork");
  136. };
  137. const dingTalkAutoLogin = async () => {
  138. // 判断是否在钉钉环境
  139. if (!isDingTalk()) {
  140. console.log("当前环境不是钉钉环境,无法自动登录");
  141. return;
  142. }
  143. // 执行钉钉微应用免登逻辑
  144. loginWithDingTalkH5();
  145. };
  146. // 钉钉登录
  147. const loginWithDingTalk = async () => {
  148. // #ifdef APP
  149. const plugin = uni.requireNativePlugin("DingTalk");
  150. // 钉钉登录,这里无法使用async,否则java端会报参数错误
  151. plugin.login((res) => {
  152. console.log(res);
  153. if (res.success === 1) {
  154. dingTalkLogin({
  155. type: 20,
  156. code: res.code,
  157. state: res.state,
  158. }).then((res) => {
  159. console.log(res);
  160. handleLoginSuccess(res);
  161. });
  162. } else if (res.success === 2) {
  163. uni.showToast({ title: t("login.dingTalkError"), icon: "none" });
  164. console.error("APP端钉钉登录失败:", res);
  165. }
  166. });
  167. // #endif
  168. // #ifdef H5
  169. if (isDingTalk()) {
  170. if (!dd) {
  171. uni.showToast({ title: t("login.dingTalkJsapiMissing"), icon: "none" });
  172. return;
  173. }
  174. loginWithDingTalkH5();
  175. } else {
  176. console.log("当前是普通 H5 环境,无法使用钉钉登录");
  177. uni.showToast({ title: t("login.h5DingTalk"), icon: "none" });
  178. }
  179. // #endif
  180. };
  181. const loginWithDingTalkH5 = async () => {
  182. const corpId = config.default.corpId;
  183. console.log("🚀 ~ loginWithDingTalkH5 ~ corpId:", corpId);
  184. const clientId = config.default.clientId;
  185. console.log("🚀 ~ loginWithDingTalkH5 ~ clientId:", clientId);
  186. if (!corpId || !clientId) {
  187. console.error("缺少必要参数");
  188. return;
  189. }
  190. dd.requestAuthCode({
  191. corpId,
  192. clientId,
  193. success: async (result) => {
  194. console.log("🚀 ~ loginWithDingTalkH5 ~ result:", result);
  195. const { code } = result;
  196. dingTalkLoginH5({
  197. type: 10,
  198. state: new Date().getTime(),
  199. code: code,
  200. })
  201. .then((res) => {
  202. console.log("🚀 ~ loginWithDingTalkH5 ~ res:", res);
  203. handleLoginSuccess(res);
  204. })
  205. .catch((err) => {
  206. console.log("🚀 ~ loginWithDingTalkH5 ~ err:", err);
  207. });
  208. },
  209. fail: (err) => {
  210. console.log("🚀 ~ loginWithDingTalkH5 ~ err:", err);
  211. uni.showToast({
  212. title: "获取code失败:" + JSON.stringify(err),
  213. icon: "none",
  214. });
  215. },
  216. });
  217. };
  218. onLoad(async (options) => {
  219. console.log(
  220. "onLoad Login",
  221. uni.getLocale(),
  222. 11,
  223. uni.getStorageSync("language"),
  224. );
  225. console.log(options);
  226. // 保存钉钉消息传递的参数
  227. if (options.userId) {
  228. uni.setStorageSync("dingTalkJson", JSON.stringify(options));
  229. const isLoggedIn = uni.getStorageSync("userId");
  230. if (!isLoggedIn) {
  231. const result = await getTokenByUserId(options.userId);
  232. await handleLoginSuccess(result);
  233. }
  234. }
  235. // #ifdef H5
  236. // 当前环境为H5时,判断是否是通过钉钉微应用打开的链接
  237. // 获取当前Url地址
  238. const url = window.location.href;
  239. console.log("当前环境为H5时 当前Url地址:", url);
  240. // 判断是否是通过钉钉微应用打开的链接
  241. if (url.includes("/deepoil")) {
  242. dingTalkAutoLogin();
  243. }
  244. // #endif
  245. });
  246. onMounted(() => {
  247. // console.log("onMounted");
  248. // 检查是否需要显示语言选择弹窗
  249. if (!uni.getStorageSync("language")) {
  250. nextTick(() => {
  251. openLanguagePopup();
  252. });
  253. }
  254. // 检查是否已登录
  255. const isLoggedIn = uni.getStorageSync("userId");
  256. // console.log("isLoggedIn", isLoggedIn);
  257. if (isLoggedIn) {
  258. uni.switchTab({
  259. url: "/pages/entry/index",
  260. });
  261. }
  262. });
  263. const placeholderStyle = ref("color:#797979;font-weight:500;font-size:16px");
  264. const inputStyles = reactive({
  265. backgroundColor: "#F0F3FB",
  266. color: "#797979",
  267. });
  268. const loginData = reactive({
  269. username: "",
  270. password: "",
  271. });
  272. const loginRules = ref({
  273. username: {
  274. rules: [
  275. {
  276. required: true,
  277. errorMessage: t("login.enterUsername"),
  278. },
  279. ],
  280. },
  281. password: {
  282. rules: [
  283. {
  284. required: true,
  285. errorMessage: t("login.enterPassword"),
  286. },
  287. ],
  288. },
  289. });
  290. const formRef = ref();
  291. const formSubmit = async (formEl) => {
  292. // if (!my_value.value) {
  293. // uni.showToast({
  294. // title: '请阅读并同意隐私政策和用户服务协议',
  295. // icon: 'none', // 可选 success/error/loading/none
  296. // duration: 2000, // 持续时间,单位ms
  297. // mask: true // 是否显示透明蒙层,防止触摸穿透
  298. // });
  299. // return;
  300. // }
  301. if (!formEl) return;
  302. await formEl
  303. .validate()
  304. .then((res) => {
  305. appLogin({
  306. ...loginData,
  307. // rememberMe: ,
  308. // tenantName: ""
  309. })
  310. .then(async (result) => {
  311. console.log("result,", result.data);
  312. if (result) {
  313. await saveUser({
  314. name: loginData.username,
  315. pwd: loginData.password,
  316. });
  317. await handleLoginSuccess(result);
  318. }
  319. })
  320. .finally(() => {});
  321. })
  322. .catch((err) => {
  323. console.log("err", err);
  324. });
  325. };
  326. const handleLoginSuccess = async (result) => {
  327. if (result) {
  328. await setUserId(result.data.userId);
  329. await setToken(result.data);
  330. await getInfo().then(async (res) => {
  331. // console.log('useres', res)
  332. const data = JSON.stringify({
  333. user: res.data.user,
  334. roles: res.data.roles,
  335. });
  336. // console.log('data', data)
  337. await setUserInfo(data);
  338. await setDeptId(res.data.user.deptId);
  339. await uni.switchTab({
  340. url: "/pages/entry/index",
  341. });
  342. });
  343. }
  344. };
  345. </script>
  346. <style lang="scss" scoped>
  347. .privacy {
  348. height: 60vh;
  349. width: 85vw;
  350. overflow: hidden;
  351. }
  352. .uni-padding-wrap {
  353. display: flex;
  354. z-index: 999;
  355. justify-content: center;
  356. align-items: center;
  357. padding: 0 10rpx;
  358. }
  359. .uni-title {
  360. font-size: 12px;
  361. }
  362. .login-top {
  363. position: relative;
  364. width: 100%;
  365. height: 422rpx;
  366. }
  367. .back-img {
  368. width: 100%;
  369. height: 100%;
  370. }
  371. .login-text {
  372. width: 100%;
  373. height: 100%;
  374. box-sizing: border-box;
  375. position: absolute;
  376. top: 0;
  377. left: 0;
  378. color: #ffffff;
  379. font-size: 40rpx;
  380. font-family: "Negreta,PingFang SC";
  381. font-weight: 600;
  382. padding: 0 56rpx;
  383. display: flex;
  384. justify-content: center;
  385. flex-direction: column;
  386. .text {
  387. width: 100%;
  388. margin-bottom: 6rpx;
  389. }
  390. }
  391. .margin-bt {
  392. margin-bottom: 25px;
  393. }
  394. .login-form-wrap {
  395. padding: 60rpx;
  396. }
  397. :deep(.uni-easyinput__content-input) {
  398. height: 45px;
  399. }
  400. :deep(.uni-input-input) {
  401. color: #999999 !important;
  402. }
  403. uni-button[type="primary"] {
  404. background: #004098;
  405. }
  406. .btn-text {
  407. color: #004098;
  408. margin-top: 20px;
  409. font-size: 14px;
  410. font-weight: 500;
  411. }
  412. </style>