人人商城

wxapp.account.class.php 12KB

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