User.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. <?php
  2. namespace app\adminapi\controller\v1\user;
  3. use app\adminapi\controller\AuthController;
  4. use Exception;
  5. use FormBuilder\exception\FormBuilderException;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\Response;
  10. use think\response\Json;
  11. use app\models\user\{
  12. User as UserModel,
  13. UserLabelRelation,
  14. UserLabel,
  15. UserLevel,
  16. UserBill as UserBillAdmin,
  17. UserTaskFinish,
  18. UserGroup
  19. };
  20. use app\models\system\{
  21. SystemUserLevel, SystemUserTask
  22. };
  23. use app\models\store\{
  24. StoreOrder, StoreVisit, StoreCouponUser
  25. };
  26. use app\models\wechat\WechatMessage;
  27. use crmeb\basic\BaseModel;
  28. use crmeb\repositories\UserRepository;
  29. use crmeb\services\{
  30. FormBuilder as Form, UtilService as Util
  31. };
  32. use think\Request;
  33. use think\facade\Route as Url;
  34. class User extends AuthController
  35. {
  36. /**
  37. * 显示资源列表头部
  38. *
  39. * @return Response
  40. */
  41. public function type_header()
  42. {
  43. //全部会员
  44. $all = UserModel::merSet($this->merId)->count();
  45. //小程序会员
  46. $routine = UserModel::merSet($this->merId)->alias('u')->join('WechatUser w', 'u.uid=w.uid')->where('w.routine_openid', '<>', '')->count();
  47. //公众号会员
  48. $wechat = UserModel::merSet($this->merId)->alias('u')->join('WechatUser w', 'u.uid=w.uid')->where('w.openid', '<>', '')->count();
  49. //H5会员
  50. $h5 = UserModel::merSet($this->merId)->alias('u')->join('WechatUser w', 'u.uid=w.uid')->where(['w.routine_openid' => '', 'w.openid' => ''])->where('u.user_type', 'h5')->count();
  51. $list = [
  52. ['user_type' => '', 'name' => '全部会员', 'count' => $all],
  53. ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
  54. ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
  55. ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
  56. ];
  57. return $this->success(compact('list'));
  58. }
  59. /**
  60. * 显示资源列表
  61. *
  62. * @return Response
  63. * @throws Exception
  64. */
  65. public function index()
  66. {
  67. $where = Util::getMore([
  68. ['page', 1],
  69. ['limit', 20],
  70. ['nickname', ''],
  71. ['status', ''],
  72. ['pay_count', ''],
  73. ['is_promoter', ''],
  74. ['order', ''],
  75. ['data', ''],
  76. ['user_type', ''],
  77. ['country', ''],
  78. ['province', ''],
  79. ['city', ''],
  80. ['user_time_type', ''],
  81. ['user_time', ''],
  82. ['sex', ''],
  83. [['level', 0], 0],
  84. [['group_id', 'd'], 0],
  85. [['label_id', 'd'], 0]
  86. ]);
  87. return $this->success(UserModel::getUserList($where));
  88. }
  89. /**
  90. * 显示创建资源表单页.
  91. *
  92. * @return void
  93. */
  94. public function create()
  95. {
  96. //
  97. }
  98. /**
  99. * 保存新建的资源
  100. *
  101. * @param Request $request
  102. * @return void
  103. */
  104. public function save(Request $request)
  105. {
  106. //
  107. }
  108. /**
  109. * 显示指定的资源
  110. *
  111. * @param int $id
  112. * @return Response
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. */
  117. public function read($id)
  118. {
  119. if (!$id) {
  120. return $this->fail('参数错误');
  121. }
  122. if (!UserModel::be(['uid' => $id, 'mer_id' => $this->merId])) {
  123. return $this->fail('参数错误');
  124. }
  125. $info = [
  126. 'uid' => $id,
  127. 'userinfo' => UserModel::getUserDetailed($id),
  128. 'is_layui' => true,
  129. 'headerList' => UserModel::getHeaderList($id),
  130. 'count' => UserModel::getCountInfo($id),
  131. 'ps_info' => UserModel::where('uid', $id)->find()
  132. ];
  133. return $this->success($info);
  134. }
  135. /**
  136. * 赠送会员等级
  137. * @param $id
  138. * @return mixed
  139. * @throws DataNotFoundException
  140. * @throws DbException
  141. * @throws ModelNotFoundException
  142. */
  143. public function give_level($id)
  144. {
  145. if (!$id) return $this->fail('缺少参数');
  146. $mer_id = $this->merId ?: '';
  147. if (!UserModel::be(['uid' => $id, 'mer_id' => $mer_id])) {
  148. return $this->fail('参数错误');
  149. }
  150. $level = UserLevel::getUserLevel($id);
  151. //获取当前会员等级
  152. if ($level === false)
  153. $grade = 0;
  154. else
  155. $grade = UserLevel::getUserLevelInfo($level, 'grade');
  156. //查询高于当前会员的所有会员等级
  157. $systemLevelList = SystemUserLevel::merSet($this->merId)->where('grade', '>', $grade)->where(['is_show' => 1, 'is_del' => 0])->field(['name', 'id'])->select();
  158. $field[] = Form::select('level_id', '会员等级')->setOptions(function () use ($systemLevelList) {
  159. $menus = [];
  160. foreach ($systemLevelList as $menu) {
  161. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  162. }
  163. return $menus;
  164. })->filterable(1);
  165. return $this->makePostForm('赠送会员', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
  166. }
  167. /**
  168. * 执行赠送会员等级
  169. * @param $id
  170. * @return mixed
  171. * @throws DataNotFoundException
  172. * @throws DbException
  173. * @throws ModelNotFoundException
  174. * @throws Exception
  175. */
  176. public function save_give_level($id)
  177. {
  178. if (!$id) return $this->fail('缺少参数');
  179. if (!UserModel::be(['uid' => $id, 'mer_id' => $this->merId])) {
  180. return $this->fail('参数错误');
  181. }
  182. list($level_id) = Util::postMore([
  183. ['level_id', 0],
  184. ], $this->request, true);
  185. //查询当前选择的会员等级
  186. $systemLevel = SystemUserLevel::merSet($this->merId)->where(['is_show' => 1, 'is_del' => 0, 'id' => $level_id])->find();
  187. if (!$systemLevel) return $this->fail('您选择赠送的会员等级不存在!');
  188. //检查是否拥有此会员等级
  189. $level = UserLevel::where(['uid' => $id, 'level_id' => $level_id, 'is_del' => 0])->field('valid_time,is_forever')->find();
  190. if ($level) {
  191. if ($level['is_forever']) {
  192. return $this->fail('此用户已有该会员等级,无法再次赠送');
  193. } else {
  194. if (time() < $level['valid_time'])
  195. return $this->fail('此用户已有该会员等级,无法再次赠送');
  196. }
  197. }
  198. //设置会员过期时间
  199. $add_valid_time = (int)$systemLevel->valid_date * 86400;
  200. UserModel::commitTrans();
  201. try {
  202. //保存会员信息
  203. $res = UserLevel::setUserLevel($id, $level_id, $this->merId);
  204. //提取等级任务并记录完成情况
  205. $levelIds = [$level_id];
  206. $lowGradeLevelIds = SystemUserLevel::merSet($this->merId)->where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id');
  207. if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds);
  208. $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id');
  209. $inserValue = [];
  210. foreach ($taskIds as $taskId) {
  211. $inserValue[] = ['uid' => $id, 'task_id' => $taskId, 'status' => 1, 'add_time' => time()];
  212. }
  213. $res = $res && UserTaskFinish::insertAll($inserValue);
  214. if ($res) {
  215. UserModel::commitTrans();
  216. return $this->success('赠送成功');
  217. } else {
  218. UserModel::rollbackTrans();
  219. return $this->success('赠送失败');
  220. }
  221. } catch (Exception $e) {
  222. UserModel::rollbackTrans();
  223. return $this->fail('赠送失败');
  224. }
  225. }
  226. /**
  227. * 清除会员等级
  228. * @param $id
  229. * @return Response
  230. */
  231. public function del_level($id)
  232. {
  233. if (!$id) return $this->fail('缺少参数');
  234. if (!UserModel::be(['uid' => $id, 'mer_id' => $this->merId])) {
  235. return $this->fail('参数错误');
  236. }
  237. if (UserLevel::cleanUpLevel($id))
  238. return $this->success('清除成功');
  239. else
  240. return $this->fail('清除失败');
  241. }
  242. /**
  243. * 设置会员分组
  244. * @return mixed
  245. * @throws DataNotFoundException
  246. * @throws DbException
  247. * @throws ModelNotFoundException
  248. * @throws Exception
  249. */
  250. public function set_group()
  251. {
  252. list($uids) = Util::postMore([
  253. ['uids', []],
  254. ], $this->request, true);
  255. $uid = implode(',', $uids);
  256. if (!$uid) return $this->fail('缺少参数');
  257. if (UserModel::where(['uid' => $uids, 'mer_id' => $this->merId])->count() != count($uids)) {
  258. return $this->fail('参数错误');
  259. }
  260. $userGroup = UserGroup::select();
  261. if (count($uids) == 1) {
  262. $user = UserModel::where('uid', $uids[0])->find();
  263. $field[] = Form::select('group_id', '会员分组', (string)$user->getData('group_id'))->setOptions(function () use ($userGroup) {
  264. $menus = [];
  265. foreach ($userGroup as $menu) {
  266. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  267. }
  268. return $menus;
  269. })->filterable(1);
  270. } else {
  271. $field[] = Form::select('group_id', '会员分组')->setOptions(function () use ($userGroup) {
  272. $menus = [];
  273. foreach ($userGroup as $menu) {
  274. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  275. }
  276. return $menus;
  277. })->filterable(1);
  278. }
  279. $field[] = Form::hidden('uids', $uid);
  280. return $this->makePostForm('设置会员分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
  281. }
  282. /**
  283. * 保存会员分组
  284. * @param $id
  285. * @return mixed
  286. * @throws Exception
  287. */
  288. public function save_set_group()
  289. {
  290. list($group_id, $uids) = Util::postMore([
  291. ['group_id', 0],
  292. ['uids', ''],
  293. ], $this->request, true);
  294. if (!$uids) return $this->fail('缺少参数');
  295. $uids = explode(',', $uids);
  296. if (UserModel::where(['uid' => $uids, 'mer_id' => $this->merId])->count() != count($uids)) {
  297. return $this->fail('参数错误');
  298. }
  299. $res = UserModel::whereIn('uid', $uids)->update(['group_id' => $group_id]);
  300. if ($res) {
  301. return $this->success('设置分组成功');
  302. } else {
  303. return $this->fail('设置分组失败或无改动');
  304. }
  305. }
  306. /**
  307. * 设置用户标签
  308. * @return mixed
  309. * @throws DataNotFoundException
  310. * @throws DbException
  311. * @throws ModelNotFoundException
  312. * @throws Exception
  313. */
  314. public function set_label()
  315. {
  316. list($uids) = Util::postMore([
  317. ['uids', []],
  318. ], $this->request, true);
  319. $uid = implode(',', $uids);
  320. if (!$uid) return $this->fail('缺少参数');
  321. if (UserModel::where(['uid' => $uids, 'mer_id' => $this->merId])->count() != count($uids)) {
  322. return $this->fail('参数错误');
  323. }
  324. $userGroup = UserLabel::select();
  325. if (count($uids) == 1) {
  326. $lids = UserLabelRelation::where('uid', $uids[0])->column('label_id');
  327. $field[] = Form::select('label_id', '会员标签', $lids)->setOptions(function () use ($userGroup) {
  328. $menus = [];
  329. foreach ($userGroup as $menu) {
  330. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  331. }
  332. return $menus;
  333. })->filterable(1)->multiple(1);
  334. } else {
  335. $field[] = Form::select('label_id', '会员标签')->setOptions(function () use ($userGroup) {
  336. $menus = [];
  337. foreach ($userGroup as $menu) {
  338. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  339. }
  340. return $menus;
  341. })->filterable(1)->multiple(1);
  342. }
  343. $field[] = Form::hidden('uids', $uid);
  344. return $this->makePostForm('设置会员标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
  345. }
  346. /**
  347. * 保存用户标签
  348. * @return mixed
  349. * @throws Exception
  350. */
  351. public function save_set_label()
  352. {
  353. list($lables, $uids) = Util::postMore([
  354. ['label_id', ''],
  355. ['uids', ''],
  356. ], $this->request, true);
  357. if (!$uids) return $this->fail('缺少参数');
  358. $uids = explode(',', $uids);
  359. if (UserModel::where([['uid', 'in', $uids], ['mer_id', '=', $this->merId]])->count() != count($uids)) {
  360. return $this->fail('参数错误');
  361. }
  362. $res = UserLabelRelation::saveUserLabel($uids, $lables);
  363. if ($res) {
  364. return $this->success('设置标签成功');
  365. } else {
  366. return $this->fail('设置标签失败');
  367. }
  368. }
  369. /**
  370. * 编辑其他
  371. * @param $id
  372. * @return mixed
  373. * @throws DataNotFoundException
  374. * @throws DbException
  375. * @throws FormBuilderException
  376. * @throws ModelNotFoundException
  377. */
  378. public function edit_other($id)
  379. {
  380. if (!$id) return $this->fail('数据不存在');
  381. $user = UserModel::merSet($this->merId)->where('uid', $id)->find();
  382. if (!$user) return $this->fail('数据不存在!');
  383. $f = array();
  384. $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  385. $f[] = Form::number('money', '余额', 0)->min(0);
  386. $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
  387. $f[] = Form::number('integration', '积分', 0)->min(0);
  388. return $this->makePostForm('修改其他', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
  389. }
  390. /**
  391. * 执行编辑其他
  392. * @param int $id
  393. * @return mixed
  394. * @throws Exception
  395. */
  396. public function update_other($id)
  397. {
  398. $data = Util::postMore([
  399. ['money_status', 0],
  400. ['money', 0],
  401. ['integration_status', 0],
  402. ['integration', 0],
  403. ], $this->request);
  404. if (!$id) return $this->fail('数据不存在');
  405. $user = UserModel::merSet($this->merId)->where('uid', $id)->find();
  406. if (!$user) return $this->fail('数据不存在!');
  407. BaseModel::beginTrans();
  408. $res1 = false;
  409. $res2 = false;
  410. $edit = array();
  411. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  412. if ($data['money_status'] == 1) {//增加
  413. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  414. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  415. try {
  416. UserRepository::adminAddMoney($user, $data['money']);
  417. } catch (Exception $e) {
  418. BaseModel::rollbackTrans();
  419. return $this->fail($e->getMessage());
  420. }
  421. } else if ($data['money_status'] == 2) {//减少
  422. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  423. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  424. try {
  425. UserRepository::adminSubMoney($user, $data['money']);
  426. } catch (Exception $e) {
  427. BaseModel::rollbackTrans();
  428. return $this->fail($e->getMessage());
  429. }
  430. }
  431. } else {
  432. $res1 = true;
  433. }
  434. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  435. if ($data['integration_status'] == 1) {//增加
  436. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  437. $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  438. try {
  439. UserRepository::adminAddIntegral($user, $data['integration']);
  440. } catch (Exception $e) {
  441. BaseModel::rollbackTrans();
  442. return $this->fail($e->getMessage());
  443. }
  444. } else if ($data['integration_status'] == 2) {//减少
  445. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  446. $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  447. try {
  448. UserRepository::adminSubIntegral($user, $data['integration']);
  449. } catch (Exception $e) {
  450. BaseModel::rollbackTrans();
  451. return $this->fail($e->getMessage());
  452. }
  453. }
  454. } else {
  455. $res2 = true;
  456. }
  457. if ($edit) $res3 = UserModel::edit($edit, $id);
  458. else $res3 = true;
  459. if ($res1 && $res2 && $res3) $res = true;
  460. else $res = false;
  461. BaseModel::checkTrans($res);
  462. if ($res) return $this->success('修改成功!');
  463. else return $this->fail('修改失败');
  464. }
  465. /**
  466. * 修改user表状态
  467. *
  468. * @return array
  469. */
  470. public function set_status($status, $id)
  471. {
  472. if ($status == '' || $id == 0) return $this->fail('参数错误');
  473. UserModel::merSet($this->merId)->where(['uid' => $id])->update(['status' => $status]);
  474. return $this->success($status == 0 ? '禁用成功' : '解禁成功');
  475. }
  476. /**
  477. * 编辑会员信息
  478. * @param $id
  479. * @return mixed|Json|void
  480. * @throws DataNotFoundException
  481. * @throws DbException
  482. * @throws FormBuilderException
  483. * @throws ModelNotFoundException
  484. */
  485. public function edit($id)
  486. {
  487. if (!$id) return $this->fail('数据不存在');
  488. $user = UserModel::merSet($this->merId)->where('uid', $id)->find();
  489. if (!$user) return $this->fail('数据不存在!');
  490. $f = array();
  491. $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(1);
  492. $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
  493. $f[] = Form::input('phone', '手机号码', $user->getData('phone'));
  494. $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '');
  495. $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
  496. $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
  497. $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
  498. $level = UserLevel::getUserLevel($id);
  499. //获取当前会员等级
  500. if ($level === false)
  501. $grade = 0;
  502. else
  503. $grade = UserLevel::getUserLevelInfo($level, 'grade');
  504. //查询高于当前会员的所有会员等级
  505. $systemLevelList = SystemUserLevel::merSet($this->merId)->where('grade', '>', $grade)->where(['is_show' => 1, 'is_del' => 0])->field(['name', 'id'])->select();
  506. $f[] = Form::select('level', '会员等级', (string)$user->getData('level'))->setOptions(function () use ($systemLevelList) {
  507. $menus = [];
  508. foreach ($systemLevelList as $menu) {
  509. $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
  510. }
  511. return $menus;
  512. })->filterable(1);
  513. $systemGroupList = UserGroup::select();
  514. $f[] = Form::select('group_id', '会员分组', (string)$user->getData('group_id'))->setOptions(function () use ($systemGroupList) {
  515. $menus = [];
  516. foreach ($systemGroupList as $menu) {
  517. $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
  518. }
  519. return $menus;
  520. })->filterable(1);
  521. $systemLabelList = UserLabel::select();
  522. $labels = UserLabelRelation::where('uid', $user->getData('uid'))->column('label_id');
  523. $f[] = Form::select('label_id', '会员标签', $labels)->setOptions(function () use ($systemLabelList) {
  524. $menus = [];
  525. foreach ($systemLabelList as $menu) {
  526. $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
  527. }
  528. return $menus;
  529. })->filterable(1)->multiple(1);
  530. $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  531. $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  532. return $this->makePostForm('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
  533. }
  534. /**
  535. * @param $id
  536. * @return mixed
  537. * @throws DataNotFoundException
  538. * @throws DbException
  539. * @throws ModelNotFoundException
  540. * @throws Exception
  541. */
  542. public function update($id)
  543. {
  544. $data = Util::postMore([
  545. ['money_status', 0],
  546. ['is_promoter', 1],
  547. ['real_name', ''],
  548. ['card_id', ''],
  549. ['birthday', ''],
  550. ['mark', ''],
  551. ['money', 0],
  552. ['integration_status', 0],
  553. ['integration', 0],
  554. ['status', 0],
  555. ['level', 0],
  556. ['phone', 0],
  557. ['addres', ''],
  558. ['label_id', ''],
  559. ['group_id', 0]
  560. ]);
  561. if ($data['phone']) {
  562. if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) return $this->fail('手机号码格式不正确');
  563. }
  564. if ($data['card_id']) {
  565. if (!preg_match('/^[1-9]{1}\d{5}[1-9]{2}\d{9}[Xx0-9]{1}$/', $data['card_id'])) return $this->fail('请输入正确的身份证');
  566. }
  567. if (!$id) return $this->fail('数据不存在');
  568. $user = UserModel::merSet($this->merId)->where('uid', $id)->find();
  569. if (!$user) return $this->fail('数据不存在!');
  570. BaseModel::beginTrans();
  571. $res1 = false;
  572. $res2 = false;
  573. $edit = array();
  574. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  575. if ($data['money_status'] == 1) {//增加
  576. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  577. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  578. try {
  579. UserRepository::adminAddMoney($user, $data['money']);
  580. } catch (Exception $e) {
  581. BaseModel::rollbackTrans();
  582. return $this->fail($e->getMessage());
  583. }
  584. } else if ($data['money_status'] == 2) {//减少
  585. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  586. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  587. try {
  588. UserRepository::adminSubMoney($user, $data['money']);
  589. } catch (Exception $e) {
  590. BaseModel::rollbackTrans();
  591. return $this->fail($e->getMessage());
  592. }
  593. }
  594. } else {
  595. $res1 = true;
  596. }
  597. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  598. if ($data['integration_status'] == 1) {//增加
  599. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  600. $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  601. try {
  602. UserRepository::adminAddIntegral($user, $data['integration']);
  603. } catch (Exception $e) {
  604. BaseModel::rollbackTrans();
  605. return $this->fail($e->getMessage());
  606. }
  607. } else if ($data['integration_status'] == 2) {//减少
  608. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  609. $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  610. try {
  611. UserRepository::adminSubIntegral($user, $data['integration']);
  612. } catch (Exception $e) {
  613. BaseModel::rollbackTrans();
  614. return $this->fail($e->getMessage());
  615. }
  616. }
  617. } else {
  618. $res2 = true;
  619. }
  620. UserLabelRelation::saveUserLabel([$id], $data['label_id']);
  621. $edit['status'] = $data['status'];
  622. $edit['real_name'] = $data['real_name'];
  623. $edit['card_id'] = $data['card_id'];
  624. $edit['birthday'] = strtotime($data['birthday']);
  625. $edit['mark'] = $data['mark'];
  626. $edit['is_promoter'] = $data['is_promoter'];
  627. $edit['level'] = $data['level'];
  628. $edit['phone'] = $data['phone'];
  629. $edit['addres'] = $data['addres'];
  630. $edit['group_id'] = $data['group_id'];
  631. if ($edit) $res3 = UserModel::edit($edit, $id);
  632. else $res3 = true;
  633. if ($res1 && $res2 && $res3) $res = true;
  634. else $res = false;
  635. BaseModel::checkTrans($res);
  636. if ($res) return $this->success('修改成功!');
  637. else return $this->fail('修改失败');
  638. }
  639. /**
  640. * TODO:需统一到数据统计模块 start
  641. * 用户图表
  642. * @return mixed
  643. * @throws DataNotFoundException
  644. * @throws DbException
  645. * @throws ModelNotFoundException
  646. * @throws Exception
  647. */
  648. public function user_analysis()
  649. {
  650. $where = Util::getMore([
  651. ['nickname', ''],
  652. ['status', ''],
  653. ['is_promoter', ''],
  654. ['date', ''],
  655. ['user_type', ''],
  656. ['export', 0]
  657. ], $this->request);
  658. $user_count = UserModel::consume($where, '', true, $this->merId);
  659. //头部信息
  660. $header = [
  661. [
  662. 'name' => '新增用户',
  663. 'class' => 'fa-line-chart',
  664. 'value' => $user_count,
  665. 'color' => 'red'
  666. ],
  667. [
  668. 'name' => '用户留存',
  669. 'class' => 'fa-area-chart',
  670. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true, $this->merId), $where) . '%',
  671. 'color' => 'lazur'
  672. ],
  673. [
  674. 'name' => '新增用户总消费',
  675. 'class' => 'fa-bar-chart',
  676. 'value' => '¥' . UserModel::consume($where, '', '', $this->merId),
  677. 'color' => 'navy'
  678. ],
  679. [
  680. 'name' => '用户活跃度',
  681. 'class' => 'fa-pie-chart',
  682. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true, $this->merId)) . '%',
  683. 'color' => 'yellow'
  684. ],
  685. ];
  686. $name = ['新增用户', '用户消费'];
  687. $dates = $this->get_user_index($where, $name);
  688. $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
  689. //用户浏览分析
  690. $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger'], $this->merId);
  691. $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger'], $this->merId);
  692. $view = array_merge($view, $view_v1);
  693. $view_v2 = [];
  694. foreach ($view as $val) {
  695. $view_v2['color'][] = '#' . rand(100000, 339899);
  696. $view_v2['name'][] = $val['name'];
  697. $view_v2['value'][] = $val['value'];
  698. }
  699. $view = $view_v2;
  700. //消费会员排行用户分析
  701. $user_null = UserModel::getUserSpend($where['date'], '', $this->merId);
  702. //消费数据
  703. $now_number = UserModel::getUserSpend($where['date'], true, $this->merId);
  704. list($paren_number, $title) = UserModel::getPostNumber($where['date'], false, 'A.add_time', '消费', $this->merId);
  705. if ($paren_number == 0) {
  706. $rightTitle = [
  707. 'number' => $now_number > 0 ? $now_number : 0,
  708. 'icon' => 'fa-level-up',
  709. 'title' => $title
  710. ];
  711. } else {
  712. $number = (float)bcsub((string)$now_number, $paren_number, 4);
  713. if ($now_number == 0) {
  714. $icon = 'fa-level-down';
  715. } else {
  716. $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
  717. }
  718. $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
  719. }
  720. unset($title, $paren_number, $now_number);
  721. list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '', $this->merId);
  722. if ($paren_user_count == 0) {
  723. $count = $user_count == 0 ? 0 : $user_count;
  724. $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
  725. } else {
  726. $count = (float)bcsub($user_count, $paren_user_count, 4);
  727. $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
  728. }
  729. $leftTitle = [
  730. 'count' => $count,
  731. 'icon' => $icon,
  732. 'title' => $title
  733. ];
  734. unset($count, $icon, $title);
  735. $consume = [
  736. 'title' => '消费金额为¥' . UserModel::consume($where, '', '', $this->merId),
  737. 'series' => UserModel::consume($where, 'xiaofei', '', $this->merId),
  738. 'rightTitle' => $rightTitle,
  739. 'leftTitle' => $leftTitle,
  740. ];
  741. $form = UserModel::consume($where, 'form', '', $this->merId);
  742. $grouping = UserModel::consume($where, 'grouping', '', $this->merId);
  743. return $this->success(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
  744. }
  745. /**
  746. * @param $chart
  747. * @param array $where
  748. * @return float|int
  749. */
  750. public function gethreaderValue($chart, $where = [])
  751. {
  752. if ($where) {
  753. switch ($where['date']) {
  754. case null:
  755. case 'today':
  756. case 'week':
  757. case 'year':
  758. if ($where['date'] == null) {
  759. $where['date'] = 'month';
  760. }
  761. $sum_user = UserModel::merSet($this->merId)->whereTime('add_time', $where['date'])->count();
  762. if ($sum_user == 0) return 0;
  763. $counts = bcdiv($chart, $sum_user, 4) * 100;
  764. return $counts;
  765. break;
  766. case 'quarter':
  767. $quarter = UserModel::getMonth('n');
  768. $quarter[0] = strtotime($quarter[0]);
  769. $quarter[1] = strtotime($quarter[1]);
  770. $sum_user = UserModel::merSet($this->merId)->where('add_time', 'between', $quarter)->count();
  771. if ($sum_user == 0) return 0;
  772. $counts = bcdiv($chart, $sum_user, 4) * 100;
  773. return $counts;
  774. default:
  775. //自定义时间
  776. $quarter = explode('-', $where['date']);
  777. $quarter[0] = strtotime($quarter[0]);
  778. $quarter[1] = strtotime($quarter[1]);
  779. $sum_user = UserModel::merSet($this->merId)->where('add_time', 'between', $quarter)->count();
  780. if ($sum_user == 0) return 0;
  781. $counts = bcdiv($chart, $sum_user, 4) * 100;
  782. return $counts;
  783. break;
  784. }
  785. } else {
  786. $num = UserModel::merSet($this->merId)->count();
  787. $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
  788. return $chart;
  789. }
  790. }
  791. /**
  792. * @param $where
  793. * @param $name
  794. * @return array
  795. * @throws DataNotFoundException
  796. * @throws DbException
  797. * @throws ModelNotFoundException
  798. */
  799. public function get_user_index($where, $name)
  800. {
  801. switch ($where['date']) {
  802. case null:
  803. $days = date("t", strtotime(date('Y-m', time())));
  804. $dates = [];
  805. $series = [];
  806. $times_list = [];
  807. foreach ($name as $key => $val) {
  808. for ($i = 1; $i <= $days; $i++) {
  809. if (!in_array($i . '号', $times_list)) {
  810. array_push($times_list, $i . '号');
  811. }
  812. $time = $this->gettime(date("Y-m", time()) . '-' . $i);
  813. if ($key == 0) {
  814. $dates['data'][] = UserModel::merSet($this->merId)->where('add_time', 'between', $time)->count();
  815. } else if ($key == 1) {
  816. $dates['data'][] = UserModel::consume(true, $time, '', $this->merId);
  817. }
  818. }
  819. $dates['name'] = $val;
  820. $dates['type'] = 'line';
  821. $series[] = $dates;
  822. unset($dates);
  823. }
  824. return ['time' => $times_list, 'series' => $series];
  825. case 'today':
  826. $dates = [];
  827. $series = [];
  828. $times_list = [];
  829. foreach ($name as $key => $val) {
  830. for ($i = 0; $i <= 24; $i++) {
  831. $strtitle = $i . '点';
  832. if (!in_array($strtitle, $times_list)) {
  833. array_push($times_list, $strtitle);
  834. }
  835. $time = $this->gettime(date("Y-m-d ", time()) . $i);
  836. if ($key == 0) {
  837. $dates['data'][] = UserModel::merSet($this->merId)->where('add_time', 'between', $time)->count();
  838. } else if ($key == 1) {
  839. $dates['data'][] = UserModel::consume(true, $time, '', $this->merId);
  840. }
  841. }
  842. $dates['name'] = $val;
  843. $dates['type'] = 'line';
  844. $series[] = $dates;
  845. unset($dates);
  846. }
  847. return ['time' => $times_list, 'series' => $series];
  848. case "week":
  849. $dates = [];
  850. $series = [];
  851. $times_list = [];
  852. foreach ($name as $key => $val) {
  853. for ($i = 0; $i <= 6; $i++) {
  854. if (!in_array('星期' . ($i + 1), $times_list)) {
  855. array_push($times_list, '星期' . ($i + 1));
  856. }
  857. $time = UserModel::getMonth('h', $i);
  858. if ($key == 0) {
  859. $dates['data'][] = UserModel::merSet($this->merId)->where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
  860. } else if ($key == 1) {
  861. $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])], '', $this->merId);
  862. }
  863. }
  864. $dates['name'] = $val;
  865. $dates['type'] = 'line';
  866. $series[] = $dates;
  867. unset($dates);
  868. }
  869. return ['time' => $times_list, 'series' => $series];
  870. case 'year':
  871. $dates = [];
  872. $series = [];
  873. $times_list = [];
  874. $year = date('Y');
  875. foreach ($name as $key => $val) {
  876. for ($i = 1; $i <= 12; $i++) {
  877. if (!in_array($i . '月', $times_list)) {
  878. array_push($times_list, $i . '月');
  879. }
  880. $t = strtotime($year . '-' . $i . '-01');
  881. $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
  882. if ($key == 0) {
  883. $dates['data'][] = UserModel::merSet($this->merId)->where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  884. } else if ($key == 1) {
  885. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])], '', $this->merId);
  886. }
  887. }
  888. $dates['name'] = $val;
  889. $dates['type'] = 'line';
  890. $series[] = $dates;
  891. unset($dates);
  892. }
  893. return ['time' => $times_list, 'series' => $series];
  894. case 'quarter':
  895. $dates = [];
  896. $series = [];
  897. $times_list = [];
  898. foreach ($name as $key => $val) {
  899. for ($i = 1; $i <= 4; $i++) {
  900. $arr = $this->gettime('quarter', $i);
  901. if (!in_array(implode('--', $arr) . '季度', $times_list)) {
  902. array_push($times_list, implode('--', $arr) . '季度');
  903. }
  904. if ($key == 0) {
  905. $dates['data'][] = UserModel::merSet($this->merId)->where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  906. } else if ($key == 1) {
  907. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])], '', $this->merId);
  908. }
  909. }
  910. $dates['name'] = $val;
  911. $dates['type'] = 'line';
  912. $series[] = $dates;
  913. unset($dates);
  914. }
  915. return ['time' => $times_list, 'series' => $series];
  916. default:
  917. $list = UserModel::consume($where, 'default', '', $this->merId);
  918. $dates = [];
  919. $series = [];
  920. $times_list = [];
  921. foreach ($name as $k => $v) {
  922. foreach ($list as $val) {
  923. $date = $val['add_time'];
  924. if (!in_array($date, $times_list)) {
  925. array_push($times_list, $date);
  926. }
  927. if ($k == 0) {
  928. $dates['data'][] = $val['num'];
  929. } else if ($k == 1) {
  930. $dates['data'][] = UserBillAdmin::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
  931. }
  932. }
  933. $dates['name'] = $v;
  934. $dates['type'] = 'line';
  935. $series[] = $dates;
  936. unset($dates);
  937. }
  938. return ['time' => $times_list, 'series' => $series];
  939. }
  940. }
  941. public function gettime($time = '', $season = '')
  942. {
  943. if (!empty($time) && empty($season)) {
  944. $timestamp0 = strtotime($time);
  945. $timestamp24 = strtotime($time) + 86400;
  946. return [$timestamp0, $timestamp24];
  947. } else if (!empty($time) && !empty($season)) {
  948. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  949. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  950. return [$firstday, $lastday];
  951. }
  952. }
  953. /**
  954. * TODO:需统一到数据统计模块 end
  955. */
  956. /**
  957. * 获取单个用户信息
  958. * @param int $id 用户id
  959. * @return mixed
  960. * @throws Exception
  961. */
  962. public function oneUserInfo($id)
  963. {
  964. $data = Util::getMore([
  965. ['type', ''],
  966. ['page', 1],
  967. ['limit', 20]
  968. ]);
  969. if (!$id || $data['type'] == '') return $this->fail('缺少参数');
  970. if (!UserModel::be(['uid' => $id, 'mer_id' => $this->merId])) {
  971. return $this->fail('参数错误');
  972. }
  973. switch ($data['type']) {
  974. case 'spread':
  975. return $this->getSpreadList($id, $data['page'], $data['limit'], $this->merId);
  976. break;
  977. case 'order':
  978. return $this->getOneOrderList($id, $data['page'], $data['limit']);
  979. break;
  980. case 'integral':
  981. return $this->getOneIntegralList($id, $data['page'], $data['limit']);
  982. break;
  983. case 'sign':
  984. return $this->getOneSignList($id, $data['page'], $data['limit']);
  985. break;
  986. case 'coupon':
  987. return $this->getOneCouponsList($id, $data['page'], $data['limit']);
  988. break;
  989. case 'balance_change':
  990. return $this->getOneBalanceChangList($id, $data['page'], $data['limit']);
  991. break;
  992. default:
  993. return $this->fail('type参数错误');
  994. }
  995. }
  996. /**
  997. * 获取某个用户的推广下线
  998. * @param $id
  999. * @param $page
  1000. * @param $limit
  1001. * @param string $mer_id
  1002. * @return Response
  1003. */
  1004. public function getSpreadList($id, $page, $limit, $mer_id = '')
  1005. {
  1006. return $this->success(UserModel::getSpreadList($id, (int)$page, (int)$limit, $mer_id));
  1007. }
  1008. /**
  1009. * 获取某用户的订单列表
  1010. * @param $id
  1011. * @param $page
  1012. * @param $limit
  1013. * @param string $mer_id
  1014. * @return Response
  1015. */
  1016. public function getOneOrderList($id, $page, $limit)
  1017. {
  1018. return $this->success(StoreOrder::getOneorderList(compact('id', 'page', 'limit')));
  1019. }
  1020. /**
  1021. * 获取某用户的积分列表
  1022. * @param $id
  1023. * @param int $page
  1024. * @param int $limit
  1025. * @return
  1026. */
  1027. public function getOneIntegralList($id, $page = 1, $limit = 20)
  1028. {
  1029. return $this->success(UserBillAdmin::getOneIntegralList(compact('id', 'page', 'limit')));
  1030. }
  1031. /**
  1032. * 获取某用户的签到明细
  1033. * @param $id
  1034. * @param int $page
  1035. * @param int $limit
  1036. * @return
  1037. */
  1038. public function getOneSignList($id, $page = 1, $limit = 20)
  1039. {
  1040. return $this->success(UserBillAdmin::getOneSignList(compact('id', 'page', 'limit')));
  1041. }
  1042. /**
  1043. * 获取某用户的持有优惠劵
  1044. * @param $id
  1045. * @param int $page
  1046. * @param int $limit
  1047. * @return
  1048. */
  1049. public function getOneCouponsList($id, $page = 1, $limit = 20)
  1050. {
  1051. return $this->success(StoreCouponUser::getOneCouponsList(compact('id', 'page', 'limit')));
  1052. }
  1053. /**
  1054. * 获取某用户的余额变动记录
  1055. * @param $id
  1056. * @param int $page
  1057. * @param int $limit
  1058. * @return
  1059. */
  1060. public function getOneBalanceChangList($id, $page = 1, $limit = 20)
  1061. {
  1062. return $this->success(UserBillAdmin::getOneBalanceChangList(compact('id', 'page', 'limit')));
  1063. }
  1064. }