login.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="container">
  3. <img class="img" src="@/static/images/school-logo.jpg" />
  4. <view class="logo_name">万载三中</view>
  5. <!-- 手机号码输入框区域 -->
  6. <view class="phone">
  7. <input class="phone_input" type="number" placeholder="手机号" placeholder-style="color:#CCCCCC;" v-model="phone" />
  8. </view>
  9. <!-- 验证码区域 -->
  10. <view class="code">
  11. <input class="code_input" type="number" placeholder="验证码" placeholder-style="color:#CCCCCC;" v-model="codeValue" />
  12. <uv-code uniqueKey="login" keepRunning ref="codeDom" @change="codeChange"></uv-code>
  13. <view class="code_box" @click="getCode">{{ tips }}</view>
  14. </view>
  15. <!-- 登录按钮区域 -->
  16. <view class="loginBtn" :class="{ active: btnFlag }" @click="handleLogin">登录</view>
  17. </view>
  18. </template>
  19. <script setup>
  20. import { ref, computed } from 'vue'
  21. import { onLoad } from '@dcloudio/uni-app'
  22. import { myRequest } from '@/utils/api.js'
  23. // 手机号码
  24. const phone = ref('')
  25. // 验证码
  26. const codeValue = ref('')
  27. // 验证码Dom
  28. const codeDom = ref(null)
  29. // 登录按钮是否高亮
  30. const btnFlag = computed(() => {
  31. // 手机号码验证规则
  32. const regPhone = /^1[3-9]\d{9}$/
  33. if (phone.value && regPhone.test(phone.value) && codeValue.value) {
  34. return true
  35. } else {
  36. return false
  37. }
  38. })
  39. // 提示语
  40. const tips = ref('')
  41. onLoad(() => {})
  42. // 获取验证码按钮点击回调
  43. const getCode = async () => {
  44. // 手机号码验证规则
  45. const regPhone = /^1[3-9]\d{9}$/
  46. if (!phone.value) {
  47. uni.showToast({
  48. title: '请先输入手机号码',
  49. icon: 'none'
  50. })
  51. return
  52. }
  53. if (!regPhone.test(phone.value)) {
  54. uni.showToast({
  55. title: '手机号码格式错误',
  56. icon: 'none'
  57. })
  58. return
  59. }
  60. if (codeDom.value.canGetCode) {
  61. const res = await myRequest({
  62. url: '/wanzai/api/wechat/sendMessage',
  63. data: {
  64. phone: phone.value
  65. }
  66. })
  67. // console.log(res)
  68. uni.showToast({
  69. title: '验证码已发送',
  70. icon: 'none'
  71. })
  72. codeDom.value.start()
  73. } else {
  74. uni.showToast({
  75. title: '倒计时结束后再发送',
  76. icon: 'none'
  77. })
  78. }
  79. }
  80. // 登录按钮回调
  81. const handleLogin = () => {
  82. const flag = verifyValue()
  83. if (flag) {
  84. uni.login({
  85. success: (res) => {
  86. // console.log(res)
  87. getBind(res.code)
  88. }
  89. })
  90. }
  91. }
  92. const getBind = async (wxcode) => {
  93. const res = await myRequest({
  94. url: '/wanzai/api/wechat/vertifyMessage',
  95. data: {
  96. phone: phone.value,
  97. code: codeValue.value,
  98. wxcode
  99. }
  100. })
  101. // console.log(res)
  102. // 1 代表家长 2 代表学生 3 代表老师
  103. uni.setStorageSync('token', res.data.token)
  104. uni.setStorageSync('userInfo', res.data.user)
  105. uni.navigateTo({
  106. url: '/pages/home/home'
  107. })
  108. }
  109. // 验证表格数据是否符合规范
  110. const verifyValue = () => {
  111. // 手机号码验证规则
  112. const regPhone = /^1[3-9]\d{9}$/
  113. if (!phone.value) {
  114. uni.showToast({
  115. title: '请输入手机号',
  116. icon: 'none'
  117. })
  118. return false
  119. }
  120. if (!regPhone.test(phone.value)) {
  121. uni.showToast({
  122. title: '手机号码格式错误',
  123. icon: 'none'
  124. })
  125. return false
  126. }
  127. if (!codeValue.value) {
  128. uni.showToast({
  129. title: '请输入验证码',
  130. icon: 'none'
  131. })
  132. return false
  133. }
  134. return true
  135. }
  136. const codeChange = (text) => {
  137. tips.value = text
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. .container {
  142. display: flex;
  143. flex-direction: column;
  144. align-items: center;
  145. min-height: 100vh;
  146. background-color: #f8f8fa;
  147. .img {
  148. margin-top: 120rpx;
  149. width: 200rpx;
  150. height: 200rpx;
  151. border-radius: 20rpx;
  152. }
  153. .logo_name {
  154. margin-top: 25rpx;
  155. font-size: 36rpx;
  156. }
  157. .phone {
  158. box-sizing: border-box;
  159. margin-top: 80rpx;
  160. padding: 0 20rpx;
  161. width: 538rpx;
  162. height: 80rpx;
  163. border-radius: 8rpx;
  164. box-shadow: 0 0 10rpx #ccc;
  165. background-color: #fff;
  166. .phone_input {
  167. height: 100%;
  168. }
  169. }
  170. .code {
  171. display: flex;
  172. justify-content: space-between;
  173. align-items: center;
  174. box-sizing: border-box;
  175. margin-top: 40rpx;
  176. padding: 0 20rpx;
  177. width: 538rpx;
  178. height: 80rpx;
  179. border-radius: 8rpx;
  180. box-shadow: 0 0 10rpx #ccc;
  181. background-color: #fff;
  182. .code_input {
  183. width: 55%;
  184. height: 100%;
  185. }
  186. .code_box {
  187. display: flex;
  188. justify-content: center;
  189. align-items: center;
  190. width: 35%;
  191. height: 80rpx;
  192. color: #3c9cff;
  193. font-size: 26rpx;
  194. }
  195. }
  196. .loginBtn {
  197. display: flex;
  198. justify-content: center;
  199. align-items: center;
  200. margin-top: 50rpx;
  201. width: 538rpx;
  202. height: 80rpx;
  203. color: #a0a1a4;
  204. border-radius: 8rpx;
  205. background-color: #e5e6eb;
  206. }
  207. .active {
  208. color: #fff;
  209. background-color: #0061ff;
  210. }
  211. }
  212. </style>