User.php 35 KB

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