MemberService.php 25 KB

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