initial.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="app-login-page">
  3. <view class="app-login">
  4. <view class="app-icon u-m-b-30 ">
  5. <image src="http://www.gzrea.org.cn:8543/icon/wxmp/login-icon.png"
  6. mode="aspectFit" style="height: 80rpx;"></image>
  7. </view>
  8. <view class="app-title u-m-b-60">
  9. 欢迎使用会员中心系统小程序
  10. </view>
  11. <view style="height: 25vh;">
  12. <u-button v-if="userBook" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" class="custom-button" style="width: 40px;" plain :hair-line="false">
  13. <view class="login-button">
  14. 本机号码一键登录
  15. </view>
  16. </u-button>
  17. <u-button v-else class="custom-button" style="width: 40px;" plain :hair-line="false" @click="check">
  18. <view class="login-button">
  19. 本机号码一键登录
  20. </view>
  21. </u-button>
  22. <view class="login-button other-button u-m-b-50 u-m-t-50" @click="toLogin">
  23. 其他手机号/账号登录
  24. </view>
  25. </view>
  26. <view class="app-user-book mb-30">
  27. <u-checkbox-group style="display: flex;justify-content: center;">
  28. <u-checkbox v-model="userBook" name="yes" shape="circle">我已阅读&#x300A;运营商一键登录服务协议&#x300B;</u-checkbox>
  29. </u-checkbox-group>
  30. </view>
  31. </view>
  32. <u-modal v-model="userBookModal" mask-close-able class="modal" show-cancel-button
  33. :title-style="{fontWeight: 'bold'}" @confirm="confirmModal">
  34. <view class="modal-content">
  35. 为了更好地保障你的合法权益,进行下一步前,请阅读并同意
  36. <text class="content-a">&#x300A;运营商一键登录服务协议&#x300B;</text>
  37. </view>
  38. </u-modal>
  39. </view>
  40. </template>
  41. <script setup>
  42. import { wxLoginOpenid, wxloginPhone } from '@/api/login.js'
  43. import {
  44. msg,
  45. msgSuccess
  46. } from "@/utils/common.js"
  47. import { ref } from 'vue'
  48. import {
  49. useAuthStore
  50. } from '@/store/authStore'
  51. import {
  52. me
  53. } from '@/api/user.js'
  54. const authStore = useAuthStore()
  55. const userBook = ref(false)
  56. const userBookModal = ref(false)
  57. function toLogin() {
  58. uni.navigateTo({
  59. url: "/pages/login/login"
  60. })
  61. }
  62. async function wxlogin(){
  63. const loginRes = await wx.login();
  64. const code = loginRes.code;
  65. wxLoginOpenid({code}).then(res=>{
  66. if(res?.data){
  67. const { openid, sessionKey } = res.data
  68. }
  69. })
  70. }
  71. function confirmModal() {
  72. userBook.value = true
  73. // this.wxlogin()
  74. }
  75. function checkUserBook() {
  76. if (!userBook.value) {
  77. userBookModal.value = true
  78. return false;
  79. }
  80. return true
  81. }
  82. function check(){
  83. if (!checkUserBook()) return;
  84. }
  85. async function decryptPhoneNumber(e){
  86. if (e.detail.encryptedData) {
  87. const {
  88. encryptedData,
  89. iv,
  90. code
  91. } = e.detail;
  92. const loginRes = await wx.login();
  93. const wxcode = loginRes.code;
  94. try{
  95. const openidRes = await wxLoginOpenid({code : wxcode})
  96. if(openidRes?.data){
  97. const { openid, sessionKey } = openidRes.data
  98. authStore.setOpenid(openid);
  99. const phoneRes = await wxloginPhone({ openid, phoneCode: code})
  100. const { phone, token, isNewUser } = phoneRes.data;
  101. authStore.setPhone(phone);
  102. authStore.setAuthToken(token);
  103. // uni.reLaunch({
  104. // url: "/pages/register/register?mode=refine"
  105. // })
  106. if(isNewUser){
  107. //新用户,跳转:完善用户信息
  108. // console.log("完善用户信息")
  109. uni.reLaunch({
  110. url: "/pages/register/register?mode=refine"
  111. })
  112. }else{
  113. // 老用户,跳转至:首页
  114. msgSuccess("授权成功!")
  115. let userRes = await me();
  116. if(userRes && userRes?.code===0){
  117. authStore.setUserInfo(userRes.data)
  118. setTimeout(()=>{
  119. uni.switchTab({
  120. url: "/pages/index/index"
  121. })
  122. }, 1000)
  123. }
  124. }
  125. }
  126. }catch(err){
  127. console.log("ee", err)
  128. }
  129. // 将 encryptedData 和 iv 发送到后端进行解密
  130. } else {
  131. msg('取消授权手机号');
  132. }
  133. }
  134. </script>
  135. <style lang="scss">
  136. .form-item .u-input--border {
  137. border-radius: 16rpx !important;
  138. }
  139. .custom-button {
  140. .u-btn {
  141. display: block !important;
  142. width: fit-content;
  143. padding: 0 !important;
  144. border-radius: 50rpx;
  145. }
  146. .u-btn--default {
  147. border: none !important;
  148. background-color: transparent !important;
  149. }
  150. }
  151. </style>
  152. <style lang="scss" scoped>
  153. .app-login-page {
  154. background-color: #ffffff;
  155. width: 100vw;
  156. height: 100vh;
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. }
  161. .app-login {
  162. width: 90%;
  163. }
  164. .app-icon {
  165. display: flex;
  166. justify-content: center;
  167. align-items: center;
  168. height: 90rpx;
  169. text-align: center;
  170. }
  171. .app-title {
  172. text-align: center;
  173. font-size: 44rpx;
  174. letter-spacing: 2px;
  175. font-weight: 700;
  176. }
  177. .mb-30 {
  178. margin-bottom: 30rpx;
  179. }
  180. .login-button {
  181. background: linear-gradient(90deg, #006af5, #21b0fc);
  182. border-radius: 20rpx;
  183. line-height: 80rpx;
  184. text-align: center;
  185. color: #fff;
  186. margin-bottom: 50rpx;
  187. width: 550rpx;
  188. margin: 0 auto;
  189. font-size: 32rpx;
  190. letter-spacing: 4rpx;
  191. }
  192. .other-button{
  193. color: #006af5;
  194. background: #e5e5e5;
  195. }
  196. // 用户手册模拟态
  197. .modal {
  198. .modal-content {
  199. padding: 50rpx;
  200. .content-a {
  201. color: #2979ff;
  202. }
  203. }
  204. }
  205. </style>