LewaimaiWeixinauth.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class LewaimaiWeixinauth {
  3. public static function getComponentAccessToken()
  4. {
  5. $weixinauthcacheModel = WeixinauthCache::model()->findByPk(1);
  6. $component_access_token = false;
  7. if ($weixinauthcacheModel->component_access_token)
  8. {
  9. $exp_time = strtotime($weixinauthcacheModel->component_access_token_expires_time) - 100;
  10. $time = time();
  11. if($exp_time >= $time) {
  12. $component_access_token = $weixinauthcacheModel->component_access_token;
  13. }
  14. }
  15. if (!$component_access_token)
  16. {
  17. $component_verify_ticket = $weixinauthcacheModel->component_verify_ticket;
  18. $postArray = array();
  19. $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
  20. $postArray["component_appsecret"] = LEWAIMAI_PINGTAI_APPSECRET;
  21. $postArray["component_verify_ticket"] = $component_verify_ticket;
  22. $postJson = json_encode($postArray);
  23. $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
  24. $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
  25. $postresArray = json_decode($post_res, true);
  26. $component_access_token = $postresArray["component_access_token"];
  27. $expires_in = time() + $postresArray["expires_in"];
  28. $weixinauthcacheModel->component_access_token = $component_access_token;
  29. $weixinauthcacheModel->component_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
  30. if (!$weixinauthcacheModel->update())
  31. {
  32. LewaimaiDebug::LogModelError($weixinauthcacheModel);
  33. return false;
  34. }
  35. }
  36. return $component_access_token;
  37. }
  38. public static function getPreauthcode()
  39. {
  40. $pre_auth_code = false;
  41. $weixinauthcacheModel = WeixinauthCache::model()->findByPk(1);
  42. if ($weixinauthcacheModel->pre_auth_code)
  43. {
  44. $exp_time = strtotime($weixinauthcacheModel->pre_auth_code_expires_time) - 100;
  45. $time = time();
  46. if($exp_time >= $time) {
  47. $pre_auth_code = $weixinauthcacheModel->pre_auth_code;
  48. }
  49. }
  50. $pre_auth_code = false;
  51. if (!$pre_auth_code)
  52. {
  53. //如果缓存里面没有pre_auth_code,那么就重新获取
  54. $component_access_token = false;
  55. if ($weixinauthcacheModel->component_access_token)
  56. {
  57. $exp_time = strtotime($weixinauthcacheModel->component_access_token_expires_time) - 100;
  58. $time = time();
  59. if($exp_time >= $time) {
  60. $component_access_token = $weixinauthcacheModel->component_access_token;
  61. }
  62. }
  63. if (!$component_access_token)
  64. {
  65. $component_verify_ticket = $weixinauthcacheModel->component_verify_ticket;
  66. $postArray = array();
  67. $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
  68. $postArray["component_appsecret"] = LEWAIMAI_PINGTAI_APPSECRET;
  69. $postArray["component_verify_ticket"] = $component_verify_ticket;
  70. $postJson = json_encode($postArray);
  71. $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
  72. $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
  73. $postresArray = json_decode($post_res, true);
  74. $component_access_token = $postresArray["component_access_token"];
  75. $expires_in = time() + $postresArray["expires_in"];
  76. $weixinauthcacheModel->component_access_token = $component_access_token;
  77. $weixinauthcacheModel->component_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
  78. if (!$weixinauthcacheModel->update())
  79. {
  80. LewaimaiDebug::LogModelError($weixinauthcacheModel);
  81. return false;
  82. }
  83. }
  84. $postArray = array();
  85. $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
  86. $postJson = json_encode($postArray);
  87. $preauthcode_url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" . $component_access_token;
  88. $post_res = LewaimaiHttp::POSTJSON($preauthcode_url, $postJson);
  89. $postresArray = json_decode($post_res, true);
  90. $pre_auth_code = $postresArray["pre_auth_code"];
  91. $expires_in = time() + $postresArray["expires_in"];
  92. $weixinauthcacheModel->pre_auth_code = $pre_auth_code;
  93. $weixinauthcacheModel->pre_auth_code_expires_time = date('Y-m-d H:i:s', $expires_in);
  94. if (!$weixinauthcacheModel->update())
  95. {
  96. LewaimaiDebug::LogModelError($weixinauthcacheModel);
  97. return false;
  98. }
  99. }
  100. return $pre_auth_code;
  101. }
  102. //获取授权令牌
  103. public static function getAuthorizerAccessToken($adminId)
  104. {
  105. $adminweixinauthinfocacheModel = AdminWeixinauthInfoCache::model()->find("admin_id = " . $adminId);
  106. if (!$adminweixinauthinfocacheModel)
  107. {
  108. Yii::log("no this adminweixinauthinfocache!");
  109. return false;
  110. }
  111. $authorizer_access_token = false;
  112. if ($adminweixinauthinfocacheModel->authorizer_access_token)
  113. {
  114. $exp_time = strtotime($adminweixinauthinfocacheModel->authorizer_access_token_expires_time) - 100;
  115. $time = time();
  116. if($exp_time >= $time) {
  117. $authorizer_access_token = $adminweixinauthinfocacheModel->authorizer_access_token;
  118. }
  119. }
  120. if (!$authorizer_access_token)
  121. {
  122. //此时通过刷新令牌去刷新
  123. $component_access_token = LewaimaiWeixinauth::getComponentAccessToken();
  124. if (!$component_access_token) {
  125. return false;
  126. }
  127. $postArray = array();
  128. $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
  129. $postArray["authorizer_appid"] = $adminweixinauthinfocacheModel->authorizer_appid;
  130. $postArray["authorizer_refresh_token"] = $adminweixinauthinfocacheModel->authorizer_refresh_token;
  131. $postJson = json_encode($postArray);
  132. $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" . $component_access_token;
  133. $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
  134. $postresArray = json_decode($post_res, true);
  135. if (!isset($postresArray["authorizer_access_token"]) || $postresArray["authorizer_access_token"] == "")
  136. {
  137. Yii::log("refresh authorizer_access_token failed! adminId:" . $adminId);
  138. return false;
  139. }
  140. $authorizer_access_token = $postresArray["authorizer_access_token"];
  141. $expires_in = time() + $postresArray["expires_in"];
  142. $authorizer_refresh_token = $postresArray["authorizer_refresh_token"];
  143. $adminweixinauthinfocacheModel->authorizer_access_token = $authorizer_access_token;
  144. $adminweixinauthinfocacheModel->authorizer_access_token_expires_time = date('Y-m-d H:i:s', $expires_in);
  145. $adminweixinauthinfocacheModel->authorizer_refresh_token = $authorizer_refresh_token;
  146. if (!$adminweixinauthinfocacheModel->update()) {
  147. LewaimaiDebug::LogModelError($adminweixinauthinfocacheModel);
  148. return false;
  149. }
  150. }
  151. return $authorizer_access_token;
  152. }
  153. //获取用户的信息(这个不用于第一次绑定时候的获取)
  154. public static function getAuthorizerInfo($adminId){
  155. $adminModel = Admin::model()->findByPk($adminId);
  156. if (!$adminModel)
  157. {
  158. Yii::log("adminModel is NULL!");
  159. return false;
  160. }
  161. $component_access_token = LewaimaiWeixinauth::getComponentAccessToken();
  162. if (!$component_access_token) {
  163. return false;
  164. }
  165. $postArray = array();
  166. $postArray["component_appid"] = LEWAIMAI_PINGTAI_APPID;
  167. $postArray["authorizer_appid"] = $adminModel->appid;
  168. $postJson = json_encode($postArray);
  169. $url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" . $component_access_token;
  170. $post_res = LewaimaiHttp::POSTJSON($url, $postJson);
  171. $postresArray = json_decode($post_res, true);
  172. if (isset($postresArray["errcode"]) && $postresArray["errcode"] != 0)
  173. {
  174. //获取信息失败,重新授权
  175. Yii::log("get authorizer_info failed! errmsg:" . $postresArray["errmsg"]);
  176. return false;
  177. }
  178. $adminModel->wx_name = $postresArray["authorizer_info"]["nick_name"];
  179. if (isset($postresArray["authorizer_info"]["head_img"]))
  180. {
  181. $adminModel->head_img = $postresArray["authorizer_info"]["head_img"];
  182. }
  183. $adminModel->wx_tousername = $postresArray["authorizer_info"]["user_name"];
  184. if (isset($postresArray["authorizer_info"]["alias"]))
  185. {
  186. $adminModel->wx_account = $postresArray["authorizer_info"]["alias"];
  187. }
  188. $adminModel->qrcode_url = $postresArray["authorizer_info"]["qrcode_url"];
  189. $service_type_info = $postresArray["authorizer_info"]["service_type_info"]["id"];
  190. $verify_type_info = $postresArray["authorizer_info"]["verify_type_info"]["id"];
  191. if (($service_type_info == 0 || $service_type_info == 1))
  192. {
  193. if (($verify_type_info == -1 || $verify_type_info == 1 || $verify_type_info == 2))
  194. {
  195. $adminModel->wx_type = 0;
  196. }
  197. else
  198. {
  199. $adminModel->wx_type = 1;
  200. }
  201. }
  202. else
  203. {
  204. if (($verify_type_info == -1 || $verify_type_info == 1 || $verify_type_info == 2))
  205. {
  206. $adminModel->wx_type = 2;
  207. }
  208. else
  209. {
  210. $adminModel->wx_type = 3;
  211. }
  212. }
  213. if (!$adminModel->update()) {
  214. LewaimaiDebug::LogModelError($adminModel);
  215. return false;
  216. }
  217. return true;
  218. }
  219. }