wxapp.account.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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()->func('communication');
  8. class WxappAccount extends WeAccount {
  9. public function __construct($account = array()) {
  10. $this->account = $account;
  11. $this->menuFrame = 'wxapp';
  12. $this->type = ACCOUNT_TYPE_APP_NORMAL;
  13. $this->typeName = '小程序';
  14. $this->typeTempalte = '-wxapp';
  15. }
  16. public function accountDisplayUrl() {
  17. return url('wxapp/display');
  18. }
  19. public function fetchAccountInfo() {
  20. $account_table = table('account');
  21. $account = $account_table->getWxappAccount($this->uniaccount['acid']);
  22. $account['encrypt_key'] = $account['key'];
  23. return $account;
  24. }
  25. public function getOauthInfo($code = '') {
  26. global $_W, $_GPC;
  27. if (!empty($_GPC['code'])) {
  28. $code = $_GPC['code'];
  29. }
  30. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->account['key']}&secret={$this->account['secret']}&js_code={$code}&grant_type=authorization_code";
  31. return $response = $this->requestApi($url);
  32. }
  33. public function checkSign() {
  34. $token = $this->account['token'];
  35. $signkey = array($token, $_GET['timestamp'], $_GET['nonce']);
  36. sort($signkey, SORT_STRING);
  37. $signString = implode($signkey);
  38. $signString = sha1($signString);
  39. return $signString == $_GET['signature'];
  40. }
  41. public function pkcs7Encode($encrypt_data, $iv) {
  42. $key = base64_decode($_SESSION['session_key']);
  43. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  44. if (is_error($result)) {
  45. return error(1, '解密失败');
  46. }
  47. $result = json_decode($result, true);
  48. if (empty($result)) {
  49. return error(1, '解密失败');
  50. }
  51. if ($result['watermark']['appid'] != $this->account['key']) {
  52. return error(1, '解密失败');
  53. }
  54. unset($result['watermark']);
  55. return $result;
  56. }
  57. public function checkIntoManage() {
  58. global $_GPC;
  59. if (empty($this->account) || (!empty($this->uniaccount['account']) && $this->uniaccount['account'] != ACCOUNT_TYPE_APP_NORMAL && !defined('IN_MODULE')) || empty($_GPC['version_id'])) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. public function getAccessToken() {
  65. $cachekey = "accesstoken:{$this->account['key']}";
  66. $cache = cache_load($cachekey);
  67. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  68. $this->account['access_token'] = $cache;
  69. return $cache['token'];
  70. }
  71. if (empty($this->account['key']) || empty($this->account['secret'])) {
  72. return error('-1', '未填写小程序的 appid 或 appsecret!');
  73. }
  74. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->account['key']}&secret={$this->account['secret']}";
  75. $response = $this->requestApi($url);
  76. $record = array();
  77. $record['token'] = $response['access_token'];
  78. $record['expire'] = TIMESTAMP + $response['expires_in'] - 200;
  79. $this->account['access_token'] = $record;
  80. cache_write($cachekey, $record);
  81. return $record['token'];
  82. }
  83. public function getJssdkConfig($url = ''){
  84. return array();
  85. }
  86. public function getCodeWithPath($path) {
  87. }
  88. public function getCodeUnlimit($scene, $width = '430', $option = array()) {
  89. if (!preg_match('/[0-9a-zA-Z\!\#\$\&\'\(\)\*\+\,\/\:\;\=\?\@\-\.\_\~]{1,32}/', $scene)) {
  90. return error(1, '场景值不合法');
  91. }
  92. $access_token = $this->getAccessToken();
  93. if(is_error($access_token)){
  94. return $access_token;
  95. }
  96. $data = array(
  97. 'scene' => $scene,
  98. 'width' => intval($width),
  99. );
  100. if (!empty($data['auto_color'])) {
  101. $data['auto_color'] = intval($data['auto_color']);
  102. }
  103. if (!empty($option['line_color'])) {
  104. $data['line_color'] = array(
  105. 'r' => $option['line_color']['r'],
  106. 'g' => $option['line_color']['g'],
  107. 'b' => $option['line_color']['b'],
  108. );
  109. $data['auto_color'] = false;
  110. }
  111. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
  112. $response = $this->requestApi($url, json_encode($data));
  113. if (is_error($response)) {
  114. return $response;
  115. }
  116. return $response['content'];
  117. }
  118. public function getQrcode() {
  119. }
  120. public function errorCode($code, $errmsg = '未知错误') {
  121. $errors = array(
  122. '-1' => '系统繁忙',
  123. '0' => '请求成功',
  124. '40001' => '获取access_token时AppSecret错误,或者access_token无效',
  125. '40002' => '不合法的凭证类型',
  126. '40003' => '不合法的OpenID',
  127. '40004' => '不合法的媒体文件类型',
  128. '40005' => '不合法的文件类型',
  129. '40006' => '不合法的文件大小',
  130. '40007' => '不合法的媒体文件id',
  131. '40008' => '不合法的消息类型',
  132. '40009' => '不合法的图片文件大小',
  133. '40010' => '不合法的语音文件大小',
  134. '40011' => '不合法的视频文件大小',
  135. '40012' => '不合法的缩略图文件大小',
  136. '40013' => '不合法的APPID',
  137. '40014' => '不合法的access_token',
  138. '40015' => '不合法的菜单类型',
  139. '40016' => '不合法的按钮个数',
  140. '40017' => '不合法的按钮个数',
  141. '40018' => '不合法的按钮名字长度',
  142. '40019' => '不合法的按钮KEY长度',
  143. '40020' => '不合法的按钮URL长度',
  144. '40021' => '不合法的菜单版本号',
  145. '40022' => '不合法的子菜单级数',
  146. '40023' => '不合法的子菜单按钮个数',
  147. '40024' => '不合法的子菜单按钮类型',
  148. '40025' => '不合法的子菜单按钮名字长度',
  149. '40026' => '不合法的子菜单按钮KEY长度',
  150. '40027' => '不合法的子菜单按钮URL长度',
  151. '40028' => '不合法的自定义菜单使用用户',
  152. '40029' => '不合法的oauth_code',
  153. '40030' => '不合法的refresh_token',
  154. '40031' => '不合法的openid列表',
  155. '40032' => '不合法的openid列表长度',
  156. '40033' => '不合法的请求字符,不能包含\uxxxx格式的字符',
  157. '40035' => '不合法的参数',
  158. '40038' => '不合法的请求格式',
  159. '40039' => '不合法的URL长度',
  160. '40050' => '不合法的分组id',
  161. '40051' => '分组名字不合法',
  162. '41001' => '缺少access_token参数',
  163. '41002' => '缺少appid参数',
  164. '41003' => '缺少refresh_token参数',
  165. '41004' => '缺少secret参数',
  166. '41005' => '缺少多媒体文件数据',
  167. '41006' => '缺少media_id参数',
  168. '41007' => '缺少子菜单数据',
  169. '41008' => '缺少oauth code',
  170. '41009' => '缺少openid',
  171. '42001' => 'access_token超时',
  172. '42002' => 'refresh_token超时',
  173. '42003' => 'oauth_code超时',
  174. '43001' => '需要GET请求',
  175. '43002' => '需要POST请求',
  176. '43003' => '需要HTTPS请求',
  177. '43004' => '需要接收者关注',
  178. '43005' => '需要好友关系',
  179. '44001' => '多媒体文件为空',
  180. '44002' => 'POST的数据包为空',
  181. '44003' => '图文消息内容为空',
  182. '44004' => '文本消息内容为空',
  183. '45001' => '多媒体文件大小超过限制',
  184. '45002' => '消息内容超过限制',
  185. '45003' => '标题字段超过限制',
  186. '45004' => '描述字段超过限制',
  187. '45005' => '链接字段超过限制',
  188. '45006' => '图片链接字段超过限制',
  189. '45007' => '语音播放时间超过限制',
  190. '45008' => '图文消息超过限制',
  191. '45009' => '接口调用超过限制',
  192. '45010' => '创建菜单个数超过限制',
  193. '45015' => '回复时间超过限制',
  194. '45016' => '系统分组,不允许修改',
  195. '45017' => '分组名字过长',
  196. '45018' => '分组数量超过上限',
  197. '45056' => '创建的标签数过多,请注意不能超过100个',
  198. '45057' => '该标签下粉丝数超过10w,不允许直接删除',
  199. '45058' => '不能修改0/1/2这三个系统默认保留的标签',
  200. '45059' => '有粉丝身上的标签数已经超过限制',
  201. '45157' => '标签名非法,请注意不能和其他标签重名',
  202. '45158' => '标签名长度超过30个字节',
  203. '45159' => '非法的标签',
  204. '46001' => '不存在媒体数据',
  205. '46002' => '不存在的菜单版本',
  206. '46003' => '不存在的菜单数据',
  207. '46004' => '不存在的用户',
  208. '47001' => '解析JSON/XML内容错误',
  209. '48001' => 'api功能未授权',
  210. '50001' => '用户未授权该api',
  211. '40070' => '基本信息baseinfo中填写的库存信息SKU不合法。',
  212. '41011' => '必填字段不完整或不合法,参考相应接口。',
  213. '40056' => '无效code,请确认code长度在20个字符以内,且处于非异常状态(转赠、删除)。',
  214. '43009' => '无自定义SN权限,请参考开发者必读中的流程开通权限。',
  215. '43010' => '无储值权限,请参考开发者必读中的流程开通权限。',
  216. '43011' => '无积分权限,请参考开发者必读中的流程开通权限。',
  217. '40078' => '无效卡券,未通过审核,已被置为失效。',
  218. '40079' => '基本信息base_info中填写的date_info不合法或核销卡券未到生效时间。',
  219. '45021' => '文本字段超过长度限制,请参考相应字段说明。',
  220. '40080' => '卡券扩展信息cardext不合法。',
  221. '40097' => '基本信息base_info中填写的参数不合法。',
  222. '49004' => '签名错误。',
  223. '43012' => '无自定义cell跳转外链权限,请参考开发者必读中的申请流程开通权限。',
  224. '40099' => '该code已被核销。',
  225. '61005' => '缺少接入平台关键数据,等待微信开放平台推送数据,请十分钟后再试或是检查“授权事件接收URL”是否写错(index.php?c=account&amp;a=auth&amp;do=ticket地址中的&amp;符号容易被替换成&amp;amp;)',
  226. '61023' => '请重新授权接入该公众号',
  227. );
  228. $code = strval($code);
  229. if($errors[$code]) {
  230. return $errors[$code];
  231. } else {
  232. return $errmsg;
  233. }
  234. }
  235. protected function requestApi($url, $post = '') {
  236. $response = ihttp_request($url, $post);
  237. $result = @json_decode($response['content'], true);
  238. if(is_error($response)) {
  239. return error($result['errcode'], "访问公众平台接口失败, 错误详情: {$this->errorCode($result['errcode'])}");
  240. }
  241. if(empty($result)) {
  242. return $response;
  243. } elseif(!empty($result['errcode'])) {
  244. return error($result['errcode'], "访问公众平台接口失败, 错误: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  245. }
  246. return $result;
  247. }
  248. public function result($errno, $message = '', $data = '') {
  249. exit(json_encode(array(
  250. 'errno' => $errno,
  251. 'message' => $message,
  252. 'data' => $data,
  253. )));
  254. }
  255. public function getDailyVisitTrend() {
  256. global $_W;
  257. $token = $this->getAccessToken();
  258. if (is_error($token)) {
  259. return $token;
  260. }
  261. $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
  262. $data = array(
  263. 'begin_date' => date('Y-m-d', strtotime('-1 days')),
  264. 'end_date' => date('Y-m-d', strtotime('-1 days'))
  265. );
  266. $response = $this->requestApi($url, json_encode($data));
  267. if (is_error($response)) {
  268. return $response;
  269. }
  270. return $response['list'][0];
  271. }
  272. }