login.vue 11 KB

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