123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?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\store\user;
- use app\services\store\DeliveryServiceServices;
- use think\facade\App;
- use app\controller\store\AuthController;
- use app\services\store\StoreUserServices;
- use app\services\user\UserServices;
- use app\services\other\queue\QueueServices;
- use app\services\user\label\UserLabelServices;
- use app\jobs\BatchHandleJob;
- /**
- * 门店用户
- * Class User
- * @package app\controller\store\user
- */
- class User extends AuthController
- {
- /**
- * DeliveryService constructor.
- * @param App $app
- * @param StoreUserServices $services
- */
- public function __construct(App $app, StoreUserServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
- /**
- * 搜索用户
- * @param UserServices $services
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function search(UserServices $services)
- {
- $data = $this->request->getMore([
- ['keyword', ''],
- ['field_key', ''],
- ]);
- if ($data['field_key'] == 'all') {
- $data['field_key'] = '';
- }
- if ($data['field_key'] && in_array($data['field_key'], ['uid', 'phone', 'bar_code'])) {
- $where[$data['field_key']] = trim($data['keyword']);
- } else {
- $where['store_like'] = trim($data['keyword']);
- }
- $result = $services->getUserList($where, 'uid,nickname,avatar,phone,now_money,user_type');
- $list = $result['list'] ?? [];
- $count = $result['count'] ?? 0;
- if ($list) {
- foreach ($list as &$item) {
- //用户类型
- if ($item['user_type'] == 'routine') {
- $item['user_type'] = '小程序';
- } else if ($item['user_type'] == 'wechat') {
- $item['user_type'] = '公众号';
- } else if ($item['user_type'] == 'h5') {
- $item['user_type'] = 'H5';
- } else if ($item['user_type'] == 'pc') {
- $item['user_type'] = 'PC';
- } else if ($item['user_type'] == 'app') {
- $item['user_type'] = 'APP';
- } else $item['user_type'] = '其他';
- }
- }
- return app('json')->success(compact('list', 'count'));
- }
- /**
- * 显示资源列表
- *
- * @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', '']
- ]);
- return app('json')->success($this->services->index($where, $this->storeId));
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create()
- {
- return app('json')->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]
- ]);
- if ($data['phone']) {
- if (!check_phone($data['phone'])) {
- return app('json')->fail('手机号码格式不正确');
- }
- if ($this->services->count(['phone' => $data['phone']])) {
- return app('json')->fail('手机号已经存在不能添加相同的手机号用户');
- }
- $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
- }
- if ($data['card_id']) {
- try {
- if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
- } catch (\Throwable $e) {
- // return app('json')->fail('请输入正确的身份证');
- }
- }
- if ($data['pwd']) {
- if (!$data['true_pwd']) {
- return app('json')->fail('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return app('json')->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';
- $lables = $data['label_id'];
- unset($data['label_id']);
- foreach ($lables as $k => $v) {
- if (!$v) {
- unset($lables[$k]);
- }
- }
- $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
- $data['add_time'] = time();
- $this->services->transaction(function () use ($data, $lables) {
- $res = true;
- $userInfo = $this->services->save($data);
- if ($lables) {
- $res = $this->services->saveSetLabel([$userInfo->uid], $lables);
- }
- 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]);
- });
- return app('json')->success('添加成功');
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @param UserServices $services
- * @return \think\Response
- */
- public function read($id, UserServices $services)
- {
- if (is_string($id)) {
- $id = (int)$id;
- }
- return app('json')->success($services->read($id));
- }
- /**
- * 设置用户标签表单
- * @param UserServices $services
- * @return mixed
- */
- public function set_label(UserServices $services)
- {
- [$uids, $all, $where] = $this->request->postMore([
- ['uids', []],
- ['all', 0],
- ['where', ""],
- ], true);
- return app('json')->success($services->setLabel($uids, $all, $where, 1, (int)$this->storeId));
- }
- /**
- * 批量设置用户标签
- * @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 app('json')->fail('缺少参数');
- if (!$lables || (count($lables) == 1 && !$lables[0])) return app('json')->fail('请选择标签');
- if ($all == 0) {
- $uids = explode(',', $uids);
- $where = [];
- }
- if ($all == 1) {
- $uids = [];
- $where = $where ? json_decode($where, true) : [];
- }
- /** @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, ['store_id' => $this->storeId]]);
- return app('json')->success('后台程序已执行批量设置用户标签任务!');
- }
- /**
- * 编辑其他
- * @param $id
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function edit_other($id)
- {
- if (!$id) return app('json')->fail('数据不存在');
- return app('json')->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 app('json')->fail('数据不存在');
- $data['adminId'] = $this->adminId;
- $data['money'] = (string)$data['money'];
- $data['integration'] = (string)$data['integration'];
- $data['is_other'] = true;
- return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
- }
- /**
- * 编辑会员信息
- * @param $id
- * @return mixed|\think\response\Json|void
- */
- public function edit($id)
- {
- if (!$id) return app('json')->fail('数据不存在');
- return app('json')->success($this->services->edit($id));
- }
- public function update($id)
- {
- $data = $this->request->postMore([
- ['money_status', 0],
- ['is_promoter', 0],
- ['real_name', ''],
- ['card_id', ''],
- ['birthday', ''],
- ['mark', ''],
- ['money', 0],
- ['integration_status', 0],
- ['integration', 0],
- ['status', 0],
- ['level', 0],
- ['phone', 0],
- ['addres', ''],
- ['label_id', ''],
- ['group_id', 0],
- ['pwd', ''],
- ['true_pwd'],
- ['spread_open', 1]
- ]);
- if ($data['phone']) {
- if (!check_phone($data['phone'])) return app('json')->fail('手机号码格式不正确');
- }
- if ($data['card_id']) {
- try {
- if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
- } catch (\Throwable $e) {
- // return app('json')->fail('请输入正确的身份证');
- }
- }
- if ($data['pwd']) {
- if (!$data['true_pwd']) {
- return app('json')->fail('请输入确认密码');
- }
- if ($data['pwd'] != $data['true_pwd']) {
- return app('json')->fail('两次输入的密码不一致');
- }
- $data['pwd'] = md5($data['pwd']);
- } else {
- unset($data['pwd']);
- }
- unset($data['true_pwd']);
- if (!$id) return app('json')->fail('数据不存在');
- $data['adminId'] = $this->adminId;
- $data['money'] = (string)$data['money'];
- $data['integration'] = (string)$data['integration'];
- return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
- }
- /**
- * 获取单个用户信息
- * @param $id 用户id
- * @param UserServices $services
- * @return mixed
- */
- public function oneUserInfo($id, UserServices $services)
- {
- $data = $this->request->getMore([
- ['type', ''],
- ]);
- $id = (int)$id;
- if ($data['type'] == '') return app('json')->fail('缺少参数');
- return app('json')->success($services->oneUserInfo($id, $data['type']));
- }
- }
|