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