login.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="container login-container">
  3. <view class="logo">
  4. <img src="../../static/image/login/logo.png" alt="" />
  5. </view>
  6. <view class="form">
  7. <view class="form-item">
  8. <view class="form-input-item">
  9. <view class="form-input-left">
  10. <uni-icons type="person" size="24" color="#93959b"></uni-icons>
  11. </view>
  12. <view class="form-input-content">
  13. <input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
  14. v-model="loginForm.phone" />
  15. </view>
  16. </view>
  17. </view>
  18. <!-- 获取验证码 -->
  19. <view class="form-item">
  20. <view class="form-input-item">
  21. <view class="form-input-left">
  22. <uni-icons type="email" size="24" color="#93959b"></uni-icons>
  23. </view>
  24. <view class="form-input-content code-input-wrapper">
  25. <input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
  26. v-model="loginForm.phoneCode" />
  27. <button class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
  28. {{codeName}}
  29. </button>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="form-btn-container">
  34. <button class="form-btn" @click="submitLogin">登录</button>
  35. </view>
  36. <!-- #ifdef MP-WEIXIN -->
  37. <view class="form-btn-container">
  38. <button type="primary" class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
  39. 微信手机号一键登录
  40. </button>
  41. </view>
  42. <!-- #endif -->
  43. </view>
  44. <y-modal :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
  45. <view class="code-input-container">
  46. <input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
  47. <view class="code-canvas-wrapper">
  48. <img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
  49. </view>
  50. </view>
  51. </y-modal>
  52. </view>
  53. </template>
  54. <script>
  55. import {
  56. login,
  57. getImgCode,
  58. sendPhoneCode
  59. } from '@/request/api/login'
  60. import {
  61. isTel
  62. } from '@/uitls/validate'
  63. import yModal from "@/components/login/yModal.vue";
  64. export default {
  65. data() {
  66. return {
  67. loginForm: {
  68. phone: '18888888888',
  69. phoneCode: '888888'
  70. },
  71. captchaShow: false,
  72. imgCode: '',
  73. codeImg: '',
  74. codeName: '获取验证码', //获取验证码text
  75. isDisabled: false, //获取验证码按钮状态
  76. }
  77. },
  78. components: {
  79. yModal
  80. },
  81. methods: {
  82. getImageCode() {
  83. if (!isTel(this.loginForm.phone)) return uni.showToast({
  84. title: '请输入正确的手机号',
  85. icon: "none"
  86. })
  87. if (this.isDisabled) return;
  88. this.imgCodeFunc();
  89. this.captchaShow = true;
  90. },
  91. imgCodeFunc() {
  92. getImgCode().then(res => {
  93. if (res.code === 200) {
  94. this.codeImg = res.data.pngBase64;
  95. }
  96. })
  97. },
  98. confirmHandle() {
  99. if (!this.imgCode) return uni.showToast({
  100. title: '请输入图片验证码',
  101. icon: "none"
  102. })
  103. sendPhoneCode(this.loginForm.phone, this.imgCode).then(res => {
  104. if (res.code === 200) {
  105. this.codeReset();
  106. uni.showToast({
  107. title: '发送成功',
  108. icon: "none"
  109. })
  110. }
  111. })
  112. },
  113. codeReset() {
  114. //重置获取验证码倒计时
  115. let codeNumber = 60;
  116. codeNumber--;
  117. this.handleCode(codeNumber);
  118. let codeRestFn = setInterval(() => {
  119. codeNumber--;
  120. this.handleCode(codeNumber);
  121. if (codeNumber == 0) clearInterval(codeRestFn); //停止
  122. }, 1000);
  123. },
  124. handleCode(codeNumber) {
  125. //code操作
  126. this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
  127. this.isDisabled = codeNumber == 0 ? false : true;
  128. },
  129. wxPhoneLogin(e) { //微信手机号一键登录
  130. this.login({
  131. pCode: e.detail.code
  132. })
  133. },
  134. submitLogin() { //手机验证码登录
  135. if (!this.loginForm.phoneCode) return uni.showToast({
  136. title: '请输入短信验证码',
  137. icon: "none"
  138. })
  139. this.login(this.loginForm);
  140. },
  141. login(postData) {
  142. uni.showLoading({
  143. title: '登录中'
  144. })
  145. // #ifdef MP-WEIXIN
  146. uni.login({
  147. provider: 'weixin',
  148. success: loginRes => {
  149. if (postData.phone != '18888888888') postData['code'] = loginRes.code;
  150. login(postData).then(this.successFunc)
  151. }
  152. })
  153. // #endif
  154. // #ifndef MP-WEIXIN
  155. login(postData).then(this.successFunc);
  156. // #endif
  157. },
  158. successFunc(res) {
  159. if (res.code === 200) {
  160. uni.showToast({
  161. title: '登录成功',
  162. icon: "none"
  163. })
  164. setTimeout(() => {
  165. uni.hideLoading();
  166. uni.navigateBack();
  167. }, 400)
  168. } else {
  169. uni.hideLoading();
  170. }
  171. }
  172. }
  173. }
  174. </script>
  175. <style scoped lang="scss">
  176. .login-container {
  177. align-items: center;
  178. .logo {
  179. margin-top: 80rpx;
  180. width: 150rpx;
  181. height: 150rpx;
  182. }
  183. //弹出输入框
  184. .code-input-container {
  185. display: flex;
  186. align-items: center;
  187. margin-top: 20rpx;
  188. .code-input {
  189. background: #F7F8FA;
  190. width: 306rpx;
  191. height: 84rpx;
  192. line-height: 84rpx;
  193. padding: 0 20rpx;
  194. border-radius: 20rpx;
  195. box-sizing: border-box;
  196. &::placeholder {
  197. line-height: 84rpx;
  198. }
  199. }
  200. .code-canvas-wrapper {
  201. flex: 1;
  202. .code-image {
  203. width: 100%;
  204. height: 84rpx;
  205. }
  206. }
  207. }
  208. .form {
  209. margin-top: 70rpx;
  210. display: flex;
  211. flex-direction: column;
  212. align-items: center;
  213. width: 686rpx;
  214. margin: 70rpx auto;
  215. // 登录表单项
  216. .form-item {
  217. width: 100%;
  218. border-radius: 200rpx;
  219. height: 100rpx;
  220. background-color: #fff;
  221. display: flex;
  222. margin-bottom: 50rpx;
  223. &:last-of-type {
  224. margin-bottom: 0;
  225. }
  226. }
  227. // 登录的input项
  228. .form-input-item {
  229. height: 100%;
  230. width: 100%;
  231. display: flex;
  232. align-items: center;
  233. box-sizing: border-box;
  234. // input左边区域
  235. .form-input-left {
  236. display: flex;
  237. align-items: center;
  238. margin-left: 30rpx;
  239. }
  240. // 手机号信息
  241. .phone-info {
  242. margin-left: 20rpx;
  243. // 手机号信息文字
  244. .phone-info-txt {
  245. margin-right: 8rpx;
  246. }
  247. // 下拉按钮图片大小
  248. .phone-info-img {
  249. width: 24rpx;
  250. height: 24rpx;
  251. }
  252. }
  253. // input内容区域
  254. .form-input-content {
  255. flex: 1 1 auto;
  256. padding-right: 20rpx;
  257. position: relative;
  258. // 具体的input样式
  259. .form-input {
  260. width: 100%;
  261. padding-left: 20rpx;
  262. // 覆盖uni的默认的提示
  263. ::v-deep .uni-input-placeholder {
  264. color: #C8C9CC;
  265. }
  266. }
  267. }
  268. }
  269. // 验证码
  270. .code-input-wrapper {
  271. display: flex;
  272. align-items: center;
  273. .code-input-btn {
  274. width: auto;
  275. width: 200rpx;
  276. height: 64rpx;
  277. font-size: 24rpx;
  278. padding: 12rpx 40rpx;
  279. white-space: nowrap;
  280. display: flex;
  281. align-items: center;
  282. justify-content: center;
  283. border-radius: 200rpx;
  284. background: $uni-primary;
  285. color: #fff;
  286. &.disabled {
  287. opacity: 0.5;
  288. }
  289. &::after {
  290. display: none;
  291. }
  292. }
  293. }
  294. // 登录按钮容器
  295. .form-btn-container {
  296. margin-top: 20rpx;
  297. width: 100%;
  298. }
  299. // 登录按钮
  300. .form-btn {
  301. height: 88rpx;
  302. border-radius: 200rpx;
  303. padding: 24rpx 32rpx;
  304. line-height: 40rpx;
  305. color: #fff;
  306. background-color: $uni-primary;
  307. font-size: 32rpx;
  308. &.disabled {
  309. opacity: 0.5;
  310. }
  311. &::after {
  312. display: none;
  313. }
  314. }
  315. .wx-btn {
  316. border-radius: 200rpx;
  317. font-size: 32rpx;
  318. &::after {
  319. display: none;
  320. }
  321. }
  322. }
  323. }
  324. </style>