1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/11
- */
- namespace app\admin\controller\user;
- use app\admin\controller\AuthController;
- use app\models\mining\MiningMachine;
- use app\models\mining\UserMiningRatio;
- use app\models\mining\UserMiningService;
- use app\models\system\Message;
- use app\models\user\UserMoney;
- use crmeb\repositories\UserRepository;
- use crmeb\traits\CurdControllerTrait;
- use phpseclib\Crypt\Base;
- use think\facade\Route as Url;
- use crmeb\basic\BaseModel;
- use app\models\user\UserLevel as Level;
- use app\admin\model\order\StoreOrder;
- use app\admin\model\wechat\WechatMessage;
- use app\admin\model\store\{StoreVisit, StoreCouponUser};
- use app\admin\model\system\{SystemStore, SystemUserLevel, SystemUserTask};
- use crmeb\services\{CacheService, FormBuilder as Form, UtilService as Util, JsonService as Json};
- use app\admin\model\user\{User as UserModel, UserBill as UserBillAdmin, UserLevel, UserGroup, UserTaskFinish};
- /**
- * 用户管理控制器
- * Class User
- * @package app\admin\controller\user
- */
- class User extends AuthController
- {
- use CurdControllerTrait;
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $level = SystemUserLevel::where('is_del', 0)->where('is_show', 1)->order('grade asc')->field(['id', 'name'])->select();
- $group = UserGroup::select();
- $this->assign(compact('group'));
- $this->assign(compact('level'));
- $this->assign('count_user', UserModel::getcount());
- return $this->fetch();
- }
- /**
- * 设置分组
- * @param int $uid
- */
- public function set_group($uid = 0)
- {
- if (!$uid) return $this->failed('缺少参数');
- $userGroup = UserGroup::select();
- $field[] = Form::select('group_id', '会员分组')->setOptions(function () use ($userGroup) {
- $menus = [];
- foreach ($userGroup as $menu) {
- $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
- }
- return $menus;
- })->filterable(1);
- $form = Form::make_post_form('设置分组', $field, Url::buildUrl('save_set_group', ['uid' => $uid]), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function save_set_group($uid = 0)
- {
- if (!$uid) return Json::fail('缺少参数');
- list($group_id) = Util::postMore([
- ['group_id', 0],
- ], $this->request, true);
- $uids = explode(',', $uid);
- $res = UserModel::whereIn('uid', $uids)->update(['group_id' => $group_id]);
- if ($res) {
- return Json::successful('设置成功');
- } else {
- return Json::successful('设置失败');
- }
- }
- /**
- * 赠送会员等级
- * @param int $uid
- * @return string|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function give_level($uid = 0)
- {
- if (!$uid) return $this->failed('缺少参数');
- $level = Level::getUserLevel($uid);
- //获取当前会员等级
- if ($level === false)
- $grade = 0;
- else
- $grade = Level::getUserLevelInfo($level, 'grade');
- //查询高于当前会员的所有会员等级
- $systemLevelList = SystemUserLevel::where('grade', '>', $grade)->where(['is_show' => 1, 'is_del' => 0])->field(['name', 'id'])->select();
- $field[] = Form::select('level_id', '会员等级')->setOptions(function () use ($systemLevelList) {
- $menus = [];
- foreach ($systemLevelList as $menu) {
- $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
- }
- return $menus;
- })->filterable(1);
- $form = Form::make_post_form('赠送会员', $field, Url::buildUrl('save_give_level', ['uid' => $uid]), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- /**
- * 赠送会员等级
- * @param int $uid
- * @return string|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function give_partner($uid = 0)
- {
- if (!$uid) return $this->failed('缺少参数');
- $user = UserModel::get($uid);
- //获取当前会员等级
- if ($user === false)
- return $this->failed('缺少参数');
- else
- $grade = $user['partner_level'];
- //查询高于当前会员的所有会员等级
- $levels = sys_data('partner_level');
- $field[] = Form::select('partner_level', '股东等级')->setOptions(function () use ($levels, $grade) {
- $menus = [];
- foreach ($levels as $menu) {
- if ($menu['value'] > $grade)
- $menus[] = ['value' => $menu['value'], 'label' => $menu['name']];
- }
- return $menus;
- })->filterable(1);
- $form = Form::make_post_form('赠送股东', $field, Url::buildUrl('save_give_partner', ['uid' => $uid]), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function save_give_partner($uid)
- {
- $data = Util::postMore([
- ['partner_level', 0],
- ]);
- if (!$uid) return $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) return Json::fail('数据不存在!');
- BaseModel::beginTrans();
- $edit['partner_level'] = $data['partner_level'];
- $levels = sys_data('partner_level');
- $level_name = null;
- foreach ($levels as $v) {
- if ($v['value'] == $data['partner_level']) {
- $level_name = $v['name'];
- }
- }
- if ($level_name === null) {
- BaseModel::rollbackTrans();
- return Json::fail('等级不存在!');
- }
- if ($edit) $res3 = UserModel::edit($edit, $uid) && Message::addMessage($uid, '恭喜你,你已升级至' . $level_name . '!', 198);
- else $res3 = true;
- BaseModel::checkTrans($res3);
- if ($res3) return Json::successful('修改成功!');
- else return Json::fail('修改失败');
- }
- public function edit_other($uid)
- {
- if (!$uid) return $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) return Json::fail('数据不存在!');
- $f = array();
- $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
- $f[] = Form::number('money', '余额')->min(0);
- $money_type = init_money_type();
- // var_dump($money_type);
- foreach ($money_type as $k => $v) {
- $f[] = Form::radio($k . '_status', '修改' . $v, 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
- $f[] = Form::number($k, $v)->min(0);
- }
- $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
- $f[] = Form::number('integration', '积分')->min(0);
- $f[] = Form::input('admin_mark', '后台备注');
- $form = Form::make_post_form('修改其他', $f, Url::buildUrl('update_other', array('uid' => $uid)));
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function update_other($uid = 0)
- {
- if (CacheService::get('last_set', 0) > time() - 10)
- Json::fail('10秒内只可请求一次,请稍后再试。');
- CacheService::set('last_set', time());
- $data = Util::postMore([
- ['money_status', 0],
- ['money', 0],
- ['integration_status', 0],
- ['integration', 0],
- ['admin_mark', ''],
- ], $this->request);
- $other_money = $this->request->param();
- if (!$uid) return $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) return Json::fail('数据不存在!');
- BaseModel::beginTrans();
- $res1 = false;
- $res2 = false;
- $edit = array();
- if ($data['money_status'] && $data['money']) {//余额增加或者减少
- if ($data['money_status'] == 1) {//增加
- $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
- $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额', 1, $data['admin_mark']);
- try {
- UserRepository::adminAddMoney($user, $data['money']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- } else if ($data['money_status'] == 2) {//减少
- $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
- $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额', 1, $data['admin_mark']);
- try {
- UserRepository::adminSubMoney($user, $data['money']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- }
- } else {
- $res1 = true;
- }
- if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
- if ($data['integration_status'] == 1) {//增加
- $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
- $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分', 1, $data['admin_mark']);
- try {
- UserRepository::adminAddIntegral($user, $data['integration']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- } else if ($data['integration_status'] == 2) {//减少
- $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
- $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分', 1, $data['admin_mark']);
- try {
- UserRepository::adminSubIntegral($user, $data['integration']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- }
- } else {
- $res2 = true;
- }
- if ($edit) $res3 = UserModel::edit($edit, $uid);
- else $res3 = true;
- //其他币
- $res_other = true;
- $money_type = init_money_type();
- foreach ($money_type as $k => $v) {
- if ($other_money[$k . '_status'] && $other_money[$k]) {//积分增加或者减少
- if ($other_money[$k . '_status'] == 1) {//增加
- $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']);
- } else if ($other_money[$k . '_status'] == 2) {//减少
- if (UserMoney::initialUserMoney($user['uid'], $k)['money'] < $other_money[$k])
- $other_money[$k] = UserMoney::initialUserMoney($user['uid'], $k)['money'];
- $res_other = $res_other && UserMoney::expendMoney($user['uid'], $k, $other_money[$k], 'system_sub', '系统扣除', '系统扣除了' . floatval($other_money[$k]) . $v, 0, true, $data['admin_mark']);
- }
- }
- }
- if ($res1 && $res2 && $res3 && $res_other) $res = true;
- else $res = false;
- BaseModel::checkTrans($res);
- if ($res) return Json::successful('修改成功!');
- else return Json::fail('修改失败');
- }
- /*
- * 赠送会员等级
- * @paran int $uid
- * @return json
- * */
- public function save_give_level($uid = 0)
- {
- if (!$uid) return Json::fail('缺少参数');
- list($level_id) = Util::postMore([
- ['level_id', 0],
- ], $this->request, true);
- //查询当前选择的会员等级
- $systemLevel = SystemUserLevel::where(['is_show' => 1, 'is_del' => 0, 'id' => $level_id])->find();
- if (!$systemLevel) return Json::fail('您选择赠送的会员等级不存在!');
- //检查是否拥有此会员等级
- $level = UserLevel::where(['uid' => $uid, 'level_id' => $level_id, 'is_del' => 0])->field('valid_time,is_forever')->find();
- if ($level) if (!(!$level['is_forever'] && time() < $level['valid_time'])) return Json::fail('此用户已有该会员等级,无法再次赠送');
- //设置会员过期时间
- $add_valid_time = (int)$systemLevel->valid_date * 86400;
- UserModel::commitTrans();
- try {
- //保存会员信息
- $res = UserLevel::create([
- 'is_forever' => $systemLevel->is_forever,
- 'status' => 1,
- 'is_del' => 0,
- 'grade' => $systemLevel->grade,
- 'uid' => $uid,
- 'add_time' => time(),
- 'level_id' => $level_id,
- 'discount' => $systemLevel->discount,
- 'valid_time' => $systemLevel->is_forever ? $add_valid_time + time() : 0,
- 'mark' => '尊敬的用户【' . UserModel::where('uid', $uid)->value('nickname') . '】在' . date('Y-m-d H:i:s', time()) . '赠送会员等级成为' . $systemLevel['name'] . '会员',
- ]);
- //提取等级任务并记录完成情况
- $levelIds = [$level_id];
- $lowGradeLevelIds = SystemUserLevel::where('grade', '<', $systemLevel->grade)->where(['is_show' => 1, 'is_del' => 0])->column('id', 'id');
- if (count($lowGradeLevelIds)) $levelIds = array_merge($levelIds, $lowGradeLevelIds);
- $taskIds = SystemUserTask::where('level_id', 'in', $levelIds)->column('id', 'id');
- $inserValue = [];
- foreach ($taskIds as $id) {
- $inserValue[] = ['uid' => $uid, 'task_id' => $id, 'status' => 1, 'add_time' => time()];
- }
- $res = $res && UserModel::where('uid', $uid)->update(['level' => $level_id]);
- if ($inserValue) $res && UserTaskFinish::insertAll($inserValue);
- if ($res) {
- UserModel::commitTrans();
- return Json::successful('赠送成功');
- } else {
- UserModel::rollbackTrans();
- return Json::fail('赠送失败');
- }
- } catch (\Exception $e) {
- UserModel::rollbackTrans();
- return Json::fail('赠送失败' . $e->getMessage());
- }
- }
- /*
- * 清除会员等级
- * @param int $uid
- * @return json
- * */
- public function del_level($uid = 0)
- {
- if (!$uid) return Json::fail('缺少参数');
- if (UserLevel::cleanUpLevel($uid))
- return Json::successful('清除成功');
- else
- return Json::fail('清除失败');
- }
- /**
- * 修改user表状态
- *
- * @return json
- */
- public function set_status($status = '', $uid = 0, $is_echo = 0)
- {
- if ($is_echo == 0) {
- if ($status == '' || $uid == 0) return Json::fail('参数错误');
- UserModel::where(['uid' => $uid])->update(['status' => $status]);
- } else {
- $uids = Util::postMore([
- ['uids', []]
- ]);
- UserModel::destrSyatus($uids['uids'], $status);
- }
- return Json::successful($status == 0 ? '禁用成功' : '解禁成功');
- }
- /**
- * 获取user表
- *
- * @return json
- */
- public function get_user_list()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['nickname', ''],
- ['status', ''],
- ['pay_count', ''],
- ['is_promoter', ''],
- ['order', ''],
- ['data', ''],
- ['user_type', ''],
- ['country', ''],
- ['province', ''],
- ['city', ''],
- ['user_time_type', ''],
- ['user_time', ''],
- ['sex', ''],
- ['level', ''],
- ['group_id', ''],
- ]);
- return Json::successlayui(UserModel::getUserList($where));
- }
- /**
- * 编辑模板消息
- * @param $id
- * @return mixed|\think\response\Json|void
- */
- public function edit($uid)
- {
- if (!$uid) return $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) return Json::fail('数据不存在!');
- $f = array();
- $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(1);
- $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
- $f[] = Form::text('account', '账号', $user->getData('account'))->disabled(1);
- $f[] = Form::text('phone', '手机号', $user->getData('phone'));
- $f[] = Form::text('email', '邮箱', $user->getData('email'));
- // $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : 0);
- $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
- $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
- // $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
- $store_list = SystemStore::verificWhere()->field('id as value,name as label')->select();
- if (count($store_list)) {
- $store_list = $store_list->toArray();
- $store_list = array_merge([['value' => 0, 'label' => '否']], $store_list);
- $f[] = Form::select('store_id', '供货商', (string)$user->getData('store_id'))->setOptions($store_list);
- }
- $f[] = Form::radio('area_admin', '区域代理', $user->getData('area_admin'))->options([['value' => 0, 'label' => '无'], ['value' => 1, 'label' => '区代'], ['value' => 2, 'label' => '市代'], ['value' => 3, 'label' => '省代']]);
- $f[] = Form::cityArea('area_address', '区域', $user->getData('area_province'), $user->getData('area_city'), $user->getData('area_district'));
- // $f[] = Form::radio('is_point', '节点', $user->getData('is_point'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
- $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
- $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('update', array('uid' => $uid)), 5);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function update($uid)
- {
- $data = Util::postMore([
- ['money_status', 0],
- ['is_promoter', 1],
- ['real_name', ''],
- ['phone', ''],
- ['email', ''],
- // ['phone', 0],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['money', 0],
- ['integration_status', 0],
- ['integration', 0],
- ['status', 0],
- ['is_point', 0],
- ['area_admin'],
- ['store_id'],
- ]);
- $data['area_address'] = $this->request->post('area_address/a');
- if (!$uid) return $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) return Json::fail('数据不存在!');
- BaseModel::beginTrans();
- $res1 = false;
- $res2 = false;
- $edit = array();
- if ($data['money_status'] && $data['money']) {//余额增加或者减少
- if ($data['money_status'] == 1) {//增加
- $edit['now_money'] = bcadd($user['now_money'], $data['money'], 2);
- $res1 = UserBillAdmin::income('系统增加余额', $user['uid'], 'now_money', 'system_add', $data['money'], $this->adminId, $edit['now_money'], '系统增加了' . floatval($data['money']) . '余额');
- try {
- UserRepository::adminAddMoney($user, $data['money']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- } else if ($data['money_status'] == 2) {//减少
- $edit['now_money'] = bcsub($user['now_money'], $data['money'], 2);
- $res1 = UserBillAdmin::expend('系统减少余额', $user['uid'], 'now_money', 'system_sub', $data['money'], $this->adminId, $edit['now_money'], '系统扣除了' . floatval($data['money']) . '余额');
- try {
- UserRepository::adminSubMoney($user, $data['money']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- }
- } else {
- $res1 = true;
- }
- if ($data['integration_status'] && $data['integration']) {//积分增加或者减少
- if ($data['integration_status'] == 1) {//增加
- $edit['integral'] = bcadd($user['integral'], $data['integration'], 2);
- $res2 = UserBillAdmin::income('系统增加积分', $user['uid'], 'integral', 'system_add', $data['integration'], $this->adminId, $edit['integral'], '系统增加了' . floatval($data['integration']) . '积分');
- try {
- UserRepository::adminAddIntegral($user, $data['integration']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- } else if ($data['integration_status'] == 2) {//减少
- $edit['integral'] = bcsub($user['integral'], $data['integration'], 2);
- $res2 = UserBillAdmin::expend('系统减少积分', $user['uid'], 'integral', 'system_sub', $data['integration'], $this->adminId, $edit['integral'], '系统扣除了' . floatval($data['integration']) . '积分');
- try {
- UserRepository::adminSubIntegral($user, $data['integration']);
- } catch (\Exception $e) {
- BaseModel::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- }
- } else {
- $res2 = true;
- }
- $edit['status'] = $data['status'];
- $edit['real_name'] = $data['real_name'];
- // if (UserModel::where('uid', '<>', $uid)->where('site_id', $user['site_id'])->where('account', $data['phone'])->find())
- // return Json::fail('手机号码已被使用');
- // else {
- $edit['phone'] = $data['phone'];
- $edit['email'] = $data['email'];
- // }
- $edit['card_id'] = $data['card_id'];
- $edit['store_id'] = $data['store_id'];
- $edit['birthday'] = strtotime($data['birthday']);
- $edit['mark'] = $data['mark'];
- $edit['is_promoter'] = $data['is_promoter'];
- $edit['area_admin'] = $data['area_admin'];
- if ($edit['area_admin'] == 1 && (!$data['area_address'][0] || !$data['area_address'][1] || !$data['area_address'][2])) {
- return Json::fail('请选择代理地区');
- }
- if ($edit['area_admin'] == 2 && (!$data['area_address'][0] || !$data['area_address'][1])) {
- return Json::fail('请选择代理地区');
- }
- if ($edit['area_admin'] == 3 && (!$data['area_address'][0])) {
- return Json::fail('请选择代理地区');
- }
- if ($edit['area_admin'] == 0) {
- $edit['area_province'] = '';
- $edit['area_city'] = '';
- $edit['area_district'] = '';
- } else {
- $edit['area_province'] = $data['area_address'][0];
- $edit['area_city'] = $data['area_address'][1];
- $edit['area_district'] = $data['area_address'][2];
- }
- if ($edit) $res3 = UserModel::edit($edit, $uid);
- else $res3 = true;
- if ($res1 && $res2 && $res3) $res = true;
- else $res = false;
- BaseModel::checkTrans($res);
- if ($res) return Json::successful('修改成功!');
- else return Json::fail('修改失败');
- }
- /**
- * 用户图表
- * @return mixed
- */
- public function user_analysis()
- {
- $where = Util::getMore([
- ['nickname', ''],
- ['status', ''],
- ['is_promoter', ''],
- ['date', ''],
- ['user_type', ''],
- ['export', 0]
- ], $this->request);
- $user_count = UserModel::consume($where, '', true);
- //头部信息
- $header = [
- [
- 'name' => '新增用户',
- 'class' => 'fa-line-chart',
- 'value' => $user_count,
- 'color' => 'red'
- ],
- [
- 'name' => '用户留存',
- 'class' => 'fa-area-chart',
- 'value' => $this->gethreaderValue(UserModel::consume($where, '', true), $where) . '%',
- 'color' => 'lazur'
- ],
- [
- 'name' => '新增用户总消费',
- 'class' => 'fa-bar-chart',
- 'value' => '¥' . UserModel::consume($where),
- 'color' => 'navy'
- ],
- [
- 'name' => '用户活跃度',
- 'class' => 'fa-pie-chart',
- 'value' => $this->gethreaderValue(UserModel::consume($where, '', true)) . '%',
- 'color' => 'yellow'
- ],
- ];
- $name = ['新增用户', '用户消费'];
- $dates = $this->get_user_index($where, $name);
- $user_index = ['name' => json_encode($name), 'date' => json_encode($dates['time']), 'series' => json_encode($dates['series'])];
- //用户浏览分析
- $view = StoreVisit::getVisit($where['date'], ['', 'warning', 'info', 'danger']);
- $view_v1 = WechatMessage::getViweList($where['date'], ['', 'warning', 'info', 'danger']);
- $view = array_merge($view, $view_v1);
- $view_v2 = [];
- foreach ($view as $val) {
- $view_v2['color'][] = '#' . rand(100000, 339899);
- $view_v2['name'][] = $val['name'];
- $view_v2['value'][] = $val['value'];
- }
- $view = $view_v2;
- //消费会员排行用户分析
- $user_null = UserModel::getUserSpend($where['date']);
- //消费数据
- $now_number = UserModel::getUserSpend($where['date'], true);
- list($paren_number, $title) = UserModel::getPostNumber($where['date']);
- if ($paren_number == 0) {
- $rightTitle = [
- 'number' => $now_number > 0 ? $now_number : 0,
- 'icon' => 'fa-level-up',
- 'title' => $title
- ];
- } else {
- $number = (float)bcsub($now_number, $paren_number, 4);
- if ($now_number == 0) {
- $icon = 'fa-level-down';
- } else {
- $icon = $now_number > $paren_number ? 'fa-level-up' : 'fa-level-down';
- }
- $rightTitle = ['number' => $number, 'icon' => $icon, 'title' => $title];
- }
- unset($title, $paren_number, $now_number);
- list($paren_user_count, $title) = UserModel::getPostNumber($where['date'], true, 'add_time', '');
- if ($paren_user_count == 0) {
- $count = $user_count == 0 ? 0 : $user_count;
- $icon = $user_count == 0 ? 'fa-level-down' : 'fa-level-up';
- } else {
- $count = (float)bcsub($user_count, $paren_user_count, 4);
- $icon = $user_count < $paren_user_count ? 'fa-level-down' : 'fa-level-up';
- }
- $leftTitle = [
- 'count' => $count,
- 'icon' => $icon,
- 'title' => $title
- ];
- unset($count, $icon, $title);
- $consume = [
- 'title' => '消费金额为¥' . UserModel::consume($where),
- 'series' => UserModel::consume($where, 'xiaofei'),
- 'rightTitle' => $rightTitle,
- 'leftTitle' => $leftTitle,
- ];
- $form = UserModel::consume($where, 'form');
- $grouping = UserModel::consume($where, 'grouping');
- $this->assign(compact('header', 'user_index', 'view', 'user_null', 'consume', 'form', 'grouping', 'where'));
- return $this->fetch();
- }
- public function gethreaderValue($chart, $where = [])
- {
- if ($where) {
- switch ($where['date']) {
- case null:
- case 'today':
- case 'week':
- case 'year':
- if ($where['date'] == null) {
- $where['date'] = 'month';
- }
- $sum_user = UserModel::whereTime('add_time', $where['date'])->count();
- if ($sum_user == 0) return 0;
- $counts = bcdiv($chart, $sum_user, 4) * 100;
- return $counts;
- break;
- case 'quarter':
- $quarter = UserModel::getMonth('n');
- $quarter[0] = strtotime($quarter[0]);
- $quarter[1] = strtotime($quarter[1]);
- $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
- if ($sum_user == 0) return 0;
- $counts = bcdiv($chart, $sum_user, 4) * 100;
- return $counts;
- default:
- //自定义时间
- $quarter = explode('-', $where['date']);
- $quarter[0] = strtotime($quarter[0]);
- $quarter[1] = strtotime($quarter[1]);
- $sum_user = UserModel::where('add_time', 'between', $quarter)->count();
- if ($sum_user == 0) return 0;
- $counts = bcdiv($chart, $sum_user, 4) * 100;
- return $counts;
- break;
- }
- } else {
- $num = UserModel::count();
- $chart = $num != 0 ? bcdiv($chart, $num, 5) * 100 : 0;
- return $chart;
- }
- }
- public function get_user_index($where, $name)
- {
- switch ($where['date']) {
- case null:
- $days = date("t", strtotime(date('Y-m', time())));
- $dates = [];
- $series = [];
- $times_list = [];
- foreach ($name as $key => $val) {
- for ($i = 1; $i <= $days; $i++) {
- if (!in_array($i . '号', $times_list)) {
- array_push($times_list, $i . '号');
- }
- $time = $this->gettime(date("Y-m", time()) . '-' . $i);
- if ($key == 0) {
- $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
- } else if ($key == 1) {
- $dates['data'][] = UserModel::consume(true, $time);
- }
- }
- $dates['name'] = $val;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- case 'today':
- $dates = [];
- $series = [];
- $times_list = [];
- foreach ($name as $key => $val) {
- for ($i = 0; $i <= 24; $i++) {
- $strtitle = $i . '点';
- if (!in_array($strtitle, $times_list)) {
- array_push($times_list, $strtitle);
- }
- $time = $this->gettime(date("Y-m-d ", time()) . $i);
- if ($key == 0) {
- $dates['data'][] = UserModel::where('add_time', 'between', $time)->count();
- } else if ($key == 1) {
- $dates['data'][] = UserModel::consume(true, $time);
- }
- }
- $dates['name'] = $val;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- case "week":
- $dates = [];
- $series = [];
- $times_list = [];
- foreach ($name as $key => $val) {
- for ($i = 0; $i <= 6; $i++) {
- if (!in_array('星期' . ($i + 1), $times_list)) {
- array_push($times_list, '星期' . ($i + 1));
- }
- $time = UserModel::getMonth('h', $i);
- if ($key == 0) {
- $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($time[0]), strtotime($time[1])])->count();
- } else if ($key == 1) {
- $dates['data'][] = UserModel::consume(true, [strtotime($time[0]), strtotime($time[1])]);
- }
- }
- $dates['name'] = $val;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- case 'year':
- $dates = [];
- $series = [];
- $times_list = [];
- $year = date('Y');
- foreach ($name as $key => $val) {
- for ($i = 1; $i <= 12; $i++) {
- if (!in_array($i . '月', $times_list)) {
- array_push($times_list, $i . '月');
- }
- $t = strtotime($year . '-' . $i . '-01');
- $arr = explode('/', date('Y-m-01', $t) . '/' . date('Y-m-', $t) . date('t', $t));
- if ($key == 0) {
- $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
- } else if ($key == 1) {
- $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
- }
- }
- $dates['name'] = $val;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- case 'quarter':
- $dates = [];
- $series = [];
- $times_list = [];
- foreach ($name as $key => $val) {
- for ($i = 1; $i <= 4; $i++) {
- $arr = $this->gettime('quarter', $i);
- if (!in_array(implode('--', $arr) . '季度', $times_list)) {
- array_push($times_list, implode('--', $arr) . '季度');
- }
- if ($key == 0) {
- $dates['data'][] = UserModel::where('add_time', 'between', [strtotime($arr[0]), strtotime($arr[1])])->count();
- } else if ($key == 1) {
- $dates['data'][] = UserModel::consume(true, [strtotime($arr[0]), strtotime($arr[1])]);
- }
- }
- $dates['name'] = $val;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- default:
- $list = UserModel::consume($where, 'default');
- $dates = [];
- $series = [];
- $times_list = [];
- foreach ($name as $k => $v) {
- foreach ($list as $val) {
- $date = $val['add_time'];
- if (!in_array($date, $times_list)) {
- array_push($times_list, $date);
- }
- if ($k == 0) {
- $dates['data'][] = $val['num'];
- } else if ($k == 1) {
- $dates['data'][] = UserBillAdmin::where(['uid' => $val['uid'], 'type' => 'pay_product'])->sum('number');
- }
- }
- $dates['name'] = $v;
- $dates['type'] = 'line';
- $series[] = $dates;
- unset($dates);
- }
- return ['time' => $times_list, 'series' => $series];
- }
- }
- public function gettime($time = '', $season = '')
- {
- if (!empty($time) && empty($season)) {
- $timestamp0 = strtotime($time);
- $timestamp24 = strtotime($time) + 86400;
- return [$timestamp0, $timestamp24];
- } else if (!empty($time) && !empty($season)) {
- $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
- $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
- return [$firstday, $lastday];
- }
- }
- /**
- * 会员等级首页
- */
- public function group()
- {
- return $this->fetch();
- }
- /**
- * 会员详情
- */
- public function see($uid = '')
- {
- $this->assign([
- 'uid' => $uid,
- 'userinfo' => UserModel::getUserDetailed($uid),
- 'is_layui' => true,
- 'headerList' => UserModel::getHeaderList($uid),
- 'count' => UserModel::getCountInfo($uid),
- ]);
- return $this->fetch();
- }
- /*
- * 获取某个用户的推广下线
- * */
- public function getSpreadList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(UserModel::getSpreadList($uid, (int)$page, (int)$limit));
- }
- /**
- * 获取某用户的订单列表
- */
- public function getOneorderList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(StoreOrder::getOneorderList(compact('uid', 'page', 'limit')));
- }
- /**
- * 获取某用户的积分列表
- */
- public function getOneIntegralList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(UserBillAdmin::getOneIntegralList(compact('uid', 'page', 'limit')));
- }
- /**
- * 获取某用户的积分列表
- */
- public function getOneSignList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(UserBillAdmin::getOneSignList(compact('uid', 'page', 'limit')));
- }
- /**
- * 获取某用户的持有优惠劵
- */
- public function getOneCouponsList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(StoreCouponUser::getOneCouponsList(compact('uid', 'page', 'limit')));
- }
- /**
- * 获取某用户的余额变动记录
- */
- public function getOneBalanceChangList($uid, $page = 1, $limit = 20)
- {
- return Json::successful(UserBillAdmin::getOneBalanceChangList(compact('uid', 'page', 'limit')));
- }
- public function set_ratio($uid)
- {
- $this->assign('uid', $uid);
- return $this->fetch();
- }
- public function set_service($uid)
- {
- $this->assign('uid', $uid);
- return $this->fetch();
- }
- public function machineList($uid)
- {
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 10);
- Json::successlayui(MiningMachine::getList((int)$page, (int)$limit, ['uid' => $uid]));
- }
- public function edit_ratio($uid, $mid)
- {
- if (!$uid) $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) $this->failed('数据不存在!');
- if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
- $f = array();
- $f[] = Form::number('ratio', '比率', UserMiningRatio::where('uid', $uid)->where('mid', $mid)->value('ratio') ?: 0);
- $form = Form::make_post_form('佣金比率', $f, Url::buildUrl('update_ratio', array('uid' => $uid, 'mid' => $mid)), 5);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function edit_service($uid, $mid)
- {
- if (!$uid) $this->failed('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) $this->failed('数据不存在!');
- $m = MiningMachine::get($mid);
- if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
- $f = array();
- $f[] = Form::number('ratio', '比率', UserMiningService::where('uid', $uid)->where('mid', $mid)->value('ratio') ?: $m['service_ratio']);
- $form = Form::make_post_form('服务费比率', $f, Url::buildUrl('update_service', array('uid' => $uid, 'mid' => $mid)), 5);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- public function update_ratio($uid, $mid)
- {
- if (!$uid) Json::fail('数据不存在');
- $user = UserModel::get($uid);
- if (!$user) Json::fail('数据不存在!');
- if (!$mid || !MiningMachine::get($mid)) $this->failed('矿机不合法');
- $ratio = $this->request->post('ratio', 0);
- $user = \app\models\user\User::getUserInfo($uid);
- if ($user['spread_uid'])
- $self = UserMiningRatio::where('mid', $mid)->where('uid', $user['spread_uid'])->find();
- if ($user['spread_uid'] && (!isset($self['ratio']) || $ratio > $self['ratio'])) {
- //var_dump(1111);
- Json::fail('佣金比例不能超过上级');
- }
- $uids = \app\models\user\User::where('spread_uid', $user['uid'])->column('uid');
- $user2 = UserMiningRatio::where('mid', $mid)->where('uid', 'in', $uids)->max('ratio');
- if ($ratio < $user2) {
- Json::fail('佣金比例不能低于目标下级');
- }
- if (UserMiningRatio::where('mid', $mid)->where('uid', $uid)->find()) {
- $res = UserMiningRatio::where('mid', $mid)->where('uid', $uid)
- ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => 1]);
- } else {
- $res = UserMiningRatio::create([
- 'uid' => $uid,
- 'mid' => $mid,
- 'ratio' => $ratio,
- 'add_time' => time(),
- 'update_time' => time(),
- 'update_uid' => 1,
- ]);
- }
- if ($res) {
- Json::success('修改成功');
- } else {
- Json::fail('修改失败或者您没有修改什么');
- }
- }
- public function update_service($uid, $mid)
- {
- $user = \app\models\user\User::getUserInfo($uid);
- $ratio = $this->request->post('ratio', 0);
- if (!$mid || !MiningMachine::get($mid)) Json::fail('矿机不合法');
- $self = UserMiningService::where('mid', $mid)->where('uid', $user['spread_uid'])->find();
- if ($user['spread_uid'] && (!isset($self['ratio']) || $ratio < $self['ratio'])) {
- Json::fail('服务费比例不能低于目标上级');
- }
- $uids = \app\models\user\User::where('spread_uid', $user['uid'])->column('uid');
- $user2 = UserMiningService::where('mid', $mid)->where('uid', 'in', $uids)->min('ratio');
- if ($user2 > 0 && $ratio > $user2) {
- Json::fail('服务费比例不能高于目标下级');
- }
- if ($ratio > MiningMachine::where('id', $mid)->value('service_ratio')) {
- Json::fail('服务费比例不能高于系统设置');
- }
- if (UserMiningService::where('mid', $mid)->where('uid', $uid)->find()) {
- $res = UserMiningService::where('mid', $mid)->where('uid', $uid)
- ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => 1]);
- } else {
- $res = UserMiningService::create([
- 'uid' => $uid,
- 'mid' => $mid,
- 'ratio' => $ratio,
- 'add_time' => time(),
- 'update_time' => time(),
- 'update_uid' => 1,
- ]);
- }
- if ($res) {
- Json::success('修改成功');
- } else {
- Json::fail('修改失败或者您没有修改什么');
- }
- }
- }
|