coupon.class.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. $params = array();
  675. if(!empty($data['type'])) {
  676. $condition .= " AND type = :type";
  677. $params[':type'] = $data['type'];
  678. } else {
  679. $ids = array();
  680. foreach($data as $da) {
  681. $da = intval($da);
  682. if($da > 0) {
  683. $ids[] = $da;
  684. }
  685. }
  686. if(empty($ids)) {
  687. $condition = '';
  688. } else {
  689. $ids_str = implode(', ', $ids);
  690. $condition .= " AND id IN ({$ids_str})";
  691. }
  692. }
  693. $card = array();
  694. if(!empty($condition)) {
  695. $params[':acid'] = $acid;
  696. $card = pdo_fetchall('SELECT id, card_id FROM ' . tablename('coupon') . " WHERE acid = :acid " . $condition, $params);
  697. }
  698. foreach($card as $ca) {
  699. $time = TIMESTAMP;
  700. $sign = array($ca['card_id'], $time);
  701. $signature = $this->SignatureCard($sign);
  702. if(is_error($signature)) {
  703. return $signature;
  704. }
  705. $post[] = array(
  706. 'cardId' => trim($ca['card_id']),
  707. 'cardExt' => array('timestamp' => $time, 'signature' => $signature),
  708. );
  709. }
  710. if(!empty($post)) {
  711. $card_json = json_encode($post);
  712. echo <<<EOF
  713. <script>
  714. wx.ready(function(){
  715. wx.addCard({
  716. cardList : {$card_json}, // 需要添加的卡券列表
  717. success: function (res) {
  718. alert(JSON.stringify(res));
  719. var cardList = res.cardList; // 添加的卡券列表信息
  720. }
  721. });
  722. });
  723. </script>
  724. EOF;
  725. } else {
  726. echo <<<EOF
  727. <script>
  728. </script>
  729. EOF;
  730. }
  731. }
  732. }
  733. define('COUPON_CODE_TYPE_TEXT', 1);
  734. define('COUPON_CODE_TYPE_QRCODE', 2);
  735. define('COUPON_CODE_TYPE_BARCODE', 3);
  736. define('COUPON_TIME_TYPE_RANGE', 1);
  737. define('COUPON_TIME_TYPE_FIX', 2);
  738. class Card {
  739. public $card_id = '';
  740. public $logo_url = '';
  741. public $brand_name = '';
  742. public $code_type = CODE_TYPE_BARCODE;
  743. public $title = '';
  744. public $sub_title = '';
  745. public $color = 'Color082';
  746. public $notice = '';
  747. public $service_phone = '';
  748. public $description = '';
  749. public $sku = array('quantity' => 50000);
  750. public $date_info = array('type' => COUPON_TIME_TYPE_RANGE);
  751. public $location_id_list = array();
  752. public $get_limit = 10; public $can_share = true;
  753. 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 = '';
  754. public $promotion_url = '';
  755. public $custom_url_name = ''; public $custom_url_sub_title = '';
  756. public $custom_url = '';
  757. public $center_title = ''; public $center_sub_title = '';
  758. public $center_url = '';
  759. public $need_push_on_view = false; public $pay_info = array();
  760. public $get_custom_code_mode = '';
  761. private $types = array('', 'DISCOUNT', 'CASH', 'GROUPON', 'GIFT', 'GENERAL_COUPON', "MEMBER_CARD", "SCENIC_TICKET", "MOVIE_TICKET");
  762. 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');
  763. static public function create($type) {
  764. $card_class = array(
  765. COUPON_TYPE_DISCOUNT => 'Discount',
  766. COUPON_TYPE_CASH => 'Cash',
  767. COUPON_TYPE_GENERAL => 'General',
  768. COUPON_TYPE_GIFT => 'Gift',
  769. COUPON_TYPE_GROUPON => 'Groupon',
  770. COUPON_TYPE_MEMBER => 'Member'
  771. );
  772. if (empty($card_class[$type])) {
  773. return error(-1, '卡券类型错误');
  774. }
  775. $classname = $card_class[$type].'Card';
  776. $card = new $classname();
  777. $card->type = $type;
  778. return $card;
  779. }
  780. public function setDateinfoRange($starttime, $endtime) {
  781. $this->date_info = array(
  782. 'type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => strtotime($starttime),
  783. 'end_timestamp' => strtotime($endtime),
  784. );
  785. return true;
  786. }
  787. public function setDateinfoFix($begin, $term) {
  788. $this->date_info = array(
  789. 'type' => 'DATE_TYPE_FIX_TERM', 'fixed_term' => $term,
  790. 'fixed_begin_term' => $begin,
  791. );
  792. return true;
  793. }
  794. public function setCodetype($type) {
  795. $this->code_type = $this->code_types[$type];
  796. return true;
  797. }
  798. public function setLocation($location) {
  799. $store = pdo_getall('activity_stores', array('id' => $location), array('location_id'), 'location_id');
  800. if (!empty($store)) {
  801. $this->location_id_list = array_keys($store);
  802. }
  803. }
  804. public function setCenterMenu($title, $subtitle, $url) {
  805. $this->center_title = urlencode($title);
  806. $this->center_sub_title = urlencode($subtitle);
  807. $this->center_url = urlencode($url);
  808. return true;
  809. }
  810. public function setCustomMenu($title, $subtitle, $url) {
  811. $this->custom_url_name = urlencode($title);
  812. $this->custom_url_sub_title = urlencode($subtitle);
  813. $this->custom_url = urlencode($url);
  814. return true;
  815. }
  816. public function setPromotionMenu($title, $subtitle, $url) {
  817. $this->promotion_url_name = urlencode($title);
  818. $this->promotion_url_sub_title = urlencode($subtitle);
  819. $this->promotion_url = urlencode($url);
  820. return true;
  821. }
  822. public function setQuantity($quantity) {
  823. $this->sku = $sku = array('quantity' => intval($quantity));
  824. }
  825. public function validate() {
  826. if (empty($this->logo_url)) {
  827. return error(7, '未设置商户logo');
  828. }
  829. if (empty($this->brand_name)) {
  830. return error(8, '未设置商户名称');
  831. }
  832. if (empty($this->title)) {
  833. return error(9, '未设置卡券标题');
  834. }
  835. if (empty($this->service_phone)) {
  836. return error(11, '客服电话不能为空');
  837. }
  838. if (empty($this->description)) {
  839. return error(12, '使用须知不能为空');
  840. }
  841. return true;
  842. }
  843. private function getBaseinfo() {
  844. $fields = array(
  845. 'logo_url', 'brand_name', 'code_type', 'title', 'sub_title', 'color', 'notice',
  846. 'service_phone', 'description', 'date_info' ,'sku', 'get_limit', 'use_custom_code',
  847. 'bind_openid', 'can_share', 'can_give_friend', 'location_id_list',
  848. 'center_title', 'center_sub_title','center_url',
  849. 'custom_url_name','custom_url','custom_url_sub_title',
  850. 'promotion_url_name','promotion_url', 'promotion_url_sub_title', 'source', 'get_custom_code_mode',
  851. );
  852. if ($this->type == 6) {
  853. $fields[] = 'need_push_on_view';
  854. $fields[] = 'pay_info';
  855. }
  856. $baseinfo = array();
  857. foreach ($this as $filed => $value) {
  858. if (in_array($filed, $fields)) {
  859. $baseinfo[$filed] = $value;
  860. }
  861. }
  862. return $baseinfo;
  863. }
  864. private function getAdvinfo() {
  865. return array();
  866. }
  867. function getCardData() {
  868. $carddata = array(
  869. 'base_info' => $this->getBaseinfo(),
  870. );
  871. $carddata = array_merge($carddata, $this->getCardExtraData());
  872. $card = array(
  873. 'card' => array(
  874. 'card_type' => $this->types[$this->type],
  875. strtolower($this->types[$this->type]) => $carddata,
  876. ),
  877. );
  878. return $card;
  879. }
  880. function getCardArray() {
  881. $data = array(
  882. 'card_id' => $this->card_id,
  883. 'type' => $this->type,
  884. 'logo_url' => urldecode($this->logo_url),
  885. 'code_type' => array_search($this->code_type, $this->code_types),
  886. 'brand_name' => $this->brand_name,
  887. 'title' => $this->title,
  888. 'sub_title' => $this->sub_title,
  889. 'color' => $this->color,
  890. 'notice' => $this->notice,
  891. 'description' => $this->description,
  892. 'quantity' => $this->sku['quantity'],
  893. 'use_custom_code' => intval($this->use_custom_code),
  894. 'bind_openid' => intval($this->bind_openid),
  895. 'can_share' => intval($this->can_share),
  896. 'can_give_friend' => intval($this->can_give_friend),
  897. 'get_limit' => $this->get_limit,
  898. 'service_phone' => $this->service_phone,
  899. 'status' => $this->status,
  900. 'is_display' => '1',
  901. 'is_selfconsume' => '0',
  902. 'promotion_url_name' => urldecode($this->promotion_url_name),
  903. 'promotion_url' => urldecode($this->promotion_url),
  904. 'promotion_url_sub_title' => urldecode($this->promotion_url_sub_title),
  905. 'source' => $this->source,
  906. );
  907. $data['date_info'] = array(
  908. 'time_type' => $this->date_info['type'] == 'DATE_TYPE_FIX_TIME_RANGE' ? 1 : 2,
  909. 'time_limit_start' => date('Y.m.d', $this->date_info['begin_timestamp']),
  910. 'time_limit_end' => date('Y.m.d', $this->date_info['end_timestamp']),
  911. 'deadline' => $this->date_info['fixed_begin_term'],
  912. 'limit' => $this->date_info['fixed_term'],
  913. );
  914. $data['date_info'] = iserializer($data['date_info']);
  915. $data['extra'] = iserializer($this->getCardExtraData());
  916. return $data;
  917. }
  918. };
  919. class MemberCard extends Card {
  920. public $background_pic_url = '';
  921. public $supply_bonus = true; public $bonus_rule = array(
  922. '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' => '' );
  923. 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');
  924. public $discount = ''; public $bonus_cleared = ''; public $format_type = true;
  925. public $grant_rate = '';
  926. public $offset_rate = '';
  927. public $offset_max = '';
  928. public $fields = array();
  929. public $grant = array();
  930. public $discount_type = '';
  931. public $nums_status = '';
  932. public $nums_text = '' ;
  933. public $times_status = '';
  934. public $times_text = '';
  935. public $params = '';
  936. public $html = '';
  937. public function GetCardArray() {
  938. return array(
  939. 'card_id' => $this->card_id,
  940. 'source' => $this->source,
  941. 'title' => $this->title,
  942. 'brand_name' => $this->brand_name,
  943. 'format_type' => $this->format_type,
  944. 'color' => $this->color,
  945. 'background' => $this->background_pic_url,
  946. 'logo' => $this->logo_url,
  947. 'description' => $this->description,
  948. 'grant_rate' => $this->grant_rate,
  949. 'offset_rate' => $this->offset_rate,
  950. 'offset_max' => $this->offset_max,
  951. 'fields' => $this->fields,
  952. 'grant' => $this->grant,
  953. 'discount_type' => $this->discount_type,
  954. 'nums_status' => $this->nums_status,
  955. 'nums_text' => $this->nums_text,
  956. 'times_status' => $this->times_status,
  957. 'times_text' => $this->times_text,
  958. 'params' => $this->params,
  959. 'html' => $this->html,
  960. 'notice' => $this->notice,
  961. 'quantity' => $this->sku['quantity'],
  962. 'least_money_to_use_bonus' => $this->bonus_rule['least_money_to_use_bonus'],
  963. 'max_increase_bonus' => $this->bonus_rule['max_increase_bonus']
  964. );
  965. }
  966. public function getMemberCardUpdateArray() {
  967. $update['card_id'] = $this->card_id;
  968. $card = $this->getCardData();
  969. $update = array_merge($update, $card['card']);
  970. unset($update['card_type']);
  971. unset($update['member_card']['base_info']['source']);
  972. unset($update['member_card']['base_info']['sub_title']);
  973. unset($update['member_card']['base_info']['sku']);
  974. unset($update['member_card']['base_info']['use_custom_code']);
  975. unset($update['member_card']['base_info']['promotion_url_name']);
  976. unset($update['member_card']['base_info']['promotion_url']);
  977. unset($update['member_card']['base_info']['custom_url_name']);
  978. unset($update['member_card']['base_info']['custom_url']);
  979. unset($update['member_card']['base_info']['brand_name']);
  980. unset($update['member_card']['custom_cell1']);
  981. $update['member_card']['base_info']['promotion_url_name'] = urlencode('广播');
  982. $update['member_card']['base_info']['custom_url_name'] = urlencode('个人消息');
  983. $update['member_card']['base_info']['center_title'] = urlencode('付款');
  984. $update['member_card']['base_info']['title'] = urlencode($update['member_card']['base_info']['title']);
  985. $update['member_card']['base_info']['description'] = urlencode($update['member_card']['base_info']['description']);
  986. $update['member_card']['prerogative'] = urlencode($update['member_card']['prerogative']);
  987. return $update;
  988. }
  989. public function GetMemberCardArray() {
  990. $data = $this->getcardarray();
  991. return $data;
  992. }
  993. 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) {
  994. $this->bonus_rule = array(
  995. 'cost_money_unit' => $cost_money_unit,
  996. 'increase_bonus' => $increase_bonus,
  997. 'max_increase_bonus' => $max_increase_bonus,
  998. 'init_increase_bonus' => $init_increase_bonus,
  999. 'cost_bonus_unit' => $cost_bonus_unit,
  1000. 'reduce_money' => $reduce_money,
  1001. 'least_money_to_use_bonus' => $least_money_to_use_bonus,
  1002. 'max_reduce_bonus' => $max_reduce_bonus,
  1003. );
  1004. return true;
  1005. }
  1006. public function setCustomCell($name, $tips, $url) {
  1007. $this->custom_cell1 = array(
  1008. 'name' => $name,
  1009. 'tips' => $tips,
  1010. 'url' => $url
  1011. );
  1012. return true;
  1013. }
  1014. public function setCustomField($name_type, $url, $num) {
  1015. $array = array(
  1016. 'name_type' => $name_type,
  1017. 'url' => $url
  1018. );
  1019. if ($num == 1) {
  1020. $this->custom_field1 = $array;
  1021. }
  1022. if ($num == 2) {
  1023. $this->custom_field2 = $array;
  1024. }
  1025. if ($num == 3) {
  1026. $this->custom_field3 = $array;
  1027. }
  1028. return true;
  1029. }
  1030. public function validate() {
  1031. $error = parent::validate();
  1032. if (is_error($error) && $error['errno'] != 11) {
  1033. return $error;
  1034. }
  1035. if (!empty($this->supply_bonus)) {
  1036. if (empty($this->bonus_rule['cost_money_unit'])) {
  1037. return error(13, '未填写积分说明中的消费金额');
  1038. }
  1039. if (empty($this->bonus_rule['increase_bonus'])) {
  1040. return error(14, '未填写积分说明中的对应增加金额');
  1041. }
  1042. if (empty($this->bonus_rule['max_increase_bonus'])) {
  1043. return error(15, '未填写积分说明中的用户单次可获取的积分上限');
  1044. }
  1045. if (empty($this->bonus_rule['init_increase_bonus'])) {
  1046. return error(16, '未填写积分说明中的初始设置积分');
  1047. }
  1048. if (empty($this->bonus_rule['cost_bonus_unit'])) {
  1049. return error(17, '未填写积分说明中的每次使用积分');
  1050. }
  1051. if (empty($this->bonus_rule['reduce_money'])) {
  1052. return error(18, '未填写积分说明中的会员卡可抵扣多少元');
  1053. }
  1054. if (empty($this->bonus_rule['least_money_to_use_bonus'])) {
  1055. return error(19, '未填写积分说明中的满xx元可用');
  1056. }
  1057. if (empty($this->bonus_rule['max_reduce_bonus'])) {
  1058. return error(20, '未填写积分说明中的单笔最多使用xx积分');
  1059. }
  1060. }
  1061. if (!empty($this->custom_cell1['name']) || !empty($this->custom_cell1['tips']) || !empty($this->custom_cell1['url'])) {
  1062. if (empty($this->custom_cell1['name'])) {
  1063. return error(21, '未填写入口名称');
  1064. }
  1065. if (empty($this->custom_cell1['url'])) {
  1066. return error(23, '未填写入口跳转链接');
  1067. }
  1068. }
  1069. if (empty($this->prerogative)) {
  1070. return error(24, '未填写会员卡特权说明');
  1071. }
  1072. if (empty($this->wx_activate) && empty($this->activate_url)) {
  1073. return error(25, '未填写激活会员卡url');
  1074. }
  1075. return true;
  1076. }
  1077. public function getCardExtraData() {
  1078. return array(
  1079. 'background_pic_url' => $this->background_pic_url,
  1080. 'supply_bonus' => $this->supply_bonus,
  1081. 'bonus_rule' => $this->bonus_rule,
  1082. 'supply_balance' => $this->supply_balance,
  1083. 'prerogative' => $this->prerogative,
  1084. 'auto_activate' => $this->auto_activate,
  1085. 'custom_field1' => $this->custom_field1,
  1086. 'activate_url' => $this->activate_url,
  1087. 'wx_activate' => $this->wx_activate,
  1088. 'bonus_url' => $this->bonus_url,
  1089. 'balance_url' => $this->balance_url,
  1090. 'bonus_rules' => $this->bonus_rules,
  1091. 'balance_rules' => $this->balance_rules,
  1092. 'custom_cell1' => $this->custom_cell1,
  1093. 'discount' => $this->discount,
  1094. 'bonus_cleared' => $this->bonus_cleared,
  1095. );
  1096. }
  1097. }
  1098. class DiscountCard extends Card {
  1099. public $discount = 0;
  1100. public function validate() {
  1101. $error = parent::validate();
  1102. if (is_error($error)) {
  1103. return $error;
  1104. }
  1105. if (empty($this->discount)) {
  1106. return error(1, '未设置折扣券折扣');
  1107. }
  1108. return true;
  1109. }
  1110. public function getCardExtraData() {
  1111. return array(
  1112. 'discount' => $this->discount,
  1113. );
  1114. }
  1115. }
  1116. class CashCard extends Card {
  1117. public $least_cost = 0; public $reduce_cost = 0;
  1118. public function validate() {
  1119. $error = parent::validate();
  1120. if (is_error($error)) {
  1121. return $error;
  1122. }
  1123. if (!isset($this->least_cost)) {
  1124. return error(2, '未设置代金券起用金额');
  1125. }
  1126. if (empty($this->least_cost)) {
  1127. return error(3, '未设置代金券减免金额');
  1128. }
  1129. return true;
  1130. }
  1131. public function getCardExtraData() {
  1132. return array(
  1133. 'least_cost' => $this->least_cost,
  1134. 'reduce_cost' => $this->reduce_cost,
  1135. );
  1136. }
  1137. }
  1138. class GiftCard extends Card {
  1139. public $gift = '';
  1140. public function validate() {
  1141. $error = parent::validate();
  1142. if (is_error($error)) {
  1143. return $error;
  1144. }
  1145. if (empty($this->gift)) {
  1146. return error(4, '未设置礼品券兑换内容');
  1147. }
  1148. return true;
  1149. }
  1150. public function getCardExtraData() {
  1151. return array(
  1152. 'gift' => $this->gift,
  1153. );
  1154. }
  1155. }
  1156. class GrouponCard extends Card {
  1157. public $deal_detail = ''; public function validate() {
  1158. $error = parent::validate();
  1159. if (is_error($error)) {
  1160. return $error;
  1161. }
  1162. if (empty($this->deal_detail)) {
  1163. return error(5, '未设置团购券详情内容');
  1164. }
  1165. return true;
  1166. }
  1167. public function getCardExtraData() {
  1168. return array(
  1169. 'deal_detail' => $this->deal_detail,
  1170. );
  1171. }
  1172. }
  1173. class GeneralCard extends Card {
  1174. public $default_detail = ''; public function validate() {
  1175. $error = parent::validate();
  1176. if (is_error($error)) {
  1177. return $error;
  1178. }
  1179. if (empty($this->default_detail)) {
  1180. return error(6, '未设置优惠券优惠详情');
  1181. }
  1182. return true;
  1183. }
  1184. public function getCardExtraData() {
  1185. return array(
  1186. 'default_detail' => $this->default_detail,
  1187. );
  1188. }
  1189. }