Inviter.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * 推荐人设置
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Db;
  8. use think\facade\Lang;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. *
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Inviter extends AdminControl
  20. {
  21. public function initialize()
  22. {
  23. parent::initialize(); // TODO: Change the autogenerated stub
  24. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/inviter.lang.php');
  25. }
  26. /**
  27. * 基本设置
  28. */
  29. public function setting()
  30. {
  31. $config_model = model('config');
  32. if (request()->isPost()) {
  33. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_COMMON;
  34. if (!empty($_FILES['inviter_back']['name'])) {
  35. $res = ds_upload_pic(ATTACH_COMMON, 'inviter_back', 'inviter_back.png');
  36. if ($res['code']) {
  37. $file_name = $res['data']['file_name'];
  38. $upload['inviter_back'] = $file_name;
  39. } else {
  40. $this->error($res['msg']);
  41. }
  42. }
  43. if (!empty($upload['inviter_back'])) {
  44. $update_array['inviter_back'] = $upload['inviter_back'];
  45. }
  46. $update_array['inviter_ratio_1'] = floatval(input('post.inviter_ratio_1'));
  47. $update_array['inviter_ratio_2'] = floatval(input('post.inviter_ratio_2'));
  48. $update_array['inviter_ratio_3'] = floatval(input('post.inviter_ratio_3'));
  49. $update_array['inviter_open'] = intval(input('post.inviter_open'));
  50. $update_array['inviter_level'] = intval(input('post.inviter_level'));
  51. $update_array['inviter_show'] = floatval(input('post.inviter_show'));
  52. $update_array['inviter_return'] = floatval(input('post.inviter_return'));
  53. $update_array['inviter_view'] = floatval(input('post.inviter_view'));
  54. $update_array['inviter_condition'] = floatval(input('post.inviter_condition'));
  55. $update_array['inviter_condition_amount'] = floatval(input('post.inviter_condition_amount'));
  56. if ($update_array['inviter_return']) {
  57. if (($update_array['inviter_ratio_1'] * 2 + $update_array['inviter_ratio_2'] + $update_array['inviter_ratio_3']) > 100) {
  58. $this->error(lang('inviter_ratio_error'));
  59. }
  60. } else {
  61. if (($update_array['inviter_ratio_1'] + $update_array['inviter_ratio_2'] + $update_array['inviter_ratio_3']) > 100) {
  62. $this->error(lang('inviter_ratio_error'));
  63. }
  64. }
  65. $result = $config_model->editConfig($update_array);
  66. if ($result) {
  67. $this->log(lang('ds_inviter_set'), 1);
  68. $this->success(lang('ds_common_op_succ'), 'Inviter/setting');
  69. } else {
  70. $this->log(lang('ds_inviter_set'), 0);
  71. }
  72. } else {
  73. $list_setting = rkcache('config', true);
  74. View::assign('list_setting', $list_setting);
  75. $this->setAdminCurItem('index');
  76. return View::fetch('index');
  77. }
  78. }
  79. public function goods()
  80. {
  81. $goods_model = model('goods');
  82. $condition = array();
  83. $condition[] = array('inviter_open', '=', 1);
  84. if ((input('param.goods_name'))) {
  85. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  86. }
  87. $goods_list = $goods_model->getGoodsCommonList($condition, '*', 10);
  88. View::assign('goods_list', $goods_list);
  89. View::assign('show_page', $goods_model->page_info->render());
  90. $this->setAdminCurItem('goods');
  91. return View::fetch('goods');
  92. }
  93. public function order()
  94. {
  95. $search_field_value = input('param.search_field_value');
  96. $search_field_name = input('param.search_field_name');
  97. $condition = array();
  98. if ($search_field_value != '') {
  99. switch ($search_field_name) {
  100. case 'orderinviter_member_name':
  101. $condition[] = array('orderinviter_member_name', '=', $search_field_value);
  102. break;
  103. case 'orderinviter_order_sn':
  104. $condition[] = array('orderinviter_order_sn', '=', $search_field_value);
  105. break;
  106. case 'orderinviter_store_name':
  107. $condition[] = array('orderinviter_store_name', '=', $search_field_value);
  108. break;
  109. }
  110. }
  111. $orderinviter_list = Db::name('orderinviter')->where($condition)->order('orderinviter_addtime desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  112. $order_list = $orderinviter_list->items();
  113. foreach ($order_list as $key => $val) {
  114. $order_list[$key]['orderinviter_valid_text'] = lang('orderinviter_valid_array')[$val['orderinviter_valid']];
  115. }
  116. View::assign('orderinviter_list', $order_list);
  117. View::assign('show_page', $orderinviter_list->render());
  118. View::assign('search_field_name', trim($search_field_name));
  119. View::assign('search_field_value', trim($search_field_value));
  120. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  121. $this->setAdminCurItem('order');
  122. return View::fetch();
  123. }
  124. public function member()
  125. {
  126. $inviter_model = model('inviter');
  127. $search_field_value = input('search_field_value');
  128. $search_field_name = input('search_field_name');
  129. $condition = array();
  130. if ($search_field_value != '') {
  131. switch ($search_field_name) {
  132. case 'member_name':
  133. $condition[] = array('member_name', 'like', '%' . trim($search_field_value) . '%');
  134. break;
  135. case 'member_email':
  136. $condition[] = array('member_email', 'like', '%' . trim($search_field_value) . '%');
  137. break;
  138. case 'member_mobile':
  139. $condition[] = array('member_mobile', 'like', '%' . trim($search_field_value) . '%');
  140. break;
  141. case 'member_truename':
  142. $condition[] = array('member_truename', 'like', '%' . trim($search_field_value) . '%');
  143. break;
  144. }
  145. }
  146. $member_list = $inviter_model->getInviterList($condition, 10, '', 'i.*,m.inviter_id as inviter_parent_id,m.member_id,m.member_ww,m.member_qq,m.member_addtime,m.member_name,m.member_avatar,m.member_email,m.member_mobile,m.member_truename');
  147. $inviterclass_model = model('inviterclass');
  148. foreach ($member_list as $key => $item) {
  149. $member_list[$key]['inviter_parent_name'] = '';
  150. $member_list[$key]['inviter_class'] = '';
  151. $member_list[$key]['inviter_class'] = $inviterclass_model->getInviterclass($item['inviter_total_amount']);
  152. if ($item['inviter_parent_id']) {
  153. $member_list[$key]['inviter_parent_name'] = Db::name('member')->where('member_id=' . $item['inviter_parent_id'])->value('member_name');
  154. }
  155. }
  156. View::assign('member_list', $member_list);
  157. View::assign('show_page', $inviter_model->page_info->render());
  158. View::assign('search_field_name', trim($search_field_name));
  159. View::assign('search_field_value', trim($search_field_value));
  160. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  161. $this->setAdminCurItem('member');
  162. return View::fetch('member');
  163. }
  164. public function memberclass()
  165. {
  166. $memberclass_list = Db::name('inviterclass')->order('inviterclass_amount asc')->select()->toArray();
  167. View::assign('memberclass_list', $memberclass_list);
  168. $this->setAdminCurItem('memberclass');
  169. return View::fetch('memberclass');
  170. }
  171. public function member_adjust()
  172. {
  173. $member_id = intval(input('param.member_id'));
  174. if (!$member_id) {
  175. $this->error(lang('param_error'));
  176. }
  177. $inviter_model = model('inviter');
  178. $inviter_info = $inviter_model->getInviterInfo(array('m.member_id' => $member_id), 'm.member_id,m.inviter_id as inviter_parent_id,i.inviter_1_quantity,i.inviter_2_quantity,i.inviter_3_quantity');
  179. if (!$inviter_info) {
  180. $this->error(lang('inviter_member_empty'));
  181. }
  182. if (request()->isPost()) {
  183. $member_name = trim(input('param.member_name'));
  184. if ($member_name) {
  185. $inviter = $inviter_model->getInviterInfo(array('m.member_name' => $member_name), 'm.member_id');
  186. $inviter_id = $inviter['member_id'];
  187. if (!$inviter_id) {
  188. $this->error(lang('inviter_member_empty'));
  189. }
  190. //上级不能是自己
  191. if ($inviter_id == $member_id) {
  192. $this->error(lang('inviter_parent_error'));
  193. }
  194. //上级不能是自己下级中(3级内)的成员
  195. if (Db::name('member')->where('inviter_id=' . $member_id . ' AND member_id=' . $inviter_id)->value('member_id')) {
  196. $this->error(lang('inviter_parent_error2'));
  197. }
  198. $subQuery = Db::name('member')->field('member_id')->where('inviter_id=' . $member_id)->buildSql();
  199. if (Db::name('member')->where('member_id=' . $inviter_id . ' AND inviter_id IN' . $subQuery)->value('member_id')) {
  200. $this->error(lang('inviter_parent_error2'));
  201. }
  202. $subQuery = Db::name('member')->field('member_id')->where('inviter_id IN' . $subQuery)->buildSql();
  203. if (Db::name('member')->where('member_id=' . $inviter_id . ' AND inviter_id IN' . $subQuery)->value('member_id')) {
  204. $this->error(lang('inviter_parent_error2'));
  205. }
  206. } else {
  207. $inviter_id = 0;
  208. }
  209. Db::name('member')->where('member_id=' . $member_id)->update(array(
  210. 'inviter_id' => $inviter_id
  211. ));
  212. //给旧的父级减去下线成员
  213. if ($inviter_info['inviter_parent_id']) {
  214. Db::name('inviter')->where('inviter_id=' . $inviter_info['inviter_parent_id'] . ' AND inviter_1_quantity>=1')->dec('inviter_1_quantity')->update();
  215. Db::name('inviter')->where('inviter_id=' . $inviter_info['inviter_parent_id'] . ' AND inviter_2_quantity>=' . $inviter_info['inviter_1_quantity'])->dec('inviter_2_quantity', $inviter_info['inviter_1_quantity'])->update();
  216. Db::name('inviter')->where('inviter_id=' . $inviter_info['inviter_parent_id'] . ' AND inviter_3_quantity>=' . $inviter_info['inviter_2_quantity'])->dec('inviter_3_quantity', $inviter_info['inviter_2_quantity'])->update();
  217. //父级的父级
  218. $temp = $inviter_model->getInviterInfo(array('m.member_id' => $inviter_info['inviter_parent_id']), 'm.inviter_id as inviter_parent_id');
  219. if ($temp['inviter_parent_id']) {
  220. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'] . ' AND inviter_2_quantity>=1')->dec('inviter_2_quantity')->update();
  221. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'] . ' AND inviter_3_quantity>=' . $inviter_info['inviter_1_quantity'])->dec('inviter_3_quantity', $inviter_info['inviter_1_quantity'])->update();
  222. //父级的父级的父级
  223. $temp = $inviter_model->getInviterInfo(array('m.member_id' => $temp['inviter_parent_id']), 'm.inviter_id as inviter_parent_id');
  224. if ($temp['inviter_parent_id']) {
  225. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'] . ' AND inviter_3_quantity>=1')->dec('inviter_3_quantity')->update();
  226. }
  227. }
  228. }
  229. //给新的父级增加下线成员
  230. if ($inviter_id) {
  231. Db::name('inviter')->where('inviter_id=' . $inviter_id)->inc('inviter_1_quantity')->update();
  232. Db::name('inviter')->where('inviter_id=' . $inviter_id)->inc('inviter_2_quantity', $inviter_info['inviter_1_quantity'])->update();
  233. Db::name('inviter')->where('inviter_id=' . $inviter_id)->inc('inviter_3_quantity', $inviter_info['inviter_2_quantity'])->update();
  234. //父级的父级
  235. $temp = $inviter_model->getInviterInfo(array('m.member_id' => $inviter_id), 'm.inviter_id as inviter_parent_id');
  236. if ($temp['inviter_parent_id']) {
  237. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'])->inc('inviter_2_quantity')->update();
  238. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'])->inc('inviter_3_quantity', $inviter_info['inviter_1_quantity'])->update();
  239. //父级的父级的父级
  240. $temp = $inviter_model->getInviterInfo(array('m.member_id' => $temp['inviter_parent_id']), 'm.inviter_id as inviter_parent_id');
  241. if ($temp['inviter_parent_id']) {
  242. Db::name('inviter')->where('inviter_id=' . $temp['inviter_parent_id'])->inc('inviter_3_quantity')->update();
  243. }
  244. }
  245. }
  246. $this->log(lang('adjust_superior') . '[ID:' . $member_id . ']', 1);
  247. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  248. } else {
  249. return View::fetch();
  250. }
  251. }
  252. /**
  253. * 添加标签
  254. */
  255. public function memberclass_add()
  256. {
  257. if (request()->isPost()) {
  258. $data = array(
  259. 'inviterclass_name' => trim(input('post.inviterclass_name')),
  260. 'inviterclass_amount' => abs(floatval(input('post.inviterclass_amount'))),
  261. );
  262. if (!$data['inviterclass_name']) {
  263. $this->error(lang('param_error'));
  264. }
  265. Db::name('inviterclass')->insert($data);
  266. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  267. } else {
  268. return View::fetch('memberclass_form');
  269. }
  270. }
  271. /**
  272. * 编辑标签
  273. */
  274. public function memberclass_edit()
  275. {
  276. $id = intval(input('param.id'));
  277. if (!$id) {
  278. $this->error(lang('param_error'));
  279. }
  280. $inviterclass_info = Db::name('inviterclass')->where('inviterclass_id', $id)->find();
  281. if (!$inviterclass_info) {
  282. $this->error(lang('inviterclass_empty'));
  283. }
  284. // 实例化模型
  285. if (request()->isPost()) {
  286. $data = array(
  287. 'inviterclass_name' => trim(input('post.inviterclass_name')),
  288. 'inviterclass_amount' => abs(floatval(input('post.inviterclass_amount'))),
  289. );
  290. if (!$data['inviterclass_name']) {
  291. $this->error(lang('param_error'));
  292. }
  293. Db::name('inviterclass')->where('inviterclass_id', $id)->update($data);
  294. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  295. } else {
  296. View::assign('inviterclass_info', $inviterclass_info);
  297. return View::fetch('memberclass_form');
  298. }
  299. }
  300. /**
  301. * 删除标签
  302. */
  303. public function memberclass_del()
  304. {
  305. $inviterclass_id = input('param.id');
  306. $inviterclass_id_array = ds_delete_param($inviterclass_id);
  307. if ($inviterclass_id_array == FALSE) {
  308. ds_json_encode('10001', lang('param_error'));
  309. }
  310. $result = Db::name('inviterclass')->where(array(array('inviterclass_id', 'in', $inviterclass_id_array)))->delete();
  311. if ($result) {
  312. ds_json_encode('10000', lang('ds_common_del_succ'));
  313. } else {
  314. ds_json_encode('10001', lang('ds_common_del_fail'));
  315. }
  316. }
  317. public function memberstate()
  318. {
  319. $member_id = input('param.member_id');
  320. $member_id_array = ds_delete_param($member_id);
  321. $member_state = input('param.member_state');
  322. if (!$member_id_array || !in_array($member_state, array(1, 2))) {
  323. ds_json_encode('10001', lang('param_error'));
  324. }
  325. $inviter_model = model('inviter');
  326. $inviter_info = $inviter_model->getInviterInfo(array(array('i.inviter_id', 'in', $member_id_array)));
  327. if (!$inviter_info) {
  328. ds_json_encode('10001', lang('inviter_member_empty'));
  329. }
  330. $inviter_model->editInviter(array(array('inviter_id', 'in', $member_id_array)), array('inviter_state' => $member_state));
  331. $this->log(($member_state == 1 ? lang('ds_enable') : lang('ds_disable')) . '[ID:' . implode(',', $member_id_array) . ']', 1);
  332. ds_json_encode('10000', ($member_state == 1 ? lang('ds_enable') : lang('ds_disable')) . lang('ds_succ'));
  333. }
  334. public function memberinfo()
  335. {
  336. $member_id = input('param.member_id');
  337. if (!$member_id) {
  338. ds_json_encode('10001', lang('param_error'));
  339. }
  340. $inviter_model = model('inviter');
  341. $inviter_info = $inviter_model->getInviterInfo(array('i.inviter_id' => $member_id), 'i.*,m.inviter_id as inviter_parent_id,m.member_id,m.member_ww,m.member_qq,m.member_addtime,m.member_name,m.member_avatar,m.member_email,m.member_mobile,m.member_truename');
  342. if (!$inviter_info) {
  343. ds_json_encode('10001', lang('inviter_member_empty'));
  344. }
  345. $inviter_info['inviter_parent_name'] = '';
  346. if ($inviter_info['inviter_parent_id']) {
  347. $inviter_info['inviter_parent_name'] = Db::name('member')->where('member_id=' . $inviter_info['inviter_parent_id'])->value('member_name');
  348. }
  349. $inviterclass_model = model('inviterclass');
  350. $inviter_info['inviter_class'] = $inviterclass_model->getInviterclass($inviter_info['inviter_total_amount']);
  351. View::assign('inviter_info', $inviter_info);
  352. $this->setAdminCurItem('member');
  353. return View::fetch('memberinfo');
  354. }
  355. public function memberlist()
  356. {
  357. $member_id = intval(input('param.member_id'));
  358. $type = input('param.type');
  359. if (!$member_id || !in_array($type, array(1, 2, 3))) {
  360. return;
  361. }
  362. $inviter_model = model('inviter');
  363. if ($type == 1) {
  364. $res = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('i.*,m.inviter_id as inviter_parent_id,m.member_id,m.member_ww,m.member_qq,m.member_addtime,m.member_name,m.member_avatar,m.member_email,m.member_mobile,m.member_truename')->where('m.inviter_id=' . $member_id)->order('inviter_applytime desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  365. $page_info = $res;
  366. $member_list = $res->items();
  367. } elseif ($type == 2) {
  368. $subQuery = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('m.member_id')->where('m.inviter_id=' . $member_id)->order('inviter_applytime desc')->buildSql();
  369. $res = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('i.*,m.inviter_id as inviter_parent_id,m.member_id,m.member_ww,m.member_qq,m.member_addtime,m.member_name,m.member_avatar,m.member_email,m.member_mobile,m.member_truename')->where('m.inviter_id IN' . $subQuery)->order('inviter_applytime desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  370. $page_info = $res;
  371. $member_list = $res->items();
  372. } elseif ($type == 3) {
  373. $subQuery = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('m.member_id')->where('m.inviter_id=' . $member_id)->order('inviter_applytime desc')->buildSql();
  374. $subQuery = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('m.member_id')->where('m.inviter_id IN' . $subQuery)->order('inviter_applytime desc')->buildSql();
  375. $res = Db::name('member')->alias('m')->join('inviter i', 'i.inviter_id=m.member_id', 'LEFT')->field('i.*,m.inviter_id as inviter_parent_id,m.member_id,m.member_ww,m.member_qq,m.member_addtime,m.member_name,m.member_avatar,m.member_email,m.member_mobile,m.member_truename')->where('m.inviter_id IN' . $subQuery)->order('inviter_applytime desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  376. $page_info = $res;
  377. $member_list = $res->items();
  378. }
  379. $inviterclass_model = model('inviterclass');
  380. foreach ($member_list as $key => $item) {
  381. $member_list[$key]['inviter_parent_name'] = '';
  382. $member_list[$key]['inviter_class'] = '';
  383. $member_list[$key]['inviter_class'] = $inviterclass_model->getInviterclass(floatval($item['inviter_total_amount']));
  384. if ($item['inviter_parent_id']) {
  385. $member_list[$key]['inviter_parent_name'] = Db::name('member')->where('member_id=' . $item['inviter_parent_id'])->value('member_name');
  386. }
  387. }
  388. View::assign('member_list', $member_list);
  389. View::assign('show_page', $page_info->render());
  390. echo View::fetch('memberlist');
  391. }
  392. /**
  393. * 获取卖家栏目列表,针对控制器下的栏目
  394. */
  395. protected function getAdminItemList()
  396. {
  397. $menu_array = array(
  398. array(
  399. 'name' => 'index',
  400. 'text' => lang('inviter_setting'),
  401. 'url' => (string)url('Inviter/setting')
  402. ),
  403. array(
  404. 'name' => 'goods',
  405. 'text' => lang('inviter_goods'),
  406. 'url' => (string)url('Inviter/goods')
  407. ),
  408. array(
  409. 'name' => 'member',
  410. 'text' => lang('inviter_member'),
  411. 'url' => (string)url('Inviter/member')
  412. ),
  413. array(
  414. 'name' => 'memberclass',
  415. 'text' => lang('inviter_memberclass'),
  416. 'url' => (string)url('Inviter/memberclass')
  417. ),
  418. array(
  419. 'name' => 'order',
  420. 'text' => lang('inviter_order'),
  421. 'url' => (string)url('Inviter/order')
  422. ),
  423. );
  424. return $menu_array;
  425. }
  426. }