人人商城

coupon.class.php 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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()->classs('weixin.account');
  8. class coupon extends WeixinAccount {
  9. public $account = null;
  10. public function __construct($acid = '') {
  11. $this->account_api = self::create($acid);
  12. $this->account = $this->account_api->account;
  13. }
  14. public function getAccessToken() {
  15. return $this->account_api->getAccessToken();
  16. }
  17. public function getCardTicket(){
  18. $cachekey = cache_system_key('cardticket', array('acid' => $this->account['acid']));
  19. $cache = cache_load($cachekey);
  20. if (!empty($cache) && !empty($cache['ticket']) && $cache['expire'] > TIMESTAMP) {
  21. $this->account['card_ticket'] = $cache;
  22. return $cache['ticket'];
  23. }
  24. load()->func('communication');
  25. $access_token = $this->getAccessToken();
  26. if(is_error($access_token)){
  27. return $access_token;
  28. }
  29. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=wx_card";
  30. $content = ihttp_get($url);
  31. if(is_error($content)) {
  32. return error(-1, '调用接口获取微信公众号 card_ticket 失败, 错误信息: ' . $content['message']);
  33. }
  34. $result = @json_decode($content['content'], true);
  35. if(empty($result) || intval(($result['errcode'])) != 0 || $result['errmsg'] != 'ok') {
  36. return error(-1, '获取微信公众号 card_ticket 结果错误, 错误信息: ' . $result['errmsg']);
  37. }
  38. $record = array();
  39. $record['ticket'] = $result['ticket'];
  40. $record['expire'] = TIMESTAMP + $result['expires_in'] - 200;
  41. $this->account['card_ticket'] = $record;
  42. cache_write($cachekey, $record);
  43. return $record['ticket'];
  44. }
  45. public function LocationLogoupload($logo){
  46. global $_W;
  47. if(!strexists($logo, 'http://') && !strexists($logo, 'https://')) {
  48. $path = rtrim(IA_ROOT .'/'. $_W['config']['upload']['attachdir'], '/') . '/';
  49. if(empty($logo) || !file_exists($path . $logo)) {
  50. return error(-1, '商户LOGO不存在');
  51. }
  52. } else {
  53. return error(-1, '商户LOGO只能上传本地图片');
  54. }
  55. $token = $this->getAccessToken();
  56. if (is_error($token)) {
  57. return $token;
  58. }
  59. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}";
  60. $data = array(
  61. 'buffer' => '@' . $path . $logo
  62. );
  63. load()->func('communication');
  64. $response = ihttp_request($url, $data);
  65. if(is_error($response)) {
  66. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  67. }
  68. $result = @json_decode($response['content'], true);
  69. if(empty($result)) {
  70. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  71. } elseif(!empty($result['errcode'])) {
  72. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}");
  73. }
  74. return $result;
  75. }
  76. public function SetTestWhiteList($data){
  77. global $_W;
  78. $token = $this->getAccessToken();
  79. if (is_error($token)) {
  80. return $token;
  81. }
  82. $url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token={$token}";
  83. load()->func('communication');
  84. $response = ihttp_request($url, json_encode($data));
  85. if(is_error($response)) {
  86. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  87. }
  88. $result = @json_decode($response['content'], true);
  89. if(empty($result)) {
  90. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  91. } elseif(!empty($result['errcode'])) {
  92. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}");
  93. }
  94. return $result;
  95. }
  96. public function LocationAdd($data) {
  97. if(empty($data)) {
  98. return error(-1, '门店信息错误');
  99. }
  100. $token = $this->getAccessToken();
  101. if (is_error($token)) {
  102. return $token;
  103. }
  104. if (!empty($data['category'])) {
  105. $data['category'] = array(rtrim(implode(',', array_values($data['category'])), ','));
  106. }
  107. $data['categories'] = $data['category'];
  108. unset($data['category']);
  109. $data['offset_type'] = 1;
  110. $post = array(
  111. 'business' => array(
  112. 'base_info' => $data,
  113. ),
  114. );
  115. $post = stripslashes(urldecode(ijson_encode($post, JSON_UNESCAPED_UNICODE)));
  116. $url = "http://api.weixin.qq.com/cgi-bin/poi/addpoi?access_token={$token}";
  117. $result = $this->requestApi($url, $post);
  118. return $result;
  119. }
  120. public function LocationEdit($data) {
  121. if(empty($data)) {
  122. return error(-1, '门店信息错误');
  123. }
  124. $post = array(
  125. 'business' => array(
  126. 'base_info' => $data
  127. ),
  128. );
  129. $token = $this->getAccessToken();
  130. if (is_error($token)) {
  131. return $token;
  132. }
  133. $url = "http://api.weixin.qq.com/cgi-bin/poi/updatepoi?access_token={$token}";
  134. load()->func('communication');
  135. $response = ihttp_request($url, urldecode(json_encode($post)));
  136. if(is_error($response)) {
  137. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  138. }
  139. $result = @json_decode($response['content'], true);
  140. if(empty($result)) {
  141. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  142. } elseif(!empty($result['errcode'])) {
  143. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  144. }
  145. return $result;
  146. }
  147. public function LocationDel($id) {
  148. if(empty($id)) {
  149. return error(-1, '门店信息错误');
  150. }
  151. $post = array(
  152. 'poi_id' => $id
  153. );
  154. $token = $this->getAccessToken();
  155. if (is_error($token)) {
  156. return $token;
  157. }
  158. $url = "http://api.weixin.qq.com/cgi-bin/poi/delpoi?access_token={$token}";
  159. load()->func('communication');
  160. $response = ihttp_request($url, json_encode($post));
  161. if(is_error($response)) {
  162. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  163. }
  164. $result = @json_decode($response['content'], true);
  165. if(empty($result)) {
  166. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  167. } elseif(!empty($result['errcode'])) {
  168. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  169. }
  170. return $result;
  171. }
  172. public function LocationBatchGet($data = array()) {
  173. if(empty($data['begin'])) {
  174. $data['begin'] = 0;
  175. }
  176. if(empty($data['limit'])) {
  177. $data['limit'] = 50;
  178. }
  179. $token = $this->getAccessToken();
  180. if (is_error($token)) {
  181. return $token;
  182. }
  183. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={$token}";
  184. load()->func('communication');
  185. $response = ihttp_request($url, json_encode($data));
  186. if(is_error($response)) {
  187. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  188. }
  189. $result = @json_decode($response['content'], true);
  190. if(empty($result)) {
  191. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  192. } elseif(!empty($result['errcode'])) {
  193. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  194. }
  195. return $result;
  196. }
  197. public function LocationGet($id) {
  198. $token = $this->getAccessToken();
  199. if (is_error($token)) {
  200. return $token;
  201. }
  202. $data = array(
  203. 'poi_id' => $id
  204. );
  205. $url = "http://api.weixin.qq.com/cgi-bin/poi/getpoi?access_token={$token}";
  206. load()->func('communication');
  207. $response = ihttp_request($url, json_encode($data));
  208. if(is_error($response)) {
  209. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  210. }
  211. $result = @json_decode($response['content'], true);
  212. if(empty($result)) {
  213. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  214. } elseif(!empty($result['errcode'])) {
  215. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  216. }
  217. return $result;
  218. }
  219. public function GetColors() {
  220. $token = $this->getAccessToken();
  221. if (is_error($token)) {
  222. return $token;
  223. }
  224. $url = "https://api.weixin.qq.com/card/getcolors?access_token={$token}";
  225. load()->func('communication');
  226. $response = ihttp_request($url);
  227. if(is_error($response)) {
  228. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  229. }
  230. $result = @json_decode($response['content'], true);
  231. if(empty($result)) {
  232. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  233. } elseif(!empty($result['errcode'])) {
  234. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  235. }
  236. return $result;
  237. }
  238. public function isCouponSupported() {
  239. global $_W;
  240. load()->model('module');
  241. $we7_coupon_module = module_fetch('we7_coupon');
  242. $setting = array();
  243. if (!empty($we7_coupon_module)) {
  244. $setting = $we7_coupon_module['config'];
  245. } else {
  246. $setting = uni_setting($_W['uniacid'], array('coupon_type'));
  247. }
  248. if ($_W['account']['level'] != ACCOUNT_SERVICE_VERIFY && $_W['account']['level'] != ACCOUNT_SUBSCRIPTION_VERIFY) {
  249. return false;
  250. } else {
  251. if (!empty($setting['setting']['coupon_type'])) {
  252. if ($setting['setting']['coupon_type'] == SYSTEM_COUPON) {
  253. return false;
  254. } else {
  255. return true;
  256. }
  257. } else {
  258. if ($setting['coupon_type'] == SYSTEM_COUPON) {
  259. return false;
  260. } else {
  261. return true;
  262. }
  263. }
  264. }
  265. }
  266. public function CreateCard($card) {
  267. $token = $this->getAccessToken();
  268. if (is_error($token)) {
  269. return $token;
  270. }
  271. $url = "https://api.weixin.qq.com/card/create?access_token={$token}";
  272. load()->func('communication');
  273. $card = stripslashes(urldecode(ijson_encode($card, JSON_UNESCAPED_UNICODE)));
  274. $response = $this->requestApi($url, $card);
  275. return $response;
  276. }
  277. public function DeleteCard($card_id) {
  278. $token = $this->getAccessToken();
  279. if (is_error($token)) {
  280. return $token;
  281. }
  282. $url = "https://api.weixin.qq.com/card/delete?access_token={$token}";
  283. load()->func('communication');
  284. $card = json_encode(array('card_id' => $card_id));
  285. $response = ihttp_request($url, $card);
  286. if(is_error($response)) {
  287. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  288. }
  289. $result = @json_decode($response['content'], true);
  290. if(empty($result)) {
  291. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  292. } elseif(!empty($result['errcode'])) {
  293. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  294. }
  295. return $result;
  296. }
  297. public function setActivateUserForm($card_id) {
  298. global $_W;
  299. $token = $this->getAccessToken();
  300. if (is_error($token)) {
  301. return $token;
  302. }
  303. $data['required_form']['common_field_id_list'] = array('USER_FORM_INFO_FLAG_MOBILE');
  304. $data['card_id'] = $card_id;
  305. $data['bind_old_card'] = array('name' => '绑定老会员卡', 'url' => 'www.weixin.qq.com');
  306. $url = "https://api.weixin.qq.com/card/membercard/activateuserform/set?access_token={$token}";
  307. load()->func('communication');
  308. $result = $this->requestApi($url, json_encode($data));
  309. return $result;
  310. }
  311. public function activateMemberCard($data) {
  312. global $_W;
  313. $token = $this->getAccessToken();
  314. if (is_error($token)) {
  315. return $token;
  316. }
  317. $url = "https://api.weixin.qq.com/card/membercard/activate?access_token={$token}";
  318. load()->func('communication');
  319. $result = $this->requestApi($url, json_encode($data));
  320. return $result;
  321. }
  322. public function ModifyStockCard($card_id, $num) {
  323. $data['card_id'] = trim($card_id);
  324. $data['increase_stock_value'] = 0;
  325. $data['reduce_stock_value'] = 0;
  326. $num = intval($num);
  327. ($num > 0) && ($data['increase_stock_value'] = $num);
  328. ($num < 0) && ($data['reduce_stock_value'] = abs($num));
  329. $token = $this->getAccessToken();
  330. if (is_error($token)) {
  331. return $token;
  332. }
  333. $url = "https://api.weixin.qq.com/card/modifystock?access_token={$token}";
  334. load()->func('communication');
  335. $response = ihttp_request($url, json_encode($data));
  336. if(is_error($response)) {
  337. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  338. }
  339. $result = @json_decode($response['content'], true);
  340. if(empty($result)) {
  341. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  342. } elseif(!empty($result['errcode'])) {
  343. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  344. }
  345. return $result;
  346. }
  347. public function QrCard($card_id, $sceneid, $expire = '') {
  348. $token = $this->getAccessToken();
  349. if (is_error($token)) {
  350. return $token;
  351. }
  352. $url = "https://api.weixin.qq.com/card/qrcode/create?access_token={$token}";
  353. load()->func('communication');
  354. $data = array(
  355. 'action_name' => 'QR_CARD',
  356. 'expire_seconds' => "{$expire}",
  357. 'action_info' => array(
  358. 'card' => array(
  359. 'card_id' => strval($card_id),
  360. 'code' => '',
  361. 'openid' => '',
  362. 'is_unique_code' => false,
  363. 'outer_id' => $sceneid,
  364. )
  365. )
  366. );
  367. $result = $this->requestApi($url, json_encode($data));
  368. return $result;
  369. }
  370. public function sendCoupons($coupon, $openids) {
  371. $token = $this->getAccessToken();
  372. if(is_error($token)){
  373. return $token;
  374. }
  375. $post = array(
  376. 'touser' => $openids,
  377. "wxcard" => array('card_id' => $coupon),
  378. "msgtype" => "wxcard"
  379. );
  380. $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=' . $token;
  381. $result = $this->requestApi($url, json_encode($post));
  382. return $result;
  383. }
  384. public function UnavailableCode($data) {
  385. $token = $this->getAccessToken();
  386. if (is_error($token)) {
  387. return $token;
  388. }
  389. $url = "https://api.weixin.qq.com/card/code/unavailable?access_token={$token}";
  390. load()->func('communication');
  391. $response = ihttp_request($url, json_encode($data));
  392. if(is_error($response)) {
  393. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  394. }
  395. $result = @json_decode($response['content'], true);
  396. if(empty($result)) {
  397. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  398. } elseif(!empty($result['errcode'])) {
  399. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  400. }
  401. return $result;
  402. }
  403. public function ConsumeCode($data) {
  404. $token = $this->getAccessToken();
  405. if (is_error($token)) {
  406. return $token;
  407. }
  408. $url = "https://api.weixin.qq.com/card/code/consume?access_token={$token}";
  409. load()->func('communication');
  410. $response = ihttp_request($url, json_encode($data));
  411. if(is_error($response)) {
  412. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  413. }
  414. $result = @json_decode($response['content'], true);
  415. if(empty($result)) {
  416. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  417. } elseif(!empty($result['errcode'])) {
  418. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  419. }
  420. return $result;
  421. }
  422. public function selfConsume($data) {
  423. $token = $this->getAccessToken();
  424. if(is_error($token)) {
  425. return $token;
  426. }
  427. $url = "https://api.weixin.qq.com/card/selfconsumecell/set?access_token={$token}";
  428. load()->func('communication');
  429. $response = ihttp_request($url, json_encode($data));
  430. if(is_error($response)) {
  431. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  432. }
  433. $result = @json_decode($response['content'], true);
  434. if(empty($result)) {
  435. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  436. } elseif(!empty($result['errcode'])) {
  437. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  438. }
  439. return $result;
  440. }
  441. public function DecryptCode($data) {
  442. $token = $this->getAccessToken();
  443. if (is_error($token)) {
  444. return $token;
  445. }
  446. $url = "https://api.weixin.qq.com/card/code/decrypt?access_token={$token}";
  447. load()->func('communication');
  448. $response = ihttp_request($url, json_encode($data));
  449. if(is_error($response)) {
  450. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  451. }
  452. $result = @json_decode($response['content'], true);
  453. if(empty($result)) {
  454. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  455. } elseif(!empty($result['errcode'])) {
  456. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  457. }
  458. return $result;
  459. }
  460. public function fetchCard($card_id) {
  461. $token = $this->getAccessToken();
  462. if (is_error($token)) {
  463. return $token;
  464. }
  465. $data = array(
  466. 'card_id' => $card_id,
  467. );
  468. $url = "https://api.weixin.qq.com/card/get?access_token={$token}";
  469. load()->func('communication');
  470. $response = ihttp_request($url, json_encode($data));
  471. if(is_error($response)) {
  472. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  473. }
  474. $result = @json_decode($response['content'], true);
  475. if(empty($result)) {
  476. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  477. } elseif(!empty($result['errcode'])) {
  478. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  479. }
  480. return $result['card'];
  481. }
  482. public function updateMemberCard($post) {
  483. $token = $this->getAccessToken();
  484. if (is_error($token)) {
  485. return $token;
  486. }
  487. $url = "https://api.weixin.qq.com/card/update?access_token={$token}";
  488. $result = $this->requestApi($url, urldecode(json_encode($post)));
  489. return $result;
  490. }
  491. public function batchgetCard($data) {
  492. $token = $this->getAccessToken();
  493. if (is_error($token)) {
  494. return $token;
  495. }
  496. $url = "https://api.weixin.qq.com/card/batchget?access_token={$token}";
  497. load()->func('communication');
  498. $response = ihttp_request($url, json_encode($data));
  499. if(is_error($response)) {
  500. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  501. }
  502. $result = @json_decode($response['content'], true);
  503. if(empty($result)) {
  504. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  505. } elseif(!empty($result['errcode'])) {
  506. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  507. }
  508. return $result;
  509. }
  510. public function updateCard($card_id) {
  511. $token = $this->getAccessToken();
  512. if (is_error($token)) {
  513. return $token;
  514. }
  515. $data = array(
  516. 'card_id' => $card_id,
  517. );
  518. $url = "https://api.weixin.qq.com/card/membercard/activate?access_token={$token}";
  519. load()->func('communication');
  520. $response = ihttp_request($url, json_encode($data));
  521. if(is_error($response)) {
  522. return error(-1, "访问公众平台接口失败, 错误: {$response['message']}");
  523. }
  524. $result = @json_decode($response['content'], true);
  525. if(empty($result)) {
  526. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  527. } elseif(!empty($result['errcode'])) {
  528. return error(-1, "访问微信接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},错误详情:{$this->errorCode($result['errcode'])}");
  529. }
  530. return $result;
  531. }
  532. public function PayConsumeCode($data) {
  533. $code_error['uniacid'] = $this->account['uniacid'];
  534. $code_error['acid'] = $this->account['acid'];
  535. $code_error['type'] = 2;
  536. $code_error['message'] = $data['encrypt_code'];
  537. $code_error['dateline'] = time();
  538. $code_error['module'] = $data['module'];
  539. $code_error['params'] = $data['card_id'];
  540. $code = $this->DecryptCode(array('encrypt_code' => $data['encrypt_code']));
  541. if(is_error($code)) {
  542. pdo_insert('core_queue', $code_error);
  543. } else {
  544. $sumecode = $this->ConsumeCode(array('code' => $code['code']));
  545. if(is_error($sumecode)) {
  546. pdo_insert('core_queue', $code_error);
  547. } else {
  548. pdo_update('coupon_record', array('status' => 3, 'usetime' => time()), array('acid' => $this->account['acid'], 'code' => $code['code'], 'card_id' => $data['card_id']));
  549. }
  550. }
  551. return true;
  552. }
  553. public function SignatureCard($data) {
  554. $ticket = $this->getCardTicket();
  555. if (is_error($ticket)) {
  556. return $ticket;
  557. }
  558. $data[] = $ticket;
  559. sort($data, SORT_STRING);
  560. return sha1(implode($data));
  561. }
  562. public function BuildCardExt($id, $openid = '', $type = 'coupon') {
  563. global $_W;
  564. if ($type == 'membercard') {
  565. $card_id = pdo_getcolumn('mc_card', array('uniacid' => $_W['uniacid']), 'card_id');
  566. } else {
  567. $acid = $this->account['acid'];
  568. $card_id = pdo_fetchcolumn('SELECT card_id FROM ' . tablename('coupon') . ' WHERE acid = :acid AND id = :id', array(':acid' => $acid, ':id' => $id));
  569. if(empty($card_id)) {
  570. return error(-1, '卡券id不合法');
  571. }
  572. }
  573. if (empty($card_id)) {
  574. $card_id = $id;
  575. }
  576. $time = TIMESTAMP;
  577. $sign = array($card_id, $time);
  578. $signature = $this->SignatureCard($sign);
  579. if(is_error($signature)) {
  580. return $signature;
  581. }
  582. $cardExt = array('timestamp' => $time, 'signature' => $signature);
  583. $cardExt = json_encode($cardExt);
  584. return array('card_id' => $card_id, 'card_ext' => $cardExt);
  585. }
  586. public function AddCard($id) {
  587. $card = $this->BuildCardExt($id);
  588. if(is_error($card)) {
  589. return $card;
  590. }
  591. $url = murl('activity/coupon/mine');
  592. return <<<EOF
  593. wx.ready(function(){
  594. wx.addCard({
  595. cardList:[
  596. {
  597. cardId:'{$card['card_id']}',
  598. cardExt:'{$card['card_ext']}'
  599. }
  600. ],
  601. success: function (res) {
  602. location.href="{$url}";
  603. }
  604. });
  605. });
  606. EOF;
  607. }
  608. public function OpenCard($id, $code) {
  609. $card = $this->BuildCardExt($id);
  610. if(is_error($card)) {
  611. return $card;
  612. }
  613. $url = murl('activity/coupon/mine');
  614. return <<<EOF
  615. wx.ready(function(){
  616. wx.openCard({
  617. cardList:[
  618. {
  619. cardId : "{$card['card_id']}",
  620. code : "{$code}"
  621. }
  622. ],
  623. });
  624. });
  625. EOF;
  626. }
  627. public function ChooseCard($card_id) {
  628. $acid = $this->account['acid'];
  629. if(empty($card_id)) {
  630. return error(-1, '卡券不存在');
  631. }
  632. $time = TIMESTAMP;
  633. $randstr = random(8);
  634. $sign = array($card_id, $time, $randstr, $this->account['key']);
  635. $signature = $this->SignatureCard($sign);
  636. if(is_error($signature)) {
  637. return $signature;
  638. }
  639. $url = murl("wechat/pay/card");
  640. return <<<EOF
  641. wx.ready(function(){
  642. wx.chooseCard({
  643. shopId: '',
  644. cardType: '',
  645. cardId:'{$card_id}',
  646. timestamp:{$time},
  647. nonceStr:'{$randstr}',
  648. signType:'SHA1',
  649. cardSign:'{$signature}',
  650. success: function(res) {
  651. if(res.errMsg == 'chooseCard:ok') {
  652. eval("var rs = " + res.cardList);
  653. $.post('{$url}', {'card_id':rs[0].card_id}, function(data){
  654. var data = $.parseJSON(data);
  655. if(!data.errno) {
  656. var card = data.error;
  657. if(card.type == 'discount') {
  658. }
  659. } else {
  660. u.message('卡券不存在', '', 'error');
  661. }
  662. });
  663. } else {
  664. u.message('使用卡券失败', '', 'error');
  665. }
  666. }
  667. });
  668. });
  669. EOF;
  670. }
  671. public function BatchAddCard($data) {
  672. $acid = $this->account['acid'];
  673. $condition = '';
  674. if(!empty($data['type'])) {
  675. $condition .= " AND type = '{$data['type']}'";
  676. } else {
  677. $ids = array();
  678. foreach($data as $da) {
  679. $da = intval($da);
  680. if($da > 0) {
  681. $ids[] = $da;
  682. }
  683. }
  684. if(empty($ids)) {
  685. $condition = '';
  686. } else {
  687. $ids_str = implode(', ', $ids);
  688. $condition .= " AND id IN ({$ids_str})";
  689. }
  690. }
  691. $card = array();
  692. if(!empty($condition)) {
  693. $card = pdo_fetchall('SELECT id, card_id FROM ' . tablename('coupon') . " WHERE acid = {$acid} " . $condition);
  694. }
  695. foreach($card as $ca) {
  696. $time = TIMESTAMP;
  697. $sign = array($ca['card_id'], $time);
  698. $signature = $this->SignatureCard($sign);
  699. if(is_error($signature)) {
  700. return $signature;
  701. }
  702. $post[] = array(
  703. 'cardId' => trim($ca['card_id']),
  704. 'cardExt' => array('timestamp' => $time, 'signature' => $signature),
  705. );
  706. }
  707. if(!empty($post)) {
  708. $card_json = json_encode($post);
  709. echo <<<EOF
  710. <script>
  711. wx.ready(function(){
  712. wx.addCard({
  713. cardList : {$card_json}, // 需要添加的卡券列表
  714. success: function (res) {
  715. alert(JSON.stringify(res));
  716. var cardList = res.cardList; // 添加的卡券列表信息
  717. }
  718. });
  719. });
  720. </script>
  721. EOF;
  722. } else {
  723. echo <<<EOF
  724. <script>
  725. </script>
  726. EOF;
  727. }
  728. }
  729. }
  730. define('COUPON_CODE_TYPE_TEXT', 1);
  731. define('COUPON_CODE_TYPE_QRCODE', 2);
  732. define('COUPON_CODE_TYPE_BARCODE', 3);
  733. define('COUPON_TIME_TYPE_RANGE', 1);
  734. define('COUPON_TIME_TYPE_FIX', 2);
  735. class Card {
  736. public $card_id = '';
  737. public $logo_url = '';
  738. public $brand_name = '';
  739. public $code_type = CODE_TYPE_BARCODE;
  740. public $title = '';
  741. public $sub_title = '';
  742. public $color = 'Color082';
  743. public $notice = '';
  744. public $service_phone = '';
  745. public $description = '';
  746. public $sku = array('quantity' => 50000);
  747. public $date_info = array('type' => COUPON_TIME_TYPE_RANGE);
  748. public $location_id_list = array();
  749. public $get_limit = 10; public $can_share = true;
  750. public $can_give_friend = true; public $use_custom_code = false; public $bind_openid = false; public $source = ''; public $status = ''; public $promotion_url_name = ''; public $promotion_url_sub_title = '';
  751. public $promotion_url = '';
  752. public $custom_url_name = ''; public $custom_url_sub_title = '';
  753. public $custom_url = '';
  754. public $center_title = ''; public $center_sub_title = '';
  755. public $center_url = '';
  756. public $need_push_on_view = false; public $pay_info = array();
  757. public $get_custom_code_mode = "GET_CUSTOM_CODE_MODE_DEPOSIT";
  758. private $types = array('', 'DISCOUNT', 'CASH', 'GROUPON', 'GIFT', 'GENERAL_COUPON', "MEMBER_CARD", "SCENIC_TICKET", "MOVIE_TICKET");
  759. private $code_types = array(COUPON_CODE_TYPE_TEXT => 'CODE_TYPE_TEXT', COUPON_CODE_TYPE_QRCODE => 'CODE_TYPE_QRCODE',COUPON_CODE_TYPE_BARCODE => 'CODE_TYPE_BARCODE');
  760. static public function create($type) {
  761. $card_class = array(
  762. COUPON_TYPE_DISCOUNT => 'Discount',
  763. COUPON_TYPE_CASH => 'Cash',
  764. COUPON_TYPE_GENERAL => 'General',
  765. COUPON_TYPE_GIFT => 'Gift',
  766. COUPON_TYPE_GROUPON => 'Groupon',
  767. COUPON_TYPE_MEMBER => 'Member'
  768. );
  769. if (empty($card_class[$type])) {
  770. return error(-1, '卡券类型错误');
  771. }
  772. $classname = $card_class[$type].'Card';
  773. $card = new $classname();
  774. $card->type = $type;
  775. return $card;
  776. }
  777. public function setDateinfoRange($starttime, $endtime) {
  778. $this->date_info = array(
  779. 'type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => strtotime($starttime),
  780. 'end_timestamp' => strtotime($endtime),
  781. );
  782. return true;
  783. }
  784. public function setDateinfoFix($begin, $term) {
  785. $this->date_info = array(
  786. 'type' => 'DATE_TYPE_FIX_TERM', 'fixed_term' => $term,
  787. 'fixed_begin_term' => $begin,
  788. );
  789. return true;
  790. }
  791. public function setCodetype($type) {
  792. $this->code_type = $this->code_types[$type];
  793. return true;
  794. }
  795. public function setLocation($location) {
  796. $store = pdo_getall('activity_stores', array('id' => $location), array('location_id'), 'location_id');
  797. if (!empty($store)) {
  798. $this->location_id_list = array_keys($store);
  799. }
  800. }
  801. public function setCenterMenu($title, $subtitle, $url) {
  802. $this->center_title = urlencode($title);
  803. $this->center_sub_title = urlencode($subtitle);
  804. $this->center_url = urlencode($url);
  805. return true;
  806. }
  807. public function setCustomMenu($title, $subtitle, $url) {
  808. $this->custom_url_name = urlencode($title);
  809. $this->custom_url_sub_title = urlencode($subtitle);
  810. $this->custom_url = urlencode($url);
  811. return true;
  812. }
  813. public function setPromotionMenu($title, $subtitle, $url) {
  814. $this->promotion_url_name = urlencode($title);
  815. $this->promotion_url_sub_title = urlencode($subtitle);
  816. $this->promotion_url = urlencode($url);
  817. return true;
  818. }
  819. public function setQuantity($quantity) {
  820. $this->sku = $sku = array('quantity' => intval($quantity));
  821. }
  822. public function validate() {
  823. if (empty($this->logo_url)) {
  824. return error(7, '未设置商户logo');
  825. }
  826. if (empty($this->brand_name)) {
  827. return error(8, '未设置商户名称');
  828. }
  829. if (empty($this->title)) {
  830. return error(9, '未设置卡券标题');
  831. }
  832. if (empty($this->service_phone)) {
  833. return error(11, '客服电话不能为空');
  834. }
  835. if (empty($this->description)) {
  836. return error(12, '使用须知不能为空');
  837. }
  838. return true;
  839. }
  840. private function getBaseinfo() {
  841. $fields = array(
  842. 'logo_url', 'brand_name', 'code_type', 'title', 'sub_title', 'color', 'notice',
  843. 'service_phone', 'description', 'date_info' ,'sku', 'get_limit', 'use_custom_code',
  844. 'bind_openid', 'can_share', 'can_give_friend', 'location_id_list',
  845. 'center_title', 'center_sub_title','center_url',
  846. 'custom_url_name','custom_url','custom_url_sub_title',
  847. 'promotion_url_name','promotion_url', 'promotion_url_sub_title', 'source', 'get_custom_code_mode',
  848. );
  849. if ($this->type == 6) {
  850. $fields[] = 'need_push_on_view';
  851. $fields[] = 'pay_info';
  852. }
  853. $baseinfo = array();
  854. foreach ($this as $filed => $value) {
  855. if (in_array($filed, $fields)) {
  856. $baseinfo[$filed] = $value;
  857. }
  858. }
  859. return $baseinfo;
  860. }
  861. private function getAdvinfo() {
  862. return array();
  863. }
  864. function getCardData() {
  865. $carddata = array(
  866. 'base_info' => $this->getBaseinfo(),
  867. );
  868. $carddata = array_merge($carddata, $this->getCardExtraData());
  869. $card = array(
  870. 'card' => array(
  871. 'card_type' => $this->types[$this->type],
  872. strtolower($this->types[$this->type]) => $carddata,
  873. ),
  874. );
  875. return $card;
  876. }
  877. function getCardArray() {
  878. $data = array(
  879. 'card_id' => $this->card_id,
  880. 'type' => $this->type,
  881. 'logo_url' => urldecode($this->logo_url),
  882. 'code_type' => array_search($this->code_type, $this->code_types),
  883. 'brand_name' => $this->brand_name,
  884. 'title' => $this->title,
  885. 'sub_title' => $this->sub_title,
  886. 'color' => $this->color,
  887. 'notice' => $this->notice,
  888. 'description' => $this->description,
  889. 'quantity' => $this->sku['quantity'],
  890. 'use_custom_code' => intval($this->use_custom_code),
  891. 'bind_openid' => intval($this->bind_openid),
  892. 'can_share' => intval($this->can_share),
  893. 'can_give_friend' => intval($this->can_give_friend),
  894. 'get_limit' => $this->get_limit,
  895. 'service_phone' => $this->service_phone,
  896. 'status' => $this->status,
  897. 'is_display' => '1',
  898. 'is_selfconsume' => '0',
  899. 'promotion_url_name' => urldecode($this->promotion_url_name),
  900. 'promotion_url' => urldecode($this->promotion_url),
  901. 'promotion_url_sub_title' => urldecode($this->promotion_url_sub_title),
  902. 'source' => $this->source,
  903. );
  904. $data['date_info'] = array(
  905. 'time_type' => $this->date_info['type'] == 'DATE_TYPE_FIX_TIME_RANGE' ? 1 : 2,
  906. 'time_limit_start' => date('Y.m.d', $this->date_info['begin_timestamp']),
  907. 'time_limit_end' => date('Y.m.d', $this->date_info['end_timestamp']),
  908. 'deadline' => $this->date_info['fixed_begin_term'],
  909. 'limit' => $this->date_info['fixed_term'],
  910. );
  911. $data['date_info'] = iserializer($data['date_info']);
  912. $data['extra'] = iserializer($this->getCardExtraData());
  913. return $data;
  914. }
  915. };
  916. class MemberCard extends Card {
  917. public $background_pic_url = '';
  918. public $supply_bonus = true; public $bonus_rule = array(
  919. 'cost_money_unit' => 100, 'increase_bonus' => '', 'max_increase_bonus' => '', 'init_increase_bonus' => '', 'cost_bonus_unit' => '', 'reduce_money' => 100, 'least_money_to_use_bonus' => '', 'max_reduce_bonus' => '', ); public $supply_balance = true; public $prerogative = ''; public $auto_activate = false; public $custom_field1 = array('name_type' => 'FIELD_NAME_TYPE_COUPON', 'url' => '' );
  920. public $activate_url = ''; public $wx_activate = false; public $bonus_url = ''; public $balance_url = ''; public $bonus_rules = ''; public $balance_rules = ''; public $custom_cell1 = array('name' => '账单', 'tips' => '', 'url' => 'http://06.we7.cc/app/index.php?i=76&c=mc&a=bond&do=credits&credittype=credit2&type=record&period=1&wxref=mp.weixin.qq.com#wechat_redirect');
  921. public $discount = ''; public $bonus_cleared = ''; public $format_type = true;
  922. public $grant_rate = '';
  923. public $offset_rate = '';
  924. public $offset_max = '';
  925. public $fields = array();
  926. public $grant = array();
  927. public $discount_type = '';
  928. public $nums_status = '';
  929. public $nums_text = '' ;
  930. public $times_status = '';
  931. public $times_text = '';
  932. public $params = '';
  933. public $html = '';
  934. public function GetCardArray() {
  935. return array(
  936. 'card_id' => $this->card_id,
  937. 'source' => $this->source,
  938. 'title' => $this->title,
  939. 'brand_name' => $this->brand_name,
  940. 'format_type' => $this->format_type,
  941. 'color' => $this->color,
  942. 'background' => $this->background_pic_url,
  943. 'logo' => $this->logo_url,
  944. 'description' => $this->description,
  945. 'grant_rate' => $this->grant_rate,
  946. 'offset_rate' => $this->offset_rate,
  947. 'offset_max' => $this->offset_max,
  948. 'fields' => $this->fields,
  949. 'grant' => $this->grant,
  950. 'discount_type' => $this->discount_type,
  951. 'nums_status' => $this->nums_status,
  952. 'nums_text' => $this->nums_text,
  953. 'times_status' => $this->times_status,
  954. 'times_text' => $this->times_text,
  955. 'params' => $this->params,
  956. 'html' => $this->html,
  957. 'notice' => $this->notice,
  958. 'quantity' => $this->sku['quantity'],
  959. 'least_money_to_use_bonus' => $this->bonus_rule['least_money_to_use_bonus'],
  960. 'max_increase_bonus' => $this->bonus_rule['max_increase_bonus']
  961. );
  962. }
  963. public function getMemberCardUpdateArray() {
  964. $update['card_id'] = $this->card_id;
  965. $card = $this->getCardData();
  966. $update = array_merge($update, $card['card']);
  967. unset($update['card_type']);
  968. unset($update['member_card']['base_info']['source']);
  969. unset($update['member_card']['base_info']['sub_title']);
  970. unset($update['member_card']['base_info']['sku']);
  971. unset($update['member_card']['base_info']['use_custom_code']);
  972. unset($update['member_card']['base_info']['promotion_url_name']);
  973. unset($update['member_card']['base_info']['promotion_url']);
  974. unset($update['member_card']['base_info']['custom_url_name']);
  975. unset($update['member_card']['base_info']['custom_url']);
  976. unset($update['member_card']['base_info']['brand_name']);
  977. unset($update['member_card']['custom_cell1']);
  978. $update['member_card']['base_info']['promotion_url_name'] = urlencode('广播');
  979. $update['member_card']['base_info']['custom_url_name'] = urlencode('个人消息');
  980. $update['member_card']['base_info']['center_title'] = urlencode('付款');
  981. $update['member_card']['base_info']['title'] = urlencode($update['member_card']['base_info']['title']);
  982. $update['member_card']['base_info']['description'] = urlencode($update['member_card']['base_info']['description']);
  983. $update['member_card']['prerogative'] = urlencode($update['member_card']['prerogative']);
  984. return $update;
  985. }
  986. public function GetMemberCardArray() {
  987. $data = $this->getcardarray();
  988. return $data;
  989. }
  990. public function setBonusRule($cost_money_unit, $increase_bonus, $max_increase_bonus, $init_increase_bonus, $cost_bonus_unit, $reduce_money, $least_money_to_use_bonus, $max_reduce_bonus) {
  991. $this->bonus_rule = array(
  992. 'cost_money_unit' => $cost_money_unit,
  993. 'increase_bonus' => $increase_bonus,
  994. 'max_increase_bonus' => $max_increase_bonus,
  995. 'init_increase_bonus' => $init_increase_bonus,
  996. 'cost_bonus_unit' => $cost_bonus_unit,
  997. 'reduce_money' => $reduce_money,
  998. 'least_money_to_use_bonus' => $least_money_to_use_bonus,
  999. 'max_reduce_bonus' => $max_reduce_bonus,
  1000. );
  1001. return true;
  1002. }
  1003. public function setCustomCell($name, $tips, $url) {
  1004. $this->custom_cell1 = array(
  1005. 'name' => $name,
  1006. 'tips' => $tips,
  1007. 'url' => $url
  1008. );
  1009. return true;
  1010. }
  1011. public function setCustomField($name_type, $url, $num) {
  1012. $array = array(
  1013. 'name_type' => $name_type,
  1014. 'url' => $url
  1015. );
  1016. if ($num == 1) {
  1017. $this->custom_field1 = $array;
  1018. }
  1019. if ($num == 2) {
  1020. $this->custom_field2 = $array;
  1021. }
  1022. if ($num == 3) {
  1023. $this->custom_field3 = $array;
  1024. }
  1025. return true;
  1026. }
  1027. public function validate() {
  1028. $error = parent::validate();
  1029. if (is_error($error) && $error['errno'] != 11) {
  1030. return $error;
  1031. }
  1032. if (!empty($this->supply_bonus)) {
  1033. if (empty($this->bonus_rule['cost_money_unit'])) {
  1034. return error(13, '未填写积分说明中的消费金额');
  1035. }
  1036. if (empty($this->bonus_rule['increase_bonus'])) {
  1037. return error(14, '未填写积分说明中的对应增加金额');
  1038. }
  1039. if (empty($this->bonus_rule['max_increase_bonus'])) {
  1040. return error(15, '未填写积分说明中的用户单次可获取的积分上限');
  1041. }
  1042. if (empty($this->bonus_rule['init_increase_bonus'])) {
  1043. return error(16, '未填写积分说明中的初始设置积分');
  1044. }
  1045. if (empty($this->bonus_rule['cost_bonus_unit'])) {
  1046. return error(17, '未填写积分说明中的每次使用积分');
  1047. }
  1048. if (empty($this->bonus_rule['reduce_money'])) {
  1049. return error(18, '未填写积分说明中的会员卡可抵扣多少元');
  1050. }
  1051. if (empty($this->bonus_rule['least_money_to_use_bonus'])) {
  1052. return error(19, '未填写积分说明中的满xx元可用');
  1053. }
  1054. if (empty($this->bonus_rule['max_reduce_bonus'])) {
  1055. return error(20, '未填写积分说明中的单笔最多使用xx积分');
  1056. }
  1057. }
  1058. if (!empty($this->custom_cell1['name']) || !empty($this->custom_cell1['tips']) || !empty($this->custom_cell1['url'])) {
  1059. if (empty($this->custom_cell1['name'])) {
  1060. return error(21, '未填写入口名称');
  1061. }
  1062. if (empty($this->custom_cell1['url'])) {
  1063. return error(23, '未填写入口跳转链接');
  1064. }
  1065. }
  1066. if (empty($this->prerogative)) {
  1067. return error(24, '未填写会员卡特权说明');
  1068. }
  1069. if (empty($this->wx_activate) && empty($this->activate_url)) {
  1070. return error(25, '未填写激活会员卡url');
  1071. }
  1072. return true;
  1073. }
  1074. public function getCardExtraData() {
  1075. return array(
  1076. 'background_pic_url' => $this->background_pic_url,
  1077. 'supply_bonus' => $this->supply_bonus,
  1078. 'bonus_rule' => $this->bonus_rule,
  1079. 'supply_balance' => $this->supply_balance,
  1080. 'prerogative' => $this->prerogative,
  1081. 'auto_activate' => $this->auto_activate,
  1082. 'custom_field1' => $this->custom_field1,
  1083. 'activate_url' => $this->activate_url,
  1084. 'wx_activate' => $this->wx_activate,
  1085. 'bonus_url' => $this->bonus_url,
  1086. 'balance_url' => $this->balance_url,
  1087. 'bonus_rules' => $this->bonus_rules,
  1088. 'balance_rules' => $this->balance_rules,
  1089. 'custom_cell1' => $this->custom_cell1,
  1090. 'discount' => $this->discount,
  1091. 'bonus_cleared' => $this->bonus_cleared,
  1092. );
  1093. }
  1094. }
  1095. class DiscountCard extends Card {
  1096. public $discount = 0;
  1097. public function validate() {
  1098. $error = parent::validate();
  1099. if (is_error($error)) {
  1100. return $error;
  1101. }
  1102. if (empty($this->discount)) {
  1103. return error(1, '未设置折扣券折扣');
  1104. }
  1105. return true;
  1106. }
  1107. public function getCardExtraData() {
  1108. return array(
  1109. 'discount' => $this->discount,
  1110. );
  1111. }
  1112. }
  1113. class CashCard extends Card {
  1114. public $least_cost = 0; public $reduce_cost = 0;
  1115. public function validate() {
  1116. $error = parent::validate();
  1117. if (is_error($error)) {
  1118. return $error;
  1119. }
  1120. if (!isset($this->least_cost)) {
  1121. return error(2, '未设置代金券起用金额');
  1122. }
  1123. if (empty($this->least_cost)) {
  1124. return error(3, '未设置代金券减免金额');
  1125. }
  1126. return true;
  1127. }
  1128. public function getCardExtraData() {
  1129. return array(
  1130. 'least_cost' => $this->least_cost,
  1131. 'reduce_cost' => $this->reduce_cost,
  1132. );
  1133. }
  1134. }
  1135. class GiftCard extends Card {
  1136. public $gift = '';
  1137. public function validate() {
  1138. $error = parent::validate();
  1139. if (is_error($error)) {
  1140. return $error;
  1141. }
  1142. if (empty($this->gift)) {
  1143. return error(4, '未设置礼品券兑换内容');
  1144. }
  1145. return true;
  1146. }
  1147. public function getCardExtraData() {
  1148. return array(
  1149. 'gift' => $this->gift,
  1150. );
  1151. }
  1152. }
  1153. class GrouponCard extends Card {
  1154. public $deal_detail = ''; public function validate() {
  1155. $error = parent::validate();
  1156. if (is_error($error)) {
  1157. return $error;
  1158. }
  1159. if (empty($this->deal_detail)) {
  1160. return error(5, '未设置团购券详情内容');
  1161. }
  1162. return true;
  1163. }
  1164. public function getCardExtraData() {
  1165. return array(
  1166. 'deal_detail' => $this->deal_detail,
  1167. );
  1168. }
  1169. }
  1170. class GeneralCard extends Card {
  1171. public $default_detail = ''; public function validate() {
  1172. $error = parent::validate();
  1173. if (is_error($error)) {
  1174. return $error;
  1175. }
  1176. if (empty($this->default_detail)) {
  1177. return error(6, '未设置优惠券优惠详情');
  1178. }
  1179. return true;
  1180. }
  1181. public function getCardExtraData() {
  1182. return array(
  1183. 'default_detail' => $this->default_detail,
  1184. );
  1185. }
  1186. }