User.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\user;
  8. use app\admin\controller\AuthController;
  9. use app\models\user\UserSpread;
  10. use crmeb\repositories\UserRepository;
  11. use crmeb\traits\CurdControllerTrait;
  12. use think\facade\Route as Url;
  13. use crmeb\basic\BaseModel;
  14. use app\models\user\UserLevel as Level;
  15. use app\admin\model\order\StoreOrder;
  16. use app\admin\model\wechat\WechatMessage;
  17. use app\admin\model\store\{StoreVisit, StoreCouponUser};
  18. use app\admin\model\system\{SystemUserLevel, SystemUserTask};
  19. use crmeb\services\{FormBuilder as Form, UtilService as Util, JsonService as Json};
  20. use app\admin\model\user\{User as UserModel, UserBill as UserBillAdmin, UserLevel, UserGroup, UserTaskFinish};
  21. /**
  22. * 用户管理控制器
  23. * Class User
  24. * @package app\admin\controller\user
  25. */
  26. class User extends AuthController
  27. {
  28. use CurdControllerTrait;
  29. /**
  30. * 显示资源列表
  31. *
  32. * @return \think\Response
  33. */
  34. public function index()
  35. {
  36. $level = SystemUserLevel::where('is_del', 0)->where('is_show', 1)->order('grade asc')->field(['id', 'name'])->select();
  37. $group = UserGroup::select();
  38. $this->assign(compact('group'));
  39. $this->assign(compact('level'));
  40. $this->assign('count_user', UserModel::getcount());
  41. return $this->fetch();
  42. }
  43. /**
  44. * 设置分组
  45. * @param int $uid
  46. */
  47. public function set_group($uid = 0)
  48. {
  49. if (!$uid) return $this->failed('缺少参数');
  50. $userGroup = UserGroup::select();
  51. $field[] = Form::select('group_id', '会员分组')->setOptions(function () use ($userGroup) {
  52. $menus = [];
  53. foreach ($userGroup as $menu) {
  54. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  55. }
  56. return $menus;
  57. })->filterable(1);
  58. $form = Form::make_post_form('设置分组', $field, Url::buildUrl('save_set_group', ['uid' => $uid]), 2);
  59. $this->assign(compact('form'));
  60. return $this->fetch('public/form-builder');
  61. }
  62. public function save_set_group($uid = 0)
  63. {
  64. if (!$uid) return Json::fail('缺少参数');
  65. list($group_id) = Util::postMore([
  66. ['group_id', 0],
  67. ], $this->request, true);
  68. $uids = explode(',', $uid);
  69. $res = UserModel::whereIn('uid', $uids)->update(['group_id' => $group_id]);
  70. if ($res) {
  71. return Json::successful('设置成功');
  72. } else {
  73. return Json::successful('设置失败');
  74. }
  75. }
  76. /**
  77. * 赠送会员等级
  78. * @param int $uid
  79. * @return string|void
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function give_level($uid = 0)
  85. {
  86. if (!$uid) return $this->failed('缺少参数');
  87. $level = Level::getUserLevel($uid);
  88. //获取当前会员等级
  89. if ($level === false)
  90. $grade = 0;
  91. else
  92. $grade = Level::getUserLevelInfo($level, 'grade');
  93. //查询高于当前会员的所有会员等级
  94. $systemLevelList = SystemUserLevel::where('grade', '>', $grade)->where(['is_show' => 1, 'is_del' => 0])->field(['name', 'id'])->select();
  95. $field[] = Form::select('level_id', '会员等级')->setOptions(function () use ($systemLevelList) {
  96. $menus = [];
  97. foreach ($systemLevelList as $menu) {
  98. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  99. }
  100. return $menus;
  101. })->filterable(1);
  102. $form = Form::make_post_form('赠送会员', $field, Url::buildUrl('save_give_level', ['uid' => $uid]), 2);
  103. $this->assign(compact('form'));
  104. return $this->fetch('public/form-builder');
  105. }
  106. public function edit_other($uid)
  107. {
  108. if (!$uid) return $this->failed('数据不存在');
  109. $user = UserModel::get($uid);
  110. if (!$user) return Json::fail('数据不存在!');
  111. $f = array();
  112. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  113. $f[] = Form::number('money', '余额')->min(0);
  114. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  115. $f[] = Form::number('integration', '积分')->min(0);
  116. $form = Form::make_post_form('修改其他', $f, Url::buildUrl('update_other', array('uid' => $uid)));
  117. $this->assign(compact('form'));
  118. return $this->fetch('public/form-builder');
  119. }
  120. public function update_other($uid = 0)
  121. {
  122. $data = Util::postMore([
  123. ['money_status', 0],
  124. ['money', 0],
  125. ['integration_status', 0],
  126. ['integration', 0],
  127. ], $this->request);
  128. if (!$uid) return $this->failed('数据不存在');
  129. $user = UserModel::get($uid);
  130. if (!$user) return Json::fail('数据不存在!');
  131. BaseModel::beginTrans();
  132. $res1 = false;
  133. $res2 = false;
  134. $edit = array();
  135. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  136. if ($data['money_status'] == 1) {//增加
  137. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  138. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  139. try {
  140. UserRepository::adminAddMoney($user, $data['money']);
  141. } catch (\Exception $e) {
  142. BaseModel::rollbackTrans();
  143. return Json::fail($e->getMessage());
  144. }
  145. } else if ($data['money_status'] == 2) {//减少
  146. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  147. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  148. try {
  149. UserRepository::adminSubMoney($user, $data['money']);
  150. } catch (\Exception $e) {
  151. BaseModel::rollbackTrans();
  152. return Json::fail($e->getMessage());
  153. }
  154. }
  155. } else {
  156. $res1 = true;
  157. }
  158. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  159. if ($data['integration_status'] == 1) {//增加
  160. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  161. $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  162. try {
  163. UserRepository::adminAddIntegral($user, $data['integration']);
  164. } catch (\Exception $e) {
  165. BaseModel::rollbackTrans();
  166. return Json::fail($e->getMessage());
  167. }
  168. } else if ($data['integration_status'] == 2) {//减少
  169. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  170. $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  171. try {
  172. UserRepository::adminSubIntegral($user, $data['integration']);
  173. } catch (\Exception $e) {
  174. BaseModel::rollbackTrans();
  175. return Json::fail($e->getMessage());
  176. }
  177. }
  178. } else {
  179. $res2 = true;
  180. }
  181. if ($edit) $res3 = UserModel::edit($edit, $uid);
  182. else $res3 = true;
  183. if ($res1 && $res2 && $res3) $res = true;
  184. else $res = false;
  185. BaseModel::checkTrans($res);
  186. if ($res) return Json::successful('修改成功!');
  187. else return Json::fail('修改失败');
  188. }
  189. /*
  190. * 赠送会员等级
  191. * @paran int $uid
  192. * @return json
  193. * */
  194. public function save_give_level($uid = 0)
  195. {
  196. if (!$uid) return Json::fail('缺少参数');
  197. list($level_id) = Util::postMore([
  198. ['level_id', 0],
  199. ], $this->request, true);
  200. //查询当前选择的会员等级
  201. $systemLevel = SystemUserLevel::where(['is_show' => 1, 'is_del' => 0, 'id' => $level_id])->find();
  202. if (!$systemLevel) return Json::fail('您选择赠送的会员等级不存在!');
  203. //检查是否拥有此会员等级
  204. $level = UserLevel::where(['uid' => $uid, 'level_id' => $level_id, 'is_del' => 0])->field('valid_time,is_forever')->find();
  205. if ($level) if (!$level['is_forever'] && time() < $level['valid_time']) return Json::fail('此用户已有该会员等级,无法再次赠送');
  206. //设置会员过期时间
  207. $add_valid_time = (int)$systemLevel->valid_date * 86400;
  208. UserModel::commitTrans();
  209. try {
  210. //保存会员信息
  211. $res = UserLevel::create([
  212. 'is_forever' => $systemLevel->is_forever,
  213. 'status' => 1,
  214. 'is_del' => 0,
  215. 'grade' => $systemLevel->grade,
  216. 'uid' => $uid,
  217. 'add_time' => time(),
  218. 'level_id' => $level_id,
  219. 'discount' => $systemLevel->discount,
  220. 'valid_time' => $systemLevel->discount ? $add_valid_time + time() : 0,
  221. 'mark' => '尊敬的用户【' . UserModel::where('uid', $uid)->value('nickname') . '】在' . date('Y-m-d H:i:s', time()) . '赠送会员等级成为' . $systemLevel['name'] . '会员',
  222. ]);
  223. //提取等级任务并记录完成情况
  224. $levelIds = [$level_id];
  225. $lowGradeLevelIds = SystemUserLevel::where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id');
  226. if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds);
  227. $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id');
  228. $inserValue = [];
  229. foreach ($taskIds as $id) {
  230. $inserValue[] = ['uid' => $uid, 'task_id' => $id, 'status' => 1, 'add_time' => time()];
  231. }
  232. $res = $res && UserModel::where('uid', $uid)->update(['level' => $level_id]);
  233. if ($inserValue) $res && UserTaskFinish::insertAll($inserValue);
  234. if ($res) {
  235. UserModel::commitTrans();
  236. return Json::successful('赠送成功');
  237. } else {
  238. UserModel::rollbackTrans();
  239. return Json::successful('赠送失败');
  240. }
  241. } catch (\Exception $e) {
  242. UserModel::rollbackTrans();
  243. return Json::fail('赠送失败');
  244. }
  245. }
  246. /*
  247. * 清除会员等级
  248. * @param int $uid
  249. * @return json
  250. * */
  251. public function del_level($uid = 0)
  252. {
  253. if (!$uid) return Json::fail('缺少参数');
  254. if (UserLevel::cleanUpLevel($uid))
  255. return Json::successful('清除成功');
  256. else
  257. return Json::fail('清除失败');
  258. }
  259. /**
  260. * 修改user表状态
  261. *
  262. * @return json
  263. */
  264. public function set_status($status = '', $uid = 0, $is_echo = 0)
  265. {
  266. if ($is_echo == 0) {
  267. if ($status == '' || $uid == 0) return Json::fail('参数错误');
  268. UserModel::where(['uid' => $uid])->update(['status' => $status]);
  269. } else {
  270. $uids = Util::postMore([
  271. ['uids', []]
  272. ]);
  273. UserModel::destrSyatus($uids['uids'], $status);
  274. }
  275. return Json::successful($status == 0 ? '禁用成功' : '解禁成功');
  276. }
  277. /**
  278. * 获取user表
  279. *
  280. * @return json
  281. */
  282. public function get_user_list()
  283. {
  284. $where = Util::getMore([
  285. ['page', 1],
  286. ['limit', 20],
  287. ['nickname', ''],
  288. ['status', ''],
  289. ['pay_count', ''],
  290. ['is_promoter', ''],
  291. ['order', ''],
  292. ['data', ''],
  293. ['user_type', ''],
  294. ['country', ''],
  295. ['province', ''],
  296. ['city', ''],
  297. ['user_time_type', ''],
  298. ['user_time', ''],
  299. ['sex', ''],
  300. ['level', ''],
  301. ['group_id', ''],
  302. ]);
  303. return Json::successlayui(UserModel::getUserList($where));
  304. }
  305. /**
  306. * 编辑模板消息
  307. * @param $id
  308. * @return mixed|\think\response\Json|void
  309. */
  310. public function edit($uid)
  311. {
  312. if (!$uid) return $this->failed('数据不存在');
  313. $user = UserModel::get($uid);
  314. if (!$user) return Json::fail('数据不存在!');
  315. $f = array();
  316. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(1);
  317. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  318. $f[] = Form::text('phone', '手机号', $user->getData('phone'));
  319. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : 0);
  320. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  321. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  322. $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  323. $f[] = Form::select('spread_uid', '推荐人', (string)$user->getData('spread_uid'))->options(array_merge([['value' => 0, 'label' => '无']], UserModel::where('uid', '<>', $uid)->field('uid as value,nickname as label')->select()->toArray()))->filterable(true);
  324. $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  325. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('uid' => $uid)), 5);
  326. $this->assign(compact('form'));
  327. return $this->fetch('public/form-builder');
  328. }
  329. public function update($uid)
  330. {
  331. $data = Util::postMore([
  332. ['money_status', 0],
  333. ['is_promoter', 1],
  334. ['real_name', ''],
  335. ['phone', 0],
  336. ['card_id', ''],
  337. ['birthday', ''],
  338. ['mark', ''],
  339. ['money', 0],
  340. ['integration_status', 0],
  341. ['integration', 0],
  342. ['status', 0],
  343. ['spread_uid', 0],
  344. ]);
  345. if (!$uid) return $this->failed('数据不存在');
  346. $user = UserModel::get($uid);
  347. if (!$user) return Json::fail('数据不存在!');
  348. BaseModel::beginTrans();
  349. $res1 = false;
  350. $res2 = false;
  351. $edit = array();
  352. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  353. if ($data['money_status'] == 1) {//增加
  354. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  355. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  356. try {
  357. UserRepository::adminAddMoney($user, $data['money']);
  358. } catch (\Exception $e) {
  359. BaseModel::rollbackTrans();
  360. return Json::fail($e->getMessage());
  361. }
  362. } else if ($data['money_status'] == 2) {//减少
  363. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  364. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  365. try {
  366. UserRepository::adminSubMoney($user, $data['money']);
  367. } catch (\Exception $e) {
  368. BaseModel::rollbackTrans();
  369. return Json::fail($e->getMessage());
  370. }
  371. }
  372. } else {
  373. $res1 = true;
  374. }
  375. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  376. if ($data['integration_status'] == 1) {//增加
  377. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  378. $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  379. try {
  380. UserRepository::adminAddIntegral($user, $data['integration']);
  381. } catch (\Exception $e) {
  382. BaseModel::rollbackTrans();
  383. return Json::fail($e->getMessage());
  384. }
  385. } else if ($data['integration_status'] == 2) {//减少
  386. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  387. $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  388. try {
  389. UserRepository::adminSubIntegral($user, $data['integration']);
  390. } catch (\Exception $e) {
  391. BaseModel::rollbackTrans();
  392. return Json::fail($e->getMessage());
  393. }
  394. }
  395. } else {
  396. $res2 = true;
  397. }
  398. $res_sp = true;
  399. if ($data['spread_uid'] != $user['spread_uid']) {
  400. $res_sp = \app\models\user\User::setSpread($data['spread_uid'], $uid);
  401. }
  402. $edit['status'] = $data['status'];
  403. $edit['real_name'] = $data['real_name'];
  404. $edit['phone'] = $data['phone'];
  405. $edit['card_id'] = $data['card_id'];
  406. $edit['birthday'] = strtotime($data['birthday']);
  407. $edit['mark'] = $data['mark'];
  408. $edit['is_promoter'] = $data['is_promoter'];
  409. if ($edit) $res3 = UserModel::edit($edit, $uid);
  410. else $res3 = true;
  411. if ($res1 && $res2 && $res3 && $res_sp) $res = true;
  412. else $res = false;
  413. BaseModel::checkTrans($res);
  414. if ($res) return Json::successful('修改成功!');
  415. else return Json::fail('修改失败');
  416. }
  417. /**
  418. * 用户图表
  419. * @return mixed
  420. */
  421. public function user_analysis()
  422. {
  423. $where = Util::getMore([
  424. ['nickname', ''],
  425. ['status', ''],
  426. ['is_promoter', ''],
  427. ['date', ''],
  428. ['user_type', ''],
  429. ['export', 0]
  430. ], $this->request);
  431. $user_count = UserModel::consume($where, '', true);
  432. //头部信息
  433. $header = [
  434. [
  435. 'name' => '新增用户',
  436. 'class' => 'fa-line-chart',
  437. 'value' => $user_count,
  438. 'color' => 'red'
  439. ],
  440. [
  441. 'name' => '用户留存',
  442. 'class' => 'fa-area-chart',
  443. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true), $where) . '%',
  444. 'color' => 'lazur'
  445. ],
  446. [
  447. 'name' => '新增用户总消费',
  448. 'class' => 'fa-bar-chart',
  449. 'value' => '¥' . UserModel::consume($where),
  450. 'color' => 'navy'
  451. ],
  452. [
  453. 'name' => '用户活跃度',
  454. 'class' => 'fa-pie-chart',
  455. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true)) . '%',
  456. 'color' => 'yellow'
  457. ],
  458. ];
  459. $name = ['新增用户', '用户消费'];
  460. $dates = $this->get_user_index($where, $name);
  461. $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
  462. //用户浏览分析
  463. $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger']);
  464. $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger']);
  465. $view = array_merge($view, $view_v1);
  466. $view_v2 = [];
  467. foreach ($view as $val) {
  468. $view_v2['color'][] = '#' . rand(100000, 339899);
  469. $view_v2['name'][] = $val['name'];
  470. $view_v2['value'][] = $val['value'];
  471. }
  472. $view = $view_v2;
  473. //消费会员排行用户分析
  474. $user_null = UserModel::getUserSpend($where['date']);
  475. //消费数据
  476. $now_number = UserModel::getUserSpend($where['date'], true);
  477. list($paren_number, $title) = UserModel::getPostNumber($where['date']);
  478. if ($paren_number == 0) {
  479. $rightTitle = [
  480. 'number' => $now_number > 0 ? $now_number : 0,
  481. 'icon' => 'fa-level-up',
  482. 'title' => $title
  483. ];
  484. } else {
  485. $number = (float)bcsub($now_number, $paren_number, 4);
  486. if ($now_number == 0) {
  487. $icon = 'fa-level-down';
  488. } else {
  489. $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
  490. }
  491. $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
  492. }
  493. unset($title, $paren_number, $now_number);
  494. list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '');
  495. if ($paren_user_count == 0) {
  496. $count = $user_count == 0 ? 0 : $user_count;
  497. $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
  498. } else {
  499. $count = (float)bcsub($user_count, $paren_user_count, 4);
  500. $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
  501. }
  502. $leftTitle = [
  503. 'count' => $count,
  504. 'icon' => $icon,
  505. 'title' => $title
  506. ];
  507. unset($count, $icon, $title);
  508. $consume = [
  509. 'title' => '消费金额为¥' . UserModel::consume($where),
  510. 'series' => UserModel::consume($where, 'xiaofei'),
  511. 'rightTitle' => $rightTitle,
  512. 'leftTitle' => $leftTitle,
  513. ];
  514. $form = UserModel::consume($where, 'form');
  515. $grouping = UserModel::consume($where, 'grouping');
  516. $this->assign(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
  517. return $this->fetch();
  518. }
  519. public function gethreaderValue($chart, $where = [])
  520. {
  521. if ($where) {
  522. switch ($where['date']) {
  523. case null:
  524. case 'today':
  525. case 'week':
  526. case 'year':
  527. if ($where['date'] == null) {
  528. $where['date'] = 'month';
  529. }
  530. $sum_user = UserModel::whereTime('add_time', $where['date'])->count();
  531. if ($sum_user == 0) return 0;
  532. $counts = bcdiv($chart, $sum_user, 4) * 100;
  533. return $counts;
  534. break;
  535. case 'quarter':
  536. $quarter = UserModel::getMonth('n');
  537. $quarter[0] = strtotime($quarter[0]);
  538. $quarter[1] = strtotime($quarter[1]);
  539. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  540. if ($sum_user == 0) return 0;
  541. $counts = bcdiv($chart, $sum_user, 4) * 100;
  542. return $counts;
  543. default:
  544. //自定义时间
  545. $quarter = explode('-', $where['date']);
  546. $quarter[0] = strtotime($quarter[0]);
  547. $quarter[1] = strtotime($quarter[1]);
  548. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  549. if ($sum_user == 0) return 0;
  550. $counts = bcdiv($chart, $sum_user, 4) * 100;
  551. return $counts;
  552. break;
  553. }
  554. } else {
  555. $num = UserModel::count();
  556. $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
  557. return $chart;
  558. }
  559. }
  560. public function get_user_index($where, $name)
  561. {
  562. switch ($where['date']) {
  563. case null:
  564. $days = date("t", strtotime(date('Y-m', time())));
  565. $dates = [];
  566. $series = [];
  567. $times_list = [];
  568. foreach ($name as $key => $val) {
  569. for ($i = 1; $i <= $days; $i++) {
  570. if (!in_array($i . '号', $times_list)) {
  571. array_push($times_list, $i . '号');
  572. }
  573. $time = $this->gettime(date("Y-m", time()) . '-' . $i);
  574. if ($key == 0) {
  575. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  576. } else if ($key == 1) {
  577. $dates['data'][] = UserModel::consume(true, $time);
  578. }
  579. }
  580. $dates['name'] = $val;
  581. $dates['type'] = 'line';
  582. $series[] = $dates;
  583. unset($dates);
  584. }
  585. return ['time' => $times_list, 'series' => $series];
  586. case 'today':
  587. $dates = [];
  588. $series = [];
  589. $times_list = [];
  590. foreach ($name as $key => $val) {
  591. for ($i = 0; $i <= 24; $i++) {
  592. $strtitle = $i . '点';
  593. if (!in_array($strtitle, $times_list)) {
  594. array_push($times_list, $strtitle);
  595. }
  596. $time = $this->gettime(date("Y-m-d ", time()) . $i);
  597. if ($key == 0) {
  598. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  599. } else if ($key == 1) {
  600. $dates['data'][] = UserModel::consume(true, $time);
  601. }
  602. }
  603. $dates['name'] = $val;
  604. $dates['type'] = 'line';
  605. $series[] = $dates;
  606. unset($dates);
  607. }
  608. return ['time' => $times_list, 'series' => $series];
  609. case "week":
  610. $dates = [];
  611. $series = [];
  612. $times_list = [];
  613. foreach ($name as $key => $val) {
  614. for ($i = 0; $i <= 6; $i++) {
  615. if (!in_array('星期' . ($i + 1), $times_list)) {
  616. array_push($times_list, '星期' . ($i + 1));
  617. }
  618. $time = UserModel::getMonth('h', $i);
  619. if ($key == 0) {
  620. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
  621. } else if ($key == 1) {
  622. $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])]);
  623. }
  624. }
  625. $dates['name'] = $val;
  626. $dates['type'] = 'line';
  627. $series[] = $dates;
  628. unset($dates);
  629. }
  630. return ['time' => $times_list, 'series' => $series];
  631. case 'year':
  632. $dates = [];
  633. $series = [];
  634. $times_list = [];
  635. $year = date('Y');
  636. foreach ($name as $key => $val) {
  637. for ($i = 1; $i <= 12; $i++) {
  638. if (!in_array($i . '月', $times_list)) {
  639. array_push($times_list, $i . '月');
  640. }
  641. $t = strtotime($year . '-' . $i . '-01');
  642. $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
  643. if ($key == 0) {
  644. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  645. } else if ($key == 1) {
  646. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  647. }
  648. }
  649. $dates['name'] = $val;
  650. $dates['type'] = 'line';
  651. $series[] = $dates;
  652. unset($dates);
  653. }
  654. return ['time' => $times_list, 'series' => $series];
  655. case 'quarter':
  656. $dates = [];
  657. $series = [];
  658. $times_list = [];
  659. foreach ($name as $key => $val) {
  660. for ($i = 1; $i <= 4; $i++) {
  661. $arr = $this->gettime('quarter', $i);
  662. if (!in_array(implode('--', $arr) . '季度', $times_list)) {
  663. array_push($times_list, implode('--', $arr) . '季度');
  664. }
  665. if ($key == 0) {
  666. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  667. } else if ($key == 1) {
  668. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  669. }
  670. }
  671. $dates['name'] = $val;
  672. $dates['type'] = 'line';
  673. $series[] = $dates;
  674. unset($dates);
  675. }
  676. return ['time' => $times_list, 'series' => $series];
  677. default:
  678. $list = UserModel::consume($where, 'default');
  679. $dates = [];
  680. $series = [];
  681. $times_list = [];
  682. foreach ($name as $k => $v) {
  683. foreach ($list as $val) {
  684. $date = $val['add_time'];
  685. if (!in_array($date, $times_list)) {
  686. array_push($times_list, $date);
  687. }
  688. if ($k == 0) {
  689. $dates['data'][] = $val['num'];
  690. } else if ($k == 1) {
  691. $dates['data'][] = UserBillAdmin::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
  692. }
  693. }
  694. $dates['name'] = $v;
  695. $dates['type'] = 'line';
  696. $series[] = $dates;
  697. unset($dates);
  698. }
  699. return ['time' => $times_list, 'series' => $series];
  700. }
  701. }
  702. public function gettime($time = '', $season = '')
  703. {
  704. if (!empty($time) && empty($season)) {
  705. $timestamp0 = strtotime($time);
  706. $timestamp24 = strtotime($time) + 86400;
  707. return [$timestamp0, $timestamp24];
  708. } else if (!empty($time) && !empty($season)) {
  709. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  710. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  711. return [$firstday, $lastday];
  712. }
  713. }
  714. /**
  715. * 会员等级首页
  716. */
  717. public function group()
  718. {
  719. return $this->fetch();
  720. }
  721. /**
  722. * 会员详情
  723. */
  724. public function see($uid = '')
  725. {
  726. $this->assign([
  727. 'uid' => $uid,
  728. 'userinfo' => UserModel::getUserDetailed($uid),
  729. 'is_layui' => true,
  730. 'headerList' => UserModel::getHeaderList($uid),
  731. 'count' => UserModel::getCountInfo($uid),
  732. ]);
  733. return $this->fetch();
  734. }
  735. public function qc($uid)
  736. {
  737. $user = \app\admin\model\user\User::where('uid', $uid)->find();
  738. if ($user['spread_uid'] == 0) return Json::fail('没有推荐人');
  739. $user['spread_uid'] = 0;
  740. if ( $user->save() and UserSpread::where('uid', $uid)->delete()){
  741. return Json::success('清除成功');
  742. }
  743. return Json::fail('清除失败');
  744. }
  745. /*
  746. * 获取某个用户的推广下线
  747. * */
  748. public function getSpreadList($uid, $page = 1, $limit = 20)
  749. {
  750. return Json::successful(UserModel::getSpreadList($uid, (int)$page, (int)$limit));
  751. }
  752. /**
  753. * 获取某用户的订单列表
  754. */
  755. public function getOneorderList($uid, $page = 1, $limit = 20)
  756. {
  757. return Json::successful(StoreOrder::getOneorderList(compact('uid', 'page', 'limit')));
  758. }
  759. /**
  760. * 获取某用户的积分列表
  761. */
  762. public function getOneIntegralList($uid, $page = 1, $limit = 20)
  763. {
  764. return Json::successful(UserBillAdmin::getOneIntegralList(compact('uid', 'page', 'limit')));
  765. }
  766. /**
  767. * 获取某用户的积分列表
  768. */
  769. public function getOneSignList($uid, $page = 1, $limit = 20)
  770. {
  771. return Json::successful(UserBillAdmin::getOneSignList(compact('uid', 'page', 'limit')));
  772. }
  773. /**
  774. * 获取某用户的持有优惠劵
  775. */
  776. public function getOneCouponsList($uid, $page = 1, $limit = 20)
  777. {
  778. return Json::successful(StoreCouponUser::getOneCouponsList(compact('uid', 'page', 'limit')));
  779. }
  780. /**
  781. * 获取某用户的余额变动记录
  782. */
  783. public function getOneBalanceChangList($uid, $page = 1, $limit = 20)
  784. {
  785. return Json::successful(UserBillAdmin::getOneBalanceChangList(compact('uid', 'page', 'limit')));
  786. }
  787. }