login.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. loginType: 1
  103. }
  104. })
  105. // console.log(res)
  106. const msg = JSON.parse(decryptDes(res.data))
  107. // console.log(msg)
  108. uni.setStorageSync('allData', msg)
  109. if (msg.user.length > 1) {
  110. let info = encodeURIComponent(JSON.stringify(msg))
  111. uni.reLaunch({
  112. url: `/pages/switch/switch?info=${info}`
  113. })
  114. } else {
  115. // 1 代表家长 2 代表学生 3 代表老师
  116. uni.setStorageSync('token', msg.token)
  117. uni.setStorageSync('userhead', msg.user[0].userhead)
  118. uni.setStorageSync('userInfo', msg.user[0])
  119. uni.reLaunch({
  120. url: '/pages/home/home'
  121. })
  122. }
  123. }
  124. // 验证表格数据是否符合规范
  125. const verifyValue = () => {
  126. // 手机号码验证规则
  127. const regPhone = /^1[3-9]\d{9}$/
  128. if (!phone.value) {
  129. uni.showToast({
  130. title: '请输入手机号',
  131. icon: 'none'
  132. })
  133. return false
  134. }
  135. if (!regPhone.test(phone.value)) {
  136. uni.showToast({
  137. title: '手机号码格式错误',
  138. icon: 'none'
  139. })
  140. return false
  141. }
  142. if (!codeValue.value) {
  143. uni.showToast({
  144. title: '请输入验证码',
  145. icon: 'none'
  146. })
  147. return false
  148. }
  149. return true
  150. }
  151. const codeChange = (text) => {
  152. tips.value = text
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. .container {
  157. display: flex;
  158. flex-direction: column;
  159. align-items: center;
  160. min-height: 100vh;
  161. background-color: #f8f8fa;
  162. .img {
  163. margin-top: 120rpx;
  164. width: 200rpx;
  165. height: 200rpx;
  166. border-radius: 20rpx;
  167. }
  168. .logo_name {
  169. margin-top: 25rpx;
  170. font-size: 36rpx;
  171. }
  172. .phone {
  173. box-sizing: border-box;
  174. margin-top: 80rpx;
  175. padding: 0 20rpx;
  176. width: 538rpx;
  177. height: 80rpx;
  178. border-radius: 8rpx;
  179. box-shadow: 0 0 10rpx #ccc;
  180. background-color: #fff;
  181. .phone_input {
  182. height: 100%;
  183. }
  184. }
  185. .code {
  186. display: flex;
  187. justify-content: space-between;
  188. align-items: center;
  189. box-sizing: border-box;
  190. margin-top: 40rpx;
  191. padding: 0 20rpx;
  192. width: 538rpx;
  193. height: 80rpx;
  194. border-radius: 8rpx;
  195. box-shadow: 0 0 10rpx #ccc;
  196. background-color: #fff;
  197. .code_input {
  198. width: 55%;
  199. height: 100%;
  200. }
  201. .code_box {
  202. display: flex;
  203. justify-content: center;
  204. align-items: center;
  205. width: 35%;
  206. height: 80rpx;
  207. color: #3c9cff;
  208. font-size: 26rpx;
  209. }
  210. }
  211. .loginBtn {
  212. display: flex;
  213. justify-content: center;
  214. align-items: center;
  215. margin-top: 50rpx;
  216. width: 538rpx;
  217. height: 80rpx;
  218. color: #a0a1a4;
  219. border-radius: 8rpx;
  220. background-color: #e5e6eb;
  221. }
  222. .active {
  223. color: #fff;
  224. background-color: #0061ff;
  225. }
  226. }
  227. </style>