MemberService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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\AccountStatisticsModel;
  13. use App\Models\ActionLogModel;
  14. use App\Models\MemberBankModel;
  15. use App\Models\MemberModel;
  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];
  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. $avatar = isset($data['avatar']) ? $data['avatar'] : '';
  83. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  84. $nickName = isset($data['nickname']) ? $data['nickname'] : '';
  85. $status = isset($data['status']) ? $data['status'] : 0;
  86. $mark = isset($data['mark']) ? $data['mark'] : 0;
  87. if ($data && $userId && $status != 1 && $mark == 1) {
  88. $this->error = 2015;
  89. return false;
  90. }
  91. $system = isset($params['system']) ? $params['system'] : [];
  92. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  93. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  94. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  95. $version = isset($system['app_version']) ? $system['app_version'] : '';
  96. if (empty($data)) {
  97. $userId = $this->model->max('id') + 1;
  98. // 推荐人
  99. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  100. $parents = '';
  101. if ($rid) {
  102. $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1])
  103. ->select(['id', 'parent_id', 'parents', 'status'])
  104. ->first();
  105. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : '';
  106. if ($inviteInfo) {
  107. $parents = $parents ? $parents . $rid . ',' : ",{$rid},";
  108. }
  109. }
  110. $data = [
  111. 'nickname' => '',
  112. 'openid' => $openid,
  113. 'mobile' => '',
  114. 'avatar' => '',
  115. 'parent_id' => $rid,
  116. 'parents' => $parents,
  117. 'code' => get_random_code(9, 'S', $userId),
  118. 'password' => get_password('a123456'),
  119. 'login_ip' => get_client_ip(),
  120. 'create_time' => time(),
  121. 'login_time' => time(),
  122. 'login_count' => DB::raw("login_count+1"),
  123. 'app_version' => $version,
  124. 'app_uuid' => $uuid,
  125. 'device' => $appSources == 'ios' ? 1 : 2,
  126. ];
  127. if (!$userId = $this->model->insertGetId($data)) {
  128. $this->error = 2018;
  129. return false;
  130. }
  131. } // 更新登录信息
  132. else if ($mark == 0 || !RedisService::get("caches:members:login_{$userId}")) {
  133. $updateData = [
  134. 'login_ip' => get_client_ip(),
  135. 'create_time' => time(),
  136. 'login_time' => time(),
  137. 'app_uuid' => $uuid,
  138. 'login_count' => DB::raw("login_count+1"),
  139. 'app_version' => $version,
  140. 'device' => $appSources == 'ios' ? 1 : 2,
  141. 'mark' => 1,
  142. ];
  143. if ($mark == 0) {
  144. $data['nickname'] = '';
  145. $data['avatar'] = '';
  146. $data['mobile'] = '';
  147. $data['openid'] = $openid;
  148. $nickName = '';
  149. $avatar = '';
  150. $mobile = '';
  151. }
  152. $this->model->where(['id' => $userId])->update($updateData);
  153. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  154. }
  155. // 如果未生成佣金账户则创建
  156. if(!$accountId = AccountStatisticsModel::where(['user_id'=>$userId])->value('id')){
  157. AccountStatisticsModel::insert(['user_id'=>$userId]);
  158. }
  159. // 获取登录授权token
  160. $token = JwtService::make()->encode($userId);
  161. // 结果返回
  162. $result = [
  163. 'access_token' => $token,
  164. 'info' => ['uid' => $userId, 'openid' => $openid, 'has_info' => $avatar && $nickName ? 1 : 0, 'mobile' => $mobile],
  165. ];
  166. // 用户缓存信息
  167. $this->error = 2019;
  168. $data['token'] = $token;
  169. unset($data['mobile']);
  170. RedisService::keyDel("caches:members:teamList*");
  171. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  172. return $result;
  173. }
  174. /**
  175. * 完善资料
  176. * @param $params
  177. * @return array|false
  178. */
  179. public function setProfile($params)
  180. {
  181. $id = isset($params['id'])? $params['id'] : 0;
  182. $code = isset($params['code'])? $params['code'] : '';
  183. $avatar = isset($params['avatar'])? $params['avatar'] : '';
  184. $nickname = isset($params['nickname'])? $params['nickname'] : '';
  185. if($id<=0){
  186. $this->error = '授权参数错误,请刷新重试';
  187. return false;
  188. }
  189. if(empty($avatar) || empty($nickname)){
  190. $this->error = '请先获取用户授权信息';
  191. return false;
  192. }
  193. $userInfo = $this->model->where(['id'=>$id,'mark'=>1])
  194. ->select(['id as uid', 'nickname','mobile', 'openid','avatar'])
  195. ->first();
  196. $phone = isset($userInfo['mobile'])? $userInfo['mobile'] : '';
  197. if(empty($userInfo)){
  198. $this->error = '授权登录失败,请刷新重试';
  199. return false;
  200. }
  201. if(empty($phone) && empty($code)){
  202. $this->error = '请先刷新重试,授权获取手机号';
  203. return false;
  204. }
  205. // 获取手机号信息
  206. if($code){
  207. $phoneData = MpService::make()->getPhoneNumber($code);
  208. $phoneData = isset($phoneData['phone_info']) ? $phoneData['phone_info'] : [];
  209. $phone = isset($phoneData['phoneNumber']) ? $phoneData['phoneNumber'] : '';
  210. if (empty($phone)) {
  211. $this->error = MpService::make()->getError();
  212. return false;
  213. }
  214. }
  215. $avatar = save_base64_image($avatar, 'avatar');
  216. if(!$this->model->where(['id'=>$id])->update(['mobile'=>$phone,'nickname'=>$nickname,'avatar'=>$avatar,'update_time'=>time()])){
  217. $this->error = '获取授权信息失败';
  218. return false;
  219. }
  220. $this->error = '登录成功';
  221. // 获取登录授权token
  222. $token = JwtService::make()->encode($id);
  223. RedisService::keyDel("caches:members:teamList*");
  224. return [
  225. 'access_token'=> $token,
  226. 'info'=> $userInfo
  227. ];
  228. }
  229. /**
  230. * 重置密码
  231. * @param $params
  232. * @return array|false
  233. */
  234. public function forget($params)
  235. {
  236. // 账号登录
  237. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  238. $password = isset($params['password']) ? trim($params['password']) : '';
  239. if (empty($params) || empty($mobile) || empty($password)) {
  240. $this->error = 1041;
  241. return false;
  242. }
  243. // 验证是否注册
  244. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  245. $this->error = 1038;
  246. return false;
  247. }
  248. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  249. $this->error = 2030;
  250. return false;
  251. }
  252. // 操作日志
  253. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  254. ActionLogModel::record();
  255. $this->error = 2031;
  256. return true;
  257. }
  258. /**
  259. * 获取资料详情
  260. * @param $where
  261. * @param array $field
  262. */
  263. public function getInfo($where, array $field = [], $refresh = true)
  264. {
  265. if (empty($where)) {
  266. return false;
  267. }
  268. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  269. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  270. $info = RedisService::get($cacheKey);
  271. if ($info && !$refresh) {
  272. return $info;
  273. }
  274. $defaultField = ['id', 'user_type', 'realname', 'mobile','is_auth','idcard', 'nickname','company','position','department','parent_id', 'balance', 'code', 'openid','create_time', 'status', 'avatar'];
  275. $field = $field ? $field : $defaultField;
  276. if (is_array($where)) {
  277. $info = $this->model->with(['account','parent'])->where(['mark' => 1])->where($where)->select($field)->first();
  278. } else {
  279. $info = $this->model->with(['account','parent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  280. }
  281. $info = $info ? $info->toArray() : [];
  282. if ($info) {
  283. $info['create_time'] = $info['create_time']?datetime(strtotime($info['create_time']),'Y-m-d H:i') : '';
  284. $info['position_text'] = $info['department'] && $info['position']? $info['department'].'/'.$info['position'] : '';
  285. if (isset($info['mobile'])) {
  286. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  287. }
  288. $params = request()->all();
  289. $type = isset($params['type'])?$params['type']:'';
  290. if($type == 'qrcode'){
  291. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/index/index',"share&rid={$info['id']}");
  292. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  293. }
  294. if($type == 'center'){
  295. $info['uppers'] = $info['parent']?[$info['parent']]:[];
  296. $info['upper_count'] = $info['parent']?1: 0;
  297. $query = $this->model->where(['parent_id'=>$info['id'],'mark'=>1])->select(['id','avatar','nickname','status']);
  298. $query1 = clone $query;
  299. $info['user_count'] = $query1->count('id');
  300. $info['users'] = $query->limit(3)->get();
  301. }
  302. RedisService::set($cacheKey, $info, rand(10, 20));
  303. }
  304. return $info;
  305. }
  306. /**
  307. * 认证资料
  308. * @param $userId
  309. * @return array|mixed
  310. */
  311. public function authInfo($userId)
  312. {
  313. $cacheKey = "caches:members:authInfo:{$userId}";
  314. $info = RedisService::get($cacheKey);
  315. if ($info) {
  316. return $info;
  317. }
  318. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  319. ->select(['id', 'realname','company','department','position','idcard','bank_name','bank_card','bank_branch','is_auth', 'status'])
  320. ->first();
  321. $info = $info?$info->toArray() : [];
  322. if($info){
  323. RedisService::set($cacheKey, $info, rand(300,600));
  324. }
  325. return $info;
  326. }
  327. /**
  328. * 绑定收款账户
  329. * @param $userId
  330. * @return array|mixed
  331. */
  332. public function bindAccount($userId, $params)
  333. {
  334. if($params['type']==1){
  335. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  336. ->select(['id','user_id','type','realname','account','account_remark','status'])
  337. ->first();
  338. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  339. $data = [
  340. 'type'=> 1,
  341. 'user_id'=> $userId,
  342. 'realname'=>$params['realname'],
  343. 'account'=>$params['account'],
  344. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  345. 'status'=>1
  346. ];
  347. if($alipayId){
  348. $data['update_time']=time();
  349. MemberBankModel::where(['id'=>$alipayId])->update($data);
  350. }else {
  351. $data['create_time']=time();
  352. MemberBankModel::insertGetId($data);
  353. }
  354. } else if($params['type']==2){
  355. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  356. ->select(['id','user_id','type','realname','account','account_remark','status'])
  357. ->first();
  358. $bankId = isset($banks['id'])?$banks['id'] : 0;
  359. $data = [
  360. 'type'=>2,
  361. 'user_id'=> $userId,
  362. 'realname'=>$params['realname'],
  363. 'account'=>$params['account'],
  364. 'account_remark'=>$params['account_remark'],
  365. 'status'=>1
  366. ];
  367. if($bankId){
  368. $data['update_time']=time();
  369. MemberBankModel::where(['id'=>$bankId])->update($data);
  370. }else {
  371. $data['create_time']=time();
  372. MemberBankModel::insertGetId($data);
  373. }
  374. }else{
  375. $this->error = '账号类型错误';
  376. return false;
  377. }
  378. RedisService::keyDel("caches:members:account:{$userId}*");
  379. $this->error = '绑定收款账号成功';
  380. return true;
  381. }
  382. /**
  383. * 团队人数
  384. * @param $uid
  385. * @return array|int|mixed
  386. */
  387. public function getTeamCount($uid)
  388. {
  389. $cacheKey = "caches:members:teamCount:{$uid}";
  390. $data = RedisService::get($cacheKey);
  391. if ($data) {
  392. return $data;
  393. }
  394. $data = $this->model->from('member as a')
  395. ->where('a.parents', 'like', "%,{$uid},%")
  396. ->where(['a.status' => 1, 'a.mark' => 1])
  397. ->count('id');
  398. if($data){
  399. RedisService::set($cacheKey, $data, rand(5,10));
  400. }
  401. return $data;
  402. }
  403. /**
  404. * 生成普通参数二维码
  405. * @param $str 参数
  406. * @param bool $refresh 是否重新生成
  407. * @return bool
  408. */
  409. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  410. {
  411. $basePath = base_path() . '/public';
  412. $qrFile = '/images/qrcode/';
  413. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  414. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  415. }
  416. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  417. $qrFile = $qrFile . "C_{$key}.png";
  418. $cacheKey = "caches:qrcodes:member_" . $key;
  419. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  420. return $qrFile;
  421. }
  422. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  423. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  424. return false;
  425. }
  426. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  427. return $qrFile;
  428. }
  429. /**
  430. * 修改信息
  431. * @param $userId
  432. * @param $params
  433. * @return bool
  434. */
  435. public function modify($userId, $params)
  436. {
  437. $cacheLockKey = "caches:members:modify_{$userId}";
  438. if (RedisService::get($cacheLockKey)) {
  439. $this->error = 1034;
  440. return false;
  441. }
  442. // 用户验证
  443. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  444. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  445. ->select(['id', 'nickname','avatar','company','position','department', 'status'])
  446. ->first();
  447. if (!$info || $info['status'] != 1) {
  448. $this->error = 2016;
  449. RedisService::clear($cacheLockKey);
  450. return false;
  451. }
  452. // 修改数据
  453. $data = ['update_time' => time()];
  454. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  455. if (isset($params['nickname']) && $nickname) {
  456. $data['nickname'] = $nickname;
  457. }
  458. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  459. if (isset($params['mobile']) && $mobile) {
  460. $data['mobile'] = $mobile;
  461. }
  462. $company = isset($params['company']) ? $params['company'] : '';
  463. if (isset($params['company']) && $company) {
  464. $data['company'] = $company;
  465. }
  466. $department = isset($params['department']) ? $params['department'] : '';
  467. if (isset($params['department']) && $department) {
  468. $data['department'] = $department;
  469. }
  470. $position = isset($params['position']) ? $params['position'] : '';
  471. if (isset($params['position']) && $position) {
  472. $data['position'] = $position;
  473. }
  474. // 头像
  475. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  476. if (isset($params['avatar']) && $avatar) {
  477. $data['avatar'] = save_base64_image($avatar, 'avatar');
  478. }
  479. if (!$this->model->where(['id' => $userId])->update($data)) {
  480. $this->error = 1014;
  481. RedisService::clear($cacheLockKey);
  482. return false;
  483. }
  484. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  485. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  486. @unlink(ATTACHMENT_PATH . $oldAvatar);
  487. }
  488. $this->error = 1013;
  489. RedisService::clear($cacheLockKey);
  490. RedisService::clear("caches:members:authInfo:{$userId}");
  491. RedisService::clear("caches:members:info_{$userId}");
  492. return true;
  493. }
  494. /**
  495. * 认证
  496. * @param $userId
  497. * @param $params
  498. * @return bool
  499. */
  500. public function auth($userId, $params)
  501. {
  502. $cacheLockKey = "caches:members:auth_{$userId}";
  503. if (RedisService::get($cacheLockKey)) {
  504. $this->error = 1034;
  505. return false;
  506. }
  507. // 用户验证
  508. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  509. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  510. ->select(['id', 'realname','idcard','is_auth', 'status'])
  511. ->first();
  512. if (!$info || $info['status'] != 1) {
  513. $this->error = '账号或已被冻结,请联系客服';
  514. RedisService::clear($cacheLockKey);
  515. return false;
  516. }
  517. if($info['is_auth'] == 1 && $info['idcard'] && $info['realname']){
  518. $this->error = '抱歉,您已完成认证';
  519. RedisService::clear($cacheLockKey);
  520. return false;
  521. }
  522. // 认证数据
  523. $data = [
  524. 'realname'=> isset($params['realname'])?$params['realname'] : '',
  525. 'company'=> isset($params['company'])?$params['company'] : '',
  526. 'idcard'=> isset($params['idcard'])?$params['idcard'] : '',
  527. 'is_auth'=> 1,
  528. 'update_time' => time()
  529. ];
  530. if (!$this->model->where(['id' => $userId])->update($data)) {
  531. $this->error = '认证提交失败';
  532. RedisService::clear($cacheLockKey);
  533. return false;
  534. }
  535. $this->error = '恭喜您,已完成认证';
  536. RedisService::clear($cacheLockKey);
  537. RedisService::clear("caches:members:authInfo:{$userId}");
  538. RedisService::keyDel("caches:members:teamList*");
  539. return true;
  540. }
  541. /**
  542. * 获取团队列表
  543. * @param $userId
  544. * @param $params
  545. * @return array
  546. */
  547. public function getTeamList($userId,$params)
  548. {
  549. $page = isset($params['page'])?$params['page']: 1;
  550. $pageSize = isset($params['pageSize'])?$params['pageSize']: 12;
  551. $cacheKey = "caches:members:teamList_{$userId}:{$page}_".md5(json_encode($params));
  552. $list = RedisService::get($cacheKey);
  553. if ($list) {
  554. return [
  555. 'cache'=>true,
  556. 'pageSize'=> $pageSize,
  557. 'total'=>isset($list['total'])? $list['total'] : 0,
  558. 'list'=> isset($list['data'])? $list['data'] : []
  559. ];
  560. }
  561. $list = $this->model->with(['account'])->from('member as a')
  562. ->leftJoin('account_statistics as b','b.user_id','=','a.id')
  563. ->where(['a.parent_id'=>$userId,'b.state'=>1,'a.mark'=>1])
  564. ->where(function($query) use($params){
  565. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  566. if($keyword){
  567. $query->where(function($query) use($keyword){
  568. $query->where('a.realname','like',"%{$keyword}%")
  569. ->orWhere('a.nickname','like',"%{$keyword}%")
  570. ->orWhere('a.mobile','like',"%{$keyword}%");
  571. });
  572. }
  573. })
  574. ->select(['a.id','a.realname','a.mobile','a.nickname','a.parent_id','a.avatar','a.is_auth','a.status'])
  575. ->orderBy('a.id','desc')
  576. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  577. $list = $list? $list->toArray() :[];
  578. $total = isset($list['total'])? $list['total'] : 0;
  579. if($total){
  580. RedisService::set($cacheKey, $list, rand(5,10));
  581. }
  582. return [
  583. 'pageSize'=> $pageSize,
  584. 'total'=>$total,
  585. 'list'=> isset($list['data'])? $list['data'] : []
  586. ];
  587. }
  588. /**
  589. * 设置账户参数
  590. * @param $userId
  591. * @param $params
  592. * @return array|false|mixed|string
  593. */
  594. public function setting($userId, $params)
  595. {
  596. $apiUrl = ConfigService::make()->getConfigByCode('bonus_settle_url','');
  597. if(empty($apiUrl)){
  598. $this->error = '设置失败,参数错误';
  599. return false;
  600. }
  601. $token = request()->headers->get('Authorization');
  602. $token = str_replace("Bearer ", null, $token);
  603. $header = [
  604. 'authorization: '.$token
  605. ];
  606. $position = isset($params['position'])?trim($params['position']): '';
  607. $point = isset($params['commission_point'])?floatval($params['commission_point']): 0;
  608. $result = httpRequest($apiUrl.'/team/setting',['id'=>$userId,'position'=>$position,'point'=>$point],'post','',5,$header);
  609. $err = isset($result['err']) && $result['err']?$result['err'] : -1;
  610. $msg = isset($result['msg']) && $result['msg']?$result['msg'] : '1003';
  611. $data = isset($result['data']) && $result['data']?$result['data'] : [];
  612. if($err==0){
  613. $this->error = '操作成功';
  614. return $data;
  615. }else{
  616. $this->error = $msg;
  617. return false;
  618. }
  619. }
  620. /**
  621. * 账号注销
  622. * @param $userId
  623. * @return bool
  624. */
  625. public function logOff($userId)
  626. {
  627. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  628. ->select(['id', 'password', 'status'])
  629. ->first();
  630. $status = isset($info['status']) ? $info['status'] : 0;
  631. if (empty($info)) {
  632. $this->error = 2044;
  633. return false;
  634. }
  635. if ($status != 1) {
  636. $this->error = 2044;
  637. return false;
  638. }
  639. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  640. $this->error = 2049;
  641. return false;
  642. }
  643. $this->error = 2048;
  644. RedisService::clear("auths:info:" . $userId);
  645. return true;
  646. }
  647. }