初始化小程序端
This commit is contained in:
@@ -1,18 +1,247 @@
|
||||
// logs.js
|
||||
import { authorizeBatch } from '../../utils/authorize'
|
||||
const util = require('../../utils/util.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
logs: []
|
||||
},
|
||||
isAgree: false,
|
||||
openid:"",
|
||||
showAuthModal: false,
|
||||
userInfo: null,
|
||||
needReg: false,
|
||||
openid: '',
|
||||
avatarUrl: '/images/Blvlogo.png',
|
||||
nickName: '',
|
||||
form: {
|
||||
name: '',
|
||||
phone: ''
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
this.handleLogin()
|
||||
},
|
||||
onAvatar(e) {
|
||||
////////debugger
|
||||
this.setData({ avatarUrl: e.detail.avatarUrl }) // 用户选的头像
|
||||
},
|
||||
onNick(e) {
|
||||
this.setData({ nickName: e.detail.value }) // 用户输入的昵称
|
||||
},
|
||||
|
||||
|
||||
|
||||
inputName(e) {this.setData({'form.name': e.detail.value})},
|
||||
inputPhone(e) {this.setData({'form.phone': e.detail.value})},
|
||||
async submitReg() {
|
||||
|
||||
try {
|
||||
wx.showLoading({ title: '登录中...', mask: true });
|
||||
|
||||
// 获取微信code
|
||||
const loginRes = await new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: resolve,
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
|
||||
if (!loginRes.code) {
|
||||
throw new Error('获取登录凭证失败');
|
||||
}
|
||||
const nickName = this.data.nickName;
|
||||
let form =this.data.form
|
||||
let openid =this.data.openid
|
||||
let avatarUrl =this.data.avatarUrl
|
||||
// 发送到后端
|
||||
const apiRes = await new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: 'https://wx-xcx-check.blv-oa.com:4433/api/Login/Register',
|
||||
method: 'POST',
|
||||
data: {
|
||||
UserName:form.name ,
|
||||
UserKey: openid,
|
||||
WeChatName:nickName,
|
||||
PhoneNumber:form.phone,
|
||||
AvatarUrl:avatarUrl
|
||||
},
|
||||
|
||||
success: res =>{
|
||||
if (res.data.success && res.data.data.userName && res.data.data.weChatName && res.data.data.phoneNumber) {
|
||||
wx.setStorageSync('openid', res.data.data.userKey);
|
||||
this.setData({openid:res.data.data.userKey});
|
||||
wx.navigateTo({url: '/pages/chat/chat'});
|
||||
}
|
||||
else{
|
||||
wx.showToast({
|
||||
title: '登录失败:'+ res.data.message,
|
||||
icon: 'none'
|
||||
});
|
||||
this.setData({needReg: true});
|
||||
}
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
wx.showToast({
|
||||
title: `登录失败: ${error.message || '未知错误'}`,
|
||||
icon: 'none'
|
||||
});
|
||||
} finally {
|
||||
wx.hideLoading();
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
onAgreeChange(e) {
|
||||
|
||||
console.log(e)
|
||||
this.setData({ isAgree: e.detail.value.length > 0 });
|
||||
},
|
||||
|
||||
openContract() {
|
||||
wx.openPrivacyContract({
|
||||
success: () => console.log('已打开协议'),
|
||||
fail: (e) => console.log('打开失败', e)
|
||||
})
|
||||
},
|
||||
// 添加新方法
|
||||
|
||||
// 显示授权弹窗
|
||||
showAuthModal() {
|
||||
this.setData({ showAuthModal: true });
|
||||
},
|
||||
|
||||
// 取消授权
|
||||
onAuthCancel() {
|
||||
this.setData({ showAuthModal: false });
|
||||
wx.showToast({ title: '授权已取消', icon: 'none' });
|
||||
},
|
||||
// 获取用户信息
|
||||
async onGetUserInfo(e) {
|
||||
try {
|
||||
if (e.detail.errMsg !== 'getUserInfo:ok') {
|
||||
throw new Error('用户拒绝了授权');
|
||||
}
|
||||
|
||||
const userInfo = e.detail.userInfo;
|
||||
this.setData({
|
||||
logs: (wx.getStorageSync('logs') || []).map(log => {
|
||||
return {
|
||||
date: util.formatTime(new Date(log)),
|
||||
timeStamp: log
|
||||
}
|
||||
})
|
||||
userInfo,
|
||||
showAuthModal: false
|
||||
});
|
||||
//////////debugger
|
||||
// 保存到全局
|
||||
getApp().globalData.userInfo = userInfo;
|
||||
await this.onGetAuth()
|
||||
// 继续登录流程
|
||||
await this.completeLogin();
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:'+error);
|
||||
wx.showToast({
|
||||
title: error.message || '获取用户信息失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
},
|
||||
async onGetAuth() {
|
||||
const res = await authorizeBatch([
|
||||
'scope.record',
|
||||
'scope.userLocation'
|
||||
])
|
||||
|
||||
if (true) {//if (res.ok) {
|
||||
wx.showToast({ title: '全部授权成功' })
|
||||
} else {
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: `您拒绝了 ${res.denied.map(s => SCOPE_MAP[s]).join('、')},部分功能可能无法使用`,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 完整的登录流程
|
||||
async completeLogin() {
|
||||
|
||||
try {
|
||||
wx.showLoading({ title: '登录中...', mask: true });
|
||||
|
||||
// 获取微信code
|
||||
const loginRes = await new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: resolve,
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
|
||||
if (!loginRes.code) {
|
||||
throw new Error('获取登录凭证失败');
|
||||
}
|
||||
|
||||
// 发送到后端
|
||||
const apiRes = await new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: 'https://wx-xcx-check.blv-oa.com:4433/api/Login/Login',
|
||||
method: 'POST',
|
||||
data: {Code: loginRes.code},
|
||||
success: res =>{
|
||||
wx.hideLoading()
|
||||
if (res.data.success){
|
||||
this.setData({openid: res.data.data.userKey});
|
||||
console.log(this.data.openid)
|
||||
wx.setStorageSync('openid', res.data.data.userKey);
|
||||
}
|
||||
|
||||
if (res.data.success && res.data.data.userName && res.data.data.weChatName && res.data.data.phoneNumber) {
|
||||
wx.navigateTo({url: '/pages/chat/chat'});
|
||||
}
|
||||
else{
|
||||
wx.showToast({
|
||||
title: `登录失败: 用户未注册。请先填写完整信息再进行登录`,
|
||||
icon: 'none'
|
||||
});
|
||||
this.setData({needReg: true});
|
||||
}
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
wx.showToast({
|
||||
title: `登录失败: ${error.message || '未知错误'}`,
|
||||
icon: 'none'
|
||||
});
|
||||
} finally {
|
||||
wx.hideLoading();
|
||||
}
|
||||
},
|
||||
|
||||
// 修改原有登录方法
|
||||
handleLogin: async function() {
|
||||
|
||||
if (this.data.isAgree) {
|
||||
// 如果已有用户信息,直接完成登录
|
||||
// if (this.data.userInfo) {
|
||||
await this.completeLogin();
|
||||
// } else {
|
||||
// // 否则显示授权弹窗
|
||||
// this.showAuthModal();
|
||||
// }
|
||||
}else{
|
||||
// 提示为勾选用户协议
|
||||
wx.showToast({
|
||||
title: '请先勾选用户协议',
|
||||
icon: 'none'
|
||||
});
|
||||
return; // 拦截后续登录逻辑
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
@@ -1,6 +1,76 @@
|
||||
<!--logs.wxml-->
|
||||
<scroll-view class="scrollarea" scroll-y type="list">
|
||||
<block wx:for="{{logs}}" wx:key="timeStamp" wx:for-item="log">
|
||||
<view class="log-item">{{index + 1}}. {{log.date}}</view>
|
||||
</block>
|
||||
</scroll-view>
|
||||
<!-- pages/login/login.wxml -->
|
||||
<view class="login-container">
|
||||
<!-- 顶部 Logo / 标题 -->
|
||||
<view class="head">
|
||||
<image src="/images/Blvlogo.png" mode="aspectFit" class="logo"/>
|
||||
<text class="title">宝来威工作助手</text>
|
||||
</view>
|
||||
|
||||
<!-- 微信登录按钮 -->
|
||||
<view wx:if="{{!needReg}}" class="wx-login-box">
|
||||
<button class="wx-btn" open-type="getUserProfile" bindtap="handleLogin">
|
||||
微信一键登录
|
||||
</button>
|
||||
</view>
|
||||
|
||||
<!-- 信息补全表单 -->
|
||||
<view wx:else class="reg-form">
|
||||
<text class="tip">请补全信息</text>
|
||||
<!-- 头像 -->
|
||||
<!--index.wxml-->
|
||||
<view class="avatar-box " >
|
||||
<button open-type="chooseAvatar" bindchooseavatar="onAvatar" style="margin: 0; padding: 0;height: 32px;width:90%; ">
|
||||
<image src="{{avatarUrl}}" style="height: 30px;width: 30px;" />
|
||||
</button>
|
||||
<text class="tip">点击更换头像</text>
|
||||
</view>
|
||||
|
||||
|
||||
|
||||
<!-- 昵称 -->
|
||||
<input type="nickname" class="input" placeholder="请输入昵称" bindblur="onNick" />
|
||||
|
||||
|
||||
<input
|
||||
class="input"
|
||||
placeholder="用户名称"
|
||||
value="{{form.name}}"
|
||||
bindinput="inputName"
|
||||
maxlength="20"
|
||||
/>
|
||||
<input
|
||||
class="input"
|
||||
placeholder="联系电话"
|
||||
type="number"
|
||||
value="{{form.phone}}"
|
||||
bindinput="inputPhone"
|
||||
maxlength="11"
|
||||
/>
|
||||
|
||||
<button class="submit-btn" style="display: flex; flex-direction:column; justify-content: center; " bindtap="submitReg">进入聊天</button>
|
||||
</view>
|
||||
|
||||
<!-- 底部协议 -->
|
||||
|
||||
<view class="footer">
|
||||
<checkbox-group bindchange="onAgreeChange" class="agree-line">
|
||||
<checkbox value="1" checked="{{isAgree}}" />
|
||||
<text class="txt">登录须要勾选</text>
|
||||
<text class="link" bindtap="openContract">《用户协议》</text>
|
||||
</checkbox-group>
|
||||
</view>
|
||||
|
||||
</view>
|
||||
<!-- 在文件底部添加 -->
|
||||
<!-- <view class="auth-modal" hidden="{{!showAuthModal}}">
|
||||
<view class="auth-content">
|
||||
<view class="auth-header">获取用户信息</view>
|
||||
<view class="auth-body">
|
||||
我们需要获取您的头像和昵称以提供更好的服务
|
||||
</view>
|
||||
<view class="auth-footer">
|
||||
<button class="auth-btn" bindtap="onAuthCancel">取消</button>
|
||||
<button class="auth-btn primary" open-type="getUserInfo" bindgetuserinfo="onGetUserInfo">授权</button>
|
||||
</view>
|
||||
</view>
|
||||
</view> -->
|
||||
@@ -14,3 +14,137 @@ page {
|
||||
.log-item:last-child {
|
||||
padding-bottom: env(safe-area-inset-bottom);
|
||||
}
|
||||
/* pages/login/login.wxss */
|
||||
.login-container{ height:100vh; display:flex; flex-direction:column; align-items:center; justify-content:center; background:#f5f5f5; }
|
||||
|
||||
.head{ margin-bottom:60rpx; text-align:center; }
|
||||
.logo{ width:120rpx; height:120rpx; }
|
||||
.title{ display:block; font-size:48rpx; color:#333; margin-top:20rpx; }
|
||||
|
||||
.wx-login-box{ width:80%; }
|
||||
.wx-btn{width:100%;
|
||||
height:88rpx;
|
||||
/* 移除 line-height,使用 Flex 布局 */
|
||||
display: flex;
|
||||
justify-content: center; /* 水平居中 */
|
||||
align-items: center; /* 垂直居中 */
|
||||
background:#07C160;
|
||||
color:#fff;
|
||||
border-radius:12rpx;
|
||||
font-size:32rpx; }
|
||||
.icon-wechat{ margin-right:10rpx; }
|
||||
|
||||
.reg-form{ width:80%; }
|
||||
.tip{ font-size:28rpx; color:#666; margin-bottom:40rpx; }
|
||||
.input{ width:100%; height:88rpx; background:#fff; border:1rpx solid #ddd; border-radius:12rpx; padding:0 20rpx; margin-bottom:30rpx; font-size:30rpx; }
|
||||
.submit-btn{ width:100%; height:88rpx; background:#07C160; color:#fff; border-radius:12rpx; font-size:32rpx; }
|
||||
|
||||
.footer{ position:absolute; bottom:40rpx; font-size:24rpx; color:#999; }
|
||||
.link{ color:#576B95; }
|
||||
/* 授权弹窗样式 */
|
||||
.auth-modal {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background-color: rgba(0,0,0,0.5);
|
||||
z-index: 999;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.auth-content {
|
||||
background: #fff;
|
||||
border-radius: 16rpx 16rpx 0 0;
|
||||
padding: 30rpx;
|
||||
animation: slideUp 0.3s ease;
|
||||
}
|
||||
|
||||
@keyframes slideUp {
|
||||
from { transform: translateY(100%); }
|
||||
to { transform: translateY(0); }
|
||||
}
|
||||
|
||||
.auth-header {
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.auth-body {
|
||||
font-size: 28rpx;
|
||||
color: #666;
|
||||
margin-bottom: 40rpx;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.auth-footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.auth-btn {
|
||||
flex: 1;
|
||||
margin: 0 10rpx;
|
||||
border-radius: 8rpx;
|
||||
background: #f2f2f2;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.auth-btn.primary {
|
||||
background: #07C160;
|
||||
color: #fff;
|
||||
}
|
||||
/*index.wxss*/
|
||||
.avatar-box {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
margin-top: 40rpx;
|
||||
}
|
||||
|
||||
.avatar {
|
||||
width: 96rpx;
|
||||
height: 96rpx;
|
||||
border-radius: 50%;
|
||||
border: 2rpx solid #e7e7e7;
|
||||
}
|
||||
|
||||
.tip {
|
||||
margin-top: 8rpx;
|
||||
font-size: 24rpx;
|
||||
color: #888;
|
||||
}
|
||||
/* wxss */
|
||||
.agree-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
.agree-row checkbox {
|
||||
margin-right: 8rpx;
|
||||
}
|
||||
.link {
|
||||
color: #1989fa;
|
||||
margin-left: 4rpx;
|
||||
}
|
||||
.footer{
|
||||
padding: 20rpx;
|
||||
/* background: #fff; */
|
||||
}
|
||||
|
||||
.agree-line{
|
||||
display: flex;
|
||||
align-items: center; /* 关键:垂直居中 */
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.agree-line checkbox{
|
||||
margin-right: 8rpx; /* 文字和框间距 */
|
||||
}
|
||||
|
||||
.link{
|
||||
color: #1989fa;
|
||||
margin-left: 4rpx;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user