compelete login system

kongweiyu_branch
孔维屿 1 year ago
parent e5478db4de
commit 8ef89e26a9

@ -0,0 +1,6 @@
{
"permissions": {
"openapi": [
]
}
}

@ -0,0 +1,17 @@
const cloud = require('wx-server-sdk');
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
});
// 获取openId云函数入口函数
exports.main = async (event, context) => {
// 获取基础信息
const wxContext = cloud.getWXContext();
return {
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
};
};

@ -0,0 +1,14 @@
{
"name": "getOpenId",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}

@ -17,24 +17,32 @@ App({
this.globalData = {
// OPENID: wx.cloud.getWXContext().OPENID
OPENID: '',
USERID: ''
USERID: '',
ISLOGIN: false
};
//尝试读取本地缓存
try {
// wx.clearStorage()
this.globalData.USERID = wx.getStorageSync('user_id')
this.globalData.OPENID = wx.getStorageSync('user_openid')
} catch (e) {
console.log(e);
console.error(e);
}
if (this.globalData.USERID) {
wx.cloud.database().collection("user_id").doc(this.globalData.USERID).get({
success: function() {
// todo
//检测用户是否首次登录
if (!this.globalData.OPENID) {
this.globalData.ISLOGIN = false
wx.cloud.callFunction({
name: "getOpenId",
success: (res) => {
this.globalData.OPENID = res.result.openid;
wx.setStorageSync('user_openid', res.result.openid)
}
})
} else {
console.log("首次登录");
}
}
//输出全局变量
console.log("[GLOABLE] User:",this.globalData);
},
});

@ -1,4 +1,4 @@
//const that = this;
const app = getApp();
const db = wx.cloud.database().collection("student_Demands");
// pages/MainTest/MainTest.js
Page({
@ -23,17 +23,17 @@ Page({
// title: '加载中',
// })
//检测用户是否注册过
wx.cloud.database()
.collection('user_Data')
.get({
success: (res) => {
var exist = (res.data.length>0)? true: false
this.setData({
exist
})
console.log(this.data.exist);
}
})
// wx.cloud.database()
// .collection('user_Data')
// .get({
// success: (res) => {
// var exist = (res.data.length>0)? true: false
// this.setData({
// exist
// })
// console.log(this.data.exist);
// }
// })
// 调用云函数获取教师信息
wx.cloud.callFunction({

@ -1,16 +1,18 @@
const db = wx.cloud.database().collection("user_Data");
const _ = wx.cloud.database().command
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' //默认头像地址
Page({
//页面数据
data:{
avatarUrl: defaultAvatarUrl,
nickname: "昵称",
sex: null,
nickname: "",
sex: "",
birthday: "",
region: "",
contact: "请输入",
details: "请输入"
contact: "",
information: ""
},
onLoad(options) {
@ -26,11 +28,43 @@ Page({
console.log(e);
}
db.get({
success:(res) => {
console.log(res)
}
})
//获取用户信息
if (!app.globalData.USERID) {
db.add({
data: {
nickname: '',
gender: '',
birthday: '',
region: '',
contact: '',
information: ''
},
success: (res) => {
app.globalData.USERID = res._id
wx.setStorageSync('user_id', res._id)
console.log("[PROFILE] AddNewUser");
}
})
} else {
db.where({
_openid: app.globalData.OPENID
}).get({
success:(res) => {
var data = res.data[0]
this.setData({
nickname: data.nickname,
sex: data.gender,
birthday: data.birthday,
region: data.region,
contact: data.contact,
information: data.information
})
app.globalData.USERID = data._id
// app.globalData.ISLOGIN = true
console.log("[PROFILE] UserQuery", data);
}
})
}
},
//选择头像后存储头像地址
@ -50,16 +84,33 @@ Page({
//表单提交方法
submit: function(e){
console.log(e.detail.value);
console.log("[PROFILE] SubmitData", e.detail.value);
//todo: 添加输入检测
//往数据库里插入的方法
db.add({
//往数据库里更新的方法
db.where({
_openid: app.globalData.OPENID
}).update({
//传入数据
data: e.detail.value,
data: {
nickname: this.data.nickname,
sex: this.data.sex,
birthday: this.data.birthday,
region: this.data.region,
contact: this.data.contact,
information: this.data.information
},
//成功后执行的方法
success:(res) => {
console.log(res)
console.log("[PROFILE] SubmitDone",res)
wx.showToast({
title: '提交成功',
icon: 'success',
duration: 2000
})
}
})
},

@ -12,18 +12,18 @@
<view class="section">
<view class="title">昵称:</view>
<input type="nickname" class="input" name="nickname" placeholder="{{nickname}}" />
<input type="nickname" class="input" model:value="{{nickname}}" name="nickname" placeholder="昵称" />
</view>
<view class="section">
<view class="title">性别:</view>
<radio-group class="mg" name="gender">
<label >
<radio value="man" checked="{{sex}}"/>
<radio value="man" checked="{{sex == 'man'}}"/>
</label>
<label >
<radio value="woman" checked="{{!sex && sex!=NULL}}"/>
<radio value="woman" checked="{{sex == 'woman'}}"/>
</label>
</radio-group>
@ -31,26 +31,26 @@
<view class="section">
<view class="title">年龄:</view>
<picker bindchange="dateChange" fields="year" value="{{birthday}}" mode="date" class="input" name="birthday">
<picker bindchange="dateChange" fields="year" model:value="{{birthday}}" mode="date" class="input" name="birthday">
<view class="picker">出生年:{{birthday}}</view>
</picker>
</view>
<view class="section">
<view class="title">地区:</view>
<picker bindchange="regionChange" value="{{region}}" value="{{region}}"mode="region" class="input" name="region">
<picker bindchange="regionChange" model:value="{{region}}" mode="region" class="input" name="region">
<view class="picker">当前选择:{{region}}</view>
</picker>
</view>
<view class="section">
<view class="title">可以添加到你的联系方式:</view>
<input type="input" class="input" name="contact" placeholder="请输入" />
<input type="input" class="input" name="contact" model:value="{{contact}}" placeholder="请输入" />
</view>
<view class="section">
<view class="title">你的基本情况:</view>
<input type="input" class="input" name="information" placeholder="请输入" />
<input type="input" class="input" name="information" model:value="{{information}}" placeholder="请输入" />
</view>
<view class="area">

Loading…
Cancel
Save