login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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" />
  15. </u-form-item>
  16. <u-form-item label="密码" borderBottom prop="password">
  17. <u-input border="none" password v-model="inputForm.password" />
  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" />
  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. // 登录
  153. bindLogin() {
  154. /**
  155. * 客户端对账号信息进行一些必要的校验。
  156. * 实际开发中,根据业务需要进行处理,这里仅做示例。
  157. */
  158. this.$refs.uForm.validate().then(res => {
  159. loginService.login(this.inputForm).then((data) => {
  160. this.$store.commit('SET_TOKEN',data.token);
  161. this.refreshUserInfo();
  162. // uni.reLaunch({
  163. // url: '/pages/index/index',
  164. // });
  165. this.$router.push('/pages/index/index');
  166. }).catch(e => {
  167. // 密码错误次数增加
  168. this.passwordErrorCount++;
  169. // 如果密码错误次数超过5次,显示验证码
  170. if (this.passwordErrorCount >= 5) {
  171. this.inputForm.code = ''; // 清空验证码输入框
  172. this.getCaptcha(); // 获取验证码
  173. }
  174. this.getCaptcha()
  175. console.error(e)
  176. })
  177. })
  178. },
  179. // 获取验证码
  180. getCaptcha () {
  181. loginService.getCode().then((data) => {
  182. this.captchaImg = 'data:image/gif;base64,' + data.codeImg
  183. this.inputForm.uuid = data.uuid
  184. })
  185. },
  186. openForgetPassword () {
  187. console.log('进来了')
  188. this.inputForm.username = ''
  189. this.inputForm.password = ''
  190. this.inputForm.code = ''
  191. this.$nextTick(() => {
  192. this.whichPage = '2' // 打开忘记密码页面
  193. })
  194. },
  195. backLogin () {
  196. this.inputForm.phoneNumber = ''
  197. this.inputForm.phoneCode = ''
  198. this.inputForm.newPassword = ''
  199. this.inputForm.newPasswordAgain = ''
  200. this.whichPage = '1' // 打开登录页面
  201. },
  202. pushPhoneCode () {
  203. // 验证手机号码格式是否正确
  204. console.log('this.inputForm.phoneNumber', this.inputForm.phoneNumber)
  205. console.log('this.checkMobile(this.inputForm.phoneNumber)', this.checkMobile(this.inputForm.phoneNumber))
  206. if (this.isNotEmpty(this.inputForm.phoneNumber) && this.checkMobile(this.inputForm.phoneNumber)) {
  207. // 向手机号发送验证码
  208. loginService.getPhoneCode(this.inputForm.phoneNumber).then((data) => {
  209. if (data.success) {
  210. uni.showToast({ title: data.message, icon: "success" });
  211. // ‘发送验证码’按钮倒计时
  212. const TIME_COUNT = 60 // 更改倒计时时间
  213. if (!this.timer) {
  214. this.count = TIME_COUNT
  215. this.showPhoneCode = false
  216. this.timer = setInterval(() => {
  217. if (this.count > 0 && this.count <= TIME_COUNT) {
  218. this.count--
  219. } else {
  220. this.showPhoneCode = true
  221. clearInterval(this.timer) // 清除定时器
  222. this.timer = null
  223. }
  224. }, 1000)
  225. }
  226. } else {
  227. this.$message.error(data.message)
  228. }
  229. })
  230. } else {
  231. uni.showToast({
  232. title: '请填写正确的手机号',
  233. icon: "none",
  234. duration:2000
  235. })
  236. }
  237. },
  238. checkMobile (str) {
  239. 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})$)/
  240. if (str && !phone.test(str)) {
  241. uni.showToast({
  242. title: '绑定手机号填写不正确,请重新填写',
  243. icon: "none",
  244. duration:2000
  245. })
  246. this.inputForm.phoneNumber = ''
  247. return false;
  248. } else {
  249. return true;
  250. }
  251. },
  252. isEmpty(value) {
  253. let result = false;
  254. if (value == null || value == undefined) {
  255. result = true;
  256. }
  257. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  258. result = true;
  259. }
  260. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  261. result = true;
  262. }
  263. return result;
  264. },
  265. isNotEmpty (value) {
  266. return !this.isEmpty(value)
  267. },
  268. saveNewPass () {
  269. return new Promise((resolve, reject) => {
  270. let errors = [];
  271. if (this.inputForm.newPassword !== this.inputForm.newPasswordAgain) {
  272. errors.push('两次输入的密码不相同,请重新输入!');
  273. }
  274. if (errors.length > 0) {
  275. // 存在错误,显示提示信息
  276. errors.forEach(error => {
  277. uni.showToast({
  278. title: error,
  279. icon: 'none',
  280. duration: 1500
  281. });
  282. });
  283. reject('Form validation failed');
  284. } else {
  285. loginService.savePwd(this.inputForm).then((data) => {
  286. if (data.success) {
  287. this.$message.success(data.message)
  288. this.inputForm.username = this.inputForm.phoneNumber
  289. this.inputForm.newPassword = this.inputForm.newPassword
  290. this.backLogin() // 修改密码成功后返回登录页面
  291. this.loading = false
  292. } else {
  293. this.$message.error(data.message)
  294. this.loading = false
  295. }
  296. }).catch(() => {
  297. this.loading = false
  298. })
  299. }
  300. });
  301. },
  302. }
  303. }
  304. </script>
  305. <style lang="scss" scoped>
  306. #box {
  307. position: relative;
  308. // color: $u-content-color;
  309. }
  310. .title {
  311. position: absolute;
  312. top: 15vh;
  313. width: 100vw;
  314. text-align: center;
  315. font-size: 28rpx;
  316. color: #198bfa;
  317. }
  318. .title view:nth-child(1) {
  319. height: 54px;
  320. font-size: 56rpx;
  321. font-family: PingFang SC;
  322. font-weight: bold;
  323. text-shadow: 0px 2px 24px rgba(58, 199, 118, 0.4);
  324. }
  325. .title view:nth-child(2) {
  326. font-size: 32rpx;
  327. font-family: PingFang SC;
  328. font-weight: 500;
  329. color: #198BFA;
  330. margin-top: 5vw;
  331. }
  332. #top {
  333. width: 100vw;
  334. height: 40vh;
  335. position: relative;
  336. }
  337. .top_ri {
  338. width: 100vw;
  339. height: 100vw;
  340. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  341. opacity: 0.1;
  342. border-radius: 50%;
  343. position: absolute;
  344. right: -47vw;
  345. top: -37vw;
  346. }
  347. .top_le {
  348. width: 100vw;
  349. height: 100vw;
  350. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  351. opacity: 0.1;
  352. border-radius: 50%;
  353. position: absolute;
  354. left: -34vw;
  355. top: -54vw;
  356. }
  357. .inp {
  358. padding-left: 5vw;
  359. position: relative;
  360. top: -4vh;
  361. }
  362. .inp text {
  363. font-size: 36rpx;
  364. font-family: PingFang SC;
  365. font-weight: 500;
  366. color: #333333;
  367. margin-left: 5px;
  368. }
  369. .but {
  370. padding-left: 50rpx;
  371. padding-right: 50rpx;
  372. margin-top: 60rpx;
  373. }
  374. .fot {
  375. width: 100vw;
  376. height: 26px;
  377. text-align: center;
  378. font-size: 28rpx;
  379. font-family: PingFang SC;
  380. font-weight: 500;
  381. color: #8E8E8E;
  382. margin-top: 20px;
  383. }
  384. .login-form {
  385. padding-left: 100rpx;
  386. padding-right: 30rpx;
  387. }
  388. </style>