clientIndex.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. <view v-if="fromAppShare" class="wechat-auth-box">
  9. <view class="auth-tips">欢迎使用小程序</view>
  10. <button class="wechat-auth-btn" open-type="getPhoneNumber" @getphonenumber="onWechatAuth">
  11. 微信一键登录
  12. </button>
  13. <button @click="handleChangeFangshi">
  14. 切换微信一键登录
  15. </button>
  16. </view>
  17. <!-- <view @click="ceshi" class="auth-tips">欢迎使用小程序12312312312312</view> -->
  18. </view>
  19. </view>
  20. </template>
  21. <script setup>
  22. import cacheManager from '@/utils/cacheManager.js'
  23. import * as httpApi from "@/api/login.js"
  24. import {
  25. ref,
  26. onMounted
  27. } from "vue"
  28. import {
  29. toast
  30. } from "@/utils/common";
  31. import {
  32. useIsCanBack
  33. } from "@/store/isCanBack.js"
  34. import {
  35. onLoad
  36. } from "@dcloudio/uni-app";
  37. // 原有变量
  38. const userName = ref('')
  39. const password = ref('')
  40. const showPassword = ref(true)
  41. const clearTelIcon = ref(false)
  42. const clearPwIcon = ref(false)
  43. const isAgreed = ref(false)
  44. const id = ref('')
  45. const code = ref('')
  46. const fromPage = ref('');
  47. const openId = ref('');
  48. // 新增变量
  49. const fromAppShare = ref(false) // 是否来自APP分享
  50. const store = useIsCanBack();
  51. onLoad((options) => {
  52. if (options.from === 'appcx') {
  53. id.value = options.id
  54. fromPage.value = options.from;
  55. }
  56. uni.login().then(res1 => {
  57. console.log('res1', res1);
  58. code.value = res1.code
  59. httpApi.loginApplet({
  60. code: res1.code,
  61. }).then(result => {
  62. console.log('result', result);
  63. openId.value = result.data.openId
  64. if (result.data.bind) {
  65. // 保存用户信息
  66. if (result.data.loginVo.type == 4) {
  67. cacheManager.set('auth', result.data.loginVo)
  68. store.setIsCanBack(false)
  69. gotoPage();
  70. } else if (result.data.loginVo.type == 6) {
  71. cacheManager.set('auth', result.data.loginVo)
  72. store.setIsCanBack(false)
  73. gotoPage2();
  74. } else {
  75. toast('登录失败,您的身份有误,请联系管理员。')
  76. return
  77. }
  78. } else {
  79. fromAppShare.value = true
  80. }
  81. })
  82. })
  83. getAllImg();
  84. })
  85. // function ceshi() {
  86. // httpApi.loginApplet({
  87. // code: '123',
  88. // }).then(result => {
  89. // console.log('result', result);
  90. // openId.value = result.data.openId
  91. // cacheManager.set('auth', result.data.loginVo)
  92. // store.setIsCanBack(false)
  93. // gotoPage();
  94. // })
  95. // }
  96. // 微信授权登录
  97. function onWechatAuth(e) {
  98. if (e.detail.errMsg.includes('fail')) {
  99. toast('授权失败')
  100. return
  101. }
  102. uni.showLoading({
  103. title: '登录中'
  104. })
  105. console.log('e123123123', e);
  106. try {
  107. httpApi.bindApplet({
  108. code: e.detail.code,
  109. openId: openId.value,
  110. }).then(result => {
  111. if (result.data.type == 4) {
  112. cacheManager.set('auth', result.data)
  113. store.setIsCanBack(false)
  114. gotoPage();
  115. } else if (result.data.type == 6) {
  116. cacheManager.set('auth', result.data)
  117. store.setIsCanBack(false)
  118. gotoPage2();
  119. } else {
  120. toast('登录失败,您的身份有误,请联系管理员。')
  121. }
  122. })
  123. } catch (error) {
  124. toast('bindApplet失败')
  125. } finally {
  126. uni.hideLoading()
  127. }
  128. }
  129. function gotoPage() {
  130. console.log('ididid', id.value);
  131. if (id.value) {
  132. uni.navigateTo({
  133. url: `/pages/client/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  134. })
  135. } else {
  136. uni.navigateTo({
  137. url: `/pages/client/ShouYe/shouye`
  138. })
  139. }
  140. }
  141. function gotoPage2() {
  142. console.log('ididid222222', id.value);
  143. if (id.value) {
  144. uni.navigateTo({
  145. url: `/pages/kehu/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
  146. })
  147. } else {
  148. uni.navigateTo({
  149. url: `/pages/kehu/shouye/shouye`
  150. })
  151. }
  152. }
  153. function getAllImg() {
  154. httpApi.getAllImgList({}).then(res => {
  155. cacheManager.set('projectImg', res.data)
  156. });
  157. }
  158. function handleChangeFangshi() {
  159. let str = ``;
  160. if (id.value) {
  161. str = str+`id=${id.value}`
  162. }
  163. if ( id.value && fromPage.value ) {
  164. str = str +`&from=${fromPage.value}`
  165. } else if (!id.value && fromPage.value) {
  166. str = str + `from=${fromPage.value}`
  167. }
  168. if (str) {
  169. uni.redirectTo({
  170. url: `/pages/Login/test?${str}`
  171. })
  172. } else {
  173. uni.redirectTo({
  174. url: `/pages/Login/test`
  175. })
  176. }
  177. }
  178. </script>
  179. <style scoped>
  180. /* 新增样式 */
  181. .wechat-auth-box {
  182. text-align: center;
  183. padding: 40rpx 0;
  184. }
  185. .auth-tips {
  186. font-size: 32rpx;
  187. color: #333;
  188. margin-bottom: 60rpx;
  189. }
  190. .wechat-auth-btn {
  191. background-color: #07C160;
  192. color: white;
  193. border-radius: 50rpx;
  194. margin-bottom: 30rpx;
  195. }
  196. .switch-login {
  197. color: #007AFF;
  198. font-size: 28rpx;
  199. }
  200. /* 你原有的所有样式都保持不变 */
  201. </style>