Applet.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace User\Lib;
  3. use Dever;
  4. # 小程序
  5. class Applet
  6. {
  7. public function getOpenid($uid, $system_source = 5, $project_id = 1)
  8. {
  9. $openid = false;
  10. $code = Dever::input('code');
  11. if ($code) {
  12. $info = $this->getInfo($code);
  13. if (isset($info['openid'])) {
  14. $openid = $info['openid'];
  15. Dever::load('user/lib/wechat')->update($uid, $openid, $system_source, $project_id);
  16. }
  17. }
  18. if (!$openid && $uid > 0) {
  19. $wechat = Dever::load('user/lib/wechat')->getInfo($uid, $system_source, $project_id);
  20. if ($wechat) {
  21. $openid = $wechat['openid'];
  22. }
  23. }
  24. return $openid;
  25. }
  26. private function getInfo($code)
  27. {
  28. $session_key = Dever::input('session_key');
  29. if ($session_key) {
  30. return array('session_key' => $session_key, 'openid' => $openid);
  31. }
  32. $applet = $this->getApplet();
  33. $appid = $applet['appid'];
  34. $secret = $applet['secret'];
  35. $url = $applet['url'];
  36. if (!$applet || !$applet['appid'] || !$applet['secret']) {
  37. Dever::alert('错误的appid');
  38. }
  39. $url .= '?appid=' . $appid;
  40. $url .= '&secret=' . $secret;
  41. $url .= '&js_code=' . $code;
  42. $url .= '&grant_type=authorization_code';
  43. $data = Dever::curl($url);
  44. Dever::log($data, 'passport_applet');
  45. if (strstr($data, 'errcode')) {
  46. Dever::alert($data);
  47. }
  48. $data = Dever::json_decode($data);
  49. return $data;
  50. }
  51. private function unionid($session_key)
  52. {
  53. $data = $this->decryptData($session_key);
  54. if ($data && isset($data->unionId)) {
  55. return $data->unionId;
  56. }
  57. return false;
  58. }
  59. private function getWechatData($session_key)
  60. {
  61. $result = array();
  62. $data = $this->decryptData($session_key);
  63. $result['openid'] = '';
  64. $result['unionid'] = '';
  65. $result['mobile'] = '';
  66. $result['phone'] = '';
  67. if ($data && isset($data->openId)) {
  68. $result['openid'] = $data->openId;
  69. if (isset($data->unionId)) {
  70. $result['unionid'] = $data->unionId;
  71. }
  72. }
  73. if ($data && isset($data->phoneNumber)) {
  74. if (isset($data->phoneNumber)) {
  75. $result['phone'] = $data->phoneNumber;
  76. }
  77. }
  78. if ($data && isset($data->purePhoneNumber)) {
  79. if (isset($data->purePhoneNumber)) {
  80. $result['mobile'] = $data->purePhoneNumber;
  81. }
  82. }
  83. return $result;
  84. }
  85. private function decryptData($session_key)
  86. {
  87. $iv = Dever::input('iv');
  88. $encryptedData = Dever::input('encryptedData');
  89. if (!$iv || !$encryptedData) {
  90. return false;
  91. }
  92. if (strlen($session_key) != 24) {
  93. return false;
  94. }
  95. if (strlen($iv) != 24) {
  96. return false;
  97. }
  98. $aesKey = base64_decode($session_key);
  99. $aesIV = base64_decode($iv);
  100. $aesCipher = base64_decode($encryptedData);
  101. $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  102. $dataObj = json_decode($result);
  103. if ($dataObj == NULL) {
  104. return false;
  105. }
  106. /*
  107. $applet = Dever::config('base', 'project')->applet;
  108. $appid = $applet['appid'];
  109. if($dataObj->watermark->appid != $appid) {
  110. return false;
  111. }*/
  112. return $dataObj;
  113. }
  114. private function getApplet()
  115. {
  116. $applet = Dever::config('base', 'project')->applet;
  117. $system = Dever::input('system', 1);
  118. $system = Dever::db('passport/system')->find($system);
  119. $project = false;
  120. if ($system && $system['token_project_id'] > 0 && Dever::project('token')) {
  121. $project = Dever::db('token/project')->find($system['token_project_id']);
  122. }
  123. if ($project) {
  124. $applet['appid'] = $project['appid'];
  125. $applet['secret'] = $project['secret'];
  126. }
  127. if (!$applet || !$applet['appid'] || !$applet['secret']) {
  128. Dever::alert('错误的appid');
  129. }
  130. return $applet;
  131. }
  132. }