site.php 31 KB

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