login.vue 10 KB

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