MemberService.php 26 KB

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