User.php 45 KB

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