index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <template v-if="changeType === 'login'">
  4. <view class="head-title ss-m-b-30">欢迎来到门墩儿</view>
  5. <view class="ss-m-t-30">
  6. <!-- 企业邮箱+密码登录 -->
  7. <uni-forms
  8. ref="accountLoginRef"
  9. v-model="state.account"
  10. :rules="state.rules"
  11. validateTrigger="bind"
  12. labelWidth="140"
  13. labelAlign="center"
  14. >
  15. <uni-forms-item name="phone" label="企业邮箱">
  16. <uni-easyinput placeholder="请输入企业邮箱" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
  17. </uni-forms-item>
  18. <uni-forms-item name="password" label="登录密码">
  19. <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
  20. </uni-forms-item>
  21. </uni-forms>
  22. <view class="quickLogon">
  23. <view></view>
  24. <view class="register" @tap="handleChangeRegister">还没有登录账户?去注册</view>
  25. </view>
  26. <button class="send-button" @tap="handleLogin"> 登 录 </button>
  27. <view class="agreement-box ss-flex ss-row-center" style="margin-bottom: 30px;">
  28. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  29. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  30. 我已阅读并遵守
  31. <view class="color-primary" @tap.stop="handleToDetail('user')">
  32. 《用户协议》
  33. </view>
  34. <view class="agreement-text">和</view>
  35. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  36. 《隐私协议》
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <template v-if="changeType === 'register'">
  43. <view class="head-title pb-20">手机号注册</view>
  44. <uni-forms
  45. ref="registerForm"
  46. v-model="state.register"
  47. :rules="state.smsRules"
  48. validateTrigger="bind"
  49. labelWidth="140"
  50. labelAlign="center"
  51. >
  52. <uni-forms-item name="phone" label="手机号">
  53. <uni-easyinput placeholder="请输入手机号" v-model="state.register.phone" :inputBorder="false" type="number">
  54. </uni-easyinput>
  55. </uni-forms-item>
  56. <uni-forms-item name="code" label="验证码">
  57. <uni-easyinput
  58. placeholder="请输入验证码"
  59. v-model="state.register.code"
  60. :inputBorder="false"
  61. type="number"
  62. maxlength="6"
  63. >
  64. <template v-slot:right>
  65. <button
  66. class="login-code"
  67. :disabled="state.isMobileEnd"
  68. :class="{ 'code-btn-end': state.isMobileEnd }"
  69. @tap="handleRegisterCode"
  70. >
  71. {{ getSmsTimer('smsRegister') }}
  72. </button>
  73. </template>
  74. </uni-easyinput>
  75. </uni-forms-item>
  76. </uni-forms>
  77. <view class="register login" style="text-align: end;" @tap="handleChangeLogin">已有账户?去登陆</view>
  78. <view>
  79. <button class="send-button" @tap="handleRegister"> 注 册 </button>
  80. </view>
  81. <view class="color-999 ss-flex ss-col-center ss-row-center ss-m-l-8 font-size-13" style="margin-bottom: 30px;">
  82. 点击注册即代表您同意
  83. <view class="color-primary" @tap.stop="handleToDetail('user')">
  84. 《用户协议》
  85. </view>
  86. <view class="agreement-text">和</view>
  87. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  88. 《隐私协议》
  89. </view>
  90. </view>
  91. </template>
  92. </view>
  93. </template>
  94. <script setup>
  95. import { ref, unref } from 'vue'
  96. import { mobile, password, code, emailRequired } from '@/utils/validate'
  97. import { getSmsCode, getSmsTimer } from '@/utils/code'
  98. import { userStore } from '@/store/user'
  99. import { closeAuthModal } from '@/hooks/useModal'
  100. import { getUserRegisterEnterpriseApply, getEnterpriseRegisterApply } from '@/api/enterprise'
  101. const useUserStore = userStore()
  102. const current = ref(0)
  103. const accountLoginRef = ref()
  104. const smsLoginRef = ref()
  105. const registerForm = ref()
  106. const protocol = ref(false)
  107. const state = ref({
  108. isMobileEnd: false, // 手机号输入完毕
  109. codeText: '获取验证码',
  110. sms: {
  111. phone: '',
  112. code: ''
  113. },
  114. register: {
  115. phone: '',
  116. code: ''
  117. },
  118. account: {
  119. phone: '1687284007@qq.com',
  120. password: 'Citu123456'
  121. },
  122. rules: {
  123. phone: emailRequired,
  124. password,
  125. },
  126. smsRules: {
  127. code,
  128. phone: mobile
  129. }
  130. })
  131. // 设置默认账号密码便于开发快捷登录
  132. if (window && window.location && window.location.hostname && window.location.hostname === 'localhost') {
  133. state.value.account.phone = '1687284007@qq.com'
  134. state.value.account.password = 'Citu123456'
  135. }
  136. const changeType = ref('login')
  137. const onClickItem = (e) => {
  138. current.value = e.currentIndex
  139. }
  140. // 获取验证码
  141. const handleCode = () => {
  142. if (!state.value.sms.phone) {
  143. uni.showToast({
  144. title: '请输入手机号',
  145. icon: 'none',
  146. duration: 2000
  147. })
  148. return
  149. }
  150. getSmsCode('smsLogin', state.value.sms.phone)
  151. }
  152. // 获取验证码
  153. const handleRegisterCode = () => {
  154. if (!state.value.register.phone) {
  155. uni.showToast({
  156. title: '请输入手机号',
  157. icon: 'none',
  158. duration: 2000
  159. })
  160. return
  161. }
  162. getSmsCode('smsRegister', state.value.register.phone)
  163. }
  164. // 查看协议详情
  165. const handleToDetail = (type) => {
  166. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  167. uni.navigateTo({
  168. url
  169. })
  170. }
  171. // 注册
  172. function handleChangeRegister () {
  173. changeType.value = 'register'
  174. }
  175. // 登录
  176. function handleChangeLogin () {
  177. changeType.value = 'login'
  178. }
  179. async function handleRegister () {
  180. const validate = await unref(registerForm).validate()
  181. if (!validate) return
  182. const query = state.value.register
  183. Object.assign(query, {
  184. account: query.phone,
  185. autoRegister: true
  186. })
  187. try {
  188. await useUserStore.handleRegister(query)
  189. uni.showToast({
  190. title: '手机号验证成功',
  191. icon: 'none',
  192. duration: 2000
  193. })
  194. const { data } = await getUserRegisterEnterpriseApply()
  195. changeType.value = 'login'
  196. const hasData = data && Object.keys(data).length > 0 && data.status !== '1'
  197. console.log(data, '查看用户是否有在申请中的数据')
  198. uni.navigateTo({
  199. url: hasData ? '/pages/register/review' : '/pages/register/index'
  200. })
  201. } finally {
  202. }
  203. }
  204. const handleCheckEnterprise = async () => {
  205. const { data } = await getEnterpriseRegisterApply(state.value.account.phone)
  206. if (data && Object.keys(data).length) {
  207. // 查看申请状态
  208. uni.setStorageSync('entRegisterData', JSON.stringify(data))
  209. uni.navigateTo({
  210. url: '/pages/register/review?hasData=true'
  211. })
  212. }
  213. closeAuthModal()
  214. }
  215. // 登录
  216. const handleLogin = async () => {
  217. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  218. const validate = await unref(accountLoginRef).validate()
  219. if (!validate) return
  220. const query = state.value.account
  221. Object.assign(query, {
  222. account: query.phone
  223. })
  224. console.log(query, '登录参数')
  225. try {
  226. await useUserStore.handleSmsLogin(query, 1)
  227. closeAuthModal()
  228. } catch (err) {
  229. console.log(err, '登录失败')
  230. if (!err.code || (err?.message && err.message.includes('timeout'))) return closeAuthModal()
  231. if (err.code === 1100017022) {
  232. // 密码不安全
  233. uni.showToast({ title: '您的密码不安全,请重置密码', icon: 'none', duration: 2000 })
  234. closeAuthModal()
  235. return
  236. }
  237. if (err.code === 1100021016) {
  238. // 企业注册申请中
  239. handleCheckEnterprise()
  240. return
  241. }
  242. if (err.code === 1100017019) {
  243. uni.showToast({ title: '您的邮箱还未注册过,请先注册', icon: 'none', duration: 2000 })
  244. changeType.value = 'register'
  245. }
  246. }
  247. }
  248. </script>
  249. <style scoped lang="scss">
  250. .login-code {
  251. width: 73px;
  252. min-width: 73px;
  253. color: #00B760;
  254. text-align: center;
  255. font-size: 12px;
  256. cursor: pointer;
  257. // border: 1px dashed #00B760;
  258. // border-radius: 26px;
  259. padding: 0;
  260. }
  261. .head-title {
  262. font-size: 40rpx;
  263. text-align: center;
  264. color: #00B760;
  265. margin-top: 30rpx;
  266. }
  267. .quickLogon {
  268. display: flex;
  269. justify-content: space-between;
  270. padding: 0 20rpx;
  271. align-items: center;
  272. }
  273. .wxLogon {
  274. font-size: .85em;
  275. color: #00B760;
  276. border: none;
  277. margin: 0;
  278. padding: 0;
  279. }
  280. .register {
  281. widows: 100%;
  282. font-size: .85em;
  283. color: red;
  284. // text-align: right;
  285. &.login {
  286. color: #00B760;
  287. }
  288. }
  289. .pb-20 {
  290. padding-bottom: 40rpx;
  291. }
  292. </style>