store.mod.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. function store_goods_type_info($group = '') {
  8. $data = array(
  9. STORE_TYPE_MODULE => array('title' => '公众号应用', 'type' => STORE_TYPE_MODULE, 'sign' => ACCOUNT_TYPE_SIGN, 'group' => 'module'),
  10. STORE_TYPE_WXAPP_MODULE => array('title' => '微信小程序应用', 'type' => STORE_TYPE_WXAPP_MODULE, 'sign' => WXAPP_TYPE_SIGN, 'group' => 'module'),
  11. STORE_TYPE_WEBAPP_MODULE => array('title' => 'PC应用', 'type' => STORE_TYPE_WEBAPP_MODULE, 'sign' => WEBAPP_TYPE_SIGN, 'group' => 'module'),
  12. STORE_TYPE_PHONEAPP_MODULE => array('title' => 'APP应用', 'type' => STORE_TYPE_PHONEAPP_MODULE, 'sign' => PHONEAPP_TYPE_SIGN, 'group' => 'module'),
  13. STORE_TYPE_XZAPP_MODULE => array('title' => '熊掌号应用', 'type' => STORE_TYPE_XZAPP_MODULE, 'sign' => XZAPP_TYPE_SIGN, 'group' => 'module'),
  14. STORE_TYPE_ALIAPP_MODULE => array('title' => '支付宝小程序应用', 'type' => STORE_TYPE_ALIAPP_MODULE, 'sign' => ALIAPP_TYPE_SIGN, 'group' => 'module'),
  15. STORE_TYPE_BAIDUAPP_MODULE => array('title' => '百度小程序应用', 'type' => STORE_TYPE_BAIDUAPP_MODULE, 'sign' => BAIDUAPP_TYPE_SIGN, 'group' => 'module'),
  16. STORE_TYPE_TOUTIAOAPP_MODULE => array('title' => '头条小程序应用', 'type' => STORE_TYPE_TOUTIAOAPP_MODULE, 'sign' => TOUTIAOAPP_TYPE_SIGN, 'group' => 'module'),
  17. STORE_TYPE_API => array('title' => '应用访问流量API', 'type' => STORE_TYPE_API, 'group' => ''),
  18. STORE_TYPE_PACKAGE => array('title' => '应用权限组', 'type' => STORE_TYPE_PACKAGE, 'group' => ''),
  19. STORE_TYPE_USER_PACKAGE => array('title' => '用户权限组', 'type' => STORE_TYPE_USER_PACKAGE, 'group' => ''),
  20. STORE_TYPE_ACCOUNT_PACKAGE => array('title' => '账号权限组', 'type' => STORE_TYPE_ACCOUNT_PACKAGE, 'group' => ''),
  21. STORE_TYPE_ACCOUNT => array('title' => '公众号平台', 'type' => STORE_TYPE_ACCOUNT, 'group' => 'account_num'),
  22. STORE_TYPE_WXAPP => array('title' => '微信小程序平台', 'type' => STORE_TYPE_WXAPP, 'group' => 'account_num'),
  23. STORE_TYPE_WEBAPP => array('title' => 'PC平台', 'type' => STORE_TYPE_WEBAPP, 'group' => 'account_num'),
  24. STORE_TYPE_PHONEAPP => array('title' => 'APP平台', 'type' => STORE_TYPE_PHONEAPP, 'group' => 'account_num'),
  25. STORE_TYPE_XZAPP => array('title' => '熊掌号平台', 'type' => STORE_TYPE_XZAPP, 'group' => 'account_num'),
  26. STORE_TYPE_ALIAPP => array('title' => '支付宝小程序平台', 'type' => STORE_TYPE_ALIAPP, 'group' => 'account_num'),
  27. STORE_TYPE_BAIDUAPP => array('title' => '百度小程序平台', 'type' => STORE_TYPE_BAIDUAPP, 'group' => 'account_num'),
  28. STORE_TYPE_TOUTIAOAPP => array('title' => '头条小程序平台', 'type' => STORE_TYPE_TOUTIAOAPP, 'group' => 'account_num'),
  29. STORE_TYPE_ACCOUNT_RENEW => array('title' => '公众号', 'type' => STORE_TYPE_ACCOUNT_RENEW, 'group' => 'renew'),
  30. STORE_TYPE_WXAPP_RENEW => array('title' => '微信小程序', 'type' => STORE_TYPE_WXAPP_RENEW, 'group' => 'renew'),
  31. STORE_TYPE_WEBAPP_RENEW => array('title' => 'PC', 'type' => STORE_TYPE_WEBAPP_RENEW, 'group' => 'renew'),
  32. STORE_TYPE_PHONEAPP_RENEW => array('title' => 'APP', 'type' => STORE_TYPE_PHONEAPP_RENEW, 'group' => 'renew'),
  33. STORE_TYPE_XZAPP_RENEW => array('title' => '熊掌号', 'type' => STORE_TYPE_XZAPP_RENEW, 'group' => 'renew'),
  34. STORE_TYPE_ALIAPP_RENEW => array('title' => '支付宝小程序', 'type' => STORE_TYPE_ALIAPP_RENEW, 'group' => 'renew'),
  35. STORE_TYPE_BAIDUAPP_RENEW => array('title' => '百度小程序', 'type' => STORE_TYPE_BAIDUAPP_RENEW, 'group' => 'renew'),
  36. STORE_TYPE_TOUTIAOAPP_RENEW => array('title' => '头条小程序', 'type' => STORE_TYPE_TOUTIAOAPP_RENEW, 'group' => 'renew'),
  37. );
  38. if (!empty($group)) {
  39. foreach ($data as $k => $item) {
  40. if ($item['group'] != $group) {
  41. unset($data[$k]);
  42. }
  43. }
  44. } else {
  45. foreach ($data as $k => $item) {
  46. if (!empty($item['group'])) {
  47. $data[$item['group']][$item['type']] = $item;
  48. }
  49. }
  50. }
  51. return $data;
  52. }
  53. function store_goods_info($id) {
  54. $result = array();
  55. $id = intval($id);
  56. if (empty($id)) {
  57. return $result;
  58. }
  59. $result = table('store')->goodsInfo($id);
  60. if (!empty($result[$id])) {
  61. $result = $result[$id];
  62. }
  63. return $result;
  64. }
  65. function store_goods_changestatus($id) {
  66. $result = false;
  67. $id = intval($id);
  68. if (empty($id)) {
  69. return $result;
  70. }
  71. $if_exist = pdo_get('site_store_goods', array('id' => $id));
  72. if (!empty($if_exist)) {
  73. $status = $if_exist['status'] == 1 ? 0 : 1;
  74. $data = array('status' => $status);
  75. $result = pdo_update('site_store_goods', $data, array('id' => $id));
  76. }
  77. return $result;
  78. }
  79. function store_goods_delete($id) {
  80. $result = false;
  81. $id = intval($id);
  82. if (empty($id)) {
  83. return $result;
  84. }
  85. $result = pdo_update('site_store_goods', array('status' => 2), array('id' => $id));
  86. return $result;
  87. }
  88. function update_wish_goods_info($update_data, $module_name) {
  89. if (!empty($update_data['title'])) {
  90. $data['title'] = $update_data['title'];
  91. $data['title_initial'] = $update_data['title_initial'];
  92. }
  93. if (!empty($update_data['logo'])) {
  94. $data['logo'] = $update_data['logo'];
  95. }
  96. $store_goods_exists = pdo_get('site_store_goods', array('module' => $module_name));
  97. $store_goods_cloud_exists = pdo_get('site_store_goods_cloud', array('name' => $module_name));
  98. $module_exists = pdo_get('modules', array('name' => $module_name));
  99. if ($store_goods_exists) {
  100. pdo_update('site_store_goods', $data, array('module' => $module_name));
  101. }
  102. if ($module_exists) {
  103. pdo_update('modules', $data, array('name' => $module_name));
  104. }
  105. if ($store_goods_cloud_exists) {
  106. unset($data['title_initial']);
  107. pdo_update('site_store_goods_cloud', $data, array('name' => $module_name));
  108. }
  109. cache_build_module_info($module_name);
  110. return true;
  111. }
  112. function store_goods_post($data) {
  113. $result = false;
  114. if (empty($data)) {
  115. return $result;
  116. }
  117. $post = array();
  118. if (!empty($data['title'])) {
  119. $post['title'] = trim($data['title']);
  120. }
  121. if (is_numeric($data['price'])) {
  122. $post['price'] = $data['price'];
  123. }
  124. $post['slide'] = $data['slide'];
  125. if (!empty($data['status'])) {
  126. $post['status'] = 1;
  127. }
  128. if (!empty($data['ability']) || !empty($data['synopsis'])) {
  129. $post['synopsis'] = empty($data['ability']) ? trim($data['synopsis']) : trim($data['ability']);
  130. }
  131. if (!empty($data['description'])) {
  132. $post['description'] = trim($data['description']);
  133. }
  134. if (!empty($data['api_num'])) {
  135. $post['api_num'] = intval($data['api_num']);
  136. }
  137. if (!empty($data['unit'])) {
  138. $post['unit'] = $data['unit'];
  139. } else {
  140. if ($data['type'] != STORE_TYPE_API) {
  141. $post['unit'] = 'month';
  142. }
  143. }
  144. $post['account_num'] = $data['account_num'];
  145. $post['wxapp_num'] = $data['wxapp_num'];
  146. $post['platform_num'] = $data['platform_num'] == 0 ? 1 : $data['platform_num'];
  147. $post['module_group'] = $data['module_group'];
  148. $post['user_group'] = $data['user_group'];
  149. $post['account_group'] = $data['account_group'];
  150. $post['user_group_price'] = $data['user_group_price'];
  151. if ($data['type'] == STORE_TYPE_ACCOUNT_RENEW) {
  152. $post['account_num'] = $data['account_num'] == 0 ? 1 : $data['account_num'];
  153. }
  154. if ($data['type'] == STORE_TYPE_WXAPP_RENEW) {
  155. $post['wxapp_num'] = $data['wxapp_num'] == 0 ? 1 : $data['wxapp_num'];
  156. }
  157. if (!empty($data['id'])) {
  158. $result = pdo_update('site_store_goods', $post, array('id' => $data['id']));
  159. if (!empty($data['module'])) {
  160. $update_data = array('title' => $post['title'], 'title_initial' => get_first_pinyin($post['title']), 'logo' => $data['logo']);
  161. $result = update_wish_goods_info($update_data, $data['module']);
  162. }
  163. } else {
  164. $post['type'] = $data['type'];
  165. $post['createtime'] = TIMESTAMP;
  166. $post['title_initial'] = get_first_pinyin($data['title']);
  167. if (empty($post['unit'])) {
  168. $post['unit'] = 'month';
  169. }
  170. if ($data['type'] == STORE_TYPE_API) {
  171. $post['unit'] = 'ten_thousand';
  172. }
  173. $post['module'] = trim($data['module']);
  174. $result = pdo_insert('site_store_goods', $post);
  175. }
  176. return $result;
  177. }
  178. function store_order_info($id) {
  179. $result = array();
  180. $id = intval($id);
  181. if (empty($id)) {
  182. return $result;
  183. }
  184. $store_table = table('store');
  185. $result = $store_table->searchOrderInfo($id);
  186. if (!empty($result[$id])) {
  187. $result = $result[$id];
  188. }
  189. return $result;
  190. }
  191. function store_order_change_price($id, $price) {
  192. global $_W;
  193. $result = false;
  194. $id = intval($id);
  195. $price = floatval($price);
  196. $if_exist = store_order_info($id);
  197. if (empty($id) || empty($if_exist)) {
  198. return $result;
  199. }
  200. if (user_is_vice_founder() || empty($_W['isfounder'])) {
  201. return $result;
  202. }
  203. pdo_update('core_paylog', array('card_fee' => $price), array('module' => 'store', 'tid' => $id));
  204. $result = pdo_update('site_store_order', array('amount' => $price, 'changeprice' => 1), array('id' => $id));
  205. return $result;
  206. }
  207. function store_order_delete($id) {
  208. $result = false;
  209. $id = intval($id);
  210. if (empty($id)) {
  211. return $result;
  212. }
  213. $result = pdo_update('site_store_order', array('type' => STORE_ORDER_DELETE), array('id' => $id));
  214. return $result;
  215. }
  216. function store_add_cash_order($orderid) {
  217. global $_W;
  218. $store_setting = $_W['setting']['store'];
  219. if (empty($store_setting['cash_status']) || empty($store_setting['cash_ratio'])) {
  220. return error(1, '未开启分销, 或者提成比例为0');
  221. }
  222. $order = store_order_info($orderid);
  223. if (empty($order)) {
  224. return error(1, '订单不存在');
  225. }
  226. if ($order['type'] != STORE_ORDER_FINISH) {
  227. return error(1, '订单未支付');
  228. }
  229. if ($order['amount'] <= 0) {
  230. return error(1, '订单金额为0');
  231. }
  232. $order_cash = pdo_get('site_store_cash_order', array('order_id' => $order['id']));
  233. if (!empty($order_cash)) {
  234. return error(1, '分销订单已存在');
  235. }
  236. $user_founder = table('users_founder_own_users')->getFounderByUid($order['buyerid']);
  237. if (empty($user_founder['founder_uid'])) {
  238. return error(1, '上级用户非副创始人');
  239. }
  240. pdo_insert('site_store_cash_order', array(
  241. 'number' => date('YmdHis') . random(6, 1),
  242. 'founder_uid' => $user_founder['founder_uid'],
  243. 'order_id' => $order['id'],
  244. 'goods_id' => $order['goodsid'],
  245. 'order_amount' => $order['amount'],
  246. 'create_time' => TIMESTAMP,
  247. 'status' => 1,
  248. ));
  249. if (pdo_insertid()) {
  250. return true;
  251. } else {
  252. return error(1, '写入数据失败');
  253. }
  254. }
  255. function store_get_cash_orders($condition = array(), $page = 1, $psize = 15) {
  256. global $_W;
  257. $cash_orders = pdo_getall('site_store_cash_order', $condition, array(), '', 'id DESC', ($page - 1) * $psize . ',' . $psize);
  258. if (empty($cash_orders)) {
  259. return array('list' => array(), 'total' => 0);
  260. }
  261. $total = pdo_getcolumn('site_store_cash_order', $condition, 'count(*)');
  262. $goods_ids = $order_ids = array();
  263. if (empty($_W['setting']['store']['cash_status']) || empty($_W['setting']['store']['cash_ratio'])) {
  264. $cash_ratio = 0;
  265. } else {
  266. $cash_ratio = $_W['setting']['store']['cash_ratio'];
  267. }
  268. foreach ($cash_orders as $k => $order) {
  269. $goods_ids[] = $order['goods_id'];
  270. $order_ids[] = $order['order_id'];
  271. $cash_orders[$k]['cash_amount'] = sprintf('%.2f', $order['order_amount'] * $cash_ratio / 100);
  272. }
  273. $store_table = table('store');
  274. $goods = $store_table->goodsInfo($goods_ids);
  275. $orders = $store_table->searchOrderInfo($order_ids);
  276. foreach ($cash_orders as $k => $order) {
  277. $cash_orders[$k]['order'] = empty($orders[$order['order_id']]) ? array() : $orders[$order['order_id']];
  278. if (empty($goods[$order['goods_id']])) {
  279. $cash_orders[$k]['goods'] = array();
  280. } else {
  281. if (in_array($goods[$order['goods_id']]['type'], array(STORE_TYPE_MODULE, STORE_TYPE_WXAPP_MODULE))) {
  282. $cash_orders[$k]['goods'] = module_fetch($goods[$order['goods_id']]['module']);
  283. $cash_orders[$k]['goods']['type'] = $goods[$order['goods_id']]['type'];
  284. } else {
  285. $cash_orders[$k]['goods'] = $goods[$order['goods_id']];
  286. }
  287. }
  288. }
  289. return array(
  290. 'list' => $cash_orders,
  291. 'total' => $total
  292. );
  293. }
  294. function store_get_founder_can_cash_amount($founder_id, $has_refuse = false) {
  295. global $_W;
  296. $store_setting = $_W['setting']['store'];
  297. if (empty($store_setting['cash_status']) || empty($store_setting['cash_ratio'])) {
  298. return 0;
  299. }
  300. $status = empty($has_refuse) ? 1 : array(1, 3);
  301. $can_cash_amount = pdo_getcolumn('site_store_cash_order', array('founder_uid' => $founder_id, 'status' => $status), 'sum(order_amount)');
  302. return sprintf('%.2f', floatval($can_cash_amount) * $store_setting['cash_ratio'] / 100);
  303. }
  304. function store_add_cash_log($founder_id) {
  305. $can_cash_amount = store_get_founder_can_cash_amount($founder_id);
  306. if ($can_cash_amount <= 0) {
  307. return error(1, '暂无待提现订单');
  308. }
  309. pdo_insert('site_store_cash_log', array(
  310. 'number' => date('YmdHis') . random(6, 1),
  311. 'founder_uid' => $founder_id,
  312. 'amount' => $can_cash_amount,
  313. 'create_time' => TIMESTAMP,
  314. 'status' => 1,
  315. ));
  316. $cash_log_id = pdo_insertid();
  317. if (empty($cash_log_id)) {
  318. return error(1, '操作失败, 请重试');
  319. }
  320. pdo_update(
  321. 'site_store_cash_order',
  322. array('status' => 2, 'cash_log_id' => $cash_log_id),
  323. array('founder_uid' => $founder_id, 'status' => 1)
  324. );
  325. return true;
  326. }
  327. function store_get_cash_logs($condition = array(), $page = 1, $psize = 15) {
  328. $cash_logs = pdo_getall('site_store_cash_log', $condition, array(), '', 'id DESC', ($page - 1) * $psize . ',' . $psize);
  329. if (empty($cash_logs)) {
  330. return array('list' => array(), 'total' => 0);
  331. }
  332. $founder_uids = array();
  333. foreach ($cash_logs as $log) {
  334. $founder_uids[] = $log['founder_uid'];
  335. }
  336. $users_info = table('users')->where('uid', $founder_uids)->getall('uid');
  337. foreach ($cash_logs as $k => $log) {
  338. $cash_logs[$k]['username'] = empty($users_info[$log['founder_uid']]['username']) ? '' : $users_info[$log['founder_uid']]['username'];
  339. }
  340. $total = pdo_getcolumn('site_store_cash_log', $condition, 'count(*)');
  341. return array(
  342. 'list' => $cash_logs,
  343. 'total' => $total
  344. );
  345. }