MemberService.php 28 KB

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