User.php 34 KB

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