User.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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('sp_user', 'VIP用户', $user->getData('sp_user'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
  424. $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
  425. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('uid' => $uid)), 5);
  426. $this->assign(compact('form'));
  427. return $this->fetch('public/form-builder');
  428. }
  429. public function update($uid)
  430. {
  431. $data = Util::postMore([
  432. ['money_status', 0],
  433. ['is_promoter', 1],
  434. ['real_name', ''],
  435. ['phone', ''],
  436. ['email', ''],
  437. // ['phone', 0],
  438. ['card_id', ''],
  439. ['birthday', ''],
  440. ['mark', ''],
  441. ['money', 0],
  442. ['integration_status', 0],
  443. ['integration', 0],
  444. ['status', 0],
  445. ['sp_user', 0],
  446. ['is_point', 0],
  447. ['area_admin'],
  448. ['store_id'],
  449. ]);
  450. $data['area_address'] = $this->request->post('area_address/a');
  451. if (!$uid) return $this->failed('数据不存在');
  452. $user = UserModel::get($uid);
  453. if (!$user) return Json::fail('数据不存在!');
  454. BaseModel::beginTrans();
  455. $res1 = false;
  456. $res2 = false;
  457. $edit = array();
  458. if ($data['money_status'] && $data['money']) {//余额增加或者减少
  459. if ($data['money_status'] == 1) {//增加
  460. $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
  461. $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
  462. try {
  463. UserRepository::adminAddMoney($user, $data['money']);
  464. } catch (\Exception $e) {
  465. BaseModel::rollbackTrans();
  466. return Json::fail($e->getMessage());
  467. }
  468. } else if ($data['money_status'] == 2) {//减少
  469. $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
  470. $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
  471. try {
  472. UserRepository::adminSubMoney($user, $data['money']);
  473. } catch (\Exception $e) {
  474. BaseModel::rollbackTrans();
  475. return Json::fail($e->getMessage());
  476. }
  477. }
  478. } else {
  479. $res1 = true;
  480. }
  481. if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
  482. if ($data['integration_status'] == 1) {//增加
  483. $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
  484. $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
  485. try {
  486. UserRepository::adminAddIntegral($user, $data['integration']);
  487. } catch (\Exception $e) {
  488. BaseModel::rollbackTrans();
  489. return Json::fail($e->getMessage());
  490. }
  491. } else if ($data['integration_status'] == 2) {//减少
  492. $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
  493. $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
  494. try {
  495. UserRepository::adminSubIntegral($user, $data['integration']);
  496. } catch (\Exception $e) {
  497. BaseModel::rollbackTrans();
  498. return Json::fail($e->getMessage());
  499. }
  500. }
  501. } else {
  502. $res2 = true;
  503. }
  504. $edit['status'] = $data['status'];
  505. $edit['real_name'] = $data['real_name'];
  506. // if (UserModel::where('uid', '<>', $uid)->where('site_id', $user['site_id'])->where('account', $data['phone'])->find())
  507. // return Json::fail('手机号码已被使用');
  508. // else {
  509. $edit['phone'] = $data['phone'];
  510. $edit['email'] = $data['email'];
  511. // }
  512. $edit['card_id'] = $data['card_id'];
  513. $edit['store_id'] = $data['store_id'];
  514. $edit['birthday'] = strtotime($data['birthday']);
  515. $edit['mark'] = $data['mark'];
  516. $edit['is_promoter'] = $data['is_promoter'];
  517. $edit['area_admin'] = $data['area_admin'];
  518. $edit['sp_user'] = $data['sp_user'];
  519. if ($edit['area_admin'] == 1 && (!$data['area_address'][0] || !$data['area_address'][1] || !$data['area_address'][2])) {
  520. return Json::fail('请选择代理地区');
  521. }
  522. if ($edit['area_admin'] == 2 && (!$data['area_address'][0] || !$data['area_address'][1])) {
  523. return Json::fail('请选择代理地区');
  524. }
  525. if ($edit['area_admin'] == 3 && (!$data['area_address'][0])) {
  526. return Json::fail('请选择代理地区');
  527. }
  528. if ($edit['area_admin'] == 0) {
  529. $edit['area_province'] = '';
  530. $edit['area_city'] = '';
  531. $edit['area_district'] = '';
  532. } else {
  533. $edit['area_province'] = $data['area_address'][0];
  534. $edit['area_city'] = $data['area_address'][1];
  535. $edit['area_district'] = $data['area_address'][2];
  536. }
  537. if ($edit) $res3 = UserModel::edit($edit, $uid);
  538. else $res3 = true;
  539. if ($res1 && $res2 && $res3) $res = true;
  540. else $res = false;
  541. BaseModel::checkTrans($res);
  542. if ($res) return Json::successful('修改成功!');
  543. else return Json::fail('修改失败');
  544. }
  545. /**
  546. * 用户图表
  547. * @return mixed
  548. */
  549. public function user_analysis()
  550. {
  551. $where = Util::getMore([
  552. ['nickname', ''],
  553. ['status', ''],
  554. ['is_promoter', ''],
  555. ['date', ''],
  556. ['user_type', ''],
  557. ['export', 0]
  558. ], $this->request);
  559. $user_count = UserModel::consume($where, '', true);
  560. //头部信息
  561. $header = [
  562. [
  563. 'name' => '新增用户',
  564. 'class' => 'fa-line-chart',
  565. 'value' => $user_count,
  566. 'color' => 'red'
  567. ],
  568. [
  569. 'name' => '用户留存',
  570. 'class' => 'fa-area-chart',
  571. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true), $where) . '%',
  572. 'color' => 'lazur'
  573. ],
  574. [
  575. 'name' => '新增用户总消费',
  576. 'class' => 'fa-bar-chart',
  577. 'value' => '¥' . UserModel::consume($where),
  578. 'color' => 'navy'
  579. ],
  580. [
  581. 'name' => '用户活跃度',
  582. 'class' => 'fa-pie-chart',
  583. 'value' => $this->gethreaderValue(UserModel::consume($where, '', true)) . '%',
  584. 'color' => 'yellow'
  585. ],
  586. ];
  587. $name = ['新增用户', '用户消费'];
  588. $dates = $this->get_user_index($where, $name);
  589. $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
  590. //用户浏览分析
  591. $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger']);
  592. $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger']);
  593. $view = array_merge($view, $view_v1);
  594. $view_v2 = [];
  595. foreach ($view as $val) {
  596. $view_v2['color'][] = '#' . rand(100000, 339899);
  597. $view_v2['name'][] = $val['name'];
  598. $view_v2['value'][] = $val['value'];
  599. }
  600. $view = $view_v2;
  601. //消费会员排行用户分析
  602. $user_null = UserModel::getUserSpend($where['date']);
  603. //消费数据
  604. $now_number = UserModel::getUserSpend($where['date'], true);
  605. list($paren_number, $title) = UserModel::getPostNumber($where['date']);
  606. if ($paren_number == 0) {
  607. $rightTitle = [
  608. 'number' => $now_number > 0 ? $now_number : 0,
  609. 'icon' => 'fa-level-up',
  610. 'title' => $title
  611. ];
  612. } else {
  613. $number = (float)bcsub($now_number, $paren_number, 4);
  614. if ($now_number == 0) {
  615. $icon = 'fa-level-down';
  616. } else {
  617. $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
  618. }
  619. $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
  620. }
  621. unset($title, $paren_number, $now_number);
  622. list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '');
  623. if ($paren_user_count == 0) {
  624. $count = $user_count == 0 ? 0 : $user_count;
  625. $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
  626. } else {
  627. $count = (float)bcsub($user_count, $paren_user_count, 4);
  628. $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
  629. }
  630. $leftTitle = [
  631. 'count' => $count,
  632. 'icon' => $icon,
  633. 'title' => $title
  634. ];
  635. unset($count, $icon, $title);
  636. $consume = [
  637. 'title' => '消费金额为¥' . UserModel::consume($where),
  638. 'series' => UserModel::consume($where, 'xiaofei'),
  639. 'rightTitle' => $rightTitle,
  640. 'leftTitle' => $leftTitle,
  641. ];
  642. $form = UserModel::consume($where, 'form');
  643. $grouping = UserModel::consume($where, 'grouping');
  644. $this->assign(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
  645. return $this->fetch();
  646. }
  647. public function gethreaderValue($chart, $where = [])
  648. {
  649. if ($where) {
  650. switch ($where['date']) {
  651. case null:
  652. case 'today':
  653. case 'week':
  654. case 'year':
  655. if ($where['date'] == null) {
  656. $where['date'] = 'month';
  657. }
  658. $sum_user = UserModel::whereTime('add_time', $where['date'])->count();
  659. if ($sum_user == 0) return 0;
  660. $counts = bcdiv($chart, $sum_user, 4) * 100;
  661. return $counts;
  662. break;
  663. case 'quarter':
  664. $quarter = UserModel::getMonth('n');
  665. $quarter[0] = strtotime($quarter[0]);
  666. $quarter[1] = strtotime($quarter[1]);
  667. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  668. if ($sum_user == 0) return 0;
  669. $counts = bcdiv($chart, $sum_user, 4) * 100;
  670. return $counts;
  671. default:
  672. //自定义时间
  673. $quarter = explode('-', $where['date']);
  674. $quarter[0] = strtotime($quarter[0]);
  675. $quarter[1] = strtotime($quarter[1]);
  676. $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
  677. if ($sum_user == 0) return 0;
  678. $counts = bcdiv($chart, $sum_user, 4) * 100;
  679. return $counts;
  680. break;
  681. }
  682. } else {
  683. $num = UserModel::count();
  684. $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
  685. return $chart;
  686. }
  687. }
  688. public function get_user_index($where, $name)
  689. {
  690. switch ($where['date']) {
  691. case null:
  692. $days = date("t", strtotime(date('Y-m', time())));
  693. $dates = [];
  694. $series = [];
  695. $times_list = [];
  696. foreach ($name as $key => $val) {
  697. for ($i = 1; $i <= $days; $i++) {
  698. if (!in_array($i . '号', $times_list)) {
  699. array_push($times_list, $i . '号');
  700. }
  701. $time = $this->gettime(date("Y-m", time()) . '-' . $i);
  702. if ($key == 0) {
  703. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  704. } else if ($key == 1) {
  705. $dates['data'][] = UserModel::consume(true, $time);
  706. }
  707. }
  708. $dates['name'] = $val;
  709. $dates['type'] = 'line';
  710. $series[] = $dates;
  711. unset($dates);
  712. }
  713. return ['time' => $times_list, 'series' => $series];
  714. case 'today':
  715. $dates = [];
  716. $series = [];
  717. $times_list = [];
  718. foreach ($name as $key => $val) {
  719. for ($i = 0; $i <= 24; $i++) {
  720. $strtitle = $i . '点';
  721. if (!in_array($strtitle, $times_list)) {
  722. array_push($times_list, $strtitle);
  723. }
  724. $time = $this->gettime(date("Y-m-d ", time()) . $i);
  725. if ($key == 0) {
  726. $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
  727. } else if ($key == 1) {
  728. $dates['data'][] = UserModel::consume(true, $time);
  729. }
  730. }
  731. $dates['name'] = $val;
  732. $dates['type'] = 'line';
  733. $series[] = $dates;
  734. unset($dates);
  735. }
  736. return ['time' => $times_list, 'series' => $series];
  737. case "week":
  738. $dates = [];
  739. $series = [];
  740. $times_list = [];
  741. foreach ($name as $key => $val) {
  742. for ($i = 0; $i <= 6; $i++) {
  743. if (!in_array('星期' . ($i + 1), $times_list)) {
  744. array_push($times_list, '星期' . ($i + 1));
  745. }
  746. $time = UserModel::getMonth('h', $i);
  747. if ($key == 0) {
  748. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
  749. } else if ($key == 1) {
  750. $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])]);
  751. }
  752. }
  753. $dates['name'] = $val;
  754. $dates['type'] = 'line';
  755. $series[] = $dates;
  756. unset($dates);
  757. }
  758. return ['time' => $times_list, 'series' => $series];
  759. case 'year':
  760. $dates = [];
  761. $series = [];
  762. $times_list = [];
  763. $year = date('Y');
  764. foreach ($name as $key => $val) {
  765. for ($i = 1; $i <= 12; $i++) {
  766. if (!in_array($i . '月', $times_list)) {
  767. array_push($times_list, $i . '月');
  768. }
  769. $t = strtotime($year . '-' . $i . '-01');
  770. $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
  771. if ($key == 0) {
  772. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  773. } else if ($key == 1) {
  774. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  775. }
  776. }
  777. $dates['name'] = $val;
  778. $dates['type'] = 'line';
  779. $series[] = $dates;
  780. unset($dates);
  781. }
  782. return ['time' => $times_list, 'series' => $series];
  783. case 'quarter':
  784. $dates = [];
  785. $series = [];
  786. $times_list = [];
  787. foreach ($name as $key => $val) {
  788. for ($i = 1; $i <= 4; $i++) {
  789. $arr = $this->gettime('quarter', $i);
  790. if (!in_array(implode('--', $arr) . '季度', $times_list)) {
  791. array_push($times_list, implode('--', $arr) . '季度');
  792. }
  793. if ($key == 0) {
  794. $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
  795. } else if ($key == 1) {
  796. $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
  797. }
  798. }
  799. $dates['name'] = $val;
  800. $dates['type'] = 'line';
  801. $series[] = $dates;
  802. unset($dates);
  803. }
  804. return ['time' => $times_list, 'series' => $series];
  805. default:
  806. $list = UserModel::consume($where, 'default');
  807. $dates = [];
  808. $series = [];
  809. $times_list = [];
  810. foreach ($name as $k => $v) {
  811. foreach ($list as $val) {
  812. $date = $val['add_time'];
  813. if (!in_array($date, $times_list)) {
  814. array_push($times_list, $date);
  815. }
  816. if ($k == 0) {
  817. $dates['data'][] = $val['num'];
  818. } else if ($k == 1) {
  819. $dates['data'][] = UserBillAdmin::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
  820. }
  821. }
  822. $dates['name'] = $v;
  823. $dates['type'] = 'line';
  824. $series[] = $dates;
  825. unset($dates);
  826. }
  827. return ['time' => $times_list, 'series' => $series];
  828. }
  829. }
  830. public function gettime($time = '', $season = '')
  831. {
  832. if (!empty($time) && empty($season)) {
  833. $timestamp0 = strtotime($time);
  834. $timestamp24 = strtotime($time) + 86400;
  835. return [$timestamp0, $timestamp24];
  836. } else if (!empty($time) && !empty($season)) {
  837. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  838. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  839. return [$firstday, $lastday];
  840. }
  841. }
  842. /**
  843. * 会员等级首页
  844. */
  845. public function group()
  846. {
  847. return $this->fetch();
  848. }
  849. /**
  850. * 会员详情
  851. */
  852. public function see($uid = '')
  853. {
  854. $this->assign([
  855. 'uid' => $uid,
  856. 'userinfo' => UserModel::getUserDetailed($uid),
  857. 'is_layui' => true,
  858. 'headerList' => UserModel::getHeaderList($uid),
  859. 'count' => UserModel::getCountInfo($uid),
  860. ]);
  861. return $this->fetch();
  862. }
  863. /*
  864. * 获取某个用户的推广下线
  865. * */
  866. public function getSpreadList($uid, $page = 1, $limit = 20)
  867. {
  868. return Json::successful(UserModel::getSpreadList($uid, (int)$page, (int)$limit));
  869. }
  870. /**
  871. * 获取某用户的订单列表
  872. */
  873. public function getOneorderList($uid, $page = 1, $limit = 20)
  874. {
  875. return Json::successful(StoreOrder::getOneorderList(compact('uid', 'page', 'limit')));
  876. }
  877. /**
  878. * 获取某用户的积分列表
  879. */
  880. public function getOneIntegralList($uid, $page = 1, $limit = 20)
  881. {
  882. return Json::successful(UserBillAdmin::getOneIntegralList(compact('uid', 'page', 'limit')));
  883. }
  884. /**
  885. * 获取某用户的积分列表
  886. */
  887. public function getOneSignList($uid, $page = 1, $limit = 20)
  888. {
  889. return Json::successful(UserBillAdmin::getOneSignList(compact('uid', 'page', 'limit')));
  890. }
  891. /**
  892. * 获取某用户的持有优惠劵
  893. */
  894. public function getOneCouponsList($uid, $page = 1, $limit = 20)
  895. {
  896. return Json::successful(StoreCouponUser::getOneCouponsList(compact('uid', 'page', 'limit')));
  897. }
  898. /**
  899. * 获取某用户的余额变动记录
  900. */
  901. public function getOneBalanceChangList($uid, $page = 1, $limit = 20)
  902. {
  903. return Json::successful(UserBillAdmin::getOneBalanceChangList(compact('uid', 'page', 'limit')));
  904. }
  905. public function set_ratio($uid)
  906. {
  907. $this->assign('uid', $uid);
  908. return $this->fetch();
  909. }
  910. public function set_service($uid)
  911. {
  912. $this->assign('uid', $uid);
  913. return $this->fetch();
  914. }
  915. public function machineList($uid)
  916. {
  917. $page = $this->request->get('page', 1);
  918. $limit = $this->request->get('limit', 10);
  919. Json::successlayui(MiningMachine::getList((int)$page, (int)$limit, ['uid' => $uid]));
  920. }
  921. public function edit_ratio($uid, $mid)
  922. {
  923. if (!$uid) $this->failed('数据不存在');
  924. $user = UserModel::get($uid);
  925. if (!$user) $this->failed('数据不存在!');
  926. if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
  927. $f = array();
  928. $f[] = Form::number('ratio', '比率', UserMiningRatio::where('uid', $uid)->where('mid', $mid)->value('ratio') ?: 0);
  929. $form = Form::make_post_form('佣金比率', $f, Url::buildUrl('update_ratio', array('uid' => $uid, 'mid' => $mid)), 5);
  930. $this->assign(compact('form'));
  931. return $this->fetch('public/form-builder');
  932. }
  933. public function edit_service($uid, $mid)
  934. {
  935. if (!$uid) $this->failed('数据不存在');
  936. $user = UserModel::get($uid);
  937. if (!$user) $this->failed('数据不存在!');
  938. $m = MiningMachine::get($mid);
  939. if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
  940. $f = array();
  941. $f[] = Form::number('ratio', '比率', UserMiningService::where('uid', $uid)->where('mid', $mid)->value('ratio') ?: $m['service_ratio']);
  942. $form = Form::make_post_form('服务费比率', $f, Url::buildUrl('update_service', array('uid' => $uid, 'mid' => $mid)), 5);
  943. $this->assign(compact('form'));
  944. return $this->fetch('public/form-builder');
  945. }
  946. public function update_ratio($uid, $mid)
  947. {
  948. if (!$uid) Json::fail('数据不存在');
  949. $user = UserModel::get($uid);
  950. if (!$user) Json::fail('数据不存在!');
  951. if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
  952. $ratio = $this->request->post('ratio', 0);
  953. $user = \app\models\user\User::getUserInfo($uid);
  954. if ($user['spread_uid'])
  955. $self = UserMiningRatio::where('mid', $mid)->where('uid', $user['spread_uid'])->find();
  956. if ($user['spread_uid'] && (!isset($self['ratio']) || $ratio > $self['ratio'])) {
  957. //var_dump(1111);
  958. Json::fail('佣金比例不能超过上级');
  959. }
  960. $uids = \app\models\user\User::where('spread_uid', $user['uid'])->column('uid');
  961. $user2 = UserMiningRatio::where('mid', $mid)->where('uid', 'in', $uids)->max('ratio');
  962. if ($ratio < $user2) {
  963. Json::fail('佣金比例不能低于目标下级');
  964. }
  965. if (UserMiningRatio::where('mid', $mid)->where('uid', $uid)->find()) {
  966. $res = UserMiningRatio::where('mid', $mid)->where('uid', $uid)
  967. ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => 1]);
  968. } else {
  969. $res = UserMiningRatio::create([
  970. 'uid' => $uid,
  971. 'mid' => $mid,
  972. 'ratio' => $ratio,
  973. 'add_time' => time(),
  974. 'update_time' => time(),
  975. 'update_uid' => 1,
  976. ]);
  977. }
  978. if ($res) {
  979. Json::success('修改成功');
  980. } else {
  981. Json::fail('修改失败或者您没有修改什么');
  982. }
  983. }
  984. public function update_service($uid, $mid)
  985. {
  986. $user = \app\models\user\User::getUserInfo($uid);
  987. $ratio = $this->request->post('ratio', 0);
  988. if (!$mid || !MiningMachine::get($mid)) Json::fail('矿机不合法');
  989. $self = UserMiningService::where('mid', $mid)->where('uid', $user['spread_uid'])->find();
  990. if ($user['spread_uid'] && (!isset($self['ratio']) || $ratio < $self['ratio'])) {
  991. Json::fail('服务费比例不能低于目标上级');
  992. }
  993. $uids = \app\models\user\User::where('spread_uid', $user['uid'])->column('uid');
  994. $user2 = UserMiningService::where('mid', $mid)->where('uid', 'in', $uids)->min('ratio');
  995. if ($user2 > 0 && $ratio > $user2) {
  996. Json::fail('服务费比例不能高于目标下级');
  997. }
  998. if ($ratio > MiningMachine::where('id', $mid)->value('service_ratio')) {
  999. Json::fail('服务费比例不能高于系统设置');
  1000. }
  1001. if (UserMiningService::where('mid', $mid)->where('uid', $uid)->find()) {
  1002. $res = UserMiningService::where('mid', $mid)->where('uid', $uid)
  1003. ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => 1]);
  1004. } else {
  1005. $res = UserMiningService::create([
  1006. 'uid' => $uid,
  1007. 'mid' => $mid,
  1008. 'ratio' => $ratio,
  1009. 'add_time' => time(),
  1010. 'update_time' => time(),
  1011. 'update_uid' => 1,
  1012. ]);
  1013. }
  1014. if ($res) {
  1015. Json::success('修改成功');
  1016. } else {
  1017. Json::fail('修改失败或者您没有修改什么');
  1018. }
  1019. }
  1020. }