人人商城

yixin.account.class.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. class YiXinAccount extends WeAccount {
  8. private $account = null;
  9. public function __construct($account) {
  10. $sql = 'SELECT * FROM ' . tablename('account_yixin') . ' WHERE `acid`=:acid';
  11. $this->account = pdo_fetch($sql, array(':acid' => $account['acid']));
  12. if(empty($this->account)) {
  13. trigger_error('error uniAccount id, can not construct ' . __CLASS__, E_USER_WARNING);
  14. }
  15. $this->account['access_token'] = iunserializer($this->account['access_token']);
  16. }
  17. public function checkSign() {
  18. $token = $this->account['token'];
  19. $signkey = array($token, $_GET['timestamp'], $_GET['nonce']);
  20. sort($signkey, SORT_STRING);
  21. $signString = implode($signkey);
  22. $signString = sha1($signString);
  23. return $signString == $_GET['signature'];
  24. }
  25. public function fetchAccountInfo() {
  26. return $this->account;
  27. }
  28. public function checkIntoManage() {
  29. }
  30. public function queryAvailableMessages() {
  31. $messages = array('text', 'image', 'voice', 'video', 'location', 'subscribe', 'unsubscribe');
  32. if(!empty($this->account['key']) && !empty($this->account['secret'])) {
  33. $messages[] = 'click';
  34. if(!empty($this->account['key'])) {
  35. $messages[] = 'qr';
  36. $messages[] = 'trace';
  37. $messages[] = 'enter';
  38. }
  39. }
  40. return $messages;
  41. }
  42. public function queryAvailablePackets() {
  43. $packets = array('text', 'music', 'news');
  44. if(!empty($this->account['key']) && !empty($this->account['secret'])) {
  45. if(!empty($this->account['key'])) {
  46. $packets[] = 'image';
  47. $packets[] = 'voice';
  48. $packets[] = 'video';
  49. $packets[] = 'link';
  50. $packets[] = 'card';
  51. }
  52. }
  53. return $packets;
  54. }
  55. public function isMenuSupported() {
  56. return !empty($this->account['key']) && !empty($this->account['secret']);
  57. }
  58. private function menuResponseParse($content) {
  59. if(!is_array($content)) {
  60. return error(-1, '接口调用失败,请重试!' . (is_string($content) ? "易信公众平台返回元数据: {$content}" : ''));
  61. }
  62. $dat = $content['content'];
  63. $result = @json_decode($dat, true);
  64. if(is_array($result) && $result['errcode'] == '0') {
  65. return true;
  66. } else {
  67. if(is_array($result)) {
  68. return error(-1, "易信公众平台返回接口错误. \n错误代码为: {$result['errcode']} \n错误信息为: {$result['errmsg']} \n错误描述为: " . $this->errorCode($result['errcode']));
  69. } else {
  70. return error(-1, '易信公众平台未知错误');
  71. }
  72. }
  73. }
  74. private function menuBuildMenuSet($menu) {
  75. $set = array();
  76. $set['button'] = array();
  77. foreach($menu as $m) {
  78. $entry = array();
  79. $entry['name'] = urlencode($m['title']);
  80. if(!empty($m['subMenus'])) {
  81. $entry['sub_button'] = array();
  82. foreach($m['subMenus'] as $s) {
  83. $e = array();
  84. $e['type'] = $s['type'] == 'url' ? 'view' : 'click';
  85. $e['name'] = urlencode($s['title']);
  86. if($e['type'] == 'view') {
  87. $e['url'] = $s['url'];
  88. } else {
  89. $e['key'] = urlencode($s['forward']);
  90. }
  91. $entry['sub_button'][] = $e;
  92. }
  93. } else {
  94. $entry['type'] = $m['type'] == 'url' ? 'view' : 'click';
  95. if($entry['type'] == 'view') {
  96. $entry['url'] = $m['url'];
  97. } else {
  98. $entry['key'] = urlencode($m['forward']);
  99. }
  100. }
  101. $set['button'][] = $entry;
  102. }
  103. $dat = json_encode($set);
  104. $dat = urldecode($dat);
  105. return $dat;
  106. }
  107. public function menuCreate($menu) {
  108. $dat = $this->menuBuildMenuSet($menu);
  109. $token = $this->fetch_token();
  110. $url = "https://api.yixin.im/cgi-bin/menu/create?access_token={$token}";
  111. $content = ihttp_post($url, $dat);
  112. return $this->menuResponseParse($content);
  113. }
  114. public function menuDelete() {
  115. $token = $this->fetch_token();
  116. $url = "https://api.yixin.im/cgi-bin/menu/delete?access_token={$token}";
  117. $content = ihttp_get($url);
  118. return $this->menuResponseParse($content);
  119. }
  120. public function menuModify($menu) {
  121. return $this->menuCreate($menu);
  122. }
  123. public function menuQuery() {
  124. $token = $this->fetch_token();
  125. $url = "https://api.yixin.im/cgi-bin/menu/get?access_token={$token}";
  126. $content = ihttp_get($url);
  127. if(!is_array($content)) {
  128. return error(-1, '接口调用失败,请重试!' . (is_string($content) ? "易信公众平台返回元数据: {$content}" : ''));
  129. }
  130. $dat = $content['content'];
  131. $result = @json_decode($dat, true);
  132. if(is_array($result) && !empty($result['menu'])) {
  133. $menus = array();
  134. foreach($result['menu']['button'] as $val) {
  135. $m = array();
  136. $m['type'] = $val['type'] == 'click' ? 'forward' : 'url';
  137. $m['title'] = $val['name'];
  138. if($m['type'] == 'forward') {
  139. $m['forward'] = $val['key'];
  140. } else {
  141. $m['url'] = $val['url'];
  142. }
  143. $m['subMenus'] = array();
  144. if(!empty($val['sub_button'])) {
  145. foreach($val['sub_button'] as $v) {
  146. $s = array();
  147. $s['type'] = $v['type'] == 'click' ? 'forward' : 'url';
  148. $s['title'] = $v['name'];
  149. if($s['type'] == 'forward') {
  150. $s['forward'] = $v['key'];
  151. } else {
  152. $s['url'] = $v['url'];
  153. }
  154. $m['subMenus'][] = $s;
  155. }
  156. }
  157. $menus[] = $m;
  158. }
  159. return $menus;
  160. } else {
  161. if(is_array($result)) {
  162. if($result['errcode'] == '46003') {
  163. return array();
  164. }
  165. return error(-1, "易信公众平台返回接口错误. \n错误代码为: {$result['errcode']} \n错误信息为: {$result['errmsg']} \n错误描述为: " . $this->errorCode($result['errcode']));
  166. } else {
  167. return error(-1, '易信公众平台未知错误');
  168. }
  169. }
  170. }
  171. private function fetch_token() {
  172. load()->func('communication');
  173. if(is_array($this->account['access_token']) && !empty($this->account['access_token']['token']) && !empty($this->account['access_token']['expire']) && $this->account['access_token']['expire'] > TIMESTAMP) {
  174. return $this->account['access_token']['token'];
  175. } else {
  176. if (empty($this->account['key']) || empty($this->account['secret'])) {
  177. itoast('请填写公众号的appid及appsecret, (需要你的号码为易信服务号)!', url('account/post', array('acid' => $this->account['acid'], 'uniacid' => $this->account['uniacid'])), 'error');
  178. }
  179. $url = "https://api.yixin.im/cgi-bin/token?grant_type=client_credential&appid={$this->account['key']}&secret={$this->account['secret']}";
  180. $content = ihttp_get($url);
  181. if(is_error($content)) {
  182. itoast('获取微信公众号授权失败, 请稍后重试!错误详情: ' . $content['message'], '', 'error');
  183. }
  184. $token = @json_decode($content['content'], true);
  185. if(empty($token) || !is_array($token) || empty($token['access_token']) || empty($token['expires_in'])) {
  186. itoast('获取微信公众号授权失败, 请稍后重试! 公众平台返回原始数据为: <br />' . $content['meta'], '', 'error');
  187. }
  188. $record = array();
  189. $record['token'] = $token['access_token'];
  190. $record['expire'] = TIMESTAMP + $token['expires_in'];
  191. $row = array();
  192. $row['access_token'] = iserializer($record);
  193. pdo_update('account_yixin', $row, array('acid' => $this->account['acid']));
  194. return $record['token'];
  195. }
  196. }
  197. }