123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?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\user;
- use app\common\AdminBaseController;
- use app\Request;
- use app\services\user\UserBillServices;
- use app\services\user\UserGroupServices;
- use app\services\user\UserLevelServices;
- use app\services\user\UserServices;
- use app\services\user\UserSpreadServices;
- use app\services\user\UserWechatuserServices;
- use app\validate\admin\user\UserValidate;
- use qiniu\exceptions\AdminException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class User extends AdminBaseController
- {
- /**
- * user constructor.
- * @param Request $request
- * @param UserServices $services
- */
- public function __construct(Request $request, UserServices $services)
- {
- parent::__construct($request);
- $this->service = $services;
- $this->searchable = [
- ['page', 1],
- ['limit', 20],
- ['nickname', ''],
- ['status', ''],
- ['is_promoter', ''],
- ['order', ''],
- ['data', ''],
- ['user_type', ''],
- ['country', ''],
- ['province', ''],
- ['city', ''],
- ['user_time_type', ''],
- ['user_time', ''],
- ['sex', ''],
- [['level', 0], 0],
- [['group_id', 'd'], 0],
- ['now_money', 'normal'],
- ['field_key', ''],
- ];
- }
- /**
- * 显示资源列表头部
- *
- * @return \think\Response
- */
- public function typeHeader()
- {
- $list = $this->service->typeHead();
- return $this->success(compact('list'));
- }
- /**
- * 显示资源列表
- *
- * @return \think\Response
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function index()
- {
- $where = $this->request->getMore($this->searchable);
- $where['user_time_type'] = $where['user_time_type'] == 'all' ? '' : $where['user_time_type'];
- return $this->success($this->service->userIndex($where));
- }
- /**
- * 后台添加用户
- *
- * @return \think\Response
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function save()
- {
- $data = $this->request->postMore([
- ['is_promoter', 0],
- ['real_name', ''],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['status', 1],
- ['level', 0],
- ['phone', 0],
- ['group_id', 0],
- ['pwd', ''],
- ['true_pwd', ''],
- ['trade_pwd', ''],
- ['true_trade_pwd', ''],
- ['sex', 0],
- ['provincials', ''],
- ['spread_uid', 0],
- ['province', 0],
- ['city', 0],
- ['area', 0],
- ['street', 0],
- ]);
- $this->validate(['pwd' => $data['pwd'], 'phone' => $data['phone']], app()->make(UserValidate::class));
- if ($this->service->be(['phone' => $data['phone']])) {
- return $this->error('手机号已经存在不能添加相同的手机号用户');
- }
- $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
- if ($data['card_id']) {
- if (!check_card($data['card_id'])) return $this->error('请输入正确的身份证号码');
- }
- if ($data['birthday']) {
- if (strtotime($data['birthday']) > time()) return $this->error('生日请选择今天之前日期');
- }
- if (!$data['true_pwd']) {
- return $this->error('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return $this->error('两次输入的密码不一致');
- }
- if (!check_password($data['pwd'])) {
- return $this->error('您设置的密码太过简单:至少6位,最多18位,包含大小写字母、数字和特殊字符');
- }
- [$data['pwd'], $data['salt']] = password($data['pwd']);
- unset($data['true_pwd']);
- if ($data['trade_pwd']) {
- if (!$data['true_trade_pwd']) {
- return $this->error('请确认交易密码');
- }
- if (!check_trade_password($data['trade_pwd'])) return $this->error('交易密码为6位数字');
- if ($data['trade_pwd'] != $data['true_trade_pwd']) {
- return $this->error('两次输入的交易密码不一致');
- }
- [$data['trade_pwd'], $data['trade_salt']] = password($data['trade_pwd']);
- unset($data['true_trade_pwd']);
- }
- $data['avatar'] = sys_config('h5_avatar');
- $data['admin_id'] = $this->adminId;
- $data['user_type'] = 'h5';
- $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
- $data['add_time'] = time();
- $data['account'] = create_account();
- $spread_uid = $data['spread_uid'] ?? 0;
- unset($data['spread_uid']);
- $this->service->transaction(function () use ($data, $spread_uid) {
- $res = true;
- $userInfo = $this->service->create($data);
- if ($spread_uid > 0) {
- /** @var UserSpreadServices $spread */
- $spread = app()->make(UserSpreadServices::class);
- $spread->adminSetSpread($userInfo->uid, $spread_uid);
- }
- if ($data['level']) {
- $res = $this->service->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
- }
- if (!$res) {
- throw new AdminException('保存添加用户失败');
- }
- });
- return $this->success('添加成功');
- }
- /**
- * 执行赠送会员等级
- * @param $uid
- * @return mixed
- * @throws DbException
- */
- public function give_level($uid)
- {
- if (!$uid) return $this->error('缺少参数');
- [$level_id] = $this->request->postMore([
- ['level_id', 0],
- ], true);
- return $this->success($this->service->saveGiveLevel((int)$uid, (int)$level_id) ? '赠送成功' : '赠送失败');
- }
- /**
- * 清除会员等级
- * @param $uid
- * @return mixed
- */
- public function del_level($uid)
- {
- if (!$uid) return $this->error('缺少参数');
- return $this->success($this->service->cleanUpLevel((int)$uid) ? '清除成功' : '清除失败');
- }
- /**
- * 保存会员分组
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function set_group()
- {
- [$group_id, $uids, $all, $where] = $this->request->postMore([
- ['group_id', 0],
- ['uids', ''],
- ['all', 0],
- ['where', ""],
- ], true);
- if (!$uids && $all == 0) return $this->error('缺少参数');
- if (!$group_id) return $this->error('请选择分组');
- if ($all == 0) {
- $uids = explode(',', $uids);
- }
- 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'));
- }
- /** @var UserGroupServices $userGroup */
- $userGroup = app()->make(UserGroupServices::class);
- if (!$userGroup->get($group_id)) {
- return $this->error('该分组不存在');
- }
- $this->service->setUserGroup($uids, $group_id);
- return $this->success('已设置用户分组!');
- }
- /**
- * 执行编辑其他
- * @param $uid
- * @return mixed
- */
- public function updateAccount($uid)
- {
- if (!$uid) return $this->error('数据不存在');
- list($money_type, $pm, $num, $mark) = $this->request->postMore([
- ['money_type', ''],
- ['pm', 1],
- ['num', 0],
- ['mark', '']
- ], true);
- if (!$money_type) return $this->error('请选择要操作的字段');
- if ($num <= 0) return $this->error('请输入要操作的金额');
- return $this->success($this->service->updateAccount($uid, $money_type, $pm, $num, $mark, $this->adminId) ? '修改成功' : '修改失败');
- }
- /**
- * 修改user表状态
- *
- * @return array
- */
- public function set_status($status, $uid)
- {
- if ($status == '' || $uid == 0) return $this->error('参数错误');
- $this->service->update($uid, ['status' => $status], 'uid');
- return $this->success($status == 0 ? '禁用成功' : '解禁成功');
- }
- /**
- * @param $id
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function update($id)
- {
- $data = $this->request->postMore([
- ['is_promoter', -1],
- ['real_name', ''],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['status', 0],
- ['level', 0],
- ['phone', 0],
- ['group_id', 0],
- ['pwd', ''],
- ['true_pwd'],
- ['trade_pwd', ''],
- ['true_trade_pwd', ''],
- ['sex', 0],
- ['provincials', ''],
- ['province', 0],
- ['city', 0],
- ['area', 0],
- ['street', 0],
- ['spread_uid', -1],
- ]);
- if ($data['phone']) {
- if (!check_phone($data['phone'])) return $this->error('手机号码格式不正确');
- }
- if ($data['card_id']) {
- if (!check_card($data['card_id'])) return $this->error('请输入正确的身份证');
- }
- if ($data['birthday']) {
- if (strtotime($data['birthday']) > time()) return $this->error('生日请选择今天之前日期');
- }
- if ($data['pwd']) {
- if (!$data['true_pwd']) {
- return $this->error('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return $this->error('两次输入的密码不一致');
- }
- if (!check_password($data['pwd'])) {
- return $this->error('您设置的密码太过简单:至少6位,最多18位,包含大小写字母、数字和特殊字符');
- }
- $this->validate(['pwd' => $data['pwd']], UserValidate::class);
- [$data['pwd'], $data['salt']] = password($data['pwd']);
- } else {
- unset($data['pwd']);
- }
- unset($data['true_pwd']);
- if ($data['trade_pwd']) {
- if (!$data['true_trade_pwd']) {
- return $this->error('请确认交易密码');
- }
- if (!check_trade_password($data['trade_pwd'])) return $this->error('交易密码为6位数字');
- if ($data['trade_pwd'] != $data['true_trade_pwd']) {
- return $this->error('两次输入的交易密码不一致');
- }
- [$data['trade_pwd'], $data['trade_salt']] = password($data['trade_pwd']);
- } else {
- unset($data['trade_pwd']);
- }
- unset($data['true_trade_pwd']);
- $userInfo = $this->service->get($id);
- if (!$userInfo) {
- return $this->error('用户不存在');
- }
- if ($data['spread_uid'] != -1) {
- $spreadUid = $data['spread_uid'];
- if ($id == $spreadUid) {
- return $this->error('上级推广人不能为自己');
- }
- if (!$this->service->be(['uid' => $spreadUid])) {
- return $this->error('上级用户不存在');
- }
- $spreadInfo = $this->service->get($spreadUid);
- if ($spreadInfo->spread_uid == $id) {
- return $this->error('上级推广人不能为自己下级');
- }
- }
- if (!$id) return $this->error('数据不存在');
- $data['admin_id'] = $this->adminId;
- $res = $this->service->updateInfo((int)$id, $data);
- if ($res) {
- $userInfo = $this->service->get($id);
- /** @var UserLevelServices $levelServices */
- $levelServices = app()->make(UserLevelServices::class);
- $levelServices->detection((int)$userInfo['uid']);
- }
- return $this->success($res ? '修改成功' : '修改失败');
- }
- /**
- * 获取单个用户信息
- * @param int $id 用户id
- * @return mixed
- */
- public function oneUserInfo($type, int $id)
- {
- $data = $this->request->get();
- if (!$type) return $this->error('缺少参数');
- return $this->success($this->service->oneUserInfo($id, $type, $data));
- }
- /**
- * 同步微信粉丝用户
- * @return mixed
- */
- public function syncWechatUsers()
- {
- $this->service->syncWechatUsers();
- return $this->success('加入消息队列成功,正在异步执行中');
- }
- /**
- * 用户注销
- * @return mixed
- */
- public function delete($id)
- {
- if (!$id) return app('json')->fail('用户不存在');
- event('user.cancelUser', [$id]);
- return $this->success('注销成功');
- }
- }
|