login.vue 4.6 KB

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