MemberService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ActionLogModel;
  13. use App\Models\MemberBankModel;
  14. use App\Models\MemberModel;
  15. use App\Models\OrderModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\JwtService;
  19. use App\Services\MpService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. use phpQrcode\QRcode;
  23. /**
  24. * 会员管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * Class MemberService
  28. * @package App\Services\Api
  29. */
  30. class MemberService extends BaseService
  31. {
  32. // 静态对象
  33. protected static $instance = null;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/11
  38. * MemberService constructor.
  39. */
  40. public function __construct()
  41. {
  42. $this->model = new MemberModel();
  43. }
  44. /**
  45. * 静态入口
  46. * @return MemberService|static|null
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = new static();
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * 验证账号
  57. * @param $code
  58. * @param array $params
  59. * @return array|false
  60. */
  61. public function login($code, $params = [])
  62. {
  63. // 账号登录
  64. if (empty($code)) {
  65. $this->error = 1041;
  66. return false;
  67. }
  68. // 获取用户信息
  69. $result = MpService::make()->getUserInfo($code);
  70. $openid = isset($result['openid']) ? $result['openid'] : '';
  71. if (empty($openid)) {
  72. $this->error = 1042;
  73. return false;
  74. }
  75. // 验证是否注册,没有则注册
  76. $where = ['openid' => $openid,'mark'=>1];
  77. $data = $this->model->where($where)
  78. ->select(['id', 'openid', 'mobile', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  79. ->first();
  80. $data = $data ? $data->toArray() : [];
  81. $userId = isset($data['id']) ? $data['id'] : 0;
  82. $status = isset($data['status']) ? $data['status'] : 0;
  83. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  84. $nickname = isset($data['nickname']) ? $data['nickname'] : '';
  85. $avatar = isset($data['nickname']) ? $data['avatar'] : '';
  86. if($data && $status!= 1){
  87. $this->error = '账号已被冻结,请联系客服~';
  88. return false;
  89. }
  90. // 未注册或未完善资料
  91. if(empty($data) || empty($nickname) || empty($mobile)){
  92. $this->error = '未注册';
  93. return [
  94. 'access_token'=>'',
  95. 'info'=>['uid'=>$userId, 'openid'=>$openid,'mobile'=>$mobile]
  96. ];
  97. }
  98. // 已注册
  99. $system = isset($params['system']) ? $params['system'] : [];
  100. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  101. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  102. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  103. $version = isset($system['app_version']) ? $system['app_version'] : '';
  104. if (!RedisService::get("caches:members:login_{$userId}")) {
  105. $updateData = [
  106. 'login_ip' => get_client_ip(),
  107. 'login_time' => time(),
  108. 'app_uuid' => $uuid,
  109. 'login_count' => DB::raw("login_count+1"),
  110. 'app_version' => $version,
  111. 'device' => $appSources == 'ios' ? 1 : 2,
  112. 'mark' => 1,
  113. ];
  114. $this->model->where(['id' => $userId])->update($updateData);
  115. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  116. }
  117. // 获取登录授权token
  118. $token = JwtService::make()->encode($userId);
  119. // 结果返回
  120. $result = [
  121. 'access_token' => $token,
  122. 'info' => ['uid' => $userId, 'openid' => $openid,'nickname'=>$nickname, 'mobile' => $mobile],
  123. ];
  124. // 用户缓存信息
  125. $this->error = 2019;
  126. $data['token'] = $token;
  127. unset($data['mobile']);
  128. RedisService::keyDel("caches:members:teamList*");
  129. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  130. return $result;
  131. }
  132. /**
  133. * 授权注册
  134. * @param $code
  135. * @param array $params
  136. * @return array|false
  137. */
  138. public function register($params = [])
  139. {
  140. $openid = isset($params['openid'])? $params['openid'] : '';
  141. $phone = isset($params['mobile'])? $params['mobile'] : '';
  142. $avatar = isset($params['avatar'])? $params['avatar'] : '';
  143. $nickname = isset($params['nickname'])? $params['nickname'] : '';
  144. if(empty($openid)){
  145. $this->error = '请先获取授权';
  146. return false;
  147. }
  148. // 手机号
  149. if (empty($phone)) {
  150. $this->error = '请先授权获取手机号';
  151. return false;
  152. }
  153. if(empty($avatar) || empty($nickname)){
  154. $this->error = '请先授权设置用户信息';
  155. return false;
  156. }
  157. $avatar = save_base64_image($avatar, 'avatar');
  158. // 验证是否注册,没有则注册
  159. $where = ['openid' => $openid,'mark'=>1];
  160. $data = $this->model->where($where)
  161. ->select(['id', 'openid', 'mobile', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  162. ->first();
  163. $data = $data ? $data->toArray() : [];
  164. $userId = isset($data['id']) ? $data['id'] : 0;
  165. $status = isset($data['status']) ? $data['status'] : 0;
  166. if ($data && $userId && $status != 1) {
  167. $this->error = '账号已被冻结,请来奶昔客服~';
  168. return false;
  169. }
  170. $coupon = [];
  171. $system = isset($params['system']) ? $params['system'] : [];
  172. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  173. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  174. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  175. $version = isset($system['app_version']) ? $system['app_version'] : '';
  176. if (empty($data)) {
  177. // 清理无效注册数据
  178. $this->model->where(['openid'=>$openid,'mark'=>0])->delete();
  179. // 用户ID
  180. $userId = $this->model->max('id') + 1;
  181. // 推荐人
  182. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  183. $parents = '';
  184. if ($rid) {
  185. $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1])
  186. ->select(['id', 'parent_id', 'parents', 'status'])
  187. ->first();
  188. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : '';
  189. if ($inviteInfo) {
  190. $parents = $parents ? $parents . $rid . ',' : ",{$rid},";
  191. }
  192. }else{
  193. $rid = 1;
  194. $parents = ',1,';
  195. }
  196. DB::beginTransaction();
  197. $data = [
  198. 'nickname' => $nickname,
  199. 'openid' => $openid,
  200. 'mobile' => $phone,
  201. 'avatar' => $avatar,
  202. 'parent_id' => $rid,
  203. 'parents' => $parents,
  204. 'code' => get_random_code(9, 'Q', $userId),
  205. 'password' => get_password('a123456'),
  206. 'login_ip' => get_client_ip(),
  207. 'create_time' => time(),
  208. 'login_time' => time(),
  209. 'login_count' => DB::raw("login_count+1"),
  210. 'app_version' => $version,
  211. 'app_uuid' => $uuid,
  212. 'device' => $appSources == 'ios' ? 1 : 2,
  213. ];
  214. if (!$userId = $this->model->insertGetId($data)) {
  215. DB::rollBack();
  216. $this->error = 2018;
  217. return false;
  218. }
  219. // 注册奖励优惠券
  220. if(!$coupon = SettleService::make()->registerReward($userId)){
  221. DB::rollBack();
  222. $this->error = 2018;
  223. return false;
  224. }
  225. DB::commit();
  226. } // 更新登录信息
  227. else if (!RedisService::get("caches:members:login_{$userId}")) {
  228. $updateData = [
  229. 'login_ip' => get_client_ip(),
  230. 'login_time' => time(),
  231. 'app_uuid' => $uuid,
  232. 'login_count' => DB::raw("login_count+1"),
  233. 'app_version' => $version,
  234. 'device' => $appSources == 'ios' ? 1 : 2,
  235. 'mark' => 1,
  236. ];
  237. $this->model->where(['id' => $userId])->update($updateData);
  238. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  239. }
  240. // 获取登录授权token
  241. $token = JwtService::make()->encode($userId);
  242. // 结果返回
  243. $result = [
  244. 'access_token' => $token,
  245. 'info' => ['uid' => $userId, 'openid' => $openid, 'mobile' => $data['mobile']],
  246. 'coupon'=>$coupon
  247. ];
  248. // 用户缓存信息
  249. $this->error = 2019;
  250. $data['token'] = $token;
  251. unset($data['mobile']);
  252. RedisService::keyDel("caches:members:teamList*");
  253. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  254. return $result;
  255. }
  256. /**
  257. * 重置密码
  258. * @param $params
  259. * @return array|false
  260. */
  261. public function forget($params)
  262. {
  263. // 账号登录
  264. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  265. $password = isset($params['password']) ? trim($params['password']) : '';
  266. if (empty($params) || empty($mobile) || empty($password)) {
  267. $this->error = 1041;
  268. return false;
  269. }
  270. // 验证是否注册
  271. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  272. $this->error = 1038;
  273. return false;
  274. }
  275. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  276. $this->error = 2030;
  277. return false;
  278. }
  279. // 操作日志
  280. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  281. ActionLogModel::record();
  282. $this->error = 2031;
  283. return true;
  284. }
  285. /**
  286. * 获取资料详情
  287. * @param $where
  288. * @param array $field
  289. */
  290. public function getInfo($where, array $field = [], $refresh = true)
  291. {
  292. if (empty($where)) {
  293. return false;
  294. }
  295. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  296. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  297. $info = RedisService::get($cacheKey);
  298. if ($info && !$refresh) {
  299. return $info;
  300. }
  301. $defaultField = ['id', 'user_type', 'realname', 'mobile','is_auth','member_level','vip_growth','vip_expired','idcard', 'nickname','parent_id', 'balance','bonus_total','withdraw_total', 'code', 'openid','create_time', 'status', 'avatar'];
  302. $field = $field ? $field : $defaultField;
  303. if (is_array($where)) {
  304. $info = $this->model->with(['parent'])->where(['mark' => 1])->where($where)->select($field)->first();
  305. } else {
  306. $info = $this->model->with(['parent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  307. }
  308. $info = $info ? $info->toArray() : [];
  309. if ($info) {
  310. $info['create_time'] = $info['create_time']?datetime(strtotime($info['create_time']),'Y-m-d H:i') : '';
  311. if (isset($info['mobile'])) {
  312. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  313. }
  314. $params = request()->all();
  315. $type = isset($params['type'])?$params['type']:'';
  316. if($type == 'qrcode'){
  317. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/index/index',"{$info['id']}");
  318. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  319. }
  320. if($type == 'center'){
  321. $info['uppers'] = $info['parent']?[$info['parent']]:[];
  322. $info['upper_count'] = $info['parent']?1: 0;
  323. $query = $this->model->where(['parent_id'=>$info['id'],'mark'=>1])->select(['id','avatar','nickname','status']);
  324. $query1 = clone $query;
  325. $info['user_count'] = $query1->count('id');
  326. $info['users'] = $query->limit(3)->get();
  327. // 成长值
  328. $levelGrowth = ConfigService::make()->getConfigByCode('vip_growth_'.$info['member_level']+1);
  329. $vipDiscount = ConfigService::make()->getConfigByCode('vip_level'.$info['member_level'].'_discount');
  330. $vipDiscount = $vipDiscount>0 && $vipDiscount<10?$vipDiscount:0;
  331. $info['vip_discount'] = intval($vipDiscount * 10);
  332. $info['vip_growth_diff'] = $levelGrowth>0?ceil(max(0,$levelGrowth-$info['vip_growth'])) : 0;
  333. $info['vip_growth_1'] = ConfigService::make()->getConfigByCode('vip_growth_1');
  334. $info['vip_growth_2'] = ConfigService::make()->getConfigByCode('vip_growth_2');
  335. $info['vip_growth_progress'] = $info['vip_growth_2']?round(min(100,$info['vip_growth']/$info['vip_growth_2'] * 100),2):0;
  336. $info['order_count_1'] = (int)OrderModel::where(['user_id'=>$info['id'],'status'=>1,'mark'=>1])->count('id');
  337. }
  338. RedisService::set($cacheKey, $info, rand(10, 20));
  339. }
  340. return $info;
  341. }
  342. /**
  343. * 认证资料
  344. * @param $userId
  345. * @return array|mixed
  346. */
  347. public function authInfo($userId)
  348. {
  349. $cacheKey = "caches:members:authInfo:{$userId}";
  350. $info = RedisService::get($cacheKey);
  351. if ($info) {
  352. return $info;
  353. }
  354. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  355. ->select(['id', 'realname','idcard','bank_name','bank_card','bank_branch','is_auth', 'status'])
  356. ->first();
  357. $info = $info?$info->toArray() : [];
  358. if($info){
  359. RedisService::set($cacheKey, $info, rand(300,600));
  360. }
  361. return $info;
  362. }
  363. /**
  364. * 绑定收款账户
  365. * @param $userId
  366. * @return array|mixed
  367. */
  368. public function bindAccount($userId, $params)
  369. {
  370. if($params['type']==1){
  371. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  372. ->select(['id','user_id','type','realname','account','account_remark','status'])
  373. ->first();
  374. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  375. $data = [
  376. 'type'=> 1,
  377. 'user_id'=> $userId,
  378. 'realname'=>$params['realname'],
  379. 'account'=>$params['account'],
  380. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  381. 'status'=>1
  382. ];
  383. if($alipayId){
  384. $data['update_time']=time();
  385. MemberBankModel::where(['id'=>$alipayId])->update($data);
  386. }else {
  387. $data['create_time']=time();
  388. MemberBankModel::insertGetId($data);
  389. }
  390. } else if($params['type']==2){
  391. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  392. ->select(['id','user_id','type','realname','account','account_remark','status'])
  393. ->first();
  394. $bankId = isset($banks['id'])?$banks['id'] : 0;
  395. $data = [
  396. 'type'=>2,
  397. 'user_id'=> $userId,
  398. 'realname'=>$params['realname'],
  399. 'account'=>$params['account'],
  400. 'account_remark'=>$params['account_remark'],
  401. 'status'=>1
  402. ];
  403. if($bankId){
  404. $data['update_time']=time();
  405. MemberBankModel::where(['id'=>$bankId])->update($data);
  406. }else {
  407. $data['create_time']=time();
  408. MemberBankModel::insertGetId($data);
  409. }
  410. }else{
  411. $this->error = '账号类型错误';
  412. return false;
  413. }
  414. RedisService::keyDel("caches:members:account:{$userId}*");
  415. $this->error = '绑定收款账号成功';
  416. return true;
  417. }
  418. /**
  419. * 团队人数
  420. * @param $uid
  421. * @return array|int|mixed
  422. */
  423. public function getTeamCount($uid)
  424. {
  425. $cacheKey = "caches:members:teamCount:{$uid}";
  426. $data = RedisService::get($cacheKey);
  427. if ($data) {
  428. return $data;
  429. }
  430. $data = $this->model->from('member as a')
  431. ->where('a.parents', 'like', "%,{$uid},%")
  432. ->where(['a.status' => 1, 'a.mark' => 1])
  433. ->count('id');
  434. if($data){
  435. RedisService::set($cacheKey, $data, rand(5,10));
  436. }
  437. return $data;
  438. }
  439. /**
  440. * 生成普通参数二维码
  441. * @param $str 参数
  442. * @param bool $refresh 是否重新生成
  443. * @return bool
  444. */
  445. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  446. {
  447. $basePath = base_path() . '/public';
  448. $qrFile = '/images/qrcode/';
  449. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  450. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  451. }
  452. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  453. $qrFile = $qrFile . "C_{$key}.png";
  454. $cacheKey = "caches:qrcodes:member_" . $key;
  455. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  456. return $qrFile;
  457. }
  458. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  459. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  460. return false;
  461. }
  462. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  463. return $qrFile;
  464. }
  465. /**
  466. * 修改信息
  467. * @param $userId
  468. * @param $params
  469. * @return bool
  470. */
  471. public function modify($userId, $params)
  472. {
  473. $cacheLockKey = "caches:members:modify_{$userId}";
  474. if (RedisService::get($cacheLockKey)) {
  475. $this->error = 1034;
  476. return false;
  477. }
  478. // 用户验证
  479. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  480. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  481. ->select(['id', 'nickname','avatar', 'status'])
  482. ->first();
  483. if (!$info || $info['status'] != 1) {
  484. $this->error = 2016;
  485. RedisService::clear($cacheLockKey);
  486. return false;
  487. }
  488. // 修改数据
  489. $data = ['update_time' => time()];
  490. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  491. if (isset($params['nickname']) && $nickname) {
  492. $data['nickname'] = $nickname;
  493. }
  494. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  495. if (isset($params['mobile']) && $mobile) {
  496. $data['mobile'] = $mobile;
  497. }
  498. $pushMessage = isset($params['push_message']) ? $params['push_message'] : '';
  499. if (isset($params['push_message']) && $pushMessage) {
  500. $data['push_message'] = $pushMessage;
  501. }
  502. $signMessage = isset($params['sign_message']) ? $params['sign_message'] : '';
  503. if (isset($params['sign_message']) && $signMessage) {
  504. $data['sign_message'] = $signMessage;
  505. }
  506. // 头像
  507. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  508. if (isset($params['avatar']) && $avatar) {
  509. $data['avatar'] = save_base64_image($avatar, 'avatar');
  510. }
  511. if (!$this->model->where(['id' => $userId])->update($data)) {
  512. $this->error = 1014;
  513. RedisService::clear($cacheLockKey);
  514. return false;
  515. }
  516. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  517. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  518. @unlink(ATTACHMENT_PATH . $oldAvatar);
  519. }
  520. $this->error = 1013;
  521. RedisService::clear($cacheLockKey);
  522. RedisService::clear("caches:members:authInfo:{$userId}");
  523. RedisService::clear("caches:members:info_{$userId}");
  524. return true;
  525. }
  526. /**
  527. * 获取团队列表
  528. * @param $userId
  529. * @param $params
  530. * @return array
  531. */
  532. public function getTeamList($userId,$params)
  533. {
  534. $page = isset($params['page'])?$params['page']: 1;
  535. $pageSize = isset($params['pageSize'])?$params['pageSize']: 12;
  536. $cacheKey = "caches:members:teamList_{$userId}:{$page}_".md5(json_encode($params));
  537. $list = RedisService::get($cacheKey);
  538. if ($list) {
  539. return [
  540. 'cache'=>true,
  541. 'pageSize'=> $pageSize,
  542. 'total'=>isset($list['total'])? $list['total'] : 0,
  543. 'list'=> isset($list['data'])? $list['data'] : []
  544. ];
  545. }
  546. $list = $this->model->from('member as a')
  547. ->where(['a.parent_id'=>$userId,'a.mark'=>1])
  548. ->where(function($query) use($params){
  549. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  550. if($keyword){
  551. $query->where(function($query) use($keyword){
  552. $query->where('a.realname','like',"%{$keyword}%")
  553. ->orWhere('a.nickname','like',"%{$keyword}%")
  554. ->orWhere('a.mobile','like',"%{$keyword}%");
  555. });
  556. }
  557. })
  558. ->select(['a.id','a.realname','a.mobile','a.nickname','a.parent_id','a.avatar','a.is_auth','a.create_time','a.status'])
  559. ->groupBy('a.id')
  560. ->orderBy('a.create_time','desc')
  561. ->orderBy('a.id','desc')
  562. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  563. $list = $list? $list->toArray() :[];
  564. $total = isset($list['total'])? $list['total'] : 0;
  565. if($total){
  566. RedisService::set($cacheKey, $list, rand(5,10));
  567. }
  568. return [
  569. 'pageSize'=> $pageSize,
  570. 'total'=>$total,
  571. 'list'=> isset($list['data'])? $list['data'] : []
  572. ];
  573. }
  574. /**
  575. * 账号注销
  576. * @param $userId
  577. * @return bool
  578. */
  579. public function logOff($userId)
  580. {
  581. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  582. ->select(['id', 'password', 'status'])
  583. ->first();
  584. $status = isset($info['status']) ? $info['status'] : 0;
  585. if (empty($info)) {
  586. $this->error = 2044;
  587. return false;
  588. }
  589. if ($status != 1) {
  590. $this->error = 2044;
  591. return false;
  592. }
  593. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  594. $this->error = 2049;
  595. return false;
  596. }
  597. $this->error = 2048;
  598. RedisService::clear("auths:info:" . $userId);
  599. return true;
  600. }
  601. }