login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="content">
  3. <view id="box" v-if="whichPage === '1'">
  4. <view id="top" class="">
  5. <view class="top_le"></view>
  6. <view class="top_ri"></view>
  7. </view>
  8. <view class="title">
  9. <view>{{ loginTitle }}</view>
  10. </view>
  11. <view class="login-form">
  12. <u--form :model="inputForm" labelWidth="100px" labelPosition="left" :rules="rules" ref="uForm">
  13. <u-form-item label="用户名" borderBottom prop="username">
  14. <u-input border="none" v-model="inputForm.username" :adjust-position="false" />
  15. </u-form-item>
  16. <u-form-item label="密码" borderBottom prop="password">
  17. <u-input border="none" password v-model="inputForm.password" :adjust-position="false" />
  18. </u-form-item>
  19. <u-form-item label="验证码" borderBottom prop="code" v-if="passwordErrorCount >= 5">
  20. <u-input border="none" v-model="inputForm.code" :adjust-position="false" />
  21. <u--image :showLoading="true" :src="captchaImg" width="140px" height="40px"
  22. @click="getCaptcha"></u--image>
  23. </u-form-item>
  24. </u--form>
  25. </view>
  26. <view class="but">
  27. <u-button type="primary" shape="circle" color="linear-gradient(90deg, #1989FA, #19C2FA)"
  28. @click="bindLogin" text="欢迎登录"></u-button>
  29. </view>
  30. <view class="fot">
  31. <text @tap="openForgetPassword" style="color: #1989FA;">忘记密码</text>
  32. </view>
  33. </view>
  34. <view id="box" v-else-if="whichPage === '2'">
  35. <view id="top" class="">
  36. <view class="top_le"></view>
  37. <view class="top_ri"></view>
  38. </view>
  39. <view class="title">
  40. <view>修改密码</view>
  41. <view>使用账号绑定的手机号找回密码</view>
  42. </view>
  43. <view class="login-form">
  44. <u--form :model="inputForm" labelWidth="100px" labelPosition="left" :rules="rules" ref="uForm">
  45. <u-form-item label="绑定手机号" borderBottom prop="phoneNumber">
  46. <u-input border="none" v-model="inputForm.phoneNumber" placeholder='请输入绑定手机号' />
  47. </u-form-item>
  48. <u-form-item label="验证码" borderBottom prop="phoneCode">
  49. <u-input border="none" v-model="inputForm.phoneCode" placeholder="请输入验证码"></u-input>
  50. <u-button type="primary" @click="pushPhoneCode" :loading="!showPhoneCode"
  51. style="background-color: #3595f9; width: 100px;">
  52. <span v-if="showPhoneCode" style="color: white;">获取验证码</span>
  53. <span v-else class="count" style="color: white;">请等待 {{ count }} s</span>
  54. </u-button>
  55. </u-form-item>
  56. <u-form-item label="新密码" borderBottom prop="newPassword">
  57. <u-input border="none" v-model="inputForm.newPassword" placeholder='请输入新密码' />
  58. </u-form-item>
  59. <u-form-item label="确认密码" borderBottom prop="newPasswordAgain">
  60. <u-input border="none" v-model="inputForm.newPasswordAgain" placeholder='请输入确认密码' />
  61. </u-form-item>
  62. </u--form>
  63. </view>
  64. <view class="but">
  65. <u-button type="primary" shape="circle" color="linear-gradient(90deg, #1989FA, #19C2FA)"
  66. @click="saveNewPass" text="保存"></u-button>
  67. </view>
  68. <view class="fot">
  69. <!-- <text @tap="reg_ok">免费注册</text>-->
  70. <!-- <text style="display: inline-block;width: 10vw;"></text>-->
  71. <text @tap="backLogin" style="color: #1989FA;">前往登录页面</text>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. import * as $auth from "@/common/auth.js"
  78. import loginService from "@/api/auth/loginService"
  79. import { mapActions } from 'vuex'
  80. var graceChecker = require("@/common/graceChecker.js");
  81. export default {
  82. data() {
  83. return {
  84. loginTitle: '兴光业务管理系统平台',
  85. showPhoneCode: true,
  86. captchaImg: '',
  87. inputForm: {
  88. 'username': '',
  89. 'password': '',
  90. 'code': '',
  91. 'uuid': '',
  92. 'phoneNumber': '',
  93. 'phoneCode': '',
  94. 'newPassword': '',
  95. 'newPasswordAgain': '',
  96. },
  97. passwordErrorCount: 0,
  98. whichPage: '1', // 1为登录页面,2为忘记密码页面
  99. rules: {
  100. username: [
  101. {
  102. required: true,
  103. message: '请输入用户名',
  104. trigger: ['blur', 'change']
  105. }
  106. ],
  107. password: [
  108. {
  109. required: true,
  110. message: '请输入密码',
  111. trigger: ['blur', 'change']
  112. }
  113. ],
  114. code: [
  115. {
  116. required: true,
  117. message: '请输入验证码',
  118. trigger: ['blur', 'change']
  119. }
  120. ],
  121. phoneNumber: [
  122. {
  123. required: true,
  124. message: '请输入绑定手机号',
  125. trigger: ['blur', 'change']
  126. }
  127. ],
  128. phoneCode: [
  129. {
  130. required: true,
  131. message: '请输入验证码',
  132. trigger: ['blur', 'change']
  133. }
  134. ],
  135. newPassword: [
  136. {
  137. required: true,
  138. message: '请输入新密码',
  139. trigger: ['blur', 'change']
  140. }
  141. ],
  142. newPasswordAgain: [
  143. {
  144. required: true,
  145. message: '请输入确认密码',
  146. trigger: ['blur', 'change']
  147. }
  148. ],
  149. }
  150. }
  151. },
  152. created() {
  153. this.getCaptcha()
  154. // 根据域名获取对应标题
  155. this.getLoginTitle()
  156. },
  157. methods: {
  158. ...mapActions(['refreshUserInfo']),
  159. safeGetStorage(key, defaultValue = '') {
  160. if (typeof key !== 'string' || !key || key === 'undefined') {
  161. return defaultValue;
  162. }
  163. try {
  164. const value = uni.getStorageSync(key);
  165. return value === undefined || value === null ? defaultValue : value;
  166. } catch (e) {
  167. console.error('getStorageSync failed:', key, e);
  168. return defaultValue;
  169. }
  170. },
  171. // 区分租户显示对应的登录标题
  172. getLoginTitle() {
  173. const host = window.location.hostname;
  174. if (host === "cpaoa.xgccpm.com") {
  175. this.loginTitle = "综合管理系统"
  176. } else if (host === "cpapmis.xgccpm.com") {
  177. this.loginTitle = "会计业务管理系统"
  178. } else if (host === "amc.xgccpm.com") {
  179. this.loginTitle = "评估管理系统"
  180. } else if (host === "zs.xgccpm.com") {
  181. this.loginTitle = "中审管理系统"
  182. } else if (host === "wz.xgccpm.com") {
  183. this.loginTitle = "物资管理系统"
  184. } else if (host === "sz.xgccpm.com") {
  185. this.loginTitle = "苏州分公司"
  186. } else if (host === "jct.xgccpm.com") {
  187. this.loginTitle = "景聚庭"
  188. }
  189. },
  190. // 登录
  191. bindLogin() {
  192. console.log(this.passwordErrorCount)
  193. console.log(this.passwordErrorCount)
  194. console.log(this.passwordErrorCount)
  195. console.log(this.passwordErrorCount)
  196. /**
  197. * 客户端对账号信息进行一些必要的校验。
  198. * 实际开发中,根据业务需要进行处理,这里仅做示例。
  199. */
  200. this.$refs.uForm.validate().then(res => {
  201. const dingTalkBindKey = this.safeGetStorage('dingTalkBindKey');
  202. if (dingTalkBindKey) {
  203. this.inputForm.dingTalkBindKey = dingTalkBindKey;
  204. this.inputForm.tenantId = this.safeGetStorage('dingTalkTenantId', '10009') || '10009';
  205. loginService.dingTalkBindLogin(this.inputForm).then((data) => {
  206. uni.removeStorageSync('dingTalkBindKey');
  207. this.$store.commit('SET_TOKEN', data.token);
  208. this.refreshUserInfo();
  209. this.$router.push('/pages/index/index');
  210. }).catch(e => {
  211. this.passwordErrorCount++;
  212. console.error(e)
  213. })
  214. return;
  215. }
  216. this.inputForm.openId = this.safeGetStorage('openId');
  217. loginService.login(this.inputForm).then((data) => {
  218. this.$store.commit('SET_TOKEN', data.token);
  219. this.refreshUserInfo();
  220. // uni.reLaunch({
  221. // url: '/pages/index/index',
  222. // });
  223. this.$router.push('/pages/index/index');
  224. }).catch(e => {
  225. // 密码错误次数增加
  226. this.passwordErrorCount++;
  227. // 如果密码错误次数超过5次,显示验证码
  228. if (this.passwordErrorCount >= 5) {
  229. this.inputForm.code = ''; // 清空验证码输入框
  230. this.getCaptcha(); // 获取验证码
  231. }
  232. this.getCaptcha()
  233. if (e.data.includes("请输入验证码")) {
  234. this.passwordErrorCount = 5
  235. }
  236. console.error(e)
  237. })
  238. })
  239. },
  240. // 获取验证码
  241. getCaptcha() {
  242. loginService.getCode().then((data) => {
  243. this.captchaImg = 'data:image/gif;base64,' + data.codeImg
  244. this.inputForm.uuid = data.uuid
  245. })
  246. },
  247. openForgetPassword() {
  248. console.log('进来了')
  249. this.inputForm.username = ''
  250. this.inputForm.password = ''
  251. this.inputForm.code = ''
  252. this.$nextTick(() => {
  253. this.whichPage = '2' // 打开忘记密码页面
  254. })
  255. },
  256. backLogin() {
  257. this.inputForm.phoneNumber = ''
  258. this.inputForm.phoneCode = ''
  259. this.inputForm.newPassword = ''
  260. this.inputForm.newPasswordAgain = ''
  261. this.whichPage = '1' // 打开登录页面
  262. },
  263. pushPhoneCode() {
  264. // 验证手机号码格式是否正确
  265. console.log('this.inputForm.phoneNumber', this.inputForm.phoneNumber)
  266. console.log('this.checkMobile(this.inputForm.phoneNumber)', this.checkMobile(this.inputForm.phoneNumber))
  267. if (this.isNotEmpty(this.inputForm.phoneNumber) && this.checkMobile(this.inputForm.phoneNumber)) {
  268. // 向手机号发送验证码
  269. loginService.getPhoneCode(this.inputForm.phoneNumber).then((data) => {
  270. if (data.success) {
  271. uni.showToast({ title: data.message, icon: "success" });
  272. // ‘发送验证码’按钮倒计时
  273. const TIME_COUNT = 60 // 更改倒计时时间
  274. if (!this.timer) {
  275. this.count = TIME_COUNT
  276. this.showPhoneCode = false
  277. this.timer = setInterval(() => {
  278. if (this.count > 0 && this.count <= TIME_COUNT) {
  279. this.count--
  280. } else {
  281. this.showPhoneCode = true
  282. clearInterval(this.timer) // 清除定时器
  283. this.timer = null
  284. }
  285. }, 1000)
  286. }
  287. } else {
  288. this.$message.error(data.message)
  289. }
  290. })
  291. } else {
  292. uni.showToast({
  293. title: '请填写正确的手机号',
  294. icon: "none",
  295. duration: 2000
  296. })
  297. }
  298. },
  299. checkMobile(str) {
  300. const phone = /(^(\d{3,4}-)?\d{6,8}$)|(^(\d{3,4}-)?\d{6,8}(-\d{1,5})?$)|(^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$)/
  301. if (str && !phone.test(str)) {
  302. uni.showToast({
  303. title: '绑定手机号填写不正确,请重新填写',
  304. icon: "none",
  305. duration: 2000
  306. })
  307. this.inputForm.phoneNumber = ''
  308. return false;
  309. } else {
  310. return true;
  311. }
  312. },
  313. isEmpty(value) {
  314. let result = false;
  315. if (value == null || value == undefined) {
  316. result = true;
  317. }
  318. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  319. result = true;
  320. }
  321. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  322. result = true;
  323. }
  324. return result;
  325. },
  326. isNotEmpty(value) {
  327. return !this.isEmpty(value)
  328. },
  329. saveNewPass() {
  330. return new Promise((resolve, reject) => {
  331. let errors = [];
  332. if (this.inputForm.newPassword !== this.inputForm.newPasswordAgain) {
  333. errors.push('两次输入的密码不相同,请重新输入!');
  334. }
  335. if (errors.length > 0) {
  336. // 存在错误,显示提示信息
  337. errors.forEach(error => {
  338. uni.showToast({
  339. title: error,
  340. icon: 'none',
  341. duration: 1500
  342. });
  343. });
  344. reject('Form validation failed');
  345. } else {
  346. loginService.savePwd(this.inputForm).then((data) => {
  347. if (data.success) {
  348. this.$message.success(data.message)
  349. this.inputForm.username = this.inputForm.phoneNumber
  350. this.inputForm.newPassword = this.inputForm.newPassword
  351. this.backLogin() // 修改密码成功后返回登录页面
  352. this.loading = false
  353. } else {
  354. this.$message.error(data.message)
  355. this.loading = false
  356. }
  357. }).catch(() => {
  358. this.loading = false
  359. })
  360. }
  361. });
  362. },
  363. }
  364. }
  365. </script>
  366. <style lang="scss" scoped>
  367. .content {
  368. height: 100vh;
  369. overflow: hidden;
  370. }
  371. #box {
  372. position: relative;
  373. min-height: 100vh;
  374. overflow: hidden;
  375. // color: $u-content-color;
  376. }
  377. .title {
  378. position: absolute;
  379. top: 160rpx;
  380. width: 100vw;
  381. text-align: center;
  382. font-size: 28rpx;
  383. color: #198bfa;
  384. }
  385. .title view:nth-child(1) {
  386. height: 54px;
  387. font-size: 56rpx;
  388. font-family: PingFang SC;
  389. font-weight: bold;
  390. text-shadow: 0px 2px 24px rgba(58, 199, 118, 0.4);
  391. }
  392. .title view:nth-child(2) {
  393. font-size: 32rpx;
  394. font-family: PingFang SC;
  395. font-weight: 500;
  396. color: #198BFA;
  397. margin-top: 5vw;
  398. }
  399. #top {
  400. width: 100vw;
  401. height: 420rpx;
  402. position: relative;
  403. }
  404. .top_ri {
  405. width: 100vw;
  406. height: 100vw;
  407. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  408. opacity: 0.1;
  409. border-radius: 50%;
  410. position: absolute;
  411. right: -47vw;
  412. top: -37vw;
  413. }
  414. .top_le {
  415. width: 100vw;
  416. height: 100vw;
  417. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  418. opacity: 0.1;
  419. border-radius: 50%;
  420. position: absolute;
  421. left: -34vw;
  422. top: -54vw;
  423. }
  424. .inp {
  425. padding-left: 5vw;
  426. position: relative;
  427. top: -4vh;
  428. }
  429. .inp text {
  430. font-size: 36rpx;
  431. font-family: PingFang SC;
  432. font-weight: 500;
  433. color: #333333;
  434. margin-left: 5px;
  435. }
  436. .but {
  437. padding-left: 50rpx;
  438. padding-right: 50rpx;
  439. margin-top: 60rpx;
  440. }
  441. .fot {
  442. width: 100vw;
  443. height: 26px;
  444. text-align: center;
  445. font-size: 28rpx;
  446. font-family: PingFang SC;
  447. font-weight: 500;
  448. color: #8E8E8E;
  449. margin-top: 20px;
  450. }
  451. .login-form {
  452. padding-left: 100rpx;
  453. padding-right: 30rpx;
  454. }
  455. </style>