MemberService.php 25 KB

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