我感觉这个书上的微信小程序登陆写得不好
基本功能是OK,但是感觉传的数据太多,不安全,需要改写。
App({
d: {
hostUrl: 'http://www.test.com/index.php', //请填写您自己的小程序主机URL
appId: "xxx",
appKey: "xxx",
ceshiUrl: 'http://www.test.com/index.php',//请填写您自己的测试URL
},
//小程序初始化完成时触发,全局只触发一次
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs);
//login
this.getUserInfo();
},
getUserInfo: function (cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(that.globalData.userInfo)
} else {
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
wx.login({
success: function (res) {
//console.log(res);
var code = res.code;
//get wx user simple info
wx.getUserInfo({
withCredentials: true,
success: function (res) {
//如果已经授权过那么会执行这里代码,console.log("已授权标记");
that.globalData.userInfo = res.userInfo;
typeof cb == "function" && cb(that.globalData.userInfo);
//get user sessionKey
that.getUserSessionKey(code);
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res)
}
}
});
}
})
} else {
// 没有授权,重定向到 loading 启动页
wx.navigateTo({
url: '../tologin/tologin',
})
}
}
})
}
},
getUserSessionKey: function (code) {
//用户的订单状态
var that = this;
wx.request({
url: that.d.ceshiUrl + '/Api/Login/getsessionkey',
method: 'post',
data: {
code: code
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data = res.data;
if (data.status == 0) {
wx.showToast({
title: data.err,
duration: 2000
});
return false;
}
that.globalData.userInfo['sessionId'] = data.session_key;
that.globalData.userInfo['openid'] = data.openid;
that.onLoginUser();
},
fail: function (e) {
wx.showToast({
title: '网络异常!err:getsessionkeys',
duration: 2000
});
},
});
},
//授权登录
onLoginUser: function () {
var that = this;
var user = that.globalData.userInfo;
wx.request({
url: that.d.ceshiUrl + '/Api/Login/authlogin',
method: 'post',
data: {
SessionId: user.sessionId,
gender: user.gender,
NickName: user.nickName,
HeadUrl: user.avatarUrl,
openid: user.openid
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
//--init data
var data = res.data.arr;
var status = res.data.status;
if (status != 1) {
wx.showToast({
title: res.data.err,
duration: 3000
});
return false;
}
that.globalData.userInfo['id'] = data.ID;
that.globalData.userInfo['NickName'] = data.NickName;
that.globalData.userInfo['HeadUrl'] = data.HeadUrl;
var userId = data.ID;
if (!userId) {
wx.showToast({
title: '登录失败!',
duration: 3000
});
return false;
}
that.d.userId = userId;
},
fail: function (e) {
wx.showToast({
title: '网络异常!err:authlogin',
duration: 2000
});
},
});
},
globalData: {
userInfo: null
},
onPullDownRefresh: function () {
wx.stopPullDownRefresh();
}
});
如果不想首页自动登陆,在其它页调用登陆:
// pages/user/user.js
var app = getApp()
Page({
data: {
userInfo: {},
orderInfo: {},
loadingText: '加载中...',
loadingHidden: false,
},
onLoad: function () {
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function (userInfo) {
//更新数据
that.setData({
userInfo: userInfo,
loadingHidden: true
})
});
console.log("个人中心:--" + app.d.userId);
this.loadOrderStatus();
},
})
我感觉这个书上的微信小程序登陆写得不好的更多相关文章
- nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId
nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId 前言: 我准备用nodejs+koa+uniapp实现一款餐饮点单小程序,以及nodejs+koa+vue实现后端管理 ...
- 补充ABP Zero集成微信小程序登陆的BUG修复部分
感谢园友 @turingguo 发布的 https://www.cnblogs.com/turingguo/p/9019026.html 文章,详细介绍了ABP Zero集成微信小程序登陆的实现过程 ...
- 02月刊(上) | 微信小程序
* { margin: 0; padding: 0 } .con { width: 802px; margin: 0 auto; text-align: center; position: inher ...
- 反编译获取线上任何微信小程序源码(转)
看到人家上线的小程序的效果,纯靠推测,部分效果在绞尽脑汁后能做出大致的实现,但是有些细节,费劲全力都没能做出来.很想一窥源码?查看究竟?看看大厂的前端大神们是如何规避了小程序的各种奇葩的坑?那么赶紧来 ...
- 微信小程序初探--写个扫雷分享给你玩
闲暇里,想学一下微信小程序, 于是,用微信小程序原生做了个扫雷玩. 以下略作总结,分享给大家. 微信里下拉,输入[mini计算器], 看到这个图标的就是了: 说好的扫雷,怎么变成计算器了?原因后面解释 ...
- 微信小程序登陆流程图时序图
微信小程序登录 小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系. 微信小程序登录流程时序图 说明 调用 wx.login() 获取 临时登录凭证cod ...
- 微信小程序-登陆、支付、模板消息
wx.login(OBJECT) 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key).用户数据的加解密通讯需要 ...
- mpvue微信小程序怎么写轮播图,和官方微信代码的差别
目前用mpvue很多第三方的ui库是引入不了的,因为它不支持含有dom操作. 那我们要做轮播图的话一个是手写另外一个就是用小程序的swiper组件了: 官方代码: <swiper indicat ...
- 微信小程序登陆授权
小程序前端代码 function WXlogin(){ wx.login({ success: function (code) { wx.getUserInfo({ success:function( ...
随机推荐
- photoshop7.0 排版一寸照片、2寸照片
说明:必须先照一张一寸电子照片,否则是无法做成 1.本例同样采用photoshop CS5制作,其它版本通用,这里采用上一教程“PS照片处理教程-制作一寸照片并排版”的处理效果图进行排版,首先在PS中 ...
- 【linux基础】如何配置ubuntu系统为静态IP地址
前言 连接远程server重启的时候发现IP发生变化,影响远程连接,此时,需要将server配置为静态IP. 系统环境 ubuntu16.04 操作过程 1. 设置IP和DNS command sud ...
- linux用户态和内核态理解
1.特权级 Intel x86架构的cpu一共有0-4四个特权级,0级最高,3级最低,硬件上在执行每条指令时都会对指令所具有的特权级做相应的检查.硬件已经提供了一套特权级使用的相关机制 ...
- spark 更改日志输出级别
package com.ideal.test import org.apache.spark.{SparkConf, SparkContext} import org.apache.log4j.{Le ...
- [ERROR ]Failed to execute goal org.codehaus.mojo:flatten-maven-plugin:1.1.0:flatten (flatten) on project
今天在启动项目的时候,莫名的Maven install命令的时候出现错误 错误提示:Failed to execute goal org.codehaus.mojo:flatten-maven-plu ...
- Maven依赖中scope的含义
https://www.jianshu.com/p/7145f01ac3ad Maven依赖中scope的含义 整理一下Maven中Scope的详细作用,都是抄的别人内容整理了一下.参考: https ...
- 网站登录注册-Session 和token的总结
1.为什么要使用session 因为http本身是无状态协议,无法确定你的本次请求和上次请求是不是你发送的.如果要进行类似论坛登陆相关的操作,就实现不了了. 2.Session 生成方式 浏览器第一次 ...
- 文件和异常练习——python编程从入门到实践
10-1 Python学习笔记:在文本编辑器中新建一个文件,写几句话来总结一下你至此学习到的python知识,其中每一行都以“In Python you can”打头.将这和文件命名为learning ...
- python requests 超时与重试
一 源起: requests模块作为python爬虫方向的基础模块实际上在日常实际工作中也会涉及到,比如用requests向对方接口url发送POST请求进行推送数据,使用GET请求拉取数据. 但是这 ...
- centos7中mysql的rpm包安装
解决依赖 yum remove mysql-libs 执行命令:yum -y install autoconf 安装依赖 yum -y install autoconf 安装mysql rpm -iv ...