login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. this.inputForm.username = ''
  188. this.inputForm.password = ''
  189. this.inputForm.code = ''
  190. this.$nextTick(() => {
  191. this.whichPage = '2' // 打开忘记密码页面
  192. })
  193. },
  194. backLogin () {
  195. this.inputForm.phoneNumber = ''
  196. this.inputForm.phoneCode = ''
  197. this.inputForm.newPassword = ''
  198. this.inputForm.newPasswordAgain = ''
  199. this.whichPage = '1' // 打开登录页面
  200. },
  201. pushPhoneCode () {
  202. // 验证手机号码格式是否正确
  203. if (this.isNotEmpty(this.inputForm.phoneNumber) && this.checkMobile(this.inputForm.phoneNumber)) {
  204. // 向手机号发送验证码
  205. loginService.getPhoneCode(this.inputForm.phoneNumber).then((data) => {
  206. if (data.success) {
  207. uni.showToast({ title: data.message, icon: "success" });
  208. // ‘发送验证码’按钮倒计时
  209. const TIME_COUNT = 60 // 更改倒计时时间
  210. if (!this.timer) {
  211. this.count = TIME_COUNT
  212. this.showPhoneCode = false
  213. this.timer = setInterval(() => {
  214. if (this.count > 0 && this.count <= TIME_COUNT) {
  215. this.count--
  216. } else {
  217. this.showPhoneCode = true
  218. clearInterval(this.timer) // 清除定时器
  219. this.timer = null
  220. }
  221. }, 1000)
  222. }
  223. } else {
  224. this.$message.error(data.message)
  225. }
  226. })
  227. } else {
  228. uni.showToast({
  229. title: '请填写正确的手机号',
  230. icon: "none",
  231. duration:2000
  232. })
  233. }
  234. },
  235. checkMobile (str) {
  236. 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})$)/
  237. if (str && !phone.test(str)) {
  238. uni.showToast({
  239. title: '绑定手机号填写不正确,请重新填写',
  240. icon: "none",
  241. duration:2000
  242. })
  243. this.inputForm.phoneNumber = ''
  244. return false;
  245. } else {
  246. return true;
  247. }
  248. },
  249. isEmpty(value) {
  250. let result = false;
  251. if (value == null || value == undefined) {
  252. result = true;
  253. }
  254. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  255. result = true;
  256. }
  257. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  258. result = true;
  259. }
  260. return result;
  261. },
  262. isNotEmpty (value) {
  263. return !this.isEmpty(value)
  264. },
  265. saveNewPass () {
  266. return new Promise((resolve, reject) => {
  267. let errors = [];
  268. if (this.inputForm.newPassword !== this.inputForm.newPasswordAgain) {
  269. errors.push('两次输入的密码不相同,请重新输入!');
  270. }
  271. if (errors.length > 0) {
  272. // 存在错误,显示提示信息
  273. errors.forEach(error => {
  274. uni.showToast({
  275. title: error,
  276. icon: 'none',
  277. duration: 1500
  278. });
  279. });
  280. reject('Form validation failed');
  281. } else {
  282. loginService.savePwd(this.inputForm).then((data) => {
  283. if (data.success) {
  284. this.$message.success(data.message)
  285. this.inputForm.username = this.inputForm.phoneNumber
  286. this.inputForm.newPassword = this.inputForm.newPassword
  287. this.backLogin() // 修改密码成功后返回登录页面
  288. this.loading = false
  289. } else {
  290. this.$message.error(data.message)
  291. this.loading = false
  292. }
  293. }).catch(() => {
  294. this.loading = false
  295. })
  296. }
  297. });
  298. },
  299. }
  300. }
  301. </script>
  302. <style lang="scss" scoped>
  303. #box {
  304. position: relative;
  305. // color: $u-content-color;
  306. }
  307. .title {
  308. position: absolute;
  309. top: 15vh;
  310. width: 100vw;
  311. text-align: center;
  312. font-size: 28rpx;
  313. color: #198bfa;
  314. }
  315. .title view:nth-child(1) {
  316. height: 54px;
  317. font-size: 56rpx;
  318. font-family: PingFang SC;
  319. font-weight: bold;
  320. text-shadow: 0px 2px 24px rgba(58, 199, 118, 0.4);
  321. }
  322. .title view:nth-child(2) {
  323. font-size: 32rpx;
  324. font-family: PingFang SC;
  325. font-weight: 500;
  326. color: #198BFA;
  327. margin-top: 5vw;
  328. }
  329. #top {
  330. width: 100vw;
  331. height: 40vh;
  332. position: relative;
  333. }
  334. .top_ri {
  335. width: 100vw;
  336. height: 100vw;
  337. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  338. opacity: 0.1;
  339. border-radius: 50%;
  340. position: absolute;
  341. right: -47vw;
  342. top: -37vw;
  343. }
  344. .top_le {
  345. width: 100vw;
  346. height: 100vw;
  347. background: linear-gradient(141deg, #19C0FA 0%, #198BFA 100%);
  348. opacity: 0.1;
  349. border-radius: 50%;
  350. position: absolute;
  351. left: -34vw;
  352. top: -54vw;
  353. }
  354. .inp {
  355. padding-left: 5vw;
  356. position: relative;
  357. top: -4vh;
  358. }
  359. .inp text {
  360. font-size: 36rpx;
  361. font-family: PingFang SC;
  362. font-weight: 500;
  363. color: #333333;
  364. margin-left: 5px;
  365. }
  366. .but {
  367. padding-left: 50rpx;
  368. padding-right: 50rpx;
  369. margin-top: 60rpx;
  370. }
  371. .fot {
  372. width: 100vw;
  373. height: 26px;
  374. text-align: center;
  375. font-size: 28rpx;
  376. font-family: PingFang SC;
  377. font-weight: 500;
  378. color: #8E8E8E;
  379. margin-top: 20px;
  380. }
  381. .login-form {
  382. padding-left: 100rpx;
  383. padding-right: 30rpx;
  384. }
  385. </style>