人人商城

session.ctrl.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('mc');
  8. $dos = array('openid', 'userinfo', 'check');
  9. $do = in_array($do, $dos) ? $do : 'openid';
  10. $account_api = WeAccount::create();
  11. if ($do == 'openid') {
  12. $code = $_GPC['code'];
  13. $openid = $_GPC['openid'];
  14. if (empty($openid) && !empty($_W['openid'])) {
  15. $openid = $_W['openid'];
  16. }
  17. if (empty($_W['account']['oauth']) || (empty($code) && empty($openid))) {
  18. exit('通信错误,请在微信中重新发起请求');
  19. }
  20. if (!empty($openid)) {
  21. $_SESSION['openid'] = $oauth['openid'];
  22. $fans = mc_fansinfo($openid);
  23. if (!empty($fans)) {
  24. $account_api->result(0, '', array('sessionid' => $_W['session_id'], 'userinfo' => $fans));
  25. } else {
  26. $account_api->result(1, 'openid不存在');
  27. }
  28. }
  29. $oauth = $account_api->getOauthInfo($code);
  30. if (!empty($oauth) && !is_error($oauth)) {
  31. $_SESSION['openid'] = $oauth['openid'];
  32. $_SESSION['session_key'] = $oauth['session_key'];
  33. $fans = mc_fansinfo($oauth['openid']);
  34. if (empty($fans)) {
  35. $record = array(
  36. 'openid' => $oauth['openid'],
  37. 'uid' => 0,
  38. 'acid' => $_W['acid'],
  39. 'uniacid' => $_W['uniacid'],
  40. 'salt' => random(8),
  41. 'updatetime' => TIMESTAMP,
  42. 'nickname' => '',
  43. 'follow' => '1',
  44. 'followtime' => TIMESTAMP,
  45. 'unfollowtime' => 0,
  46. 'tag' => '',
  47. );
  48. $email = md5($oauth['openid']).'@we7.cc';
  49. $email_exists_member = pdo_getcolumn('mc_members', array('email' => $email), 'uid');
  50. if (!empty($email_exists_member)) {
  51. $uid = $email_exists_member;
  52. } else {
  53. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  54. $data = array(
  55. 'uniacid' => $_W['uniacid'],
  56. 'email' => $email,
  57. 'salt' => random(8),
  58. 'groupid' => $default_groupid,
  59. 'createtime' => TIMESTAMP,
  60. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  61. 'nickname' => '',
  62. 'avatar' => '',
  63. 'gender' => '',
  64. 'nationality' => '',
  65. 'resideprovince' => '',
  66. 'residecity' => '',
  67. );
  68. pdo_insert('mc_members', $data);
  69. $uid = pdo_insertid();
  70. }
  71. $record['uid'] = $uid;
  72. $_SESSION['uid'] = $uid;
  73. pdo_insert('mc_mapping_fans', $record);
  74. }
  75. $account_api->result(0, '', array('sessionid' => $_W['session_id'], 'userinfo' => $fans, 'openid' => $oauth['openid']));
  76. } else {
  77. $account_api->result(1, $oauth['message']);
  78. }
  79. } elseif ($do == 'userinfo') {
  80. $encrypt_data = $_GPC['encryptedData'];
  81. $iv = $_GPC['iv'];
  82. if (empty($_SESSION['session_key']) || empty($encrypt_data) || empty($iv)) {
  83. $account_api->result(1, '请先登录');
  84. }
  85. $sign = sha1($_POST['rawData'].$_SESSION['session_key']);
  86. if ($sign !== $_GPC['signature']) {
  87. $account_api->result(1, '签名错误');
  88. }
  89. $userinfo = $account_api->pkcs7Encode($encrypt_data, $iv);
  90. $userinfo['nickname'] = $userinfo['nickName'];
  91. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  92. $fans = mc_fansinfo($userinfo['openId']);
  93. $fans_update = array(
  94. 'nickname' => $userinfo['nickName'],
  95. 'unionid' => $userinfo['unionId'],
  96. 'tag' => base64_encode(iserializer(array(
  97. 'subscribe' => 1,
  98. 'openid' => $userinfo['openId'],
  99. 'nickname' => $userinfo['nickName'],
  100. 'sex' => $userinfo['gender'],
  101. 'language' => $userinfo['language'],
  102. 'city' => $userinfo['city'],
  103. 'province' => $userinfo['province'],
  104. 'country' => $userinfo['country'],
  105. 'headimgurl' => $userinfo['avatarUrl'],
  106. ))),
  107. );
  108. if (!empty($userinfo['unionId'])) {
  109. $union_fans = pdo_get('mc_mapping_fans', array('unionid' => $userinfo['unionId'], 'openid !=' => $userinfo['openId']));
  110. if (!empty($union_fans['uid'])) {
  111. if (!empty($fans['uid'])) {
  112. }
  113. $fans_update['uid'] = $union_fans['uid'];
  114. $_SESSION['uid'] = $union_fans['uid'];
  115. }
  116. }
  117. pdo_update('mc_mapping_fans', $fans_update, array('fanid' => $fans['fanid']));
  118. pdo_update('mc_members', array('nickname' => $userinfo['nickName'], 'avatar' => $userinfo['avatarUrl'], 'gender' => $userinfo['gender']), array('uid' => $fans['uid']));
  119. $member = mc_fetch($fans['uid']);
  120. unset($member['password']);
  121. unset($member['salt']);
  122. $account_api->result(0, '', $member);
  123. } elseif ($do == 'check') {
  124. if (!empty($_W['openid'])) {
  125. $account_api->result(0);
  126. } else {
  127. $account_api->result(1, 'session失效,请重新发起登录请求');
  128. }
  129. }