MemberService.php 27 KB

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