基本流程走通了

This commit is contained in:
callmeyan 2019-06-23 22:26:19 +08:00
parent 3ef98415b0
commit 9a713fdfb9
11 changed files with 365 additions and 208 deletions

86
app.js
View File

@ -1,49 +1,81 @@
//app.js
// global.regeneratorRuntime = require('./utils/regenerator/runtime-module')
import api from './utils/api.js';
App({
onLaunch: function() {
async onLaunch() {
// 展示本地存储能力
// var logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
var openId = this.globalData.openId = wx.getStorageSync('openId') || '';
var openId = wx.getStorageSync('openId') || '';
// 登录
// wx.login({
// success: res => {
// // 发送 res.code 到后台换取 openId, sessionKey, unionId
// console.log('login==>', res);
// }
// })
if (openId){
this.globalData.openId = openId
if (openId) { // 已经有openid了 可以直接获取用户信息
console.log('init get user info');
await this.getUserInfo(openId);
return;
} else {
// 登录并获取openid
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
console.log('login==>', res);
if (res.errMsg == 'login:ok') {
this.processLogin(res.code);
}
}
});
return;
}
// 获取用户信息
wx.getSetting({
success: res => {
console.log('getSetting==>', res);
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.wxuserInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
},
async processLogin(code) {
try {
let data = await api.userLogin(code);
// 获取openid
this.globalData.openId = data.openid;
// 保存用户openid
wx.setStorageSync('openId', this.globalData.openId);
} catch (e) {
console.log('login error->', e);
}
},
getOpenId() {
return new Promise(async(resolve, reject) => {
if(this.globalData.openId){
resolve(this.globalData.openId)
}else{
wx.login({
success:async res => {
if (res.errMsg == 'login:ok') {
await this.processLogin(res.code);
resolve(this.globalData.openId)
}
})
}
}
});
}
})
},
async getUserInfo() {
try {
let openid = this.globalData.openId;
let userInfo = await api.userInfo(openid, false);
console.log('init user info data', userInfo);
this.globalData.userInfo = userInfo;
} catch (e) {
console.log('init user info error', e);
}
},
globalData: {
wxuserInfo:null,
wxuserInfo: null,
userInfo: null,
openId: ''
}

View File

@ -1,10 +1,10 @@
{
"pages": [
"pages/index/index",
"pages/init/userinfo",
"pages/user/result",
"pages/user/evaluation",
"pages/user/index",
"pages/init/userinfo",
"pages/init/agreement"
],
"window": {

1
assets/icon-error.svg Normal file
View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1561285411508" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1985" width="48" height="48" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M149.961 240.472c-24.993-24.993-24.993-65.517 0-90.51s65.517-24.993 90.51 0L512 421.491l271.529-271.529c24.992-24.993 65.516-24.993 90.508 0 24.994 24.993 24.994 65.517 0 90.51L602.51 512l271.529 271.528c24.992 24.993 24.992 65.516 0 90.51-24.994 24.993-65.518 24.993-90.51-0.001L512 602.508 240.471 874.037c-24.993 24.994-65.517 24.994-90.51 0-24.993-24.993-24.993-65.516 0-90.509L421.49 512 149.961 240.472z" p-id="1986" fill="#ffffff"></path></svg>

After

Width:  |  Height:  |  Size: 826 B

View File

@ -2,14 +2,15 @@
//index.js
//获取应用实例
const app = getApp()
import api from './../../utils/api.js';
Page({
data: {
motto: 'Hello World',
motto: 'Hello',
userInfo: {
avatarUrl: '/assets/head.svg',
realname:'张二娃',
lastEvaluation:'2019-06-23 09:43'
realname: '张二娃',
lastEvaluation: '2019-06-23 09:43'
},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
@ -21,51 +22,132 @@ Page({
content: '',
})
},
onUserInfoTap(){
wx.navigateTo({
url: '../user/index'
})
},
onLoad: function() {
console.log('index load data',app.globalData)
// 加载
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
if (app.globalData.openId) { // 已经获取了用户数据
this.processGetUserInfo();
} else if (this.data.canIUse) { // 可以获取用户信息
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
// app.userInfoReadyCallback = res => {
// this.processGetUserInfo(res.userInfo)
// }
// 获取用户信息
wx.getSetting({
success: res => {
console.log('getSetting==>', res);
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
this.processGetUserInfo(res.userInfo)
// // 可以将 res 发送给后台解码出 unionId
// this.globalData.wxuserInfo = res.userInfo
// // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// // 所以此处加入 callback 以防止这种情况
// if (this.userInfoReadyCallback) {
// this.userInfoReadyCallback(res)
// }
}
})
}
}
})
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
this.processGetUserInfo(res.userInfo)
}
})
}
},
getUserInfo: function(e) {
console.log(e)
app.globalData.wxuserInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
getUserInfo: function(e) { // 获取草用户数据
this.processGetUserInfo(e.detail.userInfo)
},
// 初始化用户数据
async initUserInfo() {
try {
let openid = await app.getOpenId();
let userInfo = await api.userInfo(openid, false);
console.log('init user info data', userInfo);
this.globalData.userInfo = userInfo;
this.processGetUserInfo(userInfo);
} catch (e) {
console.log('init user info error', e);
}
},
// 处理获取到用户数据的逻辑
async processGetUserInfo(userInfo) {
//
if (app.globalData.userInfo) { // 已经存在数据则直接跳过
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
});
return;
}else{ // 尝试创建该用户
let openid = app.globalData.openId;
let userInfo = null;
try{
userInfo = await api.userInfo(openid, false);
if (userInfo.detail){
userInfo['realname'] = userInfo.detail.realname;
}
app.globalData.userInfo = userInfo;
this.setData({
userInfo: userInfo,
hasUserInfo: true
});
return;
}catch(e){}
try {
let data = await api.createUserInfo({
open_id: app.globalData.openId,
nickname: userInfo.nickName,
username: app.globalData.openId,
avatarUrl: userInfo.avatarUrl,
});
// 保存数据
app.globalData.userInfo = data;
this.setData({
userInfo: data,
hasUserInfo: true
});
} catch (e) {
console.log('create user error', e);
}
}
// 判断是否需要跳转
if (this.data.userInfo.detail) { return; }
this.gotoAgreementPage()
},
gotoAgreementPage() {
wx.navigateTo({
url: '../init/agreement'
})
},
gotoEvaluation(){
wx.navigateTo({
url: './../user/evaluation',
})
gotoUserIndex() {
if (this.data.userInfo.detail) { // 判断是否已经存在用户数据
wx.navigateTo({
url: '../user/index'
})
}else{
this.gotoAgreementPage()
}
},
gotoEvaluation() {
if (this.data.userInfo.detail) { // 判断是否已经存在用户数据
wx.navigateTo({
url: './../user/evaluation',
})
} else {
this.gotoAgreementPage()
}
}
})

