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

流程图

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

页面原型

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

微信一键登录

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

绑定用户手机号码

前台代码

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

相关推荐

  • 报名信息系统项目管理师

    报名信息系统项目管理师 报名信息系统项目管理师是一个重要的职业,负责规划、执行和监督报名信息系统项目的完成。这是一个需要高度技能和经验的职业,因为报名信息系统项目通常涉及大量的时间…

    科研百科 2025年1月26日
    0
  • 激发为战创新活力,战略支援部队某部出台科研攻关制度(战略支援部队新型作战力量)

    来源:中国军网 战略支援部队某部出台科研攻关制度 “揭榜领衔”激发为战创新活力 解放军报讯 特约记者吕炳宏、记者安普忠报道:日前,战略支援部队某部演训大厅内,随着最后一道指令发出,…

    科研百科 2023年10月8日
    138
  • 科研项目怎么审核报告单

    科研项目的审核是确保项目进展和成果质量的关键步骤。审核报告单是一个重要的文件,需要仔细查看和审核以确保项目按照计划顺利进行,并达到预期的成果。在审核报告单时,需要注意以下几点: 1…

    科研百科 2025年5月14日
    2
  • 「PMP考试计算题大全」第六章-项目时间管理(项目时间管理试题及答案)

      项目管理中的计算题总是让人头疼,科科过小编为大家整理了PMP考试中涉及到的计算题,方便大家针对计算题有一个针对性的复习。    第6章 项目时间管理   本节术语较多、涉及的工…

    科研百科 2022年7月21日
    404
  • 卫生健康科研项目立项

    卫生健康科研项目立项 随着人口老龄化和疾病负担的增加,卫生健康领域面临着前所未有的挑战。为了应对这些挑战,国家卫生健康委员会提出了一系列科研项目,以推动卫生健康事业的发展。 本次立…

    科研百科 2025年3月11日
    1
  • 有哪些好用的客服软件?(客服软件哪个好用)

    磨刀不误砍柴工,工欲善其事必先利其器,这两句话都告诉我们一个道理,有一个趁手的好工具往往能够带来事半功倍的效果。那么对于我们客服人员来说,也是一样的,在接待的过程中,拥有一个好的客…

    科研百科 2022年7月20日
    369
  • 科研项目立项课题有哪些

    科研项目立项课题有哪些 随着科技的不断发展,科研项目立项课题越来越多。科研项目立项课题的选择对于项目的成功实施至关重要。本文将介绍一些常见的科研项目立项课题,以帮助读者更好地选择适…

    科研百科 2025年5月22日
    1
  • 中铁十九局04年有新项目吗

    中铁十九局在04年有很多新项目。该公司是一家著名的中国土木工程师公司,致力于为客户提供高质量的土木工程服务。在04年,中铁十九局开展了多项新项目,其中包括: 1. 铁路项目:中铁十…

    科研百科 2024年10月12日
    12
  • 骨质疏松方面的科研项目

    骨质疏松方面的科研项目 随着人口老龄化和生活方式的改变,骨质疏松已成为一个全球性的问题。骨质疏松是指骨组织发生骨折或失去功能,导致身体失去大量的钙和其他营养物质。骨质疏松对于老年人…

    科研百科 2025年4月21日
    1
  • WINCC V7.0建项目和通讯(wincc7.0通讯设置)

    新建一个项目 在WinCC资源管理器中,单击菜单上“文件”按钮并在下拉菜单中选择“新建”按钮(或在工具栏上单击“新建”按钮),出现“WinCC Explorer” 对话框,我们一般…

    科研百科 2023年7月29日
    454