123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\controller\admin\v1\user;
- use app\jobs\BatchHandleJob;
- use app\services\other\queue\QueueServices;
- use app\services\product\product\StoreProductLogServices;
- use app\services\user\group\UserGroupServices;
- use app\services\user\label\UserLabelServices;
- use app\services\user\level\UserLevelServices;
- use app\services\user\UserBatchProcessServices;
- use app\services\user\UserServices;
- use app\controller\admin\AuthController;
- use app\services\user\UserSpreadServices;
- use app\services\user\UserWechatuserServices;
- use think\exception\ValidateException;
- use think\facade\App;
- class User extends AuthController
- {
- /**
- * user constructor.
- * @param App $app
- * @param UserServices $services
- */
- public function __construct(App $app, UserServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
- /**
- * 显示资源列表头部
- *
- * @return \think\Response
- */
- public function type_header()
- {
- $list = $this->services->typeHead();
- return $this->success(compact('list'));
- }
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $where = $this->request->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', 0], 0],
- [['group_id', 'd'], 0],
- [['label_id', 'd'], 0],
- ['now_money', 'normal'],
- ['field_key', ''],
- ['isMember', ''],
- ['label_ids', '']
- ]);
- if ($where['label_ids']) {
- $where['label_id'] = explode(',', $where['label_ids']);
- unset($where['label_ids']);
- }
- $where['user_time_type'] = $where['user_time_type'] == 'all' ? '' : $where['user_time_type'];
- return $this->success($this->services->index($where));
- }
- /**
- * 补充信息表单
- * @param $id
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function extendInfoForm($id)
- {
- return $this->success($this->services->extendInfoForm((int)$id));
- }
- /**
- * 保存用户补充信息
- * @param $id
- * @return mixed
- */
- public function saveExtendForm($id)
- {
- $data = $this->request->post();
- if (!$data) {
- return $this->fail('请提交要保存信息');
- }
- $this->services->saveExtendForm((int)$id, $data);
- return $this->success('保存成功');
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create()
- {
- return $this->success($this->services->saveForm());
- }
- /**
- * 保存新建的资源
- *
- * @param \think\Request $request
- * @return \think\Response
- */
- public function save()
- {
- $data = $this->request->postMore([
- ['is_promoter', 0],
- ['real_name', ''],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['status', 0],
- ['level', 0],
- ['phone', 0],
- ['addres', ''],
- ['label_id', []],
- ['group_id', 0],
- ['pwd', ''],
- ['true_pwd', ''],
- ['spread_open', 1],
- ['sex', 0],
- ['provincials', ''],
- ['province', 0],
- ['city', 0],
- ['area', 0],
- ['street', 0],
- ['extend_info', []],
- ]);
- $this->validate(['pwd' => $data['pwd']], \app\validate\admin\user\UserValidate::class);
- if ($data['phone']) {
- if (!check_phone($data['phone'])) {
- return $this->fail('手机号码格式不正确');
- }
- if ($this->services->count(['phone' => $data['phone']])) {
- return $this->fail('手机号已经存在不能添加相同的手机号用户');
- }
- $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
- }
- if ($data['card_id']) {
- try {
- if (!check_card($data['card_id'])) return $this->fail('请输入正确的身份证');
- } catch (\Throwable $e) {
- // return $this->fail('请输入正确的身份证');
- }
- }
- if ($data['birthday']) {
- if (strtotime($data['birthday']) > time()) return $this->fail('生日请选择今天之前日期');
- }
- if ($data['pwd']) {
- if (!$data['true_pwd']) {
- return $this->fail('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return $this->fail('两次输入的密码不一致');
- }
- if ($data['pwd'] == '123456') {
- return $this->fail('您设置的密码太过简单');
- }
- $data['pwd'] = md5($data['pwd']);
- } else {
- unset($data['pwd']);
- }
- unset($data['true_pwd']);
- $data['avatar'] = sys_config('h5_avatar');
- $data['adminId'] = $this->adminId;
- $data['user_type'] = 'h5';
- $labels = $data['label_id'];
- unset($data['label_id']);
- foreach ($labels as $k => $v) {
- if (!$v) {
- unset($labels[$k]);
- }
- }
- $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
- $data['add_time'] = time();
- $data['extend_info'] = $this->services->handelExtendInfo($data['extend_info']);
- $this->services->transaction(function () use ($data, $labels) {
- $res = true;
- $userInfo = $this->services->save($data);
- $res = $this->services->saveSetLabel([$userInfo->uid], $labels);
- if ($data['level']) {
- $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
- }
- if (!$res) {
- throw new ValidateException('保存添加用户失败');
- }
- event('user.register', [$this->services->get((int)$userInfo->uid), true, 0]);
- });
- event('user.create', $data);
- return $this->success('添加成功');
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function read($id)
- {
- if (is_string($id)) {
- $id = (int)$id;
- }
- return $this->success($this->services->read($id));
- }
- /**
- * 赠送会员等级
- * @param int $uid
- * @return mixed
- * */
- public function give_level($id)
- {
- if (!$id) return $this->fail('缺少参数');
- return $this->success($this->services->giveLevel((int)$id));
- }
- /**
- * 执行赠送会员等级
- * @param int $uid
- * @return mixed
- * */
- public function save_give_level($id)
- {
- if (!$id) return $this->fail('缺少参数');
- [$level_id] = $this->request->postMore([
- ['level_id', 0],
- ], true);
- return $this->success($this->services->saveGiveLevel((int)$id, (int)$level_id) ? '赠送成功' : '赠送失败');
- }
- /**
- * 赠送付费会员时长
- * @param int $uid
- * @return mixed
- * */
- public function give_level_time($id)
- {
- if (!$id) return $this->fail('缺少参数');
- return $this->success($this->services->giveLevelTime((int)$id));
- }
- /**
- * 执行赠送付费会员时长
- * @param int $uid
- * @return mixed
- * */
- public function save_give_level_time($id)
- {
- if (!$id) return $this->fail('缺少参数');
- [$days_status, $days] = $this->request->postMore([
- ['days_status', 1],
- ['days', 0],
- ], true);
- return $this->success($this->services->saveGiveLevelTime((int)$id, (int)$days, (int)$days_status) ? '赠送成功' : '赠送失败');
- }
- /**
- * 清除会员等级
- * @param int $uid
- * @return json
- */
- public function del_level($id)
- {
- if (!$id) return $this->fail('缺少参数');
- return $this->success($this->services->cleanUpLevel((int)$id) ? '清除成功' : '清除失败');
- }
- /**
- * 设置会员分组
- * @param $id
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function set_group()
- {
- [$uids, $all, $where] = $this->request->postMore([
- ['uids', []],
- ['all', 0],
- ['where', []],
- ], true);
- return $this->success($this->services->setGroup($uids, $all, $where));
- }
- /**
- * 保存会员分组
- * @param $id
- * @return mixed
- */
- public function save_set_group()
- {
- [$group_id, $uids, $all, $where] = $this->request->postMore([
- ['group_id', 0],
- ['uids', ''],
- ['all', 0],
- ['where', ""],
- ], true);
- if (!$uids && $all == 0) return $this->fail('缺少参数');
- if (!$group_id) return $this->fail('请选择分组');
- $type = 2;//代表设置用户标签
- if ($all == 0) {
- $uids = explode(',', $uids);
- $where = [];
- }
- if ($all == 1) {
- $where = $where ? json_decode($where, true) : [];
- /** @var UserWechatuserServices $userWechatUser */
- $userWechatUser = app()->make(UserWechatuserServices::class);
- $fields = 'u.uid';
- [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
- $uids = array_unique(array_column($list, 'uid'));
- $where = [];
- }
- /** @var UserGroupServices $userGroup */
- $userGroup = app()->make(UserGroupServices::class);
- if (!$userGroup->getGroup($group_id)) {
- return $this->fail('该分组不存在');
- }
- /** @var QueueServices $queueService */
- $queueService = app()->make(QueueServices::class);
- $queueService->setQueueData($where, 'uid', $uids, $type, $group_id);
- //加入队列
- BatchHandleJob::dispatch([$group_id, $type]);
- return $this->success('后台程序已执行用户分组任务!');
- }
- /**
- * 设置用户标签
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function set_label()
- {
- [$uids, $all, $where] = $this->request->postMore([
- ['uids', []],
- ['all', 0],
- ['where', ""],
- ], true);
- return $this->success($this->services->setLabel($uids, $all, $where));
- }
- /**
- * 保存用户标签
- * @return mixed
- */
- public function save_set_label()
- {
- [$lables, $uids, $all, $where] = $this->request->postMore([
- ['label_id', []],
- ['uids', ''],
- ['all', 0],
- ['where', ""],
- ], true);
- if (!$uids && $all == 0) return $this->fail('缺少参数');
- if (!$lables) return $this->fail('请选择标签');
- if ($all == 0) {
- $uids = is_array($uids) ? $uids : explode(',', $uids);
- $where = [];
- }
- if ($all == 1) {
- $where = $where ? (is_string($where) ? json_decode($where, true) : $where) : [];
- /** @var UserWechatuserServices $userWechatUser */
- $userWechatUser = app()->make(UserWechatuserServices::class);
- $fields = 'u.uid';
- [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
- $uids = array_unique(array_column($list, 'uid'));
- $where = [];
- }
- /** @var UserLabelServices $userLabelServices */
- $userLabelServices = app()->make(UserLabelServices::class);
- $count = $userLabelServices->getCount([['id', 'IN', $lables]]);
- if ($count != count($lables)) {
- return app('json')->fail('用户标签不存在或被删除');
- }
- $type = 3;//批量设置用户标签
- /** @var QueueServices $queueService */
- $queueService = app()->make(QueueServices::class);
- $queueService->setQueueData($where, 'uid', $uids, $type, $lables);
- //加入队列
- BatchHandleJob::dispatch([$lables, $type]);
- return $this->success('后台程序已执行批量设置用户标签任务!');
- }
- /**
- * 编辑其他
- * @param $id
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function edit_other($id)
- {
- if (!$id) return $this->fail('数据不存在');
- return $this->success($this->services->editOther((int)$id));
- }
- /**
- * 执行编辑其他
- * @param int $id
- * @return mixed
- */
- public function update_other($id)
- {
- $data = $this->request->postMore([
- ['money_status', 0],
- ['money', 0],
- ['integration_status', 0],
- ['integration', 0],
- ]);
- if (!$id) return $this->fail('数据不存在');
- $data['adminId'] = $this->adminId;
- $data['money'] = (string)$data['money'];
- $data['integration'] = (string)$data['integration'];
- $data['is_other'] = true;
- return $this->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
- }
- /**
- * 修改user表状态
- *
- * @return array
- */
- public function set_status($status, $id)
- {
- // if ($status == '' || $id == 0) return $this->fail('参数错误');
- // UserModel::where(['uid' => $id])->update(['status' => $status]);
- return $this->success($status == 0 ? '禁用成功' : '解禁成功');
- }
- /**
- * 编辑会员信息
- * @param $id
- * @return mixed|\think\response\Json|void
- */
- public function edit($id)
- {
- if (!$id) return $this->fail('数据不存在');
- return $this->success($this->services->edit($id));
- }
- public function update($id)
- {
- $data = $this->request->postMore([
- ['money_status', 0],
- ['is_promoter', -1],
- ['real_name', ''],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['money', 0],
- ['integration_status', 0],
- ['integration', 0],
- ['status', 0],
- ['award_switch', 0],
- ['level', 0],
- ['phone', 0],
- ['addres', ''],
- ['label_id', []],
- ['group_id', 0],
- ['pwd', ''],
- ['true_pwd'],
- ['spread_open', 1],
- ['sex', 0],
- ['provincials', ''],
- ['province', 0],
- ['city', 0],
- ['area', 0],
- ['street', 0],
- ['spread_uid', -1],
- ['area_admin', -1],
- ['is_shop', -1],
- ['reorder', -1],
- ['max_order_count', -1],
- ['area_admin_province', ''],
- ['area_admin_city', ''],
- ['area_admin_district', ''],
- ['area_provincials', ''],
- ['extend_info', []]
- ]);
- if ($data['phone']) {
- if (!check_phone($data['phone'])) return $this->fail('手机号码格式不正确');
- }
- if ($data['card_id']) {
- try {
- if (!check_card($data['card_id'])) return $this->fail('请输入正确的身份证');
- } catch (\Throwable $e) {
- // return $this->fail('请输入正确的身份证');
- }
- }
- if ($data['birthday']) {
- if (strtotime($data['birthday']) > time()) return $this->fail('生日请选择今天之前日期');
- }
- if ($data['pwd']) {
- if (!$data['true_pwd']) {
- return $this->fail('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return $this->fail('两次输入的密码不一致');
- }
- if ($data['pwd'] == '123456') {
- return $this->fail('您设置的密码太过简单');
- }
- $this->validate(['pwd' => $data['pwd']], \app\validate\admin\user\UserValidate::class);
- $data['pwd'] = md5($data['pwd']);
- } else {
- unset($data['pwd']);
- }
- $userInfo = $this->services->get($id);
- if (!$userInfo) {
- return $this->fail('用户不存在');
- }
- if (!in_array($data['spread_uid'], [0, -1])) {
- $spreadUid = $data['spread_uid'];
- if ($id == $spreadUid) {
- return $this->fail('上级推广人不能为自己');
- }
- if (!$this->services->count(['uid' => $spreadUid])) {
- return $this->fail('上级用户不存在');
- }
- $spreadInfo = $this->services->get($spreadUid);
- if ($spreadInfo->spread_uid == $id) {
- return $this->fail('上级推广人不能为自己下级');
- }
- }
- unset($data['true_pwd']);
- if (!$id) return $this->fail('数据不存在');
- $data['adminId'] = $this->adminId;
- $data['money'] = (string)$data['money'];
- $data['integration'] = (string)$data['integration'];
- if ($data['extend_info']) {
- $data['extend_info'] = $this->services->handelExtendInfo($data['extend_info']);
- }
- if ($data['area_admin'] >= 0) {
- if (!in_array($data['area_admin'], [0, 1, 2, 3])) {
- return $this->fail('请选择正确的代理等级');
- }
- if ($data['area_admin'] == 3) {
- if (!$data['area_admin_province'])
- return $this->fail('请选择代理省份');
- else {
- $data['area_admin_city'] = '';
- $data['area_admin_district'] = '';
- }
- }
- if ($data['area_admin'] == 2) {
- if (!$data['area_admin_province'] || !$data['area_admin_city'])
- return $this->fail('请选择代理省市');
- else {
- $data['area_admin_district'] = '';
- }
- }
- if ($data['area_admin'] == 1) {
- if (!$data['area_admin_province'] || !$data['area_admin_city'] || !$data['area_admin_district'])
- return $this->fail('请选择代理省市区');
- }
- if ($data['area_admin'] == 0) {
- $data['area_admin_province'] = '';
- $data['area_admin_city'] = '';
- $data['area_admin_district'] = '';
- }
- }
- $res = $this->services->updateInfo((int)$id, $data);
- if ($res) {
- $userInfo = $this->services->get($id);
- /** @var UserLevelServices $levelServices */
- $levelServices = app()->make(UserLevelServices::class);
- $levelServices->detection((int)$userInfo['uid']);
- $pass = [$userInfo['uid']];
- $spread = $this->services->getUserInfo($userInfo['spread_uid']);
- while ($spread && !in_array($spread['uid'], $pass)) {
- $levelServices->detection((int)$spread['uid']);
- $pass[] = $spread['uid'];
- $spread = $this->services->getUserInfo($spread['spread_uid']);
- }
- }
- return $this->success($res ? '修改成功' : '修改失败');
- }
- /**
- * 获取单个用户信息
- * @param $id 用户id
- * @return mixed
- */
- public function oneUserInfo($id)
- {
- $data = $this->request->getMore([
- ['type', ''],
- ]);
- $id = (int)$id;
- if ($data['type'] == '') return $this->fail('缺少参数');
- return $this->success($this->services->oneUserInfo($id, $data['type']));
- }
- /**
- * 同步微信粉丝用户
- * @return mixed
- */
- public function syncWechatUsers()
- {
- $this->services->syncWechatUsers();
- return $this->success('加入消息队列成功,正在异步执行中');
- }
- /**
- * 商品浏览记录
- * @param $id
- * @param StoreProductLogServices $services
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function visitList($id, StoreProductLogServices $services)
- {
- $where['uid'] = (int)$id;
- $where['type'] = 'visit';
- return app('json')->success($services->getList($where, 'product_id'));
- }
- /**
- * 获取推广人记录
- * @param $id
- * @param UserSpreadServices $services
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function spreadList($id, UserSpreadServices $services)
- {
- $where['store_id'] = 0;
- $where['staff_id'] = 0;
- $where['uid'] = $id;
- return app('json')->success($services->getSpreadList($where, '*', ['spreadUser', 'admin'], false));
- }
- /**
- * 用户批量操作
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function batchProcess(UserBatchProcessServices $batchProcessServices)
- {
- [$type, $uids, $all, $where, $data] = $this->request->postMore([
- ['type', 1],
- ['uids', ''],
- ['all', 0],
- ['where', ""],
- ['data', []]
- ], true);
- if (!$uids && $all == 0) return $this->fail('请选择批处理用户');
- if (!$data) {
- return $this->fail('请选择批处理数据');
- }
- //批量操作
- $batchProcessServices->batchProcess((int)$type, $uids, $data, !!$all, (array)$where);
- return app('json')->success('已加入消息队列,请稍后查看');
- }
- /**
- * 用户注销
- * @return mixed
- */
- public function cancelUser()
- {
- $uid = $this->request->post('uid', 0);
- if (!$uid) return app('json')->fail('用户不存在');
- event('user.cancelUser', [$uid]);
- return app('json')->success('注销成功');
- }
- }
|