MemberService.php 27 KB

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