site.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. class StoreModuleSite extends WeModuleSite {
  8. public $modulename = 'store';
  9. private $left_menus;
  10. public function __construct() {
  11. global $_W, $_GPC;
  12. if ($_GPC['c'] == 'site') {
  13. checklogin();
  14. }
  15. load()->model('store');
  16. $this->store_setting = (array)$_W['setting']['store'];
  17. $this->left_menus = $this->leftMenu();
  18. }
  19. public function storeIsOpen() {
  20. global $_W;
  21. if ((!$_W['isfounder'] || user_is_vice_founder()) && $this->store_setting['status'] == 1) {
  22. itoast('商城已被创始人关闭!', referer(), 'error');
  23. }
  24. if (in_array($_W['username'], (array)$this->store_setting['blacklist'])) {
  25. itoast('您无权限进入商城,请联系管理员!', referer(), 'error');
  26. }
  27. return true;
  28. }
  29. public function getTypeName($type) {
  30. $sign = array(
  31. STORE_TYPE_MODULE => '应用模块',
  32. STORE_TYPE_ACCOUNT => '公众号个数',
  33. STORE_TYPE_WXAPP => '小程序个数',
  34. STORE_TYPE_WXAPP_MODULE => '小程序模块',
  35. STORE_TYPE_PACKAGE => '应用权限组',
  36. STORE_TYPE_API => '应用访问流量(API)',
  37. STORE_TYPE_ACCOUNT_RENEW => '公众号续费',
  38. STORE_TYPE_WXAPP_RENEW => '小程序续费'
  39. );
  40. return $sign[$type];
  41. }
  42. public function payResult($params) {
  43. global $_W;
  44. if($params['result'] == 'success' && $params['from'] == 'notify') {
  45. $order = pdo_get('site_store_order', array('id' => $params['tid'], 'type' => 1));
  46. if(!empty($order)) {
  47. $goods = pdo_get('site_store_goods', array('id' => $order['goodsid']));
  48. pdo_update('site_store_order', array('type' => 3), array('id' => $params['tid']));
  49. if (in_array($goods['type'], array(STORE_TYPE_ACCOUNT_RENEW, STORE_TYPE_WXAPP_RENEW))) {
  50. $account_type = $goods['type'] == STORE_TYPE_ACCOUNT_RENEW ? 'uniacid' : 'wxapp';
  51. $account_num = $goods['type'] == STORE_TYPE_ACCOUNT_RENEW ? $goods['account_num'] : $goods['wxapp_num'];
  52. $account_info = uni_fetch($order[$account_type]);
  53. $account_endtime = strtotime('+' . $order['duration'] * $account_num . $goods['unit'], max(TIMESTAMP, $account_info['endtime']));
  54. pdo_update('account', array('endtime' => $account_endtime), array('uniacid' => $order[$account_type]));
  55. cache_delete("uniaccount:{$order[$account_type]}");
  56. }
  57. cache_delete(cache_system_key($order['uniacid'] . ':site_store_buy_' . $goods['type']));
  58. cache_build_account_modules($order['uniacid']);
  59. }
  60. }
  61. if($params['result'] == 'success' && $params['from'] == 'return') {
  62. header('Location: ' . $_W['siteroot'] . $this->createWebUrl('orders', array('direct' => 1)));
  63. }
  64. }
  65. public function doWebPaySetting() {
  66. $this->storeIsOpen();
  67. global $_W, $_GPC;
  68. if (!$_W['isfounder'] || user_is_vice_founder()) {
  69. itoast('', referer(), 'info');
  70. }
  71. $operate = $_GPC['operate'];
  72. $operates = array('alipay', 'wechat');
  73. $operate = in_array($operate, $operates) ? $operate : 'alipay';
  74. $_W['page']['title'] = '支付设置 - 商城';
  75. $settings = $_W['setting']['store_pay'];
  76. if (checksubmit('submit')) {
  77. if ($operate == 'alipay') {
  78. $settings['alipay'] = array(
  79. 'switch' => intval($_GPC['switch']),
  80. 'account' => trim($_GPC['account']),
  81. 'partner' => trim($_GPC['partner']),
  82. 'secret' => trim($_GPC['secret']),
  83. );
  84. } elseif ($operate == 'wechat') {
  85. if ($_GPC['switch'] == 1 && (empty($_GPC['appid']) || empty($_GPC['mchid']) || empty($_GPC['signkey']))) {
  86. itoast('请完善支付设置。', referer(), 'info');
  87. }
  88. $settings['wechat'] = array(
  89. 'switch' => intval($_GPC['switch']),
  90. 'appid' => $_GPC['appid'],
  91. 'mchid' => $_GPC['mchid'],
  92. 'signkey' => $_GPC['signkey'],
  93. );
  94. }
  95. setting_save($settings, 'store_pay');
  96. itoast('设置成功!', referer(), 'success');
  97. }
  98. if ($operate == 'alipay') {
  99. $alipay = $settings['alipay'];
  100. } elseif ($operate == 'wechat') {
  101. $wechat = $settings['wechat'];
  102. }
  103. include $this->template('paysetting');
  104. }
  105. public function doWebOrders() {
  106. $this->storeIsOpen();
  107. global $_GPC, $_W;
  108. load()->model('module');
  109. load()->model('message');
  110. $operates = array('display', 'change_price', 'delete');
  111. $operate = $_GPC['operate'];
  112. $operate = in_array($operate, $operates) ? $operate : 'display';
  113. $_W['page']['title'] = '订单管理 - 商城';
  114. if (user_is_vice_founder()) {
  115. $role = 'buyer';
  116. } elseif (!empty($_W['isfounder'])) {
  117. $role = 'seller';
  118. } else {
  119. $role = 'buyer';
  120. }
  121. if ($operate == 'display') {
  122. if (user_is_founder($_W['uid']) && !user_is_vice_founder($_W['uid'])) {
  123. $message_id = $_GPC['message_id'];
  124. message_notice_read($message_id);
  125. }
  126. $pindex = max(1, intval($_GPC['page']));
  127. $psize = 15;
  128. $store_table = table('store');
  129. if (isset($_GPC['type']) && intval($_GPC['type']) > 0) {
  130. $order_type = intval($_GPC['type']);
  131. $store_table->searchOrderType($order_type);
  132. }
  133. $store_table->searchWithOrderid($_GPC['orderid']);
  134. if (empty($_W['isfounder']) || user_is_vice_founder()) {
  135. $store_table->searchOrderWithUid($_W['uid']);
  136. }
  137. $order_list = $store_table->searchOrderList($pindex, $psize);
  138. $total = $store_table->getLastQueryTotal();
  139. $pager = pagination($total, $pindex, $psize);
  140. if (!empty($order_list)) {
  141. foreach ($order_list as $key => &$order) {
  142. if (empty($_W['isfounder']) && $order['type'] == 2) {
  143. unset($order_list[$key]);
  144. }
  145. $order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
  146. $order['goods_info'] = store_goods_info($order['goodsid']);
  147. $order['abstract_amount'] = $order['duration'] * $order['goods_info']['price'];
  148. if (!empty($order['goods_info']) && ($order['goods_info']['type'] == STORE_TYPE_MODULE || $order['goods_info']['type'] == STORE_TYPE_WXAPP_MODULE)) {
  149. $order['goods_info']['module_info'] = module_fetch($order['goods_info']['module']);
  150. }
  151. }
  152. unset($order);
  153. }
  154. }
  155. if ($operate == 'change_price') {
  156. if (user_is_vice_founder() || empty($_W['isfounder'])) {
  157. iajax(-1, '无权限更改!');
  158. }
  159. $id = intval($_GPC['id']);
  160. $price = floatval($_GPC['price']);
  161. $if_exists = store_order_info($id);
  162. if (empty($if_exists)) {
  163. iajax(-1, '订单不存在!');
  164. }
  165. $result = store_order_change_price($id, $price);
  166. if (!empty($result)) {
  167. iajax(0, '修改成功!');
  168. } else {
  169. iajax(-1, '修改失败!');
  170. }
  171. }
  172. if ($operate == 'delete') {
  173. $id = intval($_GPC['id']);
  174. if (empty($id)) {
  175. itoast('订单错误,请刷新后重试!');
  176. }
  177. $order_info = store_order_info($id);
  178. if (empty($order_info)) {
  179. itoast('订单不存在!');
  180. }
  181. if ($order_info['type'] != STORE_ORDER_PLACE) {
  182. itoast('只可删除未完成交易的订单!');
  183. }
  184. $result = store_order_delete($id);
  185. if (!empty($result)) {
  186. itoast('删除成功!', referer(), 'success');
  187. } else {
  188. itoast('删除失败,请稍候重试!', referer(), 'error');
  189. }
  190. }
  191. include $this->template('orders');
  192. }
  193. public function doWebSetting() {
  194. $this->storeIsOpen();
  195. global $_GPC, $_W;
  196. if (!$_W['isfounder'] || user_is_vice_founder()) {
  197. itoast('', referer(), 'info');
  198. }
  199. $operate = $_GPC['operate'];
  200. $operates = array('store_status', 'menu');
  201. $operate = in_array($operate, $operates) ? $operate : 'store_status';
  202. $_W['page']['title'] = '商城设置 - 商城';
  203. $settings = $this->store_setting;
  204. if ($operate == 'store_status') {
  205. if (checksubmit('submit')) {
  206. $status = intval($_GPC['status']) > 0 ? 1 : 0;
  207. $settings['status'] = $status;
  208. setting_save($settings, 'store');
  209. itoast('更新设置成功!', referer(), 'success');
  210. }
  211. }
  212. if ($operate == 'menu') {
  213. $left_menu = $this->leftMenu();
  214. $goods_menu = !empty($left_menu['store_goods']) ? $left_menu['store_goods']['menu'] : array();
  215. if (checksubmit('submit')) {
  216. foreach ($goods_menu as $key => $menu) {
  217. $settings[$key] = intval($_GPC['hide'][$key]) > 0 ? 1 : 0;
  218. }
  219. setting_save($settings, 'store');
  220. itoast('更新设置成功!', referer(), 'success');
  221. }
  222. }
  223. include $this->template('storesetting');
  224. }
  225. public function doWebGoodsSeller() {
  226. $this->storeIsOpen();
  227. global $_GPC, $_W;
  228. load()->model('module');
  229. if (!$_W['isfounder'] || user_is_vice_founder()) {
  230. itoast('', referer(), 'info');
  231. }
  232. $operate = $_GPC['operate'];
  233. $operates = array('display', 'delete', 'changestatus');
  234. $operate = in_array($operate, $operates) ? $operate : 'display';
  235. $type = intval($_GPC['type']) > 0 ? intval($_GPC['type']) : STORE_TYPE_MODULE;
  236. $_W['page']['title'] = '商品列表 - 商城管理 - 商城';
  237. if ($operate == 'display') {
  238. $pindex = max(1, intval($_GPC['page']));
  239. $psize = 15;
  240. $store_table = table('store');
  241. $keyword = trim($_GPC['keyword']);
  242. if (!empty($keyword)) {
  243. $store_table->searchWithKeyword($keyword);
  244. }
  245. $status = isset($_GPC['online']) && $_GPC['online'] == 0 ? 0 : 1;
  246. $store_table->searchWithStatus($status);
  247. if(isset($_GPC['letter']) && strlen($_GPC['letter']) == 1) {
  248. $store_table->searchWithLetter($_GPC['letter']);
  249. }
  250. $goods_list = $store_table->searchGoodsList($type, $pindex, $psize);
  251. $total = $goods_list['total'];
  252. $goods_list = $goods_list['goods_list'];
  253. $pager = pagination($total, $pindex, $psize);
  254. if (!empty($goods_list)) {
  255. foreach ($goods_list as &$good) {
  256. $good['module_info'] = module_fetch($good['module']);
  257. }
  258. unset($good);
  259. }
  260. $module_list = array();
  261. if (in_array($type, array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE))) {
  262. $modules = user_uniacid_modules($_W['uid']);
  263. $have_module_goods = $store_table->searchHaveModule($type);
  264. $have_module_goods = array_keys($have_module_goods);
  265. $have_module_goods = array_unique($have_module_goods);
  266. if (!empty($modules)) {
  267. foreach ($modules as $module) {
  268. if (in_array ($module['name'], $have_module_goods) || $type == STORE_TYPE_MODULE && $module['app_support'] != 2 || $type == STORE_TYPE_WXAPP_MODULE && $module['wxapp_support'] != 2) {
  269. continue;
  270. }
  271. $module = module_fetch ($module['name']);
  272. $module_list[] = $module;
  273. }
  274. }
  275. }
  276. if ($type == STORE_TYPE_PACKAGE) {
  277. $groups = uni_groups();
  278. }
  279. }
  280. if ($operate == 'changestatus' || $operate == 'delete') {
  281. $id = intval($_GPC['id']);
  282. $if_exist = store_goods_info($id);
  283. if (empty($if_exist)) {
  284. itoast('商品不存在,请刷新后重试!', referer(), 'error');
  285. }
  286. }
  287. if ($operate == 'changestatus') {
  288. $result = store_goods_changestatus($id);
  289. if (!empty($result)) {
  290. itoast('更新成功!', referer(), 'success');
  291. } else {
  292. itoast('更新失败!', referer(), 'error');
  293. }
  294. }
  295. if ($operate == 'delete') {
  296. $result = store_goods_delete($id);
  297. if (!empty($result)) {
  298. itoast('删除成功!', referer(), 'success');
  299. } else {
  300. itoast('删除失败!', referer(), 'error');
  301. }
  302. }
  303. include $this->template('goodsseller');
  304. }
  305. public function doWebGoodsPost() {
  306. $this->storeIsOpen();
  307. global $_GPC, $_W;
  308. if (!$_W['isfounder'] || user_is_vice_founder()) {
  309. itoast('', referer(), 'info');
  310. }
  311. $operate = $_GPC['operate'];
  312. $operates = array('post', 'add');
  313. $operate = in_array($operate, $operates) ? $operate : 'post';
  314. $type = intval($_GPC['type']) > 0 ? intval($_GPC['type']) : STORE_TYPE_MODULE;
  315. $_W['page']['title'] = '编辑商品 - 商城管理 - 商城';
  316. if ($operate == 'post') {
  317. $id = intval($_GPC['id']);
  318. if (checksubmit('submit')) {
  319. if (!empty($_GPC['price']) && !is_numeric($_GPC['price'])) {
  320. itoast('请填写有效数字!', referer(), 'error');
  321. }
  322. $data = array(
  323. 'unit' => $_GPC['unit'],
  324. 'account_num' => $_GPC['account_num'],
  325. 'wxapp_num' => $_GPC['wxapp_num'],
  326. 'module_group' => $_GPC['module_group'],
  327. 'type' => $_GPC['type'],
  328. 'title' => !empty($_GPC['title']) ? trim($_GPC['title']) : '',
  329. 'price' => is_numeric($_GPC['price']) ? floatval($_GPC['price']) : 0,
  330. 'slide' => !empty($_GPC['slide']) ? iserializer($_GPC['slide']) : '',
  331. 'api_num' => is_numeric($_GPC['api_num']) ? intval($_GPC['api_num']) : 0,
  332. 'description' => safe_gpc_html(htmlspecialchars_decode($_GPC['description'])),
  333. );
  334. if ($_GPC['type'] == STORE_TYPE_API) {
  335. $data['title'] = '应用访问量';
  336. }
  337. if ($_GPC['type'] == STORE_TYPE_PACKAGE) {
  338. $data['title'] = '应用权限组';
  339. }
  340. if ($_GPC['submit'] == '保存并上架') {
  341. $data['status'] = 1;
  342. }
  343. if (!empty($id)) {
  344. $data['id'] = $id;
  345. }
  346. $result = store_goods_post($data);
  347. if (!empty($result)) {
  348. if (!empty($id)) {
  349. itoast('编辑成功!', $this->createWebUrl('goodsseller', array('direct' =>1, 'type' => $type, 'online' => $data['status'])), 'success');
  350. } else {
  351. itoast('添加成功!', $this->createWebUrl('goodsSeller', array('direct' =>1, 'type' => $type)), 'success');
  352. }
  353. } else {
  354. itoast('未作任何更改或编辑/添加失败!', referer(), 'error');
  355. }
  356. }
  357. if (!empty($id)) {
  358. $goods_info = store_goods_info($id);
  359. $goods_info['slide'] = !empty($goods_info['slide']) ? (array)iunserializer($goods_info['slide']) : array();
  360. $goods_info['price'] = floatval($goods_info['price']);
  361. }
  362. if ($_GPC['type'] == STORE_TYPE_PACKAGE) {
  363. $module_groups = uni_groups();
  364. }
  365. }
  366. if ($operate == 'add') {
  367. if (empty($_GPC['module']) && $type == STORE_TYPE_MODULE) {
  368. iajax(-1, '请选择一个模块!');
  369. }
  370. $data = array(
  371. 'type' => $type,
  372. 'title' => !empty($_GPC['module']['title']) ? trim($_GPC['module']['title']) : trim($_GPC['title']),
  373. 'module' => !empty($_GPC['module']['name']) ? trim($_GPC['module']['name']) : '',
  374. 'synopsis' => !empty($_GPC['module']['ability']) ? trim($_GPC['module']['ability']) : '',
  375. 'description' => !empty($_GPC['module']['description']) ? trim($_GPC['module']['description']) : '',
  376. 'api_num' => is_numeric($_GPC['visit_times']) ? intval($_GPC['visit_times']) : 0,
  377. 'price' => is_numeric($_GPC['price']) ? floatval($_GPC['price']) : 0,
  378. 'status' => !empty($_GPC['online']) ? STATUS_ON : STATUS_OFF,
  379. );
  380. $result = store_goods_post($data);
  381. if (!empty($result)) {
  382. if (isset($_GPC['toedit']) && !empty($_GPC['toedit'])) {
  383. $id = pdo_insertid();
  384. iajax(0, $id);
  385. } else {
  386. iajax(0, '添加成功!');
  387. }
  388. } else {
  389. iajax(-1, '添加失败!');
  390. }
  391. }
  392. include $this->template('goodspost');
  393. }
  394. public function doWebGoodsBuyer() {
  395. $this->storeIsOpen();
  396. global $_GPC, $_W;
  397. load()->model('module');
  398. load()->model('payment');
  399. load()->model('message');
  400. load()->func('communication');
  401. load()->library('qrcode');
  402. $operate = $_GPC['operate'];
  403. $operates = array ('display', 'goods_info', 'get_expiretime', 'submit_order', 'pay_order');
  404. $operate = in_array($operate, $operates) ? $operate : 'display';
  405. $_W['page']['title'] = '商品列表 - 商城';
  406. if ($operate == 'display') {
  407. $pageindex = max(intval($_GPC['page']), 1);
  408. $pagesize = 24;
  409. $type = 0;
  410. if (!empty($_GPC['type']) && in_array($_GPC['type'], array(STORE_TYPE_MODULE, STORE_TYPE_ACCOUNT, STORE_TYPE_WXAPP, STORE_TYPE_WXAPP_MODULE, STORE_TYPE_PACKAGE, STORE_TYPE_API, STORE_TYPE_ACCOUNT_RENEW, STORE_TYPE_WXAPP_RENEW))) {
  411. $type = $_GPC['type'];
  412. }
  413. $store_table = table ('store');
  414. $store_table->searchWithStatus (1);
  415. $store_table = $store_table->searchGoodsList ($type, $pageindex, $pagesize);
  416. $store_goods = $store_table['goods_list'];
  417. if ((empty($type) || in_array($type, array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE))) && is_array($store_goods)) {
  418. foreach ($store_goods as $key => &$goods) {
  419. if (empty($goods) || !in_array($goods['type'], array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE))) {
  420. continue;
  421. }
  422. $goods['module'] = module_fetch ($goods['module']);
  423. }
  424. unset($goods);
  425. }
  426. if ($_GPC['type'] == STORE_TYPE_PACKAGE) {
  427. $module_groups = uni_groups();
  428. }
  429. $pager = pagination ($store_table['total'], $pageindex, $pagesize);
  430. }
  431. if ($operate == 'goods_info') {
  432. $goods = intval ($_GPC['goods']);
  433. if (empty($goods)) {
  434. itoast ('商品不存在', '', 'info');
  435. }
  436. $goods = pdo_get ('site_store_goods', array ('id' => $goods));
  437. if (in_array($goods['type'], array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE, STORE_TYPE_API))) {
  438. $goods['module'] = module_fetch ($goods['module']);
  439. $goods['slide'] = iunserializer ($goods['slide']);
  440. } elseif (in_array($goods['type'], array(STORE_TYPE_ACCOUNT, STORE_TYPE_WXAPP))) {
  441. $goods['title'] = $goods['type'] == STORE_TYPE_ACCOUNT ? '公众号' : '小程序';
  442. $goods['num'] = $goods['type'] == STORE_TYPE_ACCOUNT ? $goods['account_num'] : $goods['wxapp_num'];
  443. } elseif ($goods['type'] == STORE_TYPE_PACKAGE) {
  444. $module_groups = uni_groups();
  445. }
  446. $account_table = table ('account');
  447. $user_account = $account_table->userOwnedAccount();
  448. $wxapp_account_list = array();
  449. if (!empty($user_account) && is_array($user_account)) {
  450. foreach ($user_account as $key => $account) {
  451. $default_account = uni_fetch($account['uniacid']);
  452. if (in_array($goods['type'], array(STORE_TYPE_MODULE, STORE_TYPE_ACCOUNT_RENEW)) && !in_array($default_account['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH)) || in_array($goods['type'], array(STORE_TYPE_WXAPP_MODULE, STORE_TYPE_WXAPP_RENEW)) && $default_account['type'] != 4) {
  453. unset($user_account[$key]);
  454. }
  455. if (in_array($goods['type'], array(STORE_TYPE_ACCOUNT_RENEW, STORE_TYPE_WXAPP_RENEW)) && $default_account['endtime'] <= 0) {
  456. unset($user_account[$key]);
  457. }
  458. if ($goods['type'] == STORE_TYPE_PACKAGE && !empty($module_groups[$goods['module_group']]['wxapp']) && $default_account['type'] == 4) {
  459. $wxapp_account_list[] = array('uniacid' => $default_account['uniacid'], 'name' => $default_account['name']);
  460. }
  461. }
  462. }
  463. reset($user_account);
  464. reset($wxapp_account_list);
  465. $default_account = current($user_account);
  466. $default_account = !empty($_GPC['uniacid']) ? $_GPC['uniacid'] : $default_account['uniacid'];
  467. $default_wxapp = current($wxapp_account_list);
  468. $default_wxapp = !empty($_GPC['wxapp']) ? $_GPC['wxapp'] : $default_wxapp['uniacid'];
  469. if (in_array($goods['type'], array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE)) && empty($user_account)) {
  470. $type_name = $goods['type'] == STORE_TYPE_MODULE ? '公众号' : '小程序';
  471. itoast("您没有可操作的{$type_name},请先创建{$type_name}后购买模块.", referer(), 'info');
  472. }
  473. $pay_way = array();
  474. if (!empty($_W['setting']['store_pay']) && is_array($_W['setting']['store_pay']) && ($_W['setting']['store_pay']['alipay']['switch'] == 1 || $_W['setting']['store_pay']['wechat']['switch'] == 1)) {
  475. foreach ($_W['setting']['store_pay'] as $way => $setting) {
  476. if ($setting['switch'] == 1) {
  477. $pay_way[$way] = $setting;
  478. if ($way == 'alipay') {
  479. $pay_way[$way]['title'] = '支付宝';
  480. } elseif ($way == 'wechat') {
  481. $pay_way[$way]['title'] = '微信';
  482. }
  483. }
  484. }
  485. } else {
  486. itoast('没有有效的支付方式.', referer(), 'info');
  487. }
  488. }
  489. if ($operate == 'get_expiretime') {
  490. $duration = intval ($_GPC['duration']);
  491. $date = date ('Y-m-d', strtotime ('+' . $duration . $_GPC['unit'], time ()));
  492. iajax (0, $date);
  493. }
  494. if ($operate == 'submit_order') {
  495. $uniacid = intval ($_GPC['uniacid']);
  496. $goodsid = intval($_GPC['goodsid']);
  497. if (empty($_GPC['type'])) {
  498. iajax(-1, '请选择支付方式。');
  499. }
  500. if (empty($goodsid)) {
  501. iajax(-1, '参数错误!');
  502. }
  503. $user_account = table('account')->userOwnedAccount();
  504. $goods_info = store_goods_info($goodsid);
  505. if (in_array($goods_info['type'], array(STORE_TYPE_PACKAGE, STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE, STORE_TYPE_API, STORE_TYPE_ACCOUNT_RENEW, STORE_TYPE_WXAPP_RENEW))) {
  506. if (empty($uniacid)) {
  507. iajax(-1, '请选择公众号!');
  508. }
  509. if (empty($user_account[$uniacid])) {
  510. iajax(-1, '非法公众号!');
  511. }
  512. }
  513. if (empty($goods_info)) {
  514. iajax(-1, '商品不存在!');
  515. }
  516. $uid = empty($_W['uid']) ? '000000' : sprintf ("%06d", $_W['uid']);
  517. $orderid = date ('YmdHis') . $uid . random (8, 1);
  518. $duration = intval ($_GPC['duration']);
  519. $order = array (
  520. 'orderid' => $orderid,
  521. 'duration' => $duration,
  522. 'amount' => $goods_info['price'] * $duration,
  523. 'goodsid' => $goodsid,
  524. 'buyer' => $_W['user']['username'],
  525. 'buyerid' => $_W['uid'],
  526. 'type' => STORE_ORDER_PLACE,
  527. 'createtime' => time(),
  528. 'uniacid' => $uniacid,
  529. 'wxapp' => intval($_GPC['wxapp'])
  530. );
  531. if (in_array($goods_info['type'], array(STORE_TYPE_ACCOUNT, STORE_TYPE_WXAPP, STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE, STORE_TYPE_PACKAGE))) {
  532. $history_order_endtime = pdo_getcolumn('site_store_order', array('goodsid' => $goodsid, 'buyerid' => $_W['uid']), 'max(endtime)');
  533. $order['endtime'] = strtotime('+' . $duration . $goods_info['unit'], max($history_order_endtime, time()));
  534. }
  535. if (in_array($goods_info['type'], array(STORE_TYPE_WXAPP, STORE_TYPE_WXAPP_RENEW))) {
  536. $order['wxapp'] = $order['uniacid'];
  537. $order['uniacid'] = 0;
  538. }
  539. pdo_insert ('site_store_order', $order);
  540. $store_orderid = pdo_insertid();
  541. $type_name = $this->getTypeName($goods_info['type']);
  542. $content = $_W['user']['username'] . date("Y-m-d H:i:s") . '在商城购买了' . $type_name . ', 支付金额' . $order['amount'];
  543. message_notice_record($content, $_W['uid'], $orderid, MESSAGE_ORDER_TYPE);
  544. $pay_log = array(
  545. 'type' => $_GPC['type'],
  546. 'uniontid' => $orderid,
  547. 'tid' => $store_orderid,
  548. 'fee' => $order['amount'],
  549. 'card_fee' => $order['amount'],
  550. 'module' => 'store'
  551. );
  552. pdo_insert('core_paylog', $pay_log);
  553. iajax (0, $store_orderid);
  554. }
  555. if ($operate == 'pay_order') {
  556. $orderid = intval ($_GPC['orderid']);
  557. $order = pdo_get ('site_store_order', array ('id' => $orderid));
  558. $goods = pdo_get ('site_store_goods', array ('id' => $order['goodsid']));
  559. if (empty($order)) {
  560. itoast ('订单不存在', referer (), 'info');
  561. }
  562. if ($order['type'] != 1) {
  563. $message = $order['type'] == 2 ? '订单已删除.' : '订单已付款成功';
  564. itoast ($message, referer (), 'info');
  565. } else {
  566. if ($order['amount'] == 0) {
  567. pdo_update('site_store_order', array('type' => 3), array('id' => $order['id']));
  568. pdo_update('core_paylog', array('status' => 1), array('uniontid' => $order['orderid']));
  569. if (in_array($goods['type'], array(STORE_TYPE_ACCOUNT_RENEW, STORE_TYPE_WXAPP_RENEW))) {
  570. $account_type = $goods['type'] == STORE_TYPE_ACCOUNT_RENEW ? 'uniacid' : 'wxapp';
  571. $account_num = $goods['type'] == STORE_TYPE_ACCOUNT_RENEW ? $goods['account_num'] : $goods['wxapp_num'];
  572. $account_info = uni_fetch($order[$account_type]);
  573. $account_endtime = strtotime('+' . $order['duration'] * $account_num . $goods['unit'], max(TIMESTAMP, $account_info['endtime']));
  574. pdo_update('account', array('endtime' => $account_endtime), array('uniacid' => $order[$account_type]));
  575. cache_delete("uniaccount:{$order[$account_type]}");
  576. }
  577. cache_delete(cache_system_key($order['uniacid'] . ':site_store_buy_modules'));
  578. cache_build_account_modules($order['uniacid']);
  579. itoast('支付成功!', $this->createWebUrl('orders', array('direct' => 1)), 'success');
  580. }
  581. }
  582. $setting = setting_load ('store_pay');
  583. $core_paylog = pdo_get('core_paylog', array('module' => 'store', 'status' => 0, 'module' => 'store', 'uniontid' => $order['orderid'], 'tid' => $order['id']));
  584. if ($core_paylog['type'] == 'wechat') {
  585. $wechat_setting = $setting['store_pay']['wechat'];
  586. $params = array(
  587. 'pay_way' => 'web',
  588. 'title' => $goods['title'],
  589. 'uniontid' => $order['orderid'],
  590. 'fee' => $order['amount'],
  591. 'goodsid' => $goods['id'],
  592. );
  593. $wechat_setting['version'] = 2;
  594. $wechat_result = wechat_build($params, $wechat_setting);
  595. if (is_error($wechat_result)) {
  596. itoast($wechat_result['message'], $this->createWebUrl('goodsBuyer', array('direct' => 1)), 'info');
  597. }
  598. file_delete('store_wechat_pay_' . $_W['uid'] . '.png');
  599. $picture_attach = 'store_wechat_pay_' . $_W['uid'] . '.png';
  600. $picture = $_W['siteroot'] . 'attachment/' . $picture_attach;
  601. QRcode::png($wechat_result['code_url'], ATTACHMENT_ROOT . $picture_attach);
  602. include $this->template('wechat_pay_qrcode');
  603. } elseif ($core_paylog['type'] == 'alipay') {
  604. $alipay_setting = $setting['store_pay']['alipay'];
  605. $alipay_params = array (
  606. 'service' => 'create_direct_pay_by_user',
  607. 'title' => $goods['title'],
  608. 'fee' => $order['amount'],
  609. 'uniontid' => $order['orderid'],
  610. );
  611. $alipay_result = alipay_build($alipay_params, $alipay_setting);
  612. header ('Location: ' . $alipay_result['url']);
  613. }
  614. exit();
  615. }
  616. include $this->template ('goodsbuyer');
  617. }
  618. public function doWebBlacklist() {
  619. global $_W, $_GPC;
  620. $this->storeIsOpen();
  621. $operation = trim($_GPC['operation']);
  622. $operations = array('display', 'post', 'delete');
  623. $operation = in_array($operation, $operations) ? $operation : 'display';
  624. $blacklist = $this->store_setting['blacklist'];
  625. if (empty($blacklist)) {
  626. $blacklist = array();
  627. }
  628. if ($operation == 'display') {
  629. include $this->template('blacklist');
  630. }
  631. if ($operation == 'post') {
  632. $username = safe_gpc_string($_GPC['username']);
  633. $user_exist = pdo_get('users', array('username' => $username));
  634. if (empty($user_exist)) {
  635. itoast('用户不存在!');
  636. }
  637. if (in_array($username, $blacklist)) {
  638. itoast('用户已在黑名单中!');
  639. }
  640. array_push($blacklist, $username);
  641. $this->store_setting['blacklist'] = $blacklist;
  642. setting_save($this->store_setting, 'store');
  643. cache_build_frame_menu();
  644. itoast('更新黑名单成功!');
  645. }
  646. if ($operation == 'delete') {
  647. $username = safe_gpc_string($_GPC['username']);
  648. if (empty($username)) {
  649. itoast('参数错误!');
  650. }
  651. if (!in_array($username, $blacklist)) {
  652. itoast('用户不在黑名单中!');
  653. }
  654. foreach ($blacklist as $key => $val) {
  655. if ($val == $username) {
  656. unset($blacklist[$key]);
  657. }
  658. }
  659. $this->store_setting['blacklist'] = $blacklist;
  660. setting_save($this->store_setting, 'store');
  661. cache_build_frame_menu();
  662. itoast('删除成功!');
  663. }
  664. }
  665. public function leftMenu() {
  666. $this->storeIsOpen();
  667. $menu = array(
  668. 'store_goods' => array(
  669. 'title' => '商品分类',
  670. 'menu' => array(
  671. 'store_goods_module' =>array(
  672. 'title' => '应用模块',
  673. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_MODULE)),
  674. 'icon' => 'wi wi-apply',
  675. 'type' => STORE_TYPE_MODULE,
  676. ),
  677. 'store_goods_wxapp_module' => array(
  678. 'title' => '小程序模块',
  679. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_WXAPP_MODULE)),
  680. 'icon' => 'wi wi-small-routine',
  681. 'type' => STORE_TYPE_WXAPP_MODULE,
  682. ),
  683. 'store_goods_account' => array(
  684. 'title' => '公众号个数',
  685. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_ACCOUNT)),
  686. 'icon' => 'wi wi-wechat',
  687. 'type' => STORE_TYPE_ACCOUNT,
  688. ),
  689. 'store_goods_wxapp' => array(
  690. 'title' => '小程序个数',
  691. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_WXAPP)),
  692. 'icon' => 'wi wi-wxapp',
  693. 'type' => STORE_TYPE_WXAPP,
  694. ),
  695. 'store_goods_api' => array(
  696. 'title' => '应用访问流量(API)',
  697. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_API)),
  698. 'icon' => 'wi wi-api',
  699. 'type' => STORE_TYPE_API,
  700. ),
  701. 'store_goods_package' => array(
  702. 'title' => '应用权限组',
  703. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_PACKAGE)),
  704. 'icon' => 'wi wi-appjurisdiction',
  705. 'type' => STORE_TYPE_PACKAGE,
  706. ),
  707. 'store_goods_account_renew' => array(
  708. 'title' => '公众号续费',
  709. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_ACCOUNT_RENEW)),
  710. 'icon' => 'wi wi-appjurisdiction',
  711. 'type' => STORE_TYPE_ACCOUNT_RENEW,
  712. ),
  713. 'store_goods_wxapp_renew' => array(
  714. 'title' => '小程序续费',
  715. 'url' => $this->createWebUrl('goodsbuyer', array('direct' => 1, 'type' => STORE_TYPE_WXAPP_RENEW)),
  716. 'icon' => 'wi wi-appjurisdiction',
  717. 'type' => STORE_TYPE_WXAPP_RENEW,
  718. ),
  719. ),
  720. ),
  721. 'store_manage' => array(
  722. 'title' => '商城管理',
  723. 'founder' => true,
  724. 'menu' => array(
  725. 'store_manage_goods' => array(
  726. 'title' => '添加商品',
  727. 'url' => $this->createWebUrl('goodsSeller', array('direct' => 1)),
  728. 'icon' => 'wi wi-goods-add',
  729. 'type' => 'goodsSeller',
  730. ),
  731. 'store_manage_setting' => array(
  732. 'title' => '商城设置',
  733. 'url' => $this->createWebUrl('setting', array('direct' => 1)),
  734. 'icon' => 'wi wi-store',
  735. 'type' => 'setting',
  736. ),
  737. 'store_manage_payset' => array(
  738. 'title' => '支付设置',
  739. 'url' => $this->createWebUrl('paySetting', array('direct' => 1)),
  740. 'icon' => 'wi wi-account',
  741. 'type' => 'paySetting',
  742. ),
  743. 'store_manage_blacklist' => array(
  744. 'title' => '黑名单',
  745. 'url' => $this->createWebUrl('blacklist', array('direct' => 1)),
  746. 'icon' => 'wi wi-blacklist',
  747. 'type' => 'blacklist',
  748. ),
  749. )
  750. ),
  751. 'store_orders' => array(
  752. 'title' => '订单管理',
  753. 'menu' => array(
  754. 'store_orders_my' => array(
  755. 'title' => '我的订单',
  756. 'url' => $this->createWebUrl('orders', array('direct' => 1)),
  757. 'icon' => 'wi wi-sale-record',
  758. 'type' => 'orders',
  759. ),
  760. ),
  761. ),
  762. 'store_payments' => array(
  763. 'title' => '收入明细',
  764. 'menu' => array(
  765. 'payments' => array (
  766. 'title' => '收入明细',
  767. 'url' => $this->createWebUrl('payments', array('direct' => 1)),
  768. 'icon' => 'wi wi-sale-record',
  769. 'type' => 'payments',
  770. )
  771. )
  772. ),
  773. );
  774. return $menu;
  775. }
  776. public function doWebPay() {
  777. $this->storeIsOpen();
  778. global $_GPC, $_W;
  779. $operate = $_GPC['operate'];
  780. $operates = array ('check_pay_result');
  781. $operate = in_array ($operate, $operates) ? $operate : 'check_pay_result';
  782. if ($operate == 'check_pay_result') {
  783. $orderid = intval($_GPC['orderid']);
  784. $pay_type = pdo_getcolumn('site_store_order', array('id' => $orderid), 'type');
  785. if ($pay_type == STORE_ORDER_FINISH) {
  786. iajax(1);
  787. } else {
  788. iajax(2);
  789. }
  790. }
  791. }
  792. public function doWebPayments() {
  793. global $_W, $_GPC;
  794. $pindex = max(1, $_GPC['page']);
  795. $pagesize = 20;
  796. $store_table = table('store');
  797. $payments_list = $store_table->searchPaymentsOrder();
  798. $pager = pagination(count($payments_list), $pindex, $pagesize);
  799. $payments_list = array_slice($payments_list, ($pindex - 1) * $pagesize, $pagesize);
  800. include $this->template ('goodspayments');
  801. }
  802. }