View File

@ -1,7 +1,7 @@
<!--index.wxml-->
<view class="container page-user">
<view hidden="{{!hasUserInfo}}" class="cu-list menu-avatar user-info">
<view class="cu-item" bindtap="onUserInfoTap">
<view class="cu-item" bindtap="gotoUserIndex">
<view class="cu-avatar round lg" style="background-image:url({{userInfo.avatarUrl}});"></view>
<view class="content">
<view class="text-grey">
@ -9,13 +9,13 @@
</view>
<view class="text-gray text-sm flex">
<view class="text-cut">
评估时间:<text>{{userInfo.lastEvaluation}}</text>
评估时间:<text>{{userInfo.lastEvaluation?userInfo.lastEvaluation:'还没有评估过'}}</text>
</view>
</view>
</view>
<view class="action">
<view class="cuIcon-right text-gray"></view>
<view class="text-gray text-sm flex"> </view>
<view class="text-gray text-sm flex"></view>
</view>
</view>
</view>

View File

@ -1,44 +1,9 @@
// pages/init/userinfo.js
const allCities = [{
'code': '123',
'name': '北京',
children: [{
"code": "110101",
"name": "东城区"
},
{
"code": "110102",
"name": "西城区"
}
]
},
{
'code': '123',
'name': '天津',
children: [{
"code": "110105",
"name": "朝阳区"
},
{
"code": "110106",
"name": "丰台区"
}
]
},
{
'code': '123',
'name': '上海',
children: [{
"code": "110107",
"name": "石景山区"
},
{
"code": "110108",
"name": "海淀区"
}
]
}
];
import {
updateUserDetail
} from './../../utils/api.js';
import allCities from './../../utils/allCities.js';
Page({
@ -53,7 +18,7 @@ Page({
'10~20支/天',
'20支/天以上',
],
medical_histories:[
medical_histories: [
'慢性支气管炎、支气管哮喘、支气管扩张病、肺心病',
'高血压、冠心病、心肌病、先天性心脏病、风湿性心脏病',
'反流性食道炎、慢性胃炎、胃溃疡、慢性胰腺炎、肠易激惹综合征、结肠炎',
@ -62,23 +27,114 @@ Page({
'其他疾病'
]
},
hiddenSelect: true,
provinces: allCities,
cities: allCities[0].children,
provinceId: 0,
cityId: 0
provinceId: -1,
cityId: -1,
userDetailModel: {
realname: "",
gender: 1,
age: '',
province: 0,
city: 0,
is_first_to_tibet: 1,
height: '',
weight: '',
smoke: 0,
drink: 1,
medical_history: ''
}
},
onBoxInput(e) {
this.setUpdateData(
e.currentTarget.dataset.key, e.detail.value
)
},
onSelectChange(e) {
this.setUpdateData(
e.currentTarget.dataset.key, e.detail.value
)
console.log(e.currentTarget.dataset.key, e.detail.value);
},
onMedicalChange(e) {
var v = e.detail.value;
this.setData({
hiddenSelect: v == 0
})
},
onMedicalValueChange(e) {
this.setUpdateData('medical_history', e.detail.value.join(','))
},
setUpdateData(key, value) {
var userDetailModel = this.data.userDetailModel;
userDetailModel[key] = value;
this.setData({
userDetailModel
})
},
bindProvinceChange(e) {
let _id = parseInt(e.detail.value);
this.setUpdateData('province', this.data.provinces[_id].code);
this.setData({
provinceId: e.detail.value,
cities: allCities[e.detail.value].children
provinceId: _id,
cities: this.data.provinces[_id].children
})
},
bindCityChange(e) {
let _id = parseInt(e.detail.value);
this.setUpdateData('city', this.data.cities[_id].code);
this.setData({
cityId: e.detail.value
cityId: _id
})
},
validateUpdate() {
var model = this.data.userDetailModel;
var title = false;
if (!model.realname) {
title = '请填写您的姓名';
} else if (!model.age) {
title = '请填写您的年龄';
} else if (model.province == 0 || model.city == 0) {
title = '请选择您的世居地';
} else if (!model.height) {
title = '请填写您的身高';
} else if (!model.weight) {
title = '请填写您的体重';
} else if (this.data.hiddenSelect == false && !model.medical_history) {
title = '请选择既往疾病记录';
}
return title;
},
async updateAndEvaluation() { // 更新数据
var err = this.validateUpdate();
if (err) {
var title = {
title: err,
icon: 'none'
};
wx.showToast({
title: err,
icon: 'none' //:err,image:'/assets/icon-error.svg'
});
return;
}
wx.showModal({
title: '确认填写正确并进行评估?',
success:async res => {
if (res.confirm) {
console.log(this.data.userDetailModel)
let data = await updateUserDetail(this.data.userDetailModel);
// // 跳转到 评估页
wx.redirectTo({
url: './../user/evaluation',
})
} else if (res.cancel) {}
}
})
},
/**
* 用户点击右上角分享

View File

@ -5,11 +5,11 @@
<view class="card">
<view class="form-group">
<label>1、您的姓名</label>
<input />
<input bindinput="onBoxInput" data-key="realname" />
</view>
<view class="form-group">
<label>2、您的性别</label>
<radio-group>
<radio-group bindchange="onSelectChange" data-key="gender">
<label class="radio" c>
<radio value="1" checked="true" />男
</label>
@ -20,11 +20,11 @@
</view>
<view class="form-group">
<label>3、您的年龄</label>
<input />
<input bindinput="onBoxInput" data-key="age" />
</view>
<view class="form-group">
<label>4、您是否第一次进藏</label>
<radio-group>
<radio-group bindchange="onSelectChange" data-key="is_first_to_tibet">
<label class="radio">
<radio value="1" checked="true" />是
</label>
@ -37,27 +37,27 @@
<label>5、您的世居地居住十年以上</label>
<picker bindchange="bindProvinceChange" value="{{provinceId}}" range="{{provinces}}" range-key="name">
<view class="select-input">{{provinces[provinceId].name}}</view>
<view class="select-input">{{provinceId==-1?"请选择省份":provinces[provinceId].name}}</view>
</picker>
<picker bindchange="bindCityChange" value="{{cityId}}" range="{{cities}}" range-key="name">
<view class="select-input">{{cities[cityId].name}}</view>
<view class="select-input">{{cityId == -1?"请选择城市":cities[cityId].name}}</view>
</picker>
</view>
<view class="form-group">
<label>6、您的身高cm</label>
<input />
<input bindinput="onBoxInput" data-key="height" />
</view>
<view class="form-group">
<label>7、您的体重kg</label>
<input />
<input bindinput="onBoxInput" data-key="weight" />
</view>
<view class="form-group">
<label>8、您是否长期吸烟吸烟量支/天):</label>
<radio-group>
<view class="item" wx:for="{{subjects.smoke}}">
<radio-group bindchange="onSelectChange" data-key="smoke">
<view class="item" wx:key="smoke" wx:for="{{subjects.smoke}}">
<label>
<radio value="{{index}}" checked="{{index == 0}}" />{{item}}</label>
</view>
@ -65,7 +65,7 @@
</view>
<view class="form-group">
<label>9、您是否长期饮酒</label>
<radio-group>
<radio-group bindchange="onSelectChange" data-key="drink">
<label class="radio">
<radio value="1" checked="true" />是
</label>
@ -76,19 +76,19 @@
</view>
<view class="form-group">
<label>10、您是否有既往基础疾病</label>
<radio-group>
<radio-group bindchange="onMedicalChange">
<label class="radio">
<radio value="1" checked="true" />是
<radio value="1" />是
</label>
<label class="radio">
<radio value="0" />否
<radio value="0" checked="true" />否
</label>
</radio-group>
</view>
<view class="form-group">
<view class="form-group" hidden="{{hiddenSelect}}">
<label>11、既往疾病包括多选</label>
<checkbox-group>
<view class="item" wx:for="{{subjects.medical_histories}}">
<checkbox-group bindchange="onMedicalValueChange">
<view class="item" wx:key="medical" wx:for="{{subjects.medical_histories}}">
<label>
<checkbox value="{{index}}" />
<text>{{item}}</text>
@ -98,6 +98,6 @@
</view>
</view>
<view class="text-center container-card">
<button class="btn-green go-to-next btn-round">下一步</button>
<button bindtap="updateAndEvaluation" class="btn-green go-to-next btn-round">下一步</button>
</view>
</view>

View File

@ -11,6 +11,10 @@ radio-group > label > radio {
.form-group picker, radio-group .item, checkbox-group .item {
margin-bottom: 30rpx;
}
.form-group checkbox{
top:-8rpx;
margin-right:10rpx;
}
checkbox-group .item>label {
display: flex;

View File

@ -3,6 +3,7 @@ import allCities from './../../utils/allCities.js';
import api from './../../utils/api.js';
// const { regeneratorRuntime } = global
const app =getApp();
Page({
/**
@ -11,7 +12,7 @@ Page({
data: {
allCities,
userInfo: {
avatarUrl: 'http://thirdwx.qlogo.cn/mmopen/vi_32/9xkaQhBrbjIsJNJkxKicKayJiajy2ZsKFHM7vhibPjic0sLhn0cxCicxgQUl9VDF9o02NsQlcxZicJPWb4K9RibQJ8ibDQ/132',
avatarUrl: '',
address: '北京 东城区',
realname: '',
},
@ -57,30 +58,13 @@ Page({
console.log(evaluationData);
evaluationData.forEach((o,i) => {
tags.push(true);
// let level = 0;
// // 判断结果
// if(o.options[0].score == 0 || o.score < 3){
// // 没有高反
// level = 0;
// } else if (o.score <= 5) {
// level = 1;
// } else if (o.score <= 9) {
// level = 2;
// } else if (o.score <= 12) {
// level = 3;
// } else {
// level = 4;
// }
// evaluationData.data[i]['level'] = level;
})
this.setData({
userDetail:app.globalData.userInfo.detail,
userInfo: app.globalData.userInfo,
dataList: evaluationData,
collapseTags: tags
});
let userDetail = await api.queryUserDetail();
this.setData({
userDetail: userDetail
});
},
switchView() {
this.setData({

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,16 @@
const API_URL = 'http://127.0.0.1:8000';
const app = getApp();
const openId = app.globalData.openId;
const requestApi = (api, data = {}, method = "GET") => {
const getOpenId = () => {
return getApp().globalData.openId;
}
const requestApi = (api, data = {}, method = "GET", showError) => {
return new Promise((resolve, reject) => {
data['open_id'] = openId;
if (typeof(showError) == "undefined") {
showError = true;
}
if (!data['open_id']) {
data['open_id'] = getOpenId();
}
wx.showLoading({
title: '请求数据中...',
});
@ -22,13 +29,19 @@ const requestApi = (api, data = {}, method = "GET") => {
responseType: 'text',
success: function(res) {
wx.hideLoading();
if(res.statusCode != 200){
if (res.statusCode != 200) {
reject(res);
} else {
if(res.data.code && res.data.code != 0){
wx.showModal({ title: res.data.message, showCancel: false })
//reject(res);
}else{
if (res.data.code && res.data.code != 0) {
if (showError) {
wx.showModal({
title: res.data.message,
showCancel: false
})
} else {
reject(res);
}
} else {
resolve(res.data);
}
}
@ -40,26 +53,47 @@ const requestApi = (api, data = {}, method = "GET") => {
});
});
}
/**
* 使用用户登录code获取用户session及open_id
*/
export function userLogin(code) {
return requestApi('/user/login', {
code
});
}
/**
* 查询用户基本信息
*/
export function queryUserInfo() {
return requestApi('/user/info');
export function userInfo(openid, showError) {
return requestApi('/user/info', {
open_id: openid
}, 'GET', showError);
}
export function createUserInfo(data) {
return requestApi('/user/create', data, 'POST', false);
}
/**
* 查询用户详细信息
*/
export function queryUserDetail() {
export function userDetail() {
return requestApi('/user/detail');
}
/**
* 更新用户详细信息
*/
export function updateUserDetail(data) {
return requestApi('/user/update', data, 'POST');
}
/**
* 查询用户所有的评估数据
*/
export function queryAllEvaluation() {
return requestApi('/evaluation/all');
}
export function queryEvaluationById(id){
return requestApi('/evaluation/detail',{id});
export function queryEvaluationById(id) {
return requestApi('/evaluation/detail', {
id
});
}
/**
* 创建用户评估数据
@ -69,8 +103,11 @@ export function createEvaluation(data) {
}
export default {
queryUserInfo,
queryUserDetail,
userInfo,
updateUserDetail,
createUserInfo,
userDetail,
queryAllEvaluation,
createEvaluation
createEvaluation,
userLogin
}