| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- /**
- * 用户管理
- * @author x.b
- */
- declare (strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Constants\ChangeLogConstant;
- use App\Exception\ApiException;
- use App\Model\Mongo\SendCardHistoryModel;
- use App\Model\VipOrderModel;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserRepository as UserRepository;
- use App\Model\CoinsLogModel;
- use App\Model\VipCardModel;
- use App\Model\UserModel as Model;
- use Carbon\Carbon;
- use Hyperf\DbConnection\Db;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use App\Repositories\MessageRepository;
- class UserController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 详细
- * @param yes id int $id 用户ID 如 100
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":14550388}}
- */
- public function detail(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- $select = ['*'];
- $model = Model::query();
- $result = $model->select($select)->where('id', $id)->first();
- if (!$result) {
- return json_fail('用户不存在');
- }
- // 查他的上级邀请人的名字
- $result['parent_nick'] = '';
- if ($result['parent_uid']) {
- $select = ['nick'];
- $r = Model::query()->select($select)->where('code', $result['parent_uid'])->first();
- if ($r) {
- $result['parent_nick'] = $r['nick'];
- }
- }
- $userRepo = make(UserRepository::class);
- $userDataInfo = $userRepo->getUserInfoData($result['code']);
- $result['qrcode'] = $userDataInfo['account_qrcode_content'];
- $orderCount = VipOrderModel::where(['user_code' => $result['code']])->count();
- $payCount = VipOrderModel::where(['user_code' => $result['code']])->where(['status' => 1])->count();
- $result['order_count'] = $orderCount;
- $result['pay_count'] = $payCount;
- return json_success($result);
- }
- /**
- * 列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"nick":"用户CH3J30","stime":"","otime":""},"order":"created_at","order_sort":"desc"}}
- */
- public function list(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
- $select = ['*'];
- $model = Model::query();
- $order_key = isset($inputData['order']) ? $inputData['order'] : 'id';
- $order_value = isset($inputData['order_sort']) ? $inputData['order_sort'] : 'desc';
- if (!$order_key) {
- $order_key = 'id';
- //不要信任前端,他可能会传个空字符串过来!
- }
- if (!$order_value) {
- $order_value = 'desc';
- }
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string) $v)) {
- continue;
- }
- $operator = '=';
- if ($k == 'channel_id' && $v == 'official') {
- // 官方代表空
- $v = "";
- }
- if ($k == 'max_view') {
- preg_match('/^(=|>|<)(\\d{1,10})$/', $v, $matches);
- if (isset($matches[2])) {
- $operator = $matches[1];
- $v = $matches[2];
- }
- }
- switch ($k) {
- case "nick":
- // 用户名
- case "mobile":
- // 手机号
- $model->where($k, 'like', "{$v}%");
- break;
- case "stime":
- // 自定义-开始日期
- $model->where('created_at', '>=', $v);
- break;
- case "otime":
- // 自定义-结束日期
- $model->where('created_at', '<=', Carbon::parse($v)->endOfDay()->toDateTimeString());
- break;
- default:
- $model->where($k, $operator, $v);
- }
- }
- }
- $result = $model->select($select)->orderBy((string) $order_key, (string) $order_value)->paginate($page_size, ['*'], 'page', $page);
- return json_success($result);
- }
- /**
- * 删除
- */
- public function delete()
- {
- }
- /**
- * 更新
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":14550388,"status":"1"}}
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- $status = isset($inputData['status']) ? $inputData['status'] : '';
- if ($status) {
- if (!in_array($status, array(1, 2, 3))) {
- return json_fail('请按文档正确传参,用户的状态值');
- }
- }
- $user_info = Model::query()->where('id', $id)->first();
- if (!$user_info) {
- return json_fail('用户不存在');
- }
- $data = array();
- if (isset($inputData['nick'])) {
- $data['nick'] = trim($inputData['nick']);
- }
- if (isset($inputData['avatar'])) {
- $data['avatar'] = trim($inputData['avatar']);
- }
- if (isset($inputData['bg_cover'])) {
- $data['bg_cover'] = trim($inputData['bg_cover']);
- }
- if (isset($inputData['intro'])) {
- $data['intro'] = rtrim($inputData['intro']);
- }
- if (isset($inputData['status'])) {
- $data['status'] = intval($inputData['status']);
- }
- if (isset($inputData['mobile'])) {
- $data['mobile'] = intval($inputData['mobile']);
- }
- $data = action_data($data);
- if ($data) {
- $result = Model::query()->where('id', $id)->update($data);
- if (!$result) {
- return json_fail('更新失败');
- }
- }
- // 创建管理员日志
- $oldItem = clone $user_info;
- $newitem = Model::query()->where('id', $id)->first();
- $logSubject = '修改一个用户信息,名称:' . $user_info['nick'];
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- make(UserRepository::class)->delUserCache($user_info['code']);
- return json_success('', '更新成功');
- }
- /**
- * 发放金币
- * @param int yes $id 用户ID, 如 14550388
- * @param int yes $coins 金币数, 如 50 则是加; 如 -100 则是减少
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":14550388,"coins":50}}
- */
- public function sendCoins(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|numeric', 'coins' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- $coins = $inputData['coins'];
- $user_info = Model::query()->where('id', $id)->first();
- if (!$user_info) {
- return json_fail('用户不存在');
- }
- if ($coins > 0) {
- $result = Model::query()->where('id', $id)->increment('coins', $coins);
- } else {
- $_coins = abs((int) $coins);
- //取绝对值
- if ($user_info->coins < $_coins) {
- return json_fail('用户金币不够,不能扣减');
- }
- $result = Model::query()->where('id', $id)->decrement('coins', $_coins);
- }
- if (!$result) {
- return json_fail('处理失败');
- }
- make(UserRepository::class)->delUserCache($user_info['code']);
- //清除用户信息redis缓存
- if ($coins < 0) {
- $way = ChangeLogConstant::WAY_2;
- $type = ChangeLogConstant::TYPE_22;
- } else {
- $way = ChangeLogConstant::WAY_1;
- $type = ChangeLogConstant::TYPE_18;
- }
- // 记录帐变使用日志
- $changeData = ['user_uid' => $user_info['code'], 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => abs((int) $coins), 'way' => $way, 'type' => $type];
- // 帐变记录
- make(QueueService::class)->saveChangeLogJob($changeData);
- // 创建管理员日志
- $oldItem = clone $user_info;
- $newitem = Model::query()->where('id', $id)->first();
- $logSubject = '对一个用户发放金币,名称:' . $user_info['nick'];
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success('', '处理成功');
- }
- /**
- * bindPhone 绑定老用户
- * 业务逻辑:
- * 1. 后台对指定新用户 输入 老用户对手机号
- * 2。查询出 老用户对设备号,设备类型
- * 3。将新/老用户的仅 设备号和设备类型,互换一下。
- * 4。调接口,退出新老用户(即 缓存中清理下 )
- * @param int yes $id 新用户的ID,如100
- * @param int yes $mobile 老用户的手机号
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":14550389,"mobile":"13873840000"}}
- */
- public function bindPhone(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|numeric', 'mobile' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $id = $inputData['id'];
- $mobile = $inputData['mobile'];
- $user_info_new = Model::query()->where('id', $id)->first();
- if (!$user_info_new) {
- return json_fail('新用户不存在');
- }
- $user_info_old = Model::query()->where('mobile', $mobile)->first();
- if (!$user_info_old) {
- return json_fail('老用户对应的电话号码不存在');
- }
- // 交换更新用户设备号,设备类型
- Db::beginTransaction();
- try {
- $data = array('device_no' => $user_info_old['device_no'], 'device_type' => $user_info_old['device_type'], 'finger_hash' => $user_info_old['finger_hash']);
- $r = Model::query()->where('id', $id)->update($data);
- if (!$r) {
- throw new ApiException('更新新用户设备信息失败');
- }
- $data = array('device_no' => $user_info_new['device_no'], 'device_type' => $user_info_new['device_type'], 'finger_hash' => $user_info_new['finger_hash']);
- $r = Model::query()->where('mobile', $mobile)->update($data);
- if (!$r) {
- throw new ApiException('更新老用户设备信息失败');
- }
- // 清理新/旧用户缓存数据
- $m = make(UserRepository::class);
- $m->userLogout($user_info_new['code']);
- $m->userLogout($user_info_old['code']);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- }
- // 创建管理员日志
- $oldItem = clone $user_info_new;
- $oldItem2 = clone $user_info_old;
- $newitem = Model::query()->where('id', $id)->first();
- $newitem2 = Model::query()->where('id', $user_info_old['id'])->first();
- $logSubject = "绑定老用户,用户id:{$user_info_new['id']}->{$user_info_old['id']}";
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [$newitem->toArray(), $newitem2->toArray()], 'before_items' => [$oldItem->toArray(), $oldItem2->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success('', '处理成功');
- }
- /**
- * 对指定用户发放会员卡
- * @param int yes $id 用户id
- * @param int yes $card_id 会员卡ID,来源会员卡列表的字段id
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":424432,"card_id":1}}
- */
- public function sendCard(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required', 'card_id' => 'required'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $uid = $inputData['id'];
- $card_id = $inputData['card_id'];
- $card_info = VipCardModel::query()->where('id', $card_id)->first();
- if (!$card_info) {
- return json_fail('会员卡不存在');
- }
- if (!$card_info['status']) {
- return json_fail('会员卡未启动');
- }
- $user_info = Model::query()->where('id', $uid)->first();
- if (!$user_info) {
- return json_fail('用户不存在');
- }
- try {
- //加会员天数
- $userInfo = make(UserRepository::class)->getUserInfoFromCache($user_info['code']);
- if ($userInfo['is_vip'] == 'y') {
- $vipTime = Carbon::parse($userInfo['vip_expired'])->addDays($card_info['day'])->toDateTimeString();
- } else {
- $vipTime = Carbon::now()->addDays($card_info['day'])->toDateTimeString();
- }
- $result = Model::query()->where('id', $user_info['id'])->update(['vip_expired' => $vipTime]);
- if (!$result) {
- return json_fail('添加用户会员卡时间失败');
- }
- make(UserRepository::class)->delUserCache($user_info['code']);
- //清除用户信息redis缓存
- } catch (\Exception $e) {
- return json_fail('处理失败');
- }
- //$managerInfo = $authService->getManagerInfo();
- // $roleId = $managerInfo['role_id'];
- // $managerId = $managerInfo['manager_id'];
- $managerInfo = $this->getCurrentManagerInfo();
- make(SendCardHistoryModel::class)->db()->insertOne(['mid_id' => $managerInfo['manager_id'], 'user_code' => $user_info['code'], 'vip_id' => $card_info['id'], 'vip_days' => $card_info['day']]);
- // 创建管理员日志
- $oldItem = clone $user_info;
- $newitem = Model::query()->where('id', $user_info['id'])->first();
- $logSubject = "发放{$card_info['day']}天的会员卡,会员卡配置ID:{$card_info['id']},用户id:{$user_info['id']}";
- $logSvalue = $oldItem->id;
- $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$oldItem->toArray()]];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success('', '处理成功');
- }
- /**
- * 指定用户的邀请详细
- * @param yes id int $id 用户ID 如 100
- * @return mixed
- * @example param_json_data string: {"handshake":"","page":1,"page_size":5,"data":{"id":30}}
- */
- public function inviteDetail(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
- $rules = ['id' => 'required|numeric'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $order_key = isset($inputData['order']) ? $inputData['order'] : 'id';
- $order_value = isset($inputData['order_sort']) ? $inputData['order_sort'] : 'desc';
- if (!$order_key) {
- $order_key = 'id';
- //不要信任前端,他可能会传个空字符串过来!
- }
- if (!$order_value) {
- $order_value = 'desc';
- }
- $id = $inputData['id'];
- $select = ['*'];
- $user_info = Model::query()->select($select)->where('id', $id)->first();
- if (!$user_info) {
- return json_fail('用户不存在');
- }
- $select = ['nick', 'id', 'mobile', 'created_at'];
- $invite_list = Model::query()->select($select)->where('parent_uid', $user_info['code'])->orderBy((string) $order_key, (string) $order_value)->paginate($page_size, ['*'], 'page', $page);
- if (!$invite_list) {
- $invite_list = array();
- }
- $result = array('user_info' => $user_info, 'invite_list' => $invite_list);
- return json_success($result);
- }
- /**
- * 邀请列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"nick":"用户CH3J30","id":"","mobile":"","code":"","stime":"","otime":""},"order":"invite","order_sort":"desc"}}
- */
- public function inviteList(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
- $select = ['id', 'nick', 'mobile', 'mobile', 'device_no', 'device_type', 'channel_id', 'code', 'status', 'invite', 'coins', 'ip'];
- $model = Model::query();
- $order_key = isset($inputData['order']) ? $inputData['order'] : 'id';
- $order_value = isset($inputData['order_sort']) ? $inputData['order_sort'] : 'desc';
- if (!$order_key) {
- $order_key = 'id';
- //不要信任前端,他可能会传个空字符串过来!
- }
- if (!$order_value) {
- $order_value = 'desc';
- }
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string) $v)) {
- continue;
- }
- switch ($k) {
- case "nick":
- // 用户名
- case "mobile":
- // 手机号
- case "code":
- // 邀请码
- $model->where($k, 'like', "{$v}%");
- break;
- case "stime":
- // 自定义-开始日期
- $model->where('created_at', '>=', $v);
- break;
- case "otime":
- // 自定义-结束日期
- $model->where('created_at', '<=', Carbon::parse($v)->endOfDay()->toDateTimeString());
- break;
- default:
- $model->where($k, $v);
- }
- }
- }
- $model->where('invite', '>', '0');
- //仅查询有邀请人数的
- $invite_all_select = [Db::raw('count(*) as id_all'), Db::raw('sum(invite) as invite_all')];
- $invite_all = $model->select($invite_all_select)->first();
- if ($invite_all) {
- if (!$invite_all['id_all']) {
- $invite_all['average'] = 0;
- } else {
- $invite_all['average'] = round($invite_all['invite_all'] / $invite_all['id_all'], 1);
- }
- } else {
- $invite_all = array("id_all" => 0, "invite_all" => 0, 'average' => 0);
- }
- $invite_list = $model->select($select)->orderBy((string) $order_key, (string) $order_value)->paginate($page_size, ['*'], 'page', $page);
- $result = array('invite_all' => $invite_all, 'invite_list' => $invite_list);
- return json_success($result);
- }
- /**
- * 发送系统消息给单个用户
- */
- public function sendMessageToUser(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['subject' => 'required|string|max:255', 'receive_uid' => 'required|max:32', 'content' => 'required|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $receiveUid = data_get($inputData, 'receive_uid', '');
- $content = data_get($inputData, 'content', '');
- $subject = data_get($inputData, 'subject', '');
- $msgRepo = make(MessageRepository::class);
- $msgRepo->sendTextFromSys($receiveUid, $content, $subject);
- return json_success(['result' => 'success']);
- }
- }
|