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