MemberService.php 26 KB

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