// pages/home/home.js
import { loginPage } from "../login/login.js"
const app = getApp()
const networkUtil = require('../../utils/network_util.js')
const api = require('../../utils/apiFile.js')
var QR_Scare = 2
Page(Object.assign({}, loginPage, {
/**
* 页面的初始数据
*/
data: {
// 1首页 2登录
pageType:0,
areaName: app.globalData.openInfo.hospitalName || '',
imageUrl:'',
info:{}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (option) {
console.log('home_options:', option)
if (app.globalData.token) {
this.homePage()
this.requestHomeInfo()
} else {
this.loginPage()
}
},
onPullDownRefresh(){
console.log('onPullDownRefresh')
if (app.globalData.token) {
this.requestHomeInfo()
}
},
homePage(){
this.setData({
pageType: 1
})
wx.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: '#477DEB'
})
wx.setNavigationBarTitle({
title: '基层辅助孕妇建档平台',
})
},
loginPage(){
this.setData({
pageType: 2
})
wx.setNavigationBarColor({
frontColor: '#000000',
backgroundColor: '#ffffff'
})
wx.setNavigationBarTitle({
title: '',
})
},
// --------------------------首页--------------------------
requestHomeInfo(){
var self = this
networkUtil.showLoading()
networkUtil._get(api.BuildCountInfo, { hospitalId: app.globalData.openInfo.hospitalId}, function (res) {
console.log(res)
self.setData({
info:res.data.data,
imageUrl: 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' + res.data.data.ticket
})
self.setQRImage()
}, function (res) {
networkUtil.showErrorToast(res.errormsg)
self.setQRImage()
})
},
logout() {
var self = this
wx.showModal({
title: '提示',
content: '是否退出登录?',
success: function (res) {
if (res.confirm) {
wx.setStorageSync('userToken', '')
wx.setStorageSync('QR_FilePath', '')
app.globalData.token = ''
self.loginPage()
} else if (res.cancel) {
}
}
})
},
longTapImage() {
var self = this
wx.showActionSheet({
itemList: ['查看大图'],
success: function (res) {
console.log('111', res)
if (res.tapIndex == 0) {
console.log('111', res.tapIndex)
wx.previewImage({
urls: [self.data.imageUrl],
})
}
},
fail: function (res) {
console.log(res.errMsg)
}
})
},
// 二维码相关
setQRImage() {
var fileP = wx.getStorageSync('QR_FilePath')
if (fileP) {
this.drawQR(fileP)
this.data.imageUrl = fileP
console.log('已经有了', fileP)
} else {
this.downLoadQR()
}
},
downLoadQR() {
networkUtil.showLoading('获取二维码中')
var self = this
wx.downloadFile({
url: self.data.imageUrl,
success: function (res) {
if (res.statusCode === 200) {
wx.setStorageSync('QR_FilePath', res.tempFilePath)
self.drawQR(res.tempFilePath)
// setTimeout(function () {
// 延迟等绘制完保存成图片
// wx.canvasToTempFilePath({
// x: 0,
// y: 0,
// width: 174,
// height: 174,
// destWidth: 696,
// destHeight: 696,
// canvasId: 'QR_canvas',
// quality: 1,
// success: function (res) {
// console.log('成功:', res.tempFilePath)
// wx.setStorageSync('QR_FilePath', res.tempFilePath)
// self.data.imageUrl = res.tempFilePath
// }
// })
// }, 3500)
}
},
fail: function () {
networkUtil.showErrorToast('未加载出二维码,请下拉刷新页面')
},
complete: function () {
wx.hideToast()
}
})
},
drawQR(QRImage) {
var ctx = wx.createCanvasContext('QR_canvas', this)
ctx.drawImage(QRImage, 0, 0, 174, 174)
// ctx.drawImage('../../utils/image/icon.png', 57, 57, 60, 60)
ctx.draw()
},
// --------------------------登录--------------------------
submitLoginData(e){
console.log(e)
var str = ''
if (!e.detail.value.phone) {
str = '请填写您的手机号'
} else if (e.detail.value.phone.length < 11) {
str = '请填写正确的手机号'
} else if (!e.detail.value.loginPwd) {
str = '请输入密码'
} else if (e.detail.value.loginPwd.length < 6) {
str = '密码必须为6位以上'
}
if (str) {
networkUtil.showErrorToast(str)
return
}
var self = this
var param = e.detail.value
if (!app.globalData.openInfo.hospitalId){
networkUtil.showErrorToast('未从二维码中获取到医院信息')
return
}
param.hospitalId = app.globalData.openInfo.hospitalId
networkUtil.showLoading()
networkUtil._get(api.login, param, function (res) {
console.log(res)
wx.showToast({
title: '登录成功',
icon: 'success',
duration: 2000
})
app.globalData.token = res.data.token
wx.setStorageSync('userToken', res.data.token)
self.homePage()
self.requestHomeInfo()
}, function (res) {
networkUtil.showErrorToast(res.errormsg)
})
}
}))