store.mod.php 14 KB

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