User.php 40 KB

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