MemberService.php 29 KB

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