| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace app\adminapi\controller\v1\application\wechat;
- use app\adminapi\controller\AuthController;
- use app\models\store\StoreOrder;
- use app\models\user\{User, UserBill};
- use app\models\wechat\WechatUser as UserModel;
- use crmeb\services\{UtilService as Util, FormBuilder as Form, WechatService};
- use think\Collection;
- use think\facade\Route as Url;
- /**
- * 微信用户管理
- * Class WechatUser
- * @package app\admin\controller\wechat
- */
- class WechatUser extends AuthController
- {
- /**
- * 显示操作记录
- */
- public function index()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['nickname', ''],
- ['data', ''],
- ['tagid_list', ''],
- ['groupid', '-1'],
- ['sex', ''],
- ['export', ''],
- ['subscribe', '']
- ], $this->request);
- $tagidList = explode(',', $where['tagid_list']);
- foreach ($tagidList as $k => $v) {
- if (!$v) {
- unset($tagidList[$k]);
- }
- }
- $tagidList = array_unique($tagidList);
- $where['tagid_list'] = implode(',', $tagidList);
- $list = UserModel::systemPage($where);
- return $this->success($list);
- }
- /**
- * 获取标签和分组
- * @return mixed
- */
- public function get_tag_group(){
- try {
- $groupList = UserModel::getUserGroup();
- $tagList = UserModel::getUserTag();
- } catch (\Exception $e) {
- $groupList = [];
- $tagList = [];
- }
- return $this->success(compact('groupList','tagList'));
- }
- /**
- * 修改用户标签表单
- * @param $openid
- * @return mixed|string
- */
- public function edit_user_tag($openid)
- {
- if (!$openid) return $this->fail('参数错误!');
- $list = Collection::make(UserModel::getUserTag())->each(function ($item) {
- return ['value' => $item['id'], 'label' => $item['name']];
- });
- $tagList = UserModel::where('openid', $openid)->value('tagid_list');
- $tagList = explode(',', $tagList) ?: [];
- $f = [Form::select('tag_id', '用户标签', $tagList)->setOptions($list->toArray())->multiple(1)];
- return $this->makePostForm('编辑用户标签', $f, Url::buildUrl('/app/wechat/user_tag/' . $openid), 'PUT');
- }
- /**
- * 修改用户标签
- * @param $openid
- * @return mixed
- */
- public function update_user_tag($openid)
- {
- if (!$openid) return $this->fail('参数错误!');
- $tagId = request()->post('tag_id/a', []);
- if (!$tagId) return $this->fail('请选择用户标签!');
- $tagList = explode(',', UserModel::where('openid', $openid)->value('tagid_list')) ?: [];
- UserModel::edit(['tagid_list' => $tagId], $openid, 'openid');
- if (!$tagId[0]) unset($tagId[0]);
- UserModel::edit(['tagid_list' => $tagId], $openid, 'openid');
- try {
- foreach ($tagList as $tag) {
- if ($tag) WechatService::userTagService()->batchUntagUsers([$openid], $tag);
- }
- foreach ($tagId as $tag) {
- WechatService::userTagService()->batchTagUsers([$openid], $tag);
- }
- } catch (\Exception $e) {
- UserModel::rollbackTrans();
- return $this->fail($e->getMessage());
- }
- UserModel::commitTrans();
- return $this->success('修改成功!');
- }
- /**
- * 修改用户分组表单
- * @param $openid
- * @return mixed|string
- */
- public function edit_user_group($openid)
- {
- if (!$openid) return $this->fail('参数错误!');
- $list = Collection::make(UserModel::getUserGroup())->each(function ($item) {
- return ['value' => $item['id'], 'label' => $item['name']];
- });
- $groupId = UserModel::where('openid', $openid)->value('groupid');
- $f = [Form::select('group_id', '用户分组', (string)$groupId)->setOptions($list->toArray())];
- return $this->makePostForm('编辑用户标签', $f, Url::buildUrl('/app/wechat/user_group/' . $openid), 'PUT');
- }
- /**
- * 修改用户分组
- * @param $openid
- * @return mixed
- */
- public function update_user_group($openid)
- {
- if (!$openid) return $this->fail('参数错误!');
- $groupId = request()->post('group_id');
- // if(!$groupId) return $this->fail('请选择用户分组!');
- UserModel::beginTrans();
- UserModel::edit(['groupid' => $groupId], $openid, 'openid');
- try {
- WechatService::userGroupService()->moveUser($openid, $groupId);
- } catch (\Exception $e) {
- UserModel::rollbackTrans();
- return $this->fail($e->getMessage());
- }
- UserModel::commitTrans();
- return $this->success('修改成功!');
- }
- /**
- * 用户标签列表
- */
- public function tag($refresh = 0)
- {
- $list = [];
- if ($refresh == 1) {
- UserModel::clearUserTag();
- $this->redirect(Url::buildUrl('tag'));
- }
- try {
- $list = UserModel::getUserTag();
- } catch (\Exception $e) {
- }
- return $this->success(compact('list'));
- }
- /**
- * 添加标签表单
- * @return mixed
- */
- public function create_tag()
- {
- $f = [Form::input('name', '标签名称')];
- return $this->makePostForm('添加标签', $f, Url::buildUrl('/app/wechat/tag'), 'POST');
- }
- /**
- * 添加
- */
- public function save_tag()
- {
- $tagName = request()->post('name');
- if (!$tagName) return $this->fail('请输入标签名称!');
- try {
- WechatService::userTagService()->create($tagName);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserTag();
- return $this->success('添加标签成功!');
- }
- /**
- * 修改标签表单
- * @param $id
- * @return mixed
- */
- public function edit_tag($id)
- {
- $f = [Form::input('name', '标签名称')];
- return $this->makePostForm('编辑标签', $f, Url::buildUrl('/app/wechat/tag/' . $id), 'PUT');
- }
- /**
- * 修改标签
- * @param $id
- */
- public function update_tag($id)
- {
- $tagName = request()->post('name');
- if (!$tagName) return $this->fail('请输入标签名称!');
- try {
- WechatService::userTagService()->update($id, $tagName);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserTag();
- return $this->success('修改标签成功!');
- }
- /**
- * 删除标签
- * @param $id
- * @return \think\response\Json
- */
- public function delete_tag($id)
- {
- try {
- WechatService::userTagService()->delete($id);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserTag();
- return $this->success('删除标签成功!');
- }
- /**
- * 用户分组列表
- */
- public function group($refresh = 0)
- {
- $list = [];
- try {
- if ($refresh == 1) {
- UserModel::clearUserGroup();
- $this->redirect(Url::buildUrl('group'));
- }
- $list = UserModel::getUserGroup();
- } catch (\Exception $e) {
- }
- return $this->success(compact('list'));
- }
- /**
- * 添加分组表单
- * @return mixed
- */
- public function create_group()
- {
- $f = [Form::input('name', '分组名称')];
- return $this->makePostForm('添加分组', $f, Url::buildUrl('/app/wechat/group'), 'POST');
- }
- /**
- * 添加
- */
- public function save_group()
- {
- $tagName = request()->post('name');
- if (!$tagName) return $this->fail('请输入分组名称!');
- try {
- WechatService::userGroupService()->create($tagName);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserGroup();
- return $this->success('添加分组成功!');
- }
- /**
- * 修改分组表单
- * @param $id
- * @return mixed
- */
- public function edit_group($id)
- {
- $f = [Form::input('name', '分组名称')];
- return $this->makePostForm('编辑分组', $f, Url::buildUrl('/app/wechat/group/' . $id), 'PUT');
- }
- /**
- * 修改分组
- * @param $id
- */
- public function update_group($id)
- {
- $tagName = request()->post('name');
- if (!$tagName) return $this->fail('请输入分组名称!');
- try {
- WechatService::userGroupService()->update($id, $tagName);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserGroup();
- return $this->success('修改分组成功!');
- }
- /**
- * 删除分组
- * @param $id
- * @return \think\response\Json
- */
- public function delete_group($id)
- {
- try {
- WechatService::userTagService()->delete($id);
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- UserModel::clearUserGroup();
- return $this->success('删除分组成功!');
- }
- /**
- * 同步标签
- * @param $openid
- * @return mixed
- */
- public function syn_tag($openid)
- {
- if (!$openid) return $this->fail('参数错误!');
- $data = array();
- if (UserModel::be($openid, 'openid')) {
- try {
- $tag = WechatService::userTagService()->userTags($openid)->toArray();
- } catch (\Exception $e) {
- return $this->fail($e->getMessage());
- }
- if ($tag['tagid_list']) $data['tagid_list'] = implode(',', $tag['tagid_list']);
- else $data['tagid_list'] = '';
- $res = UserModel::edit($data, $openid, 'openid');
- if ($res) return $this->success('同步成功');
- else return $this->fail('同步失败!');
- } else return $this->fail('参数错误!');
- }
- /**
- * 一级推荐人页面
- * @return mixed
- */
- public function stair($uid = '')
- {
- if ($uid == '') return $this->fail('参数错误');
- $list = User::alias('u')
- ->where('u.spread_uid', $uid)
- ->field('u.avatar,u.nickname,u.now_money,u.add_time,u.uid')
- ->where('u.status', 1)
- ->order('u.add_time DESC')
- ->select()
- ->toArray();
- foreach ($list as $key => $value) $list[$key]['orderCount'] = StoreOrder::getOrderCount($value['uid']);
- return $this->success(compact('list'));
- }
- /**
- * 个人资金详情页面
- * @return mixed
- */
- public function now_money($uid = '')
- {
- if ($uid == '') return $this->fail('参数错误');
- $list = UserBill::where('uid', $uid)->where('category', 'now_money')
- ->field('mark,pm,number,add_time')
- ->where('status', 1)->order('add_time DESC')->select()->toArray();
- foreach ($list as &$v) {
- $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
- }
- return $this->success(compact('list'));
- }
- }
|