MemberService.php 31 KB

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