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