User.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\AdminConstants;
  6. use app\main\constants\CacheConstants;
  7. use app\main\constants\ErrorCodeConstants;
  8. use app\main\constants\OrderContents;
  9. use app\main\constants\UserConstants;
  10. use app\main\service\ApiService;
  11. use app\main\service\FinancialService;
  12. use app\main\service\OrderService;
  13. use app\main\service\UserDdFlushService;
  14. use app\main\service\UserService;
  15. use app\source\model\UserUpdate;
  16. use think\Config;
  17. use think\Exception;
  18. use think\Log;
  19. /**
  20. * 读者管理
  21. *
  22. * @icon fa fa-user
  23. */
  24. class User extends Backend
  25. {
  26. /**
  27. * @var \app\common\model\User User模型对象
  28. */
  29. protected $model = null;
  30. protected $dataLimit = false;
  31. protected $searchFields = 'id,nickname';
  32. protected $adminId;
  33. protected $groupId;
  34. /**
  35. * @var \Redis
  36. */
  37. protected $redis;
  38. /**
  39. * @var \app\common\model\Orders
  40. */
  41. protected $orderModel = null;
  42. /**
  43. * @var \app\common\model\Consume
  44. */
  45. protected $consumeModel = null;
  46. /**
  47. * @var \app\common\model\UserRecentlyRead
  48. */
  49. protected $userRecentlyReadModel = null;
  50. /**
  51. * @var \app\common\model\Recharge
  52. */
  53. protected $rechargeModel = null;
  54. /**
  55. * @var \app\common\model\AdminConfig
  56. */
  57. protected $adminConfigModel = null;
  58. /**
  59. * @var \app\common\model\Admin
  60. */
  61. protected $adminModel = null;
  62. protected $noNeedRight = ['ridblack'];
  63. public function _initialize()
  64. {
  65. parent::_initialize();
  66. $this->model = model('User');
  67. $this->orderModel = model('Orders');
  68. $this->consumeModel = model('consume');
  69. $this->userRecentlyReadModel = model('user_recently_read');
  70. $this->rechargeModel = model('recharge');
  71. $this->adminConfigModel = model('admin_config');
  72. $this->adminModel = model('admin');
  73. $this->redis = Redis::instance();
  74. $this->adminId = $this->auth->id;
  75. $group = model('AuthGroupAccess')->where('uid',$this->adminId)->find();
  76. $this->groupId = $group->group_id;
  77. $this->view->assign("sexList", $this->model->getSexList());
  78. $this->view->assign("isSubscribeList", $this->model->getIsSubscribeList());
  79. $this->view->assign("isPayList", $this->model->getIsPayList());
  80. $this->view->assign("stateList", $this->model->getStateList());
  81. $this->view->assign("groupId",$this->groupId);
  82. $this->assignconfig("groupId",$this->groupId);
  83. }
  84. /**
  85. * 查看
  86. */
  87. public function index()
  88. {
  89. $search = [];
  90. if($this->groupId == 3){ //处理渠道商查询读者
  91. $this->assign('groupId',$this->groupId);
  92. if(!empty($_GET['search'])){
  93. $search['id'] = $_GET['search'];
  94. $this->assign('search',$_GET['search']);
  95. }else{
  96. $this->assign('search',null);
  97. return $this->fetch();
  98. }
  99. }
  100. //设置过滤方法
  101. $this->request->filter(['strip_tags']);
  102. $channel = $this->request->get('channel',0);
  103. $agent = $this->request->get('agent',0);
  104. if($this->groupId == 3){
  105. $path = 'auth/user/index?search='.$_GET['search'];
  106. }else{
  107. $path = 'auth/user/index?channel='.$channel.'&agent='.$agent;
  108. }
  109. $this->assignconfig('urlPath',$path);
  110. if ($this->request->isAjax())
  111. {
  112. //如果发送的来源是Selectpage,则转发到Selectpage
  113. if ($this->request->request('pkey_name'))
  114. {
  115. return $this->selectpage();
  116. }
  117. $condition = [];
  118. if($channel || $agent){
  119. if($channel){
  120. $condition['channel_id'] = $channel;
  121. }
  122. if($agent){
  123. $condition['agent_id'] = $agent;
  124. }
  125. }else{
  126. if($this->groupId==3){ //渠道商
  127. $condition['channel_id'] = $this->adminId;
  128. }elseif($this->groupId==4){ //代理商
  129. $condition['agent_id'] = $this->adminId;
  130. }
  131. }
  132. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  133. $list = $this->model
  134. ->setConnect($_GET['search'])
  135. ->where($where)
  136. ->where($search)
  137. ->where($condition)
  138. ->order($sort, $order)
  139. ->limit($offset, $limit)
  140. ->select();
  141. $result = array("total" => count($list), "rows" => $list);
  142. return json($result);
  143. }
  144. return $this->view->fetch();
  145. }
  146. /**
  147. * 禁用用户
  148. * @param $ids
  149. * @return string
  150. * @throws Exception
  151. */
  152. public function disable($ids = ''){
  153. if($this->request->isAjax()){
  154. $redis = Redis::instance();
  155. $where = $this->request->param("row/a");
  156. if (ApiService::instance()->checkApiOn()) {
  157. $userUpdate = new UserUpdate();
  158. $userUpdate->setId($ids)->setState($where['state']);
  159. $result = \app\source\service\UserService::instance()->updateUser($userUpdate)->id;
  160. } else{
  161. $result = UserService::instance()->update(['state'=>$where['state']], ['id'=>$ids]);
  162. $redis->del('UN:' . $where['id']);
  163. }
  164. if($result){
  165. $this->success();
  166. }else{
  167. $this->error('修改失败');
  168. }
  169. }else{
  170. if(!$ids){
  171. $this->error('参数错误');
  172. }
  173. $row = UserService::instance()->getUserModel()->getUserInfo($ids);
  174. // $row = $this->model->setConnect($ids)->where('id',$ids)->find();
  175. $this->assign('row',$row);
  176. return $this->view->fetch();
  177. }
  178. }
  179. /**
  180. * 批量禁用用户
  181. * @return string
  182. * @throws Exception
  183. */
  184. public function batchIsDisabled(){
  185. if($this->request->isAjax()){
  186. $redis = Redis::instance();
  187. $err_ids = [];
  188. $where = $this->request->param("row/a");
  189. if($ids = array_filter(explode(',',$where['id']))){
  190. foreach($ids as $user_id){
  191. // if(false === $this->model->setConnect($user_id)->where('id',$user_id)->update(['state'=>$where['state']])){
  192. if (ApiService::instance()->checkApiOn()) {
  193. $userUpdate = new UserUpdate();
  194. $userUpdate->setId($user_id)->setState($where['state']);
  195. $result = \app\source\service\UserService::instance()->updateUser($userUpdate)->id;
  196. } else{
  197. $result = UserService::instance()->update(['state'=>$where['state']], ['id'=>$ids]);
  198. $redis->del('UN:' . $where['id']);
  199. }
  200. if(!$result){
  201. array_push($err_ids,$user_id);
  202. }
  203. }
  204. }else{
  205. $this->error('用户ID为空');
  206. }
  207. if(count($err_ids)){
  208. $this->error("用户ID: [".implode(',',$err_ids)."] 修改失败");
  209. }
  210. $this->success();
  211. }else{
  212. return $this->view->fetch();
  213. }
  214. }
  215. //渠道商查询用户
  216. public function chsearch(){
  217. $res = null;
  218. $id = '';
  219. $search = '';
  220. if(!empty($_POST['search'])){
  221. $search = $_POST['search'];
  222. if(isMobile($search)){
  223. $user_phone_data = UserService::instance()->getUserIdByPhone($search);
  224. if($user_phone_data){
  225. $id = $user_phone_data['uid'];
  226. }
  227. }else{
  228. $id = $search;
  229. }
  230. $admin_id = $this->adminId;
  231. if($id){
  232. if (ApiService::instance()->checkApiOn()) {
  233. $res = \app\source\service\UserService::instance()->getUserInfo($id);
  234. } else {
  235. $res = UserService::instance()->getUserModel()->setConnect($id)->where('id', $id)->find();
  236. }
  237. if($this->group != AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE){
  238. if ($res->is_black) {
  239. $res = false;
  240. }
  241. }
  242. if ($res && !$res->id) {
  243. $res = false;
  244. }
  245. if ($res) {
  246. if ($this->groupId == AdminConstants::ADMIN_GROUP_ID_CHANNEL || $this->groupId == AdminConstants::ADMIN_GROUP_ID_AGENT) { //渠道商 or 代理商
  247. $count = OrderService::instance()->getOrderModel()
  248. ->where('user_id', $id)
  249. ->where('state', OrderContents::ORDER_STATE_PAID)
  250. ->where('deduct', OrderContents::ORDER_DEDUCT_YES)
  251. ->count();
  252. if ($res->flush_state != UserConstants::USER_FLUSH_STATE_ADMIN) {
  253. UserDdFlushService::instance()->checkUserFlushState($id);
  254. UserDdFlushService::instance()->flushVip($id);
  255. if ($count) {
  256. UserDdFlushService::instance()->flushUserConsume($id);
  257. }
  258. UserService::instance()->update(['flush_state' => UserConstants::USER_FLUSH_STATE_ADMIN], ['id' => $id]);
  259. // UserService::instance()->getUserModel()->setConnect($id)->update(['flush_state' => UserConstants::USER_FLUSH_STATE_ADMIN], ['id' => $id]);
  260. $cacheUser = CacheConstants::getUserCacheKey($id);
  261. Redis::instance()->del($cacheUser);
  262. } else if ($count) {
  263. $total_consume = $this->consumeModel->setConnect($id)
  264. ->where(['user_id' => $id])
  265. ->field('SUM(free_kandian-dd_free_kandian) as total_consume')
  266. ->find();
  267. $total_remain = FinancialService::instance()->getRechargeModel()
  268. ->setConnect($id)
  269. ->where('user_id', $id)
  270. ->where('dd', OrderContents::ORDER_DEDUCT_NO)
  271. ->field([
  272. 'sum(free_kandian-remain_free_kandian)' => 'total_remain',
  273. ])->find();
  274. if ($total_consume['total_consume'] != $total_remain['total_remain']) {
  275. UserDdFlushService::instance()->flushUserConsume($id);
  276. }
  277. }
  278. $o_count = model('Orders')->where(['user_id'=>$id,'state'=>'1', 'deduct'=> 0])->count();
  279. $res->is_pay = $o_count ? 1 : 0;
  280. $res->kandian = FinancialService::instance()->getTotalRemainKandian($id, 0)->data;
  281. } else {
  282. if ($this->groupId == AdminConstants::ADMIN_GROUP_ID_VIP || $this->groupId == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR){
  283. $res->kandian = FinancialService::instance()->getTotalRemainKandian($id, 0)->data;
  284. }else{
  285. $res->kandian = FinancialService::instance()->getTotalKandian($id)->data;
  286. }
  287. $count = OrderService::instance()->getOrderModel()
  288. ->where('user_id', $id)
  289. ->where('state', OrderContents::ORDER_STATE_PAID)
  290. ->where('deduct', OrderContents::ORDER_DEDUCT_YES)
  291. ->count();
  292. if ($res->flush_state != UserConstants::USER_FLUSH_STATE_ADMIN) {
  293. UserDdFlushService::instance()->checkUserFlushState($id);
  294. UserDdFlushService::instance()->flushVip($id);
  295. if ($count) {
  296. UserDdFlushService::instance()->flushUserConsume($id);
  297. }
  298. UserService::instance()->update(['flush_state' => UserConstants::USER_FLUSH_STATE_ADMIN], ['id' => $id]);
  299. } else if ($count) {
  300. $total_consume = $this->consumeModel->setConnect($id)
  301. ->where(['user_id' => $id])
  302. ->field('SUM(free_kandian-dd_free_kandian) as total_consume')
  303. ->find();
  304. $total_remain = FinancialService::instance()->getRechargeModel()
  305. ->setConnect($id)
  306. ->where('user_id', $id)
  307. ->where('dd', OrderContents::ORDER_DEDUCT_NO)
  308. ->field([
  309. 'sum(free_kandian-remain_free_kandian)' => 'total_remain',
  310. ])->find();
  311. if ($total_consume['total_consume'] != $total_remain['total_remain']) {
  312. UserDdFlushService::instance()->flushUserConsume($id);
  313. }
  314. }
  315. if ($this->groupId == AdminConstants::ADMIN_GROUP_ID_VIP || $this->groupId == AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR) {
  316. if (model("AuthGroupAccess")->getGroupId($res->channel_id) == 4) {
  317. $channelInfo = model("AdminExtend")->getInfo($res->channel_id);
  318. $channelId = $channelInfo['create_by'];
  319. } else {
  320. $channelId = $res->channel_id;
  321. }
  322. $vipids = model("VipAdminBind")->where("admin_id_master", $admin_id)->where("flag", 1)->column("admin_id_slave");
  323. if (!in_array($channelId, $vipids)){
  324. $res = [];
  325. }
  326. }
  327. }
  328. } else {
  329. $res = [];
  330. }
  331. }
  332. if($res){
  333. if($this->group == AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE){
  334. if($res->vip_endtime>time()){
  335. $res->isvip = 'VIP';
  336. }else{
  337. $res->isvip = '否';
  338. }
  339. }else{
  340. //
  341. $rechargeVipWhere = ['user_id' => $id ,'dd' => '0', 'type' => ['in', UserConstants::CHANNEL_VIP_TYPE]];
  342. $ddRechargObj = $this->rechargeModel->setConnect($id)->where($rechargeVipWhere)->order('id', 'desc')->find();
  343. if($ddRechargObj){
  344. $ddVipEndTime = $ddRechargObj->channel_vip_starttime + $ddRechargObj->day * 86400 + $ddRechargObj->hour * 3600;
  345. if ($ddVipEndTime > time()) {
  346. $res->isvip = 'VIP';
  347. } else {
  348. $res->isvip = '否';
  349. }
  350. $res->vip_endtime = $ddVipEndTime;
  351. }else{
  352. $res->isvip = '否';
  353. $res->vip_endtime = '-';
  354. }
  355. }
  356. $res = $res->toArray();
  357. }
  358. }
  359. // dump($res);
  360. $this->assign('groupId',$this->groupId);
  361. $this->assign('time',time());
  362. $this->assign('id',$id);
  363. $this->assign('searchkey',$search);
  364. $this->assign('user',$res);
  365. return $this->view->fetch();
  366. }
  367. public function detail($ids=0,$type=1,$pay=0){
  368. $this->hasAuth($ids);
  369. // $row = $this->model->join('admin','admin.id = user.channel_id','left')->where('user.id',$ids)->field('user.*,admin.nickname as channel')->find();
  370. $row = $this->model->setConnect($ids)->where('id',$ids)->find();
  371. $admin = model('admin')->where(['id'=>$row['channel_id']])->find();
  372. $row['admin'] = $admin;
  373. $adminConfig = model('AdminConfig')->where(['admin_id'=>$row['channel_id']])->find();
  374. $row['qrcode_image'] = !empty($adminConfig['qrcode_image']) ? $adminConfig['qrcode_image'] : ('https://open.weixin.qq.com/qr/code?username='.$adminConfig['json']['authorizer_info']['user_name']);
  375. //得到用户剩余免费书币
  376. $free_kandian = model('Recharge')->setConnect($ids)->where('free_endtime','gt',time())->where('user_id',$ids)->sum('remain_free_kandian');
  377. $old_free_kandian = model('Recharge')->setConnect($ids)->where('free_endtime','lt',time())->where('user_id',$ids)->sum('remain_free_kandian');
  378. //
  379. $site_url = getCurrentDomain($row['channel_id']);
  380. $this->view->assign('site_url',$site_url);
  381. $this->view->assign('free_kandian',$free_kandian);
  382. $this->view->assign('old_free_kandian',$old_free_kandian);
  383. $this->view->assign('row',$row);
  384. $this->view->assign('id',$ids);
  385. $this->view->assign('type',$type);
  386. $this->view->assign('pay',$pay);
  387. $tj =0;
  388. $strName = '';
  389. if($type==1){ //充值记录
  390. $strName = '充值金额';
  391. $tj = model('Orders')->where(['user_id'=>$ids,'state'=>'1'])->sum('money');
  392. $tj = '¥'.$tj;
  393. //dump(model('Orders')->getLastSql());
  394. //全部充值记录
  395. if($pay == 1){ //待支付
  396. $Czjl = model('Orders')->where(['user_id'=>$ids,'state'=>0])->order('id desc')->paginate(10);
  397. }elseif($pay == 2){ //已支付
  398. $Czjl = model('Orders')->where(['user_id'=>$ids,'state'=>'1'])->order('id desc')->paginate(10);
  399. } else{ //全部
  400. $Czjl = model('Orders')->where(['user_id'=>$ids])->order('id desc')->paginate(10);
  401. }
  402. if($Czjl){
  403. foreach($Czjl as $key=>$val){
  404. $Czjl[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
  405. }
  406. }
  407. $this->assign('list',$Czjl);
  408. //dump($Czjl);die;
  409. }elseif($type==2){ //消费记录
  410. $strName = '书币总消费';
  411. $xfkd = model('consume')->setConnect($ids)->where(['user_id'=>$ids])->sum('kandian');
  412. $xffreekd = model('consume')->setConnect($ids)->where(['user_id'=>$ids])->sum('free_kandian');
  413. $tj = $xfkd + $xffreekd;
  414. //dump(Db::connect(get_db_connect($ids))->getLastSql());
  415. $xfjl = model('consume')->setConnect($ids)->where(['user_id'=>$ids])->order('id desc')->paginate(10);
  416. $data = $xfjl->all();
  417. foreach($data as $key=>$val){
  418. $data[$key]['kandian'] = intval($val['free_kandian'])+intval($val['kandian']);
  419. $data[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
  420. }
  421. $this->assign('data',$data);
  422. $this->view->assign('list',$xfjl);
  423. }elseif($type==3){ //阅读记录
  424. $strName = '阅读书籍量';
  425. $ydjl = model('user_recently_read')->setConnect($ids)->where(['user_id'=>$ids])->order('updatetime desc')->paginate(10);
  426. $tj = model('user_recently_read')->setConnect($ids)->where(['user_id'=>$ids])->count('id');
  427. $data = $ydjl->all();
  428. foreach ($data as $key => $item) {
  429. $bookName = model('Book')->where(['id'=>$item['book_id']])->find();
  430. if($bookName){
  431. $data[$key]['bookname'] = $bookName->name;
  432. }else{
  433. $data[$key]['bookname'] = '未知';
  434. }
  435. $data[$key]['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
  436. $data[$key]['updatetime'] = date('Y-m-d H:i:s',$item['updatetime']);
  437. }
  438. $this->view->assign('data',$data);
  439. $this->view->assign('list',$ydjl);
  440. }elseif($type==4){ //充值记录
  441. $strName='充值记录';
  442. $rlist = model('recharge')->setConnect($ids)->where(['user_id'=>$ids])->order('id desc')->paginate(10);
  443. $data = $rlist->all();
  444. foreach ($data as $key => $item) {
  445. if($item['type']=='1'){
  446. if(empty($item['free_endtime'])){
  447. $data[$key]['type'] = '看点充值';
  448. }else{
  449. $data[$key]['type'] = '赠送看点';
  450. }
  451. }elseif($item['type']=='2'){
  452. $data[$key]['type'] = 'VIP充值';
  453. }elseif($item['type']=='3'){
  454. $data[$key]['type'] = '系统操作看点';
  455. }elseif($item['type']=='4'){
  456. $data[$key]['type'] = '系统操作vip';
  457. }else{
  458. $data[$key]['type'] = '签到';
  459. }
  460. $data[$key]['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
  461. }
  462. $this->view->assign('data',$data);
  463. $this->assign('list',$rlist);
  464. }
  465. $this->assign('tj',$tj);
  466. $this->assign('strName',$strName);
  467. return $this->view->fetch();
  468. }
  469. /**
  470. * 修改书币或vip时长
  471. * @param int $ids
  472. * @return int|string
  473. * @throws \Exception
  474. */
  475. public function kandian($ids=0){
  476. if ($this->request->isAjax())
  477. {
  478. $params = $this->request->post();
  479. if ($params){
  480. $err_list = [];
  481. $ids = strpos($params['userids'], '\r') ? explode("\r", $params['userids']) : explode("\n", $params['userids']);
  482. if (!trim($params['userids'])) {
  483. $err_list[] = "请填写要修改的用户id";
  484. }
  485. foreach ($ids as $k => $v) {
  486. $v = trim($v);
  487. if (empty($v)) {
  488. unset($ids[$k]);
  489. } else {
  490. $ids[$k] = $v;
  491. }
  492. }
  493. $ids = array_unique($ids); //去重
  494. $changeType = $params['changeType'];
  495. $kandianType = $params['kandianType'];
  496. $vipType = $params['vipType'];
  497. $kandian = intval($params['kandian']);
  498. $free_kandian = intval($params['free_kandian']);
  499. $czlx = $params['czlx']; //操作类型 1.活动奖励 2.系统操作
  500. $operateAdmin = $this->auth->getUserInfo();
  501. $bz = $params['bz'] . "--操作人ID:{$operateAdmin['id']}--操作人昵称:{$operateAdmin['nickname']}"; //备注
  502. $day = intval($params['day']);
  503. $hour = intval($params['hour']);
  504. $errIds = '';
  505. if($changeType == 1){ //修改书币
  506. foreach($ids as $id){
  507. if (FinancialService::instance()->modifyUserKandian($id, $kandianType, $czlx, $kandian, $free_kandian, $bz,0,20)->code != ErrorCodeConstants::SUCCESS) {
  508. $errIds .= $id . ','; //记录操作失败的id
  509. }
  510. }
  511. }elseif($changeType == 2){ //修改VIP
  512. foreach($ids as $id){
  513. if (!$this->editVipTime($id, $vipType, $czlx, $day, $hour, $bz)) {
  514. $errIds .= $id . ','; //记录操作失败的id
  515. }
  516. }
  517. }
  518. if($errIds){
  519. $errIds= substr($errIds,0,-1);
  520. $err_list[] = 'ID为('.$errIds.')的用户执行失败';
  521. }
  522. if(count($err_list)>0){
  523. $errMsg = implode('<br/>',$err_list);
  524. $errArr = [];
  525. $errArr['error_code'] = 1;
  526. $errArr['msg'] = $errMsg;
  527. return json_encode($errArr,JSON_UNESCAPED_UNICODE);
  528. }else {
  529. $errArr = [];
  530. $errArr['error_code'] = 0;
  531. $errArr['msg'] = '执行成功';
  532. return json_encode($errArr,JSON_UNESCAPED_UNICODE);
  533. }
  534. }
  535. }
  536. if($ids){
  537. $this->view->assign('id',$ids);
  538. }else{
  539. $this->view->assign('id','');
  540. }
  541. return $this->view->fetch();
  542. }
  543. //处理修改vip时长逻辑
  544. public function editVipTime($id,$vipType,$czlx,$day=0,$hour=0,$bz=''){
  545. $user = $this->model->getUserInfo($id);
  546. if($user){
  547. $saveDate = []; //新增充值记录
  548. $userDate = []; //修改用户数据
  549. $userDate['id'] = $id;
  550. $saveDate['user_id'] = $id;
  551. $saveDate['type'] = 4;
  552. $saveDate['notes'] = $bz;
  553. $saveDate['edit_type'] = $czlx;
  554. $saveDate['createtime'] = time();
  555. if($day){
  556. if($vipType == 1){
  557. $saveDate['day'] = $day;
  558. }else{
  559. $saveDate['day'] = $day*-1;
  560. }
  561. }
  562. if($hour){
  563. if($vipType == 1){
  564. $saveDate['hour'] = $hour;
  565. }else{
  566. $saveDate['hour'] = $hour*-1;
  567. }
  568. }
  569. if($vipType==1){ //增加VIP时长
  570. if($day || $hour){
  571. if(intval($user['vip_endtime'])>time()){
  572. $userDate['vip_endtime'] = $day*86400 + $hour*3600 + intval($user['vip_endtime']);
  573. $saveDate['vip_starttime'] = intval($user['vip_endtime']);
  574. }else{
  575. $userDate['vip_endtime'] = $day*86400 + $hour*3600 +time();
  576. $saveDate['vip_starttime'] = time();
  577. }
  578. $vipAddTime = $day * 86400 + $hour * 3600;
  579. $saveDate['vip_starttime'] = model('Recharge')->getVipStartTime($id);
  580. $saveDate['channel_vip_starttime'] = model('Recharge')->getChannelVipStartTime($id);
  581. }
  582. }elseif($vipType==2){ //减少VIP时长
  583. if(intval($user['vip_endtime'])>time()) {
  584. $times = $day*86400 + $hour*3600;
  585. if($user['vip_endtime'] > $times){
  586. $userDate['vip_endtime'] = intval($user['vip_endtime']) - $times;
  587. }else{
  588. $userDate['vip_endtime'] = 0;
  589. }
  590. $vipAddTime = -$times;
  591. $saveDate['vip_starttime'] = model('Recharge')->getVipStartTime($id);
  592. $saveDate['channel_vip_starttime'] = model('Recharge')->getChannelVipStartTime($id);
  593. }
  594. }
  595. if (isset($vipAddTime)) {
  596. if (ApiService::instance()->checkApiOn()) {
  597. $userUpdate = new UserUpdate();
  598. $userUpdate->setAddvipday($vipAddTime)->setId($id);
  599. $flagUser = \app\source\service\UserService::instance()->updateUser($userUpdate);
  600. } else {
  601. $flagUser = $this->model->setConnect($id)->update($userDate); //更改用户表数据
  602. //user加入redis缓存
  603. $userKey = 'UN:'.$id;
  604. if($this->redis->exists($userKey)){
  605. $this->redis->hmset($userKey,$userDate);
  606. $this->redis->expire($userKey,86400);
  607. }
  608. }
  609. }
  610. $flagRecharge = model('Recharge')->setConnect($id)->insert($saveDate); //插入充值记录
  611. if(!$flagRecharge || !$flagUser){
  612. return false;
  613. }
  614. }else{ //没有此用户
  615. return false;
  616. }
  617. return true;
  618. }
  619. /**
  620. * 从黑名单中踢出,加入白名单
  621. * @param $ids 用户ID
  622. */
  623. public function ridBlack(){
  624. $ids = $this->request->param('userid');
  625. if (ApiService::instance()->checkApiOn()) {
  626. $updateUser = new UserUpdate();
  627. $updateUser->setId($ids)->setIsBlack(UserConstants::USER_BLACK_NO)->setIsWhite(UserConstants::USER_WHITE_YES);
  628. \app\source\service\UserService::instance()->updateUser($updateUser);
  629. } else {
  630. $this->model->setConnect($ids)->update(['is_black' => UserConstants::USER_BLACK_NO, 'is_white' => UserConstants::USER_WHITE_YES], ['id' => $ids]);
  631. Redis::instance()->del("UN:{$ids}");
  632. }
  633. $this->success('操作成功');
  634. }
  635. //判断权限
  636. public function hasAuth($ids=0){
  637. $condition = [];
  638. $condition['id'] = $ids;
  639. if($this->groupId==3 || $this->groupId==4){ //渠道商 or 代理商
  640. $condition['channel_id'] = $this->adminId;
  641. }
  642. $isFather = $this->model->setConnect($ids)->where($condition)->find();
  643. if(empty($isFather)){
  644. $this->error('您没有此权限','/admin/auth/user?ref=addtabs');
  645. }
  646. }
  647. }