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