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

流程图

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

页面原型

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

微信一键登录

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

绑定用户手机号码

前台代码

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

相关推荐

  • 项目管理工时系统

    项目管理工时系统 随着信息技术的不断发展,项目管理已经成为企业进行项目管理的重要工具。项目管理工时系统是一种专门用于项目管理的工具,它可以帮助项目经理有效地管理项目进度、资源、成本…

    科研百科 2024年12月18日
    0
  • 齐鲁红星丨党建引领 助力经营管理提升(党建引领经营工作)

    中石化济南石油经营业务党支部始终坚持把政治建设摆在首位,坚持以习近平新时代中国特色社会主义思想为指引,紧紧围绕强基固本和服务提升这两项核心主题,发挥党支部战斗堡垒和党员先锋模范作用…

    科研百科 2023年11月17日
    167
  • 科研项目终止的一般原因

    科研项目终止的一般原因 科研项目的终止是科研过程中常见的问题。原因有很多,以下是一些常见的原因: 1. 无法找到合适的研究资源:科研项目需要大量的研究资源,如实验室设备、资金支持、…

    科研百科 2024年4月10日
    118
  • 郑州颐和医院oa协同办公系统(协同办公系统)

    协同办公系统:让办公更高效 随着数字化时代的到来,协同办公系统已经成为现代企业不可或缺的一部分。协同办公系统可以帮助企业实现内部信息的共享、协作和沟通,从而提高工作效率和团队合作能…

    科研百科 2024年6月8日
    1.1K
  • 建立工程项目管理台账

    建立工程项目管理台账是项目管理中至关重要的一步,可以帮助我们更好地跟踪项目的进度、成本、质量和风险等方面的情况,为项目的顺利完成提供有力的支持。 在建立工程项目管理台账时,我们需要…

    科研百科 2024年8月29日
    34
  • 普天新零售管理系统项目

    普天新零售管理系统项目 普天新零售管理系统是一套针对零售行业进行定制化开发的管理系统,旨在提高企业的运营效率和管理水平。该系统涵盖了从库存管理、销售管理、财务管理等多个方面,可以帮…

    科研百科 2025年1月27日
    0
  • 农业部关于印发《现代农业人才支撑计划项目资金管理办法》的通知

    各有关单位: 为进一步加强和规范我部现代农业人才支撑计划项目管理,提高项目资金使用效益,我部制定了《现代农业人才支撑计划项目资金管理办法》。现印发给你们,请遵照执行。 农业部 20…

    科研百科 2022年10月3日
    249
  • 协同办公系统的好处(什么是协同办公系统)

    什么是协同办公系统? 协同办公系统是一种用于管理和协调多个团队成员和组织的计算机软件系统。这些系统通常用于共享文件、日程安排、任务分配、沟通和协作,以加快工作流程、提高工作效率和减…

    科研百科 2024年6月3日
    96
  • 项目自筹资金来源说明

    项目自筹资金来源说明 随着现代社会的不断发展,企业和个人都面临着越来越多的挑战和机遇。在项目的开发过程中,资金是一个非常重要的因素,它关系到项目的进度、质量和可行性。因此,如何寻找…

    科研百科 2024年11月13日
    2
  • 财务软件哪个好?如何选择才合适?(财务软件哪个好-如何选择才合适呢)

    有关于财务的工作,都是有严格的规定,企业可以顺利稳定的经营下去,也是因为财务相关工作都可以正常完成,保证收支流水可以正常运转,如此可以协助财务工作顺利完成的软件,自然现在也是很受欢…

    科研百科 2022年7月31日
    241