MemberService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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\GoodsModel;
  14. use App\Models\MemberBankModel;
  15. use App\Models\MemberModel;
  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','area_id','buy_type', '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' => ['id' => $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("caches:index:area_".get_client_ip(),['uid'=>$userId,'area_id'=>$data['area_id'],'buy_type'=>$data['buy_type']], 6 * 30 * 86400);
  129. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  130. RedisService::set("caches:members:cacheInfo_{$userId}", $data, 600);
  131. return $result;
  132. }
  133. /**
  134. * 授权注册
  135. * @param $code
  136. * @param array $params
  137. * @return array|false
  138. */
  139. public function register($params = [])
  140. {
  141. $openid = isset($params['openid'])? $params['openid'] : '';
  142. $areaId = isset($params['area_id']) && $params['area_id']? $params['area_id'] : 0;
  143. $phone = isset($params['mobile'])? $params['mobile'] : '';
  144. $avatar = isset($params['avatar'])? $params['avatar'] : '';
  145. $nickname = isset($params['nickname'])? $params['nickname'] : '';
  146. if(empty($openid)){
  147. $this->error = '请先获取授权';
  148. return false;
  149. }
  150. // 所属区域
  151. if($areaId<=0){
  152. $this->error = '请设置您所在区域';
  153. return false;
  154. }
  155. // 手机号
  156. if (empty($phone)) {
  157. $this->error = '请先授权获取手机号';
  158. return false;
  159. }
  160. if(empty($avatar) || empty($nickname)){
  161. $this->error = '请先授权设置用户信息';
  162. return false;
  163. }
  164. $avatar = save_base64_image($avatar, 'avatar');
  165. // 验证是否注册,没有则注册
  166. $where = ['openid' => $openid,'mark'=>1];
  167. $data = $this->model->where($where)
  168. ->select(['id', 'openid', 'mobile','area_id','buy_type', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  169. ->first();
  170. $data = $data ? $data->toArray() : [];
  171. $userId = isset($data['id']) ? $data['id'] : 0;
  172. $status = isset($data['status']) ? $data['status'] : 0;
  173. if ($data && $userId && $status != 1) {
  174. $this->error = '账号已被冻结,请来奶昔客服~';
  175. return false;
  176. }
  177. $system = isset($params['system']) ? $params['system'] : [];
  178. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  179. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  180. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  181. $version = isset($system['app_version']) ? $system['app_version'] : '';
  182. if (empty($data)) {
  183. // 清理无效注册数据
  184. $this->model->where(['openid'=>$openid,'mark'=>0])->delete();
  185. // 用户ID
  186. $userId = $this->model->max('id') + 1;
  187. // 推荐人
  188. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  189. $parents = '';
  190. if ($rid) {
  191. $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1])
  192. ->select(['id', 'parent_id', 'parents', 'status'])
  193. ->first();
  194. $rid = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  195. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : '';
  196. if ($inviteInfo) {
  197. $parents = $parents ? $parents . $rid . ',' : ",{$rid},";
  198. }else{
  199. $rid = 1;
  200. $parents = ',1,';
  201. }
  202. }else{
  203. $rid = 1;
  204. $parents = ',1,';
  205. }
  206. // 滑落节点
  207. $pointId = 0;
  208. $points = '';
  209. $pointSort = $userId;
  210. if($rid>0){
  211. $pointData = $this->getPointParentId($rid);
  212. $pointId = isset($pointData['point_id'])?$pointData['point_id'] : 0;
  213. $pointSort = isset($pointData['point_sort']) && $pointData['point_sort']?$pointData['point_sort'] : $pointSort;
  214. if($pointId){
  215. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points','point_sort'])->first();
  216. $points = isset($pointParent['points']) && $pointParent['points']? $pointParent['points'].$pointId.',' : ($pointId ? $pointId . ',' : '');
  217. }
  218. }
  219. DB::beginTransaction();
  220. $data = [
  221. 'nickname' => $nickname,
  222. 'openid' => $openid,
  223. 'mobile' => $phone,
  224. 'avatar' => $avatar,
  225. 'parent_id' => $rid,
  226. 'parents' => $parents,
  227. 'point_id' => $pointId,
  228. 'buy_type' => $areaId,
  229. 'area_id' => $areaId,
  230. 'points' => $points,
  231. 'point_sort' => $pointSort,
  232. 'code' => get_random_code(9, 'Q', $userId),
  233. 'password' => get_password('a123456'),
  234. 'login_ip' => get_client_ip(),
  235. 'create_time' => time(),
  236. 'login_time' => time(),
  237. 'login_count' => DB::raw("login_count+1"),
  238. 'app_version' => $version,
  239. 'app_uuid' => $uuid,
  240. 'device' => $appSources == 'ios' ? 1 : 2,
  241. ];
  242. if (!$userId = $this->model->insertGetId($data)) {
  243. DB::rollBack();
  244. $this->error = 2018;
  245. return false;
  246. }
  247. DB::commit();
  248. } // 更新登录信息
  249. else if (!RedisService::get("caches:members:login_{$userId}")) {
  250. $updateData = [
  251. 'login_ip' => get_client_ip(),
  252. 'login_time' => time(),
  253. 'app_uuid' => $uuid,
  254. 'login_count' => DB::raw("login_count+1"),
  255. 'app_version' => $version,
  256. 'device' => $appSources == 'ios' ? 1 : 2,
  257. 'mark' => 1,
  258. ];
  259. $this->model->where(['id' => $userId])->update($updateData);
  260. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  261. }
  262. // 获取登录授权token
  263. $token = JwtService::make()->encode($userId);
  264. // 结果返回
  265. $result = [
  266. 'access_token' => $token,
  267. 'info' => ['id' => $userId, 'openid' => $openid, 'mobile' => $data['mobile']],
  268. ];
  269. // 用户缓存信息
  270. $this->error = 2019;
  271. $data['token'] = $token;
  272. unset($data['mobile']);
  273. RedisService::keyDel("caches:members:teamList*");
  274. RedisService::set("caches:index:area_".get_client_ip(),['uid'=>$userId,'area_id'=>$data['area_id'],'buy_type'=>$data['buy_type']], 6 * 30 * 86400);
  275. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  276. RedisService::set("caches:members:cacheInfo_{$userId}", $data, 600);
  277. return $result;
  278. }
  279. /**
  280. * 获取点对点上级节点
  281. * @param $userId 推荐人ID
  282. * @return int|mixed
  283. */
  284. public function getPointParentId($userId)
  285. {
  286. if($userId<=0){
  287. return false;
  288. }
  289. // 上级
  290. $userInfo = $this->model->where(['id'=>$userId])->select(['id','parent_id','point_sort','points','point_id'])->first();
  291. $parentId = isset($userInfo['parent_id'])?$userInfo['parent_id']:0;
  292. $pointSort = isset($userInfo['point_sort'])&&$userInfo['point_sort']?$userInfo['point_sort']:0;
  293. $model = $this->model->where(['mark'=> 1]);
  294. $model1= clone $model;
  295. // 自己节点人数
  296. $childrenCount = $model1->where(function($query) use($userId){
  297. $query->where('point_id', $userId);
  298. })->count('id');
  299. // 自己团队和平级用户
  300. $userList = $model->whereNotIn('id',[$userId])
  301. ->where(function($query) use($userId,$parentId){
  302. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  303. // 节点平级用户
  304. if($parentId>0){
  305. $query->orWhere('point_id', $parentId);
  306. }
  307. })
  308. ->select(['id','point_id','point_sort','points'])
  309. ->withCount(['points'])
  310. ->orderBy('point_sort','asc')
  311. ->orderBy('id','asc')
  312. ->get();
  313. $userList = $userList? $userList->toArray() : [];
  314. if($childrenCount < 2){
  315. $pointSort = ($pointSort*2 + ($childrenCount+1)-1);
  316. return ['point_id'=>$userId,'point_sort'=>$pointSort,'index'=>$childrenCount+1];
  317. }
  318. // dump($childrenCount);
  319. // dump($userList);
  320. $pointId = 0;
  321. $tempPointLen = 0;
  322. $tempChildren = 0;
  323. $pointSort = 0;
  324. foreach ($userList as $item) {
  325. $id = isset($item['id'])? $item['id'] : 0;
  326. $sort = isset($item['point_sort']) && $item['point_sort']? $item['point_sort'] : 0;
  327. $children = isset($item['points_count']) && $item['points_count']? $item['points_count'] : 0;
  328. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  329. $points = array_filter($points);
  330. $pointLen = count($points);
  331. //dump("ID:{$id}-{$pointId}-{$tempChildren}-{$children}-{$pointLen}-{$sort}");
  332. if($pointLen<=$tempPointLen || $pointId == 0){
  333. if($children < 2 && $pointId <= 0){
  334. $pointId = $id;
  335. $tempPointLen = $pointLen;
  336. $tempChildren = $children+1;
  337. $pointSort = ($sort*2 + $tempChildren - 1);
  338. //dump("ID:{$id}-{$children}命中");
  339. }
  340. }
  341. }
  342. return ['point_id'=>$pointId,'point_sort'=>$pointSort,'index'=>$tempChildren];
  343. }
  344. /**
  345. * 重置密码
  346. * @param $params
  347. * @return array|false
  348. */
  349. public function forget($params)
  350. {
  351. // 账号登录
  352. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  353. $password = isset($params['password']) ? trim($params['password']) : '';
  354. if (empty($params) || empty($mobile) || empty($password)) {
  355. $this->error = 1041;
  356. return false;
  357. }
  358. // 验证是否注册
  359. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  360. $this->error = 1038;
  361. return false;
  362. }
  363. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  364. $this->error = 2030;
  365. return false;
  366. }
  367. // 操作日志
  368. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  369. ActionLogModel::record();
  370. $this->error = 2031;
  371. return true;
  372. }
  373. /**
  374. * 获取资料详情
  375. * @param $where
  376. * @param array $field
  377. */
  378. public function getInfo($where, array $field = [], $refresh = true)
  379. {
  380. if (empty($where)) {
  381. return false;
  382. }
  383. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  384. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  385. $info = RedisService::get($cacheKey);
  386. if ($info && !$refresh) {
  387. return $info;
  388. }
  389. $defaultField = ['id', 'user_type', 'realname', 'mobile','is_auth','member_level','idcard','buy_type','area_id', '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'];
  390. $field = $field ? $field : $defaultField;
  391. if (is_array($where)) {
  392. $info = $this->model->with(['parent','point','levelData'])->where(['mark' => 1])->where($where)->select($field)->first();
  393. } else {
  394. $info = $this->model->with(['parent','point','levelData'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  395. }
  396. $info = $info ? $info->toArray() : [];
  397. if ($info) {
  398. $info['create_time'] = $info['create_time']?datetime(strtotime($info['create_time']),'Y-m-d H:i') : '';
  399. if (isset($info['mobile'])) {
  400. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  401. }
  402. $params = request()->all();
  403. $type = isset($params['type'])?$params['type']:'';
  404. if($type == 'qrcode'){
  405. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/index/index',"{$info['id']}");
  406. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  407. }
  408. if($type == 'center'){
  409. $info['invite_count'] = $this->model->where(['parent_id'=>$info['id'],'mark'=>1])->count('id');
  410. }
  411. if($type == 'account' || $type == 'center'){
  412. $info['property_price'] = PriceService::make()->getTodayPrice(1);
  413. $info['property_total'] = floatval($info['property'] * $info['property_price']);
  414. }
  415. RedisService::set($cacheKey, $info, rand(10, 20));
  416. }
  417. return $info;
  418. }
  419. /**
  420. * 缓存资料
  421. * @param $userId
  422. * @return array|mixed
  423. */
  424. public function getCacheInfo($userId,$refresh = true)
  425. {
  426. $cacheKey = "caches:members:cacheInfo_{$userId}";
  427. $info = RedisService::get($cacheKey);
  428. if ($info && !$refresh) {
  429. return $info;
  430. }
  431. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  432. ->select(['id', 'openid', 'mobile','balance','property','ls_score','area_id','buy_type', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  433. ->first();
  434. $info = $info?$info->toArray() : [];
  435. if($info){
  436. RedisService::set($cacheKey, $info, rand(3600,7200));
  437. }
  438. return $info;
  439. }
  440. /**
  441. * 绑定收款账户
  442. * @param $userId
  443. * @return array|mixed
  444. */
  445. public function bindAccount($userId, $params)
  446. {
  447. if($params['type']==1){
  448. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  449. ->select(['id','user_id','type','realname','account','account_remark','status'])
  450. ->first();
  451. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  452. $data = [
  453. 'type'=> 1,
  454. 'user_id'=> $userId,
  455. 'realname'=>$params['realname'],
  456. 'account'=>$params['account'],
  457. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  458. 'status'=>1
  459. ];
  460. if($alipayId){
  461. $data['update_time']=time();
  462. MemberBankModel::where(['id'=>$alipayId])->update($data);
  463. }else {
  464. $data['create_time']=time();
  465. MemberBankModel::insertGetId($data);
  466. }
  467. } else if($params['type']==2){
  468. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  469. ->select(['id','user_id','type','realname','account','account_remark','status'])
  470. ->first();
  471. $bankId = isset($banks['id'])?$banks['id'] : 0;
  472. $data = [
  473. 'type'=>2,
  474. 'user_id'=> $userId,
  475. 'realname'=>$params['realname'],
  476. 'account'=>$params['account'],
  477. 'account_remark'=>$params['account_remark'],
  478. 'status'=>1
  479. ];
  480. if($bankId){
  481. $data['update_time']=time();
  482. MemberBankModel::where(['id'=>$bankId])->update($data);
  483. }else {
  484. $data['create_time']=time();
  485. MemberBankModel::insertGetId($data);
  486. }
  487. }else{
  488. $this->error = '账号类型错误';
  489. return false;
  490. }
  491. RedisService::keyDel("caches:members:account:{$userId}*");
  492. $this->error = '绑定收款账号成功';
  493. return true;
  494. }
  495. /**
  496. * 团队人数
  497. * @param $uid
  498. * @return array|int|mixed
  499. */
  500. public function getTeamCount($uid)
  501. {
  502. $cacheKey = "caches:members:teamCount:{$uid}";
  503. $data = RedisService::get($cacheKey);
  504. if ($data) {
  505. return $data;
  506. }
  507. $data = $this->model->from('member as a')
  508. ->where('a.parents', 'like', "%,{$uid},%")
  509. ->where(['a.status' => 1, 'a.mark' => 1])
  510. ->count('id');
  511. if($data){
  512. RedisService::set($cacheKey, $data, rand(5,10));
  513. }
  514. return $data;
  515. }
  516. /**
  517. * 生成普通参数二维码
  518. * @param $str 参数
  519. * @param bool $refresh 是否重新生成
  520. * @return bool
  521. */
  522. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  523. {
  524. $basePath = base_path() . '/public';
  525. $qrFile = '/images/qrcode/';
  526. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  527. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  528. }
  529. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  530. $qrFile = $qrFile . "C_{$key}.png";
  531. $cacheKey = "caches:qrcodes:member_" . $key;
  532. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  533. return $qrFile;
  534. }
  535. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  536. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  537. return false;
  538. }
  539. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  540. return $qrFile;
  541. }
  542. /**
  543. * 修改信息
  544. * @param $userId
  545. * @param $params
  546. * @return bool
  547. */
  548. public function modify($userId, $params)
  549. {
  550. $cacheLockKey = "caches:members:modify_{$userId}";
  551. if (RedisService::get($cacheLockKey)) {
  552. $this->error = 1034;
  553. return false;
  554. }
  555. // 用户验证
  556. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  557. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  558. ->select(['id', 'nickname','avatar','company','position','department', 'status'])
  559. ->first();
  560. if (!$info || $info['status'] != 1) {
  561. $this->error = 2016;
  562. RedisService::clear($cacheLockKey);
  563. return false;
  564. }
  565. // 修改数据
  566. $data = ['update_time' => time()];
  567. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  568. if (isset($params['nickname']) && $nickname) {
  569. $data['nickname'] = $nickname;
  570. }
  571. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  572. if (isset($params['mobile']) && $mobile) {
  573. $data['mobile'] = $mobile;
  574. }
  575. $company = isset($params['company']) ? $params['company'] : '';
  576. if (isset($params['company']) && $company) {
  577. $data['company'] = $company;
  578. }
  579. $department = isset($params['department']) ? $params['department'] : '';
  580. if (isset($params['department']) && $department) {
  581. $data['department'] = $department;
  582. }
  583. $position = isset($params['position']) ? $params['position'] : '';
  584. if (isset($params['position']) && $position) {
  585. $data['position'] = $position;
  586. }
  587. // 头像
  588. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  589. if (isset($params['avatar']) && $avatar) {
  590. $data['avatar'] = save_base64_image($avatar, 'avatar');
  591. }
  592. if (!$this->model->where(['id' => $userId])->update($data)) {
  593. $this->error = 1014;
  594. RedisService::clear($cacheLockKey);
  595. return false;
  596. }
  597. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  598. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  599. @unlink(ATTACHMENT_PATH . $oldAvatar);
  600. }
  601. $this->error = 1013;
  602. RedisService::clear($cacheLockKey);
  603. RedisService::clear("caches:members:authInfo:{$userId}");
  604. RedisService::clear("caches:members:info_{$userId}");
  605. return true;
  606. }
  607. /**
  608. * 认证
  609. * @param $userId
  610. * @param $params
  611. * @return bool
  612. */
  613. public function auth($userId, $params)
  614. {
  615. $cacheLockKey = "caches:members:auth_{$userId}";
  616. if (RedisService::get($cacheLockKey)) {
  617. $this->error = 1034;
  618. return false;
  619. }
  620. // 用户验证
  621. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  622. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  623. ->select(['id', 'realname','idcard','is_auth', 'status'])
  624. ->first();
  625. if (!$info || $info['status'] != 1) {
  626. $this->error = '账号或已被冻结,请联系客服';
  627. RedisService::clear($cacheLockKey);
  628. return false;
  629. }
  630. if($info['is_auth'] == 1 && $info['idcard'] && $info['realname']){
  631. $this->error = '抱歉,您已完成认证';
  632. RedisService::clear($cacheLockKey);
  633. return false;
  634. }
  635. // 认证数据
  636. $data = [
  637. 'realname'=> isset($params['realname'])?$params['realname'] : '',
  638. 'company'=> isset($params['company'])?$params['company'] : '',
  639. 'idcard'=> isset($params['idcard'])?$params['idcard'] : '',
  640. 'is_auth'=> 1,
  641. 'update_time' => time()
  642. ];
  643. if (!$this->model->where(['id' => $userId])->update($data)) {
  644. $this->error = '认证提交失败';
  645. RedisService::clear($cacheLockKey);
  646. return false;
  647. }
  648. $this->error = '恭喜您,已完成认证';
  649. RedisService::clear($cacheLockKey);
  650. RedisService::clear("caches:members:authInfo:{$userId}");
  651. RedisService::keyDel("caches:members:teamList*");
  652. return true;
  653. }
  654. /**
  655. * 获取团队列表
  656. * @param $userId
  657. * @param $params
  658. * @return array
  659. */
  660. public function getTeamList($userId,$params)
  661. {
  662. $page = isset($params['page'])?$params['page']: 1;
  663. $pageSize = isset($params['pageSize'])?$params['pageSize']: 12;
  664. $cacheKey = "caches:members:teamList_{$userId}:{$page}_".md5(json_encode($params));
  665. $list = RedisService::get($cacheKey);
  666. if ($list) {
  667. return [
  668. 'cache'=>true,
  669. 'pageSize'=> $pageSize,
  670. 'total'=>isset($list['total'])? $list['total'] : 0,
  671. 'list'=> isset($list['data'])? $list['data'] : []
  672. ];
  673. }
  674. $list = $this->model->from('member as a')
  675. ->where(['a.parent_id'=>$userId,'a.mark'=>1])
  676. ->where(function($query) use($params){
  677. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  678. if($keyword){
  679. $query->where(function($query) use($keyword){
  680. $query->where('a.realname','like',"%{$keyword}%")
  681. ->orWhere('a.nickname','like',"%{$keyword}%")
  682. ->orWhere('a.mobile','like',"%{$keyword}%");
  683. });
  684. }
  685. })
  686. ->select(['a.id','a.realname','a.mobile','a.nickname','a.parent_id','a.avatar','a.is_auth','a.create_time','a.status'])
  687. ->groupBy('a.id')
  688. ->orderBy('a.create_time','desc')
  689. ->orderBy('a.id','desc')
  690. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  691. $list = $list? $list->toArray() :[];
  692. $total = isset($list['total'])? $list['total'] : 0;
  693. if($total){
  694. RedisService::set($cacheKey, $list, rand(5,10));
  695. }
  696. return [
  697. 'pageSize'=> $pageSize,
  698. 'total'=>$total,
  699. 'list'=> isset($list['data'])? $list['data'] : []
  700. ];
  701. }
  702. /**
  703. * 设置账户参数
  704. * @param $userId
  705. * @param $params
  706. * @return array|false|mixed|string
  707. */
  708. public function setting($userId, $params)
  709. {
  710. $apiUrl = ConfigService::make()->getConfigByCode('bonus_settle_url','');
  711. if(empty($apiUrl)){
  712. $this->error = '设置失败,参数错误';
  713. return false;
  714. }
  715. $token = request()->headers->get('Authorization');
  716. $token = str_replace("Bearer ", null, $token);
  717. $header = [
  718. 'authorization: '.$token
  719. ];
  720. $position = isset($params['position'])?trim($params['position']): '';
  721. $point = isset($params['commission_point'])?floatval($params['commission_point']): 0;
  722. $result = httpRequest($apiUrl.'/team/setting',['id'=>$userId,'position'=>$position,'point'=>$point],'post','',5,$header);
  723. $err = isset($result['err']) && $result['err']?$result['err'] : -1;
  724. $msg = isset($result['msg']) && $result['msg']?$result['msg'] : '1003';
  725. $data = isset($result['data']) && $result['data']?$result['data'] : [];
  726. if($err==0){
  727. $this->error = '操作成功';
  728. return $data;
  729. }else{
  730. $this->error = $msg;
  731. return false;
  732. }
  733. }
  734. /*
  735. * 各等级身份人数
  736. * @return array|mixed
  737. */
  738. public function getCountsByLevel()
  739. {
  740. $cacheKey = "caches:member:counts_by_level";
  741. $data = RedisService::get($cacheKey);
  742. if($data){
  743. return $data;
  744. }
  745. $data = $this->model->where('member_level','>', 0)
  746. ->where(['mark'=>1])
  747. ->select(['id','member_level',DB::raw("count(id) as count")])
  748. ->groupBy('member_level')
  749. ->get()
  750. ->keyBy('member_level');
  751. $data = $data?$data->toArray() : [];
  752. if($data){
  753. RedisService::set($cacheKey, $data, rand(5,10));
  754. }
  755. return $data;
  756. }
  757. /**
  758. * 账号注销
  759. * @param $userId
  760. * @return bool
  761. */
  762. public function logOff($userId)
  763. {
  764. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  765. ->select(['id', 'password', 'status'])
  766. ->first();
  767. $status = isset($info['status']) ? $info['status'] : 0;
  768. if (empty($info)) {
  769. $this->error = 2044;
  770. return false;
  771. }
  772. if ($status != 1) {
  773. $this->error = 2044;
  774. return false;
  775. }
  776. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  777. $this->error = 2049;
  778. return false;
  779. }
  780. $this->error = 2048;
  781. RedisService::clear("auths:info:" . $userId);
  782. return true;
  783. }
  784. /**
  785. * 获取需要复购的用户列表
  786. * @param int $limit
  787. * @return array|mixed
  788. */
  789. public function getReplyBuyUsers($limit=500)
  790. {
  791. $cacheKey = "caches:members:replyList";
  792. $data = RedisService::get($cacheKey);
  793. if($data){
  794. return $data;
  795. }
  796. $maxLevel = GoodsModel::where(['type'=>2,'status'=>1,'mark'=>1])->max('id');
  797. $maxLevel = $maxLevel>0 && $maxLevel<=10?$maxLevel: 4;
  798. $data = $this->model->where('buy_type','<=', $maxLevel)
  799. ->where('balance','>',0)
  800. ->where(['bonus_status'=>2,'status'=>1,'mark'=>1])
  801. ->select(['id','mobile','nickname','buy_type','status'])
  802. ->limit($limit)
  803. ->get();
  804. $data = $data? $data->toArray() : [];
  805. if($data){
  806. RedisService::set($cacheKey, $data, rand(300,600));
  807. }
  808. return $data;
  809. }
  810. }