User.php 35 KB

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