index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <script setup lang="ts">
  2. import { createGlobalLoadingMiddleware } from '@/api/core/middleware'
  3. import router from '@/router'
  4. import { StaticUrl } from '@/config'
  5. definePage({
  6. name: 'smqjh-login',
  7. islogin: false,
  8. style: {
  9. navigationBarTitleText: '市民请集合',
  10. },
  11. })
  12. const { token, redirectName, userInfo } = storeToRefs(useUserStore())
  13. const { data, send } = useRequest((code, phoneCode) => Apis.sys.auth({ params: {
  14. grant_type: 'wechat',
  15. code,
  16. phoneCode,
  17. } }), { middleware: createGlobalLoadingMiddleware({ loadingText: '手机号授权登录中' }), immediate: false })
  18. const tabList = ['/pages/my/index', '/pages/index/index', '/pages/cart/index', '/pages/classfiy/index']
  19. // 表单数据
  20. const phone = ref('')
  21. const code = ref('')
  22. // 验证码倒计时
  23. const countdown = ref(0)
  24. const isSending = ref(false)
  25. // 发送验证码
  26. async function handleSendCode() {
  27. if (!phone.value) {
  28. useGlobalToast().show('请输入手机号')
  29. return
  30. }
  31. if (!/^1[3-9]\d{9}$/.test(phone.value)) {
  32. useGlobalToast().show('请输入正确的手机号')
  33. return
  34. }
  35. if (isSending.value)
  36. return
  37. isSending.value = true
  38. countdown.value = 60
  39. const timer = setInterval(() => {
  40. countdown.value--
  41. if (countdown.value <= 0) {
  42. clearInterval(timer)
  43. isSending.value = false
  44. }
  45. }, 1000)
  46. // TODO: 调用发送验证码接口
  47. await Apis.general.post_smqjh_auth_api_v1_auth_sms_code({ params: { mobile: phone.value } })
  48. useGlobalToast().show('验证码已发送')
  49. }
  50. // 登录
  51. async function handleLogin() {
  52. if (!phone.value) {
  53. useGlobalToast().show('请输入手机号')
  54. return
  55. }
  56. if (!code.value) {
  57. useGlobalToast().show('请输入验证码')
  58. return
  59. }
  60. uni.showLoading({ mask: true })
  61. try {
  62. const res = await Apis.general.post_smqjh_auth_oauth2_token({ params: { grant_type: 'sms_code', mobile: phone.value, code: code.value } })
  63. uni.hideLoading()
  64. token.value = `Bearer ${res.data.access_token}`
  65. useGlobalToast().show('登录成功')
  66. const user = await Apis.general.get_smqjh_system_app_api_v1_members_me()
  67. userInfo.value = user.data as Api.userInfo
  68. setTimeout(() => {
  69. if (tabList.includes(redirectName.value)) {
  70. router.pushTab({ path: redirectName.value })
  71. }
  72. else {
  73. router.replace({ path: redirectName.value })
  74. }
  75. }, 1000)
  76. }
  77. catch {
  78. uni.hideLoading()
  79. }
  80. }
  81. async function handleGetPhone(e: UniHelper.ButtonOnGetphonenumberDetail) {
  82. console.log(e, '手机号')
  83. uni.showLoading({ mask: true })
  84. uni.login({
  85. provider: 'weixin',
  86. success: async (res) => {
  87. uni.hideLoading()
  88. await send(res.code, e.code)
  89. token.value = `Bearer ${data.value.data.access_token}`
  90. useGlobalToast().show({ msg: '登录成功' })
  91. useUserStore().getUserInfo()
  92. setTimeout(() => {
  93. if (tabList.includes(redirectName.value)) {
  94. router.pushTab({ path: redirectName.value })
  95. }
  96. else {
  97. console.log(redirectName.value, 'JSON.parse(path[1])')
  98. router.replace({ path: redirectName.value })
  99. }
  100. }, 1000)
  101. },
  102. })
  103. }
  104. function goBack() {
  105. uni.switchTab({
  106. url: '/pages/index/index',
  107. })
  108. useTabbar().setTabbarItemActive('smqjh-home')
  109. }
  110. </script>
  111. <template>
  112. <view>
  113. <!-- #ifdef MP-WEIXIN -->
  114. <view class="header px24rpx">
  115. <view class="pt150rpx">
  116. <view class="w-full flex items-center justify-center">
  117. <image
  118. :src="`${StaticUrl}/smqjh-logo.png`"
  119. class="h120rpx w120rpx"
  120. />
  121. </view>
  122. <view class="mt50rpx">
  123. <wd-button block size="large" open-type="getPhoneNumber" @getphonenumber="handleGetPhone">
  124. 手机号快捷登录
  125. </wd-button>
  126. <view class="mt40rpx">
  127. <wd-button block size="large" type="info" @click="goBack">
  128. 暂不登录
  129. </wd-button>
  130. </view>
  131. </view>
  132. </view>
  133. </view>
  134. <!-- #endif -->
  135. <!-- #ifdef H5 -->
  136. <view class="login-page min-h-100vh px48rpx">
  137. <view class="w-full flex items-center justify-center pt40rpx">
  138. <image
  139. :src="`${StaticUrl}/smqjh-logo.png`"
  140. class="h120rpx w120rpx"
  141. />
  142. </view>
  143. <!-- 表单区域 -->
  144. <view class="pt242rpx">
  145. <!-- 手机号 -->
  146. <view class="mb48rpx">
  147. <view class="mb24rpx text-32rpx text-#333 font-medium">
  148. 手机号
  149. </view>
  150. <view class="h96rpx flex items-center rounded-48rpx bg-#E5E8D8 px32rpx">
  151. <input
  152. v-model="phone"
  153. type="number"
  154. :maxlength="11"
  155. placeholder="请输入手机号"
  156. placeholder-class="text-#999"
  157. class="flex-1 text-32rpx"
  158. >
  159. </view>
  160. </view>
  161. <!-- 验证码 -->
  162. <view class="mb64rpx">
  163. <view class="mb24rpx text-32rpx text-#333 font-medium">
  164. 验证码
  165. </view>
  166. <view class="h96rpx flex items-center rounded-48rpx bg-#E5E8D8 px32rpx">
  167. <input
  168. v-model="code"
  169. type="tel"
  170. :maxlength="4"
  171. placeholder="请输入验证码"
  172. placeholder-class="text-#999"
  173. class="flex-1 text-32rpx"
  174. >
  175. <view
  176. class="ml20rpx flex-shrink-0 text-28rpx"
  177. :class="[isSending ? 'text-#999' : 'text-#7CB305']"
  178. @click="handleSendCode"
  179. >
  180. {{ isSending ? `${countdown}秒后重新发送` : '获取验证码' }}
  181. </view>
  182. </view>
  183. </view>
  184. <!-- 登录按钮 -->
  185. <wd-button block size="large" @click="handleLogin">
  186. 登录
  187. </wd-button>
  188. <view class="mt20rpx text-center text-#999">
  189. 请使用市民请集合小程序账号进行登录
  190. </view>
  191. </view>
  192. </view>
  193. <!-- #endif -->
  194. </view>
  195. </template>
  196. <style scoped>
  197. .header{
  198. height: calc(100vh - var(--window-top));
  199. }
  200. .login-page {
  201. background: linear-gradient( 180deg, #F3FFD1 0%, #FFFFFF 37.42%, #F3FFD1 100%);
  202. }
  203. </style>