人人商城

wxapp.account.class.php 12KB

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