login.vue 12 KB

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