MemberService.php 27 KB

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