clientIndex.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <view class="phone-login-page">
  3. <view class="login-wrap-box">
  4. <view class="bjcx-head-box">
  5. <icon class="bjcx-logo-box"></icon>
  6. <view class="bjcx-logo-title">家政学</view>
  7. </view>
  8. <!-- 如果是APP分享过来的,显示微信授权界面 @getuserinfo="onWechatAuth"-->
  9. <view v-if="fromAppShare" class="wechat-auth-box">
  10. <view class="auth-tips">欢迎使用小程序</view>
  11. <button class="wechat-auth-btn" open-type="getUserInfo"@getuserinfo="onWechatAuth">
  12. 微信一键登录
  13. </button>
  14. <view class="switch-login" @click="switchToNormalLogin">使用账号登录</view>
  15. </view>
  16. <!-- 普通登录表单 -->
  17. <view v-else>
  18. <view class="login-input-box">
  19. <icon class="user-icon"></icon>
  20. <input class="login-input" type="text" v-model="userName" placeholder="请输入手机号"
  21. @input="userInputChange">
  22. <view class="close-btn" v-if="clearTelIcon" @click="clearTel"></view>
  23. </view>
  24. <view class="login-input-box">
  25. <icon class="tel-icon"></icon>
  26. <input class="login-input" placeholder="请输入证件号后六位" v-model="password" :password="showPassword"
  27. @input="passwordInputChange" />
  28. <text class="login-eye" :class="[!showPassword ? 'uni-eye-active' : '']"
  29. @click="changePassword"></text>
  30. <view class="close-btn" v-if="clearPwIcon" @click="clearPw"></view>
  31. </view>
  32. <!-- 协议勾选框和按钮 -->
  33. <view class="agreement-checkbox-box">
  34. <checkbox-group @change="handleChange">
  35. <checkbox class="agreement-checkbox-input" color="#3fd2a1" value="agree" :checked="isAgreed"
  36. style="transform:scale(0.7)" />
  37. </checkbox-group>
  38. <view class="agreement-text-box">
  39. 在使用当前小程序服务之前,请仔细阅读<view class="agreement-text" @click="agreeBtn('yhxy')">《诚祥学用户协议》</view>和<view
  40. @click="agreeBtn('ystk')" class="agreement-text">《诚祥学隐私政策》</view>,如您同意,请勾选后开始使用
  41. </view>
  42. </view>
  43. <button type="default" @click="handleLogin" :disabled="!isAgreed"
  44. class="phone-green-btn login-btn">登录</button>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script setup>
  50. import cacheManager from '@/utils/cacheManager.js'
  51. import * as httpApi from "@/api/login.js"
  52. import {
  53. ref,
  54. onMounted
  55. } from "vue"
  56. import {
  57. toast
  58. } from "@/utils/common";
  59. import {
  60. useIsCanBack
  61. } from "@/store/isCanBack.js"
  62. import {
  63. onLoad
  64. } from "@dcloudio/uni-app";
  65. // 原有变量
  66. const userName = ref('')
  67. const password = ref('')
  68. const showPassword = ref(true)
  69. const clearTelIcon = ref(false)
  70. const clearPwIcon = ref(false)
  71. const isAgreed = ref(false)
  72. const id = ref('')
  73. const fromPage = ref('');
  74. // 新增变量
  75. const fromAppShare = ref(false) // 是否来自APP分享
  76. const store = useIsCanBack();
  77. onLoad((options) => {
  78. // 判断是否来自APP分享
  79. if (options.from === 'appcx') {
  80. fromAppShare.value = true
  81. id.value = options.id
  82. // APP分享过来直接显示微信授权界面,不需要自动弹窗
  83. fromPage.value = options.from;
  84. }
  85. getAllImg();
  86. })
  87. // 切换到普通登录
  88. function switchToNormalLogin() {
  89. fromAppShare.value = false
  90. }
  91. function phoneBind() {
  92. uni.navigateTo({
  93. url: '/pages/Login/AliyunCaptcha',
  94. events: {
  95. getCaptchaVerifyParam: (captchaVerifyParam) => {
  96. // 接收到验证码返回结果
  97. console.log('验证码返回结果:', captchaVerifyParam)
  98. }
  99. },
  100. success: () => {
  101. console.log('跳转到验证码页面成功')
  102. },
  103. fail: (err) => {
  104. console.error('跳转到验证码页面失败:', err)
  105. toast('验证码加载失败,请重试')
  106. }
  107. })
  108. }
  109. // 微信授权登录
  110. async function onWechatAuth(e) {
  111. if (e.detail.errMsg.includes('fail')) {
  112. toast('授权失败')
  113. return
  114. }
  115. uni.showLoading({
  116. title: '登录中'
  117. })
  118. console.log('e', e);
  119. try {
  120. // 获取微信code
  121. const loginRes = await uni.login()
  122. console.log('loginRes', loginRes);
  123. // 调用后端微信登录接口
  124. const result = await httpApi.loginApplet({
  125. code: loginRes.code,
  126. userInfo: e.detail.userInfo
  127. })
  128. console.log('result', result);
  129. console.log('result', result);
  130. if (result.data.bind) {
  131. // 保存用户信息
  132. if (result.data.loginVo.type == 4) {
  133. cacheManager.set('auth', result.data.loginVo)
  134. store.setIsCanBack(false)
  135. gotoPage();
  136. } else if (result.data.loginVo.type == 6) {
  137. cacheManager.set('auth', result.data.loginVo)
  138. store.setIsCanBack(false)
  139. gotoPage2();
  140. } else {
  141. toast('登录失败,您的身份有误,请联系管理员。')
  142. }
  143. } else {
  144. // bind 为 false 未绑定
  145. phoneBind()
  146. }
  147. } catch (error) {
  148. toast('微信登录失败')
  149. } finally {
  150. uni.hideLoading()
  151. }
  152. }
  153. // 你原有的所有方法都不变
  154. function handleChange() {
  155. isAgreed.value = !isAgreed.value
  156. }
  157. function agreeBtn(code) {
  158. if (code === 'yhxy') {
  159. uni.navigateTo({
  160. url: "/pages/client/my/xieyi"
  161. })
  162. } else {
  163. uni.navigateTo({
  164. url: "/pages/client/my/zhengce"
  165. })
  166. }
  167. }
  168. function userInputChange(event) {
  169. if (event.detail.value.length > 0) {
  170. clearTelIcon.value = true;
  171. } else {
  172. clearTelIcon.value = false;
  173. }
  174. }
  175. function passwordInputChange(event) {
  176. if (event.detail.value.length > 0) {
  177. clearPwIcon.value = true;
  178. } else {
  179. clearPwIcon.value = false;
  180. }
  181. }
  182. function clearTel() {
  183. userName.value = '';
  184. clearTelIcon.value = false;
  185. }
  186. function clearPw() {
  187. password.value = '';
  188. clearPwIcon.value = false;
  189. }
  190. function changePassword() {
  191. showPassword.value = !showPassword.value;
  192. }
  193. function handleLogin() {
  194. if (userName.value.length === 0) {
  195. toast('请输入手机号!')
  196. return
  197. }
  198. if (password.value.length === 0) {
  199. toast('请输入密码!')
  200. return
  201. }
  202. const trimmedUserName = userName.value;
  203. const trimmedPassword = password.value;
  204. uni.showLoading({
  205. title: '登录中'
  206. })
  207. httpApi.loginTemp({
  208. type: 2,
  209. userName: trimmedUserName,
  210. password: trimmedPassword,
  211. }).then(res => {
  212. if (res.data.type === 4) {
  213. cacheManager.set('auth', res.data)
  214. store.setIsCanBack(false)
  215. gotoPage();
  216. } else if (res.data.type === 6) {
  217. cacheManager.set('auth', res.data)
  218. store.setIsCanBack(false)
  219. gotoPage2();
  220. } else {
  221. toast('登录失败,您的身份有误,请联系管理员。')
  222. }
  223. }).catch(err => {
  224. store.setIsCanBack(true)
  225. }).finally(err => {
  226. uni.hideLoading()
  227. })
  228. }
  229. function gotoPage() {
  230. console.log('ididid',id.value);
  231. if (id.value) {
  232. uni.navigateTo({
  233. url: `/pages/client/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  234. })
  235. } else {
  236. uni.navigateTo({
  237. url: `/pages/client/ShouYe/shouye`
  238. })
  239. }
  240. }
  241. function gotoPage2() {
  242. console.log('ididid222222',id.value);
  243. if (id.value) {
  244. uni.navigateTo({
  245. url: `/pages/kehu/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  246. })
  247. } else {
  248. uni.navigateTo({
  249. url: `/pages/kehu/shouYe/shouye`
  250. })
  251. }
  252. }
  253. function getAllImg() {
  254. httpApi.getAllImgList({}).then(res => {
  255. cacheManager.set('projectImg', res.data)
  256. });
  257. }
  258. </script>
  259. <style scoped>
  260. /* 新增样式 */
  261. .wechat-auth-box {
  262. text-align: center;
  263. padding: 40rpx 0;
  264. }
  265. .auth-tips {
  266. font-size: 32rpx;
  267. color: #333;
  268. margin-bottom: 60rpx;
  269. }
  270. .wechat-auth-btn {
  271. background-color: #07C160;
  272. color: white;
  273. border-radius: 50rpx;
  274. margin-bottom: 30rpx;
  275. }
  276. .switch-login {
  277. color: #007AFF;
  278. font-size: 28rpx;
  279. }
  280. /* 你原有的所有样式都保持不变 */
  281. </style>