微信小程序一键登录设计以及实现(微信小程序一键登录安全吗)

流程图

微信小程序一键登录设计以及实现(微信小程序一键登录安全吗)

页面原型

微信小程序一键登录设计以及实现(微信小程序一键登录安全吗)

微信一键登录

微信小程序一键登录设计以及实现(微信小程序一键登录安全吗)

绑定用户手机号码

前台代码

index.wxml

<button class='phone-text' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">请登录</button><!--登录弹窗--><view class="modal-mask" catchtouchmove="preventTouchMove" wx:if="{{showModal}}"></view> <view class="modal-dialog" wx:if="{{showModal}}"> <view class="modal-content"> <view><image src='../images/show.png' class='show'></image></view> <view>绑定手机号</view> <view>请先绑定手机号在进行此操作</view> <button open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber"> <image src='../images/showWx.png' class='iconWx'></image>微信用户一键绑定 </button> </view></view>

index.js

Page({ /** * 页面的初始数据 */ data: { openid: "", loginstate: "0", openid: "", userEntity: null, terminal: "", osVersion: "", phoneNumber: "", showModal: false,//定义登录弹窗 }, //在页面加载的时候,判断缓存中是否有内容,如果有,存入到对应的字段里 onLoad: function () { var that = this; wx.getStorage({ key: 'openid', success: function (res) { that.setData({ openid: res.data }); }, fail: function (res) { that.getcode(); } }); wx.getStorage({ key: 'userEntity', success: function (res) { that.setData({ userEntity: res.data }); }, fail: function (res) { console.log("fail1"); } }); wx.getStorage({ key: 'loginstate', success: function (res) { that.setData({ loginstate: res.data }); }, fail: function (res) { console.log("fail2"); } }); }, onGotUserInfo: function (e) { var that = this; if (e.detail.errMsg == "getUserInfo:ok") { wx.setStorage({ key: "userinfo", data: e.detail.userInfo }) this.setData({ userInfo: e.detail.userInfo }); that.showDialogBtn();//调用一键获取手机号弹窗(自己写的) } }, // 显示一键获取手机号弹窗 showDialogBtn: function () { this.setData({ showModal: true//修改弹窗状态为true,即显示 }) }, // 隐藏一键获取手机号弹窗 hideModal: function () { this.setData({ showModal: false//修改弹窗状态为false,即隐藏 }); }, onshow: function (openid, userInfo, phoneNumber) { var that = this; wx.getSystemInfo({ success: function (res) { that.setData({ terminal: res.model }); that.setData({ osVersion: res.system }); } }) wx.request({ url: '登录接口', method: 'POST', header: { 'content-type': 'application/json' // 默认值 }, data: { username: phoneNumber, parentuser: 'xudeihai', wximg: userInfo.avatarUrl, nickname: userInfo.nickName, identity: "", terminal: that.data.terminal, osVersion: that.data.system, logintype: "10",//微信登录 openid: that.data.openid, }, success(res) { if (res.data.r == "T") { that.setData({ userEntity: res.data.d }); wx.setStorage({ key: "userEntity", data: res.data.d }) that.setData({ loginstate: "1" }); wx.setStorage({ key: "loginstate", data: "1" }) wx.setStorage({ key: 'userinfo', data: "1" }) } else { return; } }, fail(res) { console.log(res); } }) }, //绑定手机 getPhoneNumber: function (e) { var that = this; that.hideModal(); wx.checkSession({ success: function () { wx.login({ success: res => { wx.request({ url: '自己的登录接口', //仅为示例,并非真实的接口地址 data: { account: '1514382701', jscode: res.code }, method: "POST", header: { 'content-type': 'application/json' // 默认值 }, success(res) { if (res.data.r == "T") { wx.setStorage({ key: "openid", data: res.data.openid }) wx.setStorage({ key: "sessionkey", data: res.data.sessionkey }) wx.setStorageSync("sessionkey", res.data.sessionkey); wx.request({ url: '自己的解密接口',//自己的解密地址 data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, code: wx.getStorageSync("sessionkey") }, method: "post", header: { 'content-type': 'application/json' }, success: function (res) { if (res.data.r == "T") { that.onshow(that.data.openid, that.data.userInfo, res.data.d.phoneNumber);//调用onshow方法,并传递三个参数 console.log("登录成功") console.log(res.data.d.phoneNumber)//成功后打印微信手机号 } else { console.log(res); } } }) } } }) } }) }, fail: function () { wx.login({ success: res => { wx.request({ url: '自己的登录接口', //仅为示例,并非真实的接口地址 data: { account: '1514382701', jscode: res.code }, method: "POST", header: { 'content-type': 'application/json' // 默认值 }, success(res) { if (res.data.r == "T") { wx.setStorage({ key: "openid", data: res.data.openid }) wx.setStorage({ key: "sessionkey", data: res.data.sessionkey }) wx.request({ url: '自己的解密接口',//自己的解密地址 data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, code: res.data.sessionkey }, method: "post", header: { 'content-type': 'application/json' }, success: function (res) { that.onshow(that.data.openid, that.data.userInfo, res.data.d.phoneNumber);//调用onshow方法,并传递三个参数 } }) } } }) } }) } }) },})

后台代码

接受code 信息调用微信获取openid

@ApiOperation("乘客微信一键登录") @PostMapping("/customer/loginByWeixin") public Result<JSONObject> loginByWeixin(@RequestParam(value = "code") String code, HttpServletRequest request) { Result<JSONObject> result = new Result<JSONObject>(); if (code == null) { result.setMessage("code不能为空!"); result.setSuccess(false); return result; } String sessionKey = null; String openId = null; try { WxMaJscode2SessionResult wxMaJscode2SessionResult = this.wxService.getUserService().getSessionInfo(code); sessionKey = wxMaJscode2SessionResult.getSessionKey(); openId = wxMaJscode2SessionResult.getOpenid(); } catch (Exception e) { e.printStackTrace(); result.setMessage("微信登录,调用官方接口失败!" e.getMessage()); result.setSuccess(false); return result; } if (sessionKey == null || openId == null) { result.setMessage("微信登录,调用官方接口失败!"); result.setSuccess(false); return result; } LeUser user = leUserService.getUserByOpenid(openId); if (user == null) { //新用户注册 user = new LeUser(); user.setWeixinOpenid(openId); user.setPasswd(openId); user.setStatus(1); user.setLastLoginTime(new Date()); leUserService.save(user); } HashMap<String, String> wxsession = new HashMap<>(); wxsession.put("openid", openId); wxsession.put("sessionkey", sessionKey); //老用户登录 userInfo(user, result); return result; }

绑定用户信息

@AutoLog(value = "乘客解密用户信息&绑定用户信息") @ApiOperation(value = "乘客解密用户信息&绑定用户信息", notes = "乘客解密用户信息&绑定用户信息") @PostMapping(value = "/customer/bindUserinfo") public Result bindUserinfo(@RequestParam(value = "sessionKey") String sessionKey, @RequestParam(value = "encryptedData") String encryptedData, @RequestParam(value = "iv") String iv, HttpServletRequest request) { WxMaUserInfo userInfo = null; try { // 解密用户信息 userInfo = this.wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv); } catch (Exception e) { e.printStackTrace(); return Result.error("解密用户信息失败!" e.getMessage()); } LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = sysUser.getId(); LeUser user = leUserService.getById(userId); user.setNickname(userInfo.getNickName()); user.setUsername(userInfo.getNickName()); user.setAvatar(userInfo.getAvatarUrl()); user.setUsex(userInfo.getGender() == "女" ? 2 : 1); leUserService.updateById(user); return Result.OK(user); }

绑定乘客信息

@AutoLog(value = "乘客解密用户手机号码&绑定用户手机号码") @ApiOperation(value = "乘客解密用户手机号码&绑定用户手机号码", notes = "乘客解密用户手机号码&绑定用户手机号码") @PostMapping(value = "/customer/bindUserphone") public Result bindUserphone(@RequestParam(value = "sessionKey") String sessionKey, @RequestParam(value = "encryptedData") String encryptedData, @RequestParam(value = "iv") String iv, HttpServletRequest request) { WxMaPhoneNumberInfo phoneNumberInfo = null; try { // 解密用户信息 phoneNumberInfo = this.wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); } catch (Exception e) { e.printStackTrace(); return Result.error("解密用户信息失败!" e.getMessage()); } LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String userId = sysUser.getId(); LeUser user = leUserService.getById(userId); user.setMobile(phoneNumberInfo.getPhoneNumber()); leUserService.updateById(user); return Result.OK(user); }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023年4月5日 上午9:01
下一篇 2023年4月5日 上午9:11

相关推荐

  • 家具管理软件定制的好处有哪些(家具管理软件定制的好处有哪些呢)

    家具管理软件定制的好处有哪些?对于家具企业来说,使用通用软件功能虽多,但由于其通用性,很多功能对各个企业是无用的,多余的。一个通用软件80%的功能都对企业无用的时候,剩下的20%再…

    科研百科 2022年7月29日
    218
  • 学生成长档案系统

    学生成长档案系统 学生成长档案系统是一种用于记录学生成长过程和成果的数字化工具。通过这个系统,学校可以对学生进行全面的评估,了解他们的学习表现、课外活动、家庭背景等方面的信息,为他…

    科研百科 2024年10月7日
    17
  • 村里的钱怎样花,咱村民说了算(村里该村民的钱,村民应该怎么办)

    “按照去年费用,今年购买种子、蔬菜苗等预计需要5万元,购买化肥、农药、地膜、包装等预计需要7万元,种植、管理等人工费、机械费约需19万元,具体明细已经发给大家,请大家投票表决。”“…

    科研百科 2022年7月4日
    244
  • 开源 平台系统监管平台

    开源平台系统监管平台: 促进开源平台可持续发展的关键 开源平台系统监管平台是一种创新性的监管方式,能够帮助企业和机构更好地管理和监管他们的开源平台系统。随着开源技术的不断发展和应用…

    科研百科 2024年10月24日
    0
  • 梦龙“双标”,消费者怒了(梦龙双标引争议)

    梦龙冰激凌上热搜了,但并不是因为它定位中高端的热销产品。 日前,有微博名为“游戏打折情报”的博主在社交媒体发文表示,梦龙国内版冰淇淋的外层巧克力和其它地区的应该一样,都是来自比利时…

    科研百科 2023年10月28日
    157
  • 中山大学毛囊再生研究团队

    中山大学毛囊再生研究团队:探索毛囊再生的新篇章 近年来,随着医学技术的不断发展,毛囊再生的研究受到了越来越多的关注和重视。毛囊是毛发的源泉,是人类头发的生长和维持的必要条件。由于毛…

    科研百科 2024年10月21日
    3
  • 科研项目有哪些特点

    科研项目的特点 科研项目的特点是多种多样的,但是它们都有一个共同点,那就是都是关于人类的知识和创新。科研项目有许多不同的类型,包括自然科学、社会科学、工程技术等等。每个类型的科研项…

    科研百科 2025年3月5日
    2
  • 施工进度管理用什么BIM软件?Innovaya Visual 4D Simulation介绍(bim施工进度模拟软件)

    Innovaya系列软件不仅支持施工进度管理,也支持工程算量以及造价管理,在进度管理方面,Innovaya Visual 4D Simulation软件兼容Autodesk公司的P…

    科研百科 2022年7月12日
    344
  • 全能签官网免费企业版

    全能签官网免费企业版: 企业数字签名认证平台的最佳选择 随着数字签名技术的不断发展,企业数字签名认证平台已经成为企业数字签名证书管理的重要工具。全能签官网免费企业版是当前最受欢迎的…

    科研百科 2024年10月5日
    25
  • 课题结题延迟申请理由

    课题结题延迟申请理由 在课题开始研究之后,我们一直在努力探索和收集数据,以确保我们的研究具有充分的可行性和有效性。然而,在研究过程中,我们遇到了一些意外的问题,导致我们的研究进度受…

    科研百科 2024年10月11日
    14