欢迎光临,了解微信小程序开发,就上易用通!

小程序开发之登录 附代码

发布:2018-01-25 09:41浏览: 来源:网络 作者:tianshu

小程序js:

 

  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. //调用API从本地缓存中获取数据
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. wx.login({
  9. success: function(res) {
  10. if (res.code) {
  11. //发起网络请求
  12. wx.getUserInfo({
  13. success: function(ures) {
  14. var userInfo = ures.userInfo
  15. var nickName = userInfo.nickName
  16. var avatarUrl = userInfo.avatarUrl
  17. var gender = userInfo.gender //性别 0:未知、1:男、2:女
  18. var province = userInfo.province
  19. var city = userInfo.city
  20. var country = userInfo.country
  21. wx.request({
  22. url: 'https://m.xxx.com/index.PHP',
  23. data: {
  24. m: 'api',
  25. c: 'wechat' ,
  26. a: 'xcx_do_auth',
  27. code: res.code,
  28. rawData: ures.rawData,
  29. encryptData: ures.encryptData,
  30. encryptedData: ures.encryptedData,
  31. iv: ures.iv,
  32. signature: ures.signature
  33. },
  34. header: {
  35. "Content-Type": "application/x-www-form-urlencoded"
  36. },
  37. success: function(lres) {
  38. console.log(lres.data)
  39. }
  40. })
  41. //request
  42. }
  43. })
  44. //getUserInfo
  45. } else {
  46. console.log('获取用户登录态失败!' + res.errMsg)
  47. }
  48. //login
  49. }
  50. });
  51. wx.checkSession({
  52. success: function(){
  53. //登录态未过期
  54. },
  55. fail: function(){
  56. //登录态过期
  57. wx.login()
  58. }
  59. });
  60. },
  61.  
  62. globalData:{
  63. userInfo:null,
  64. ajaxUrl: "https://m.xxx.com/index.php",
  65. baseUrl: "https://www.xxx.com/",
  66. code : null,
  67. pageNum : 1,
  68. }
  69. })

php

 

  1. //获取session_key
  2.  
  3. function xcx_openid_session_key($code)
  4. {
  5. require_once ROOT_PATH.'/weixin/wxHelper.php';
  6. $wxHelper = new wxHelper();
  7. //小程序配置参数
  8. $xcxConfig = include_once ROOT_PATH.'/weixin/xcxConfig.php';
  9. //crul
  10. $result = $wxHelper->httpGet("https://api.weixin.qq.com/sns/jscode2session?appid={$xcxConfig['AppId']}&secret={$xcxConfig['AppSecret']}&js_code=$code&grant_type=authorization_code");
  11. $result = json_decode($result, true);
  12. if(!empty($result['openid'])){
  13. $res['AppId'] = $xcxConfig['AppId'];
  14. $res['AppSecret'] = $xcxConfig['AppSecret'];
  15. $res['session_key'] = $result['session_key'];
  16. $res['openid'] = $result['openid'];
  17. }else{
  18. $res['errcode'] = 1;
  19. $res['errmsg'] = 'no openid';
  20. }
  21.  
  22. return $res;
  23. }

//登录

 

  1. public function xcx_do_auth()
  2. {
  3. $msg = array();
  4. $code = $_REQUEST['code'];
  5. $rawData = $_REQUEST['rawData'];
  6. $signature = $_REQUEST['signature'];
  7. $encryptData = $_REQUEST['encryptData'];
  8. $encryptedData = $_REQUEST['encryptedData'];
  9. $iv = $_REQUEST['iv'];
  10.  
  11.  
  12. //验证code
  13. if(empty($code))
  14. {
  15. $msg['error'] = '106';
  16. $msg['msg'] = 'lack of the param code';
  17. die(json_encode($msg));
  18. }
  19. //请求微信服务器,获取openid和session_key
  20. $result = xcx_openid_session_key($code);
  21. if(empty($result['errmsg'])){
  22.  
  23. $rawData2 = str_replace("\\", "", $rawData);
  24. $rawData3 = $rawData2.$result['session_key'];
  25. $signature2 = sha1($rawData3);
  26. if($signature == $signature2){
  27. include_once(ROOT_PATH."/weixin/wxBizDataCrypt.php");
  28. $pc = new WXBizDataCrypt($result['AppId'], $result['session_key']);
  29. $lastData = $pc->decryptData($encryptedData, $iv, $data );
  30.  
  31. if (empty($lastData['errorMsg'])) {
  32.  
  33. $lastDataArr = json_decode($lastData, true);
  34. $lastDataArr2['openid'] = $lastDataArr['openId'];
  35. $lastDataArr2['nickName'] = $lastDataArr['nickName'];
  36. $lastDataArr2['sex'] = $lastDataArr['gender'];
  37. $lastDataArr2['language'] = $lastDataArr['language'];
  38. $lastDataArr2['city'] = $lastDataArr['city'];
  39. $lastDataArr2['province'] = $lastDataArr['province'];
  40. $lastDataArr2['country'] = $lastDataArr['country'];
  41. $lastDataArr2['headimgurl'] = $lastDataArr['avatarUrl'];
  42. $lastDataArr2['unionid'] = $lastDataArr['unionId'];
  43. self::update_xcxweixin_user($lastDataArr2); //更新用户信息
  44. die($_SESSION['user_id']);
  45. } else {
  46. $msg['error'] = '106';
  47. $msg['msg'] = 'lack of the param unionId';
  48. die(json_encode($msg, true));
  49. }
  50. }else{
  51. $msg['error'] = '106';
  52. $msg['msg'] = 'lack of the param signature';
  53. die(json_encode($msg));
  54. }
  55. }else{
  56. $msg['error'] = '106';
  57. $msg['msg'] = 'lack of the param openid';
  58. die(json_encode($msg));
  59. }
  60.  
  61. }




免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。