MemberService.php 27 KB

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