classs('weixin.account'); class coupon extends WeixinAccount { public $account = null; public function __construct($acid = '') { $this->account_api = self::create($acid); $this->account = $this->account_api->account; } public function getAccessToken() { return $this->account_api->getAccessToken(); } public function getCardTicket() { $cachekey = cache_system_key('cardticket', array('uniacid' => $this->account['uniacid'])); $cache = cache_load($cachekey); if (!empty($cache) && !empty($cache['ticket']) && $cache['expire'] > TIMESTAMP) { $this->account['card_ticket'] = $cache; return $cache['ticket']; } load()->func('communication'); $access_token = $this->getAccessToken(); if (is_error($access_token)) { return $access_token; } $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=wx_card"; $content = ihttp_get($url); if (is_error($content)) { return error(-1, '调用接口获取微信公众号 card_ticket 失败, 错误信息: ' . $content['message']); } $result = @json_decode($content['content'], true); if (empty($result) || 0 != intval(($result['errcode'])) || 'ok' != $result['errmsg']) { return error(-1, '获取微信公众号 card_ticket 结果错误, 错误信息: ' . $result['errmsg']); } $record = array(); $record['ticket'] = $result['ticket']; $record['expire'] = TIMESTAMP + $result['expires_in'] - 200; $this->account['card_ticket'] = $record; cache_write($cachekey, $record); return $record['ticket']; } public function LocationLogoupload($logo) { global $_W; if (!strexists($logo, 'http://') && !strexists($logo, 'https://')) { $path = rtrim(IA_ROOT . '/' . $_W['config']['upload']['attachdir'], '/') . '/'; if (empty($logo) || !file_exists($path . $logo)) { return error(-1, '商户LOGO不存在'); } } else { return error(-1, '商户LOGO只能上传本地图片'); } $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}"; $data = array( 'buffer' => '@' . $path . $logo, ); load()->func('communication'); $response = ihttp_request($url, $data); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function SetTestWhiteList($data) { global $_W; $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function LocationAdd($data) { if (empty($data)) { return error(-1, '门店信息错误'); } $token = $this->getAccessToken(); if (is_error($token)) { return $token; } if (!empty($data['category'])) { $data['category'] = array(rtrim(implode(',', array_values($data['category'])), ',')); } $data['categories'] = $data['category']; unset($data['category']); $data['offset_type'] = 1; $post = array( 'business' => array( 'base_info' => $data, ), ); $post = stripslashes(urldecode(ijson_encode($post, JSON_UNESCAPED_UNICODE))); $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token={$token}"; $result = $this->requestApi($url, $post); return $result; } public function LocationEdit($data) { if (empty($data)) { return error(-1, '门店信息错误'); } $post = array( 'business' => array( 'base_info' => $data, ), ); $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, urldecode(json_encode($post))); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function LocationDel($id) { if (empty($id)) { return error(-1, '门店信息错误'); } $post = array( 'poi_id' => $id, ); $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($post)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function LocationBatchGet($data = array()) { if (empty($data['begin'])) { $data['begin'] = 0; } if (empty($data['limit'])) { $data['limit'] = 50; } $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function LocationGet($id) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $data = array( 'poi_id' => $id, ); $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function GetColors() { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/getcolors?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function isCouponSupported() { global $_W; load()->model('module'); $we7_coupon_module = module_fetch('we7_coupon'); $setting = array(); if (!empty($we7_coupon_module)) { $setting = $we7_coupon_module['config']; } else { $setting = uni_setting($_W['uniacid'], array('coupon_type')); } if ($_W['account']['level'] != ACCOUNT_SERVICE_VERIFY && $_W['account']['level'] != ACCOUNT_SUBSCRIPTION_VERIFY) { return false; } else { if (!empty($setting['setting']['coupon_type'])) { if ($setting['setting']['coupon_type'] == SYSTEM_COUPON) { return false; } else { return true; } } else { if (SYSTEM_COUPON == $setting['coupon_type']) { return false; } else { return true; } } } } public function CreateCard($card) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/create?access_token={$token}"; load()->func('communication'); $card = stripslashes(urldecode(ijson_encode($card, JSON_UNESCAPED_UNICODE))); $response = $this->requestApi($url, $card); return $response; } public function DeleteCard($card_id) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/delete?access_token={$token}"; load()->func('communication'); $card = json_encode(array('card_id' => $card_id)); $response = ihttp_request($url, $card); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function setActivateUserForm($card_id) { global $_W; $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $data['required_form']['common_field_id_list'] = array('USER_FORM_INFO_FLAG_MOBILE'); $data['card_id'] = $card_id; $data['bind_old_card'] = array('name' => '绑定老会员卡', 'url' => 'www.weixin.qq.com'); $url = "https://api.weixin.qq.com/card/membercard/activateuserform/set?access_token={$token}"; load()->func('communication'); $result = $this->requestApi($url, json_encode($data)); return $result; } public function activateMemberCard($data) { global $_W; $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/membercard/activate?access_token={$token}"; load()->func('communication'); $result = $this->requestApi($url, json_encode($data)); return $result; } public function ModifyStockCard($card_id, $num) { $data['card_id'] = trim($card_id); $data['increase_stock_value'] = 0; $data['reduce_stock_value'] = 0; $num = intval($num); ($num > 0) && ($data['increase_stock_value'] = $num); ($num < 0) && ($data['reduce_stock_value'] = abs($num)); $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/modifystock?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function QrCard($card_id, $sceneid, $expire = '') { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/qrcode/create?access_token={$token}"; load()->func('communication'); $data = array( 'action_name' => 'QR_CARD', 'expire_seconds' => "{$expire}", 'action_info' => array( 'card' => array( 'card_id' => strval($card_id), 'code' => '', 'openid' => '', 'is_unique_code' => false, 'outer_id' => $sceneid, ), ), ); $result = $this->requestApi($url, json_encode($data)); return $result; } public function sendCoupons($coupon, $openids) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $post = array( 'touser' => $openids, 'wxcard' => array('card_id' => $coupon), 'msgtype' => 'wxcard', ); $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=' . $token; $result = $this->requestApi($url, json_encode($post)); return $result; } public function UnavailableCode($data) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/code/unavailable?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function ConsumeCode($data) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/code/consume?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function selfConsume($data) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/selfconsumecell/set?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function DecryptCode($data) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/code/decrypt?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function fetchCard($card_id) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $data = array( 'card_id' => $card_id, ); $url = "https://api.weixin.qq.com/card/get?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result['card']; } public function updateMemberCard($post) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/update?access_token={$token}"; $result = $this->requestApi($url, urldecode(json_encode($post))); return $result; } public function batchgetCard($data) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $url = "https://api.weixin.qq.com/card/batchget?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function updateCard($card_id) { $token = $this->getAccessToken(); if (is_error($token)) { return $token; } $data = array( 'card_id' => $card_id, ); $url = "https://api.weixin.qq.com/card/membercard/activate?access_token={$token}"; load()->func('communication'); $response = ihttp_request($url, json_encode($data)); if (is_error($response)) { return error(-1, "访问公众平台接口失败, 错误: {$response['message']}"); } $result = @json_decode($response['content'], true); if (empty($result)) { return error(-1, "接口调用失败, 元数据: {$response['meta']}"); } elseif (!empty($result['errcode'])) { return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}"); } return $result; } public function PayConsumeCode($data) { $code_error['uniacid'] = $this->account['uniacid']; $code_error['acid'] = $this->account['acid']; $code_error['type'] = 2; $code_error['message'] = $data['encrypt_code']; $code_error['dateline'] = time(); $code_error['module'] = $data['module']; $code_error['params'] = $data['card_id']; $code = $this->DecryptCode(array('encrypt_code' => $data['encrypt_code'])); if (is_error($code)) { pdo_insert('core_queue', $code_error); } else { $sumecode = $this->ConsumeCode(array('code' => $code['code'])); if (is_error($sumecode)) { pdo_insert('core_queue', $code_error); } else { pdo_update('coupon_record', array('status' => 3, 'usetime' => time()), array('acid' => $this->account['acid'], 'code' => $code['code'], 'card_id' => $data['card_id'])); } } return true; } public function SignatureCard($data) { $ticket = $this->getCardTicket(); if (is_error($ticket)) { return $ticket; } $data[] = $ticket; sort($data, SORT_STRING); return sha1(implode($data)); } public function BuildCardExt($id, $openid = '', $type = 'coupon') { global $_W; if ('membercard' == $type) { $card_id = pdo_getcolumn('mc_card', array('uniacid' => $_W['uniacid']), 'card_id'); } else { $acid = $this->account['acid']; $card_id = pdo_fetchcolumn('SELECT card_id FROM ' . tablename('coupon') . ' WHERE acid = :acid AND id = :id', array(':acid' => $acid, ':id' => $id)); if (empty($card_id)) { return error(-1, '卡券id不合法'); } } if (empty($card_id)) { $card_id = $id; } $time = TIMESTAMP; $sign = array($card_id, $time); $signature = $this->SignatureCard($sign); if (is_error($signature)) { return $signature; } $cardExt = array('timestamp' => $time, 'signature' => $signature); $cardExt = json_encode($cardExt); return array('card_id' => $card_id, 'card_ext' => $cardExt); } public function AddCard($id) { $card = $this->BuildCardExt($id); if (is_error($card)) { return $card; } $url = murl('activity/coupon/mine'); return <<BuildCardExt($id); if (is_error($card)) { return $card; } $url = murl('activity/coupon/mine'); return <<account['key']); $signature = $this->SignatureCard($sign); if (is_error($signature)) { return $signature; } $url = murl('wechat/pay/card'); return <<