MemberService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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\Common;
  12. use App\Helpers\Jwt;
  13. use App\Models\AccountModel;
  14. use App\Models\ActionLogModel;
  15. use App\Models\MemberModel;
  16. use App\Models\ShopModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  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\Common
  28. */
  29. class MemberService extends BaseService
  30. {
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * MemberService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new MemberModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取资料详情
  55. * @param $where
  56. * @param array $field
  57. */
  58. public function getInfo($where, array $field = [])
  59. {
  60. $field = $field ? $field : ['id', 'username', 'realname','mobile', 'nickname','is_trade','login_shop_id','code','parent_id', 'openid','score_rate', 'idcard', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'member_level', 'bonus','bonus_total','score', 'status', 'avatar'];
  61. if (is_array($where)) {
  62. $info = $this->model->where($where)->select($field)->first();
  63. } else {
  64. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  65. }
  66. $info = $info ? $info->toArray() : [];
  67. if ($info) {
  68. $info['version'] = env('VERSION','v1.1.20');
  69. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  70. $info['bonus'] = round($info['bonus'],0);
  71. $info['mobile_text'] = $info['mobile'];
  72. $info['mobile'] = $info['mobile']? format_mobile($info['mobile']):'';
  73. $info['show_bonus'] = GoodsService::make()->checkNewGoods($info['id']);
  74. // 积分
  75. $scoreRate = isset($info['score_rate'])? $info['score_rate'] : 1;
  76. $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  77. $info['real_score'] = intval($scoreRate * $info['score']);
  78. // 二维码
  79. $inviteUrl = env('WEB_URL').'h5/#/pages/register/index?code='.$info['code'];
  80. $qrcode = $this->makeQrcode($inviteUrl);
  81. $info['qrcode'] = $qrcode? get_image_url($qrcode):'';
  82. $info['invite_url'] = $inviteUrl;
  83. $info['shop_info'] = [];
  84. if(isset($info['login_shop_id']) && $info['login_shop_id']){
  85. $shopInfo = ShopService::make()->getInfo($info['login_shop_id']);
  86. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  87. $snapTime = $snapTime? $snapTime : 5;
  88. $curTime = strtotime(date('H:i:s'));
  89. $startTime = isset($shopInfo['start_time'])&&$shopInfo['start_time']? strtotime($shopInfo['start_time']) : 0;
  90. $endTime = isset($shopInfo['end_time'])&&$shopInfo['end_time']? strtotime($shopInfo['end_time']) : 0;
  91. $timeLock = $startTime - $curTime>0? $startTime - $curTime : 0;
  92. $shopInfo['timeData'] = [
  93. 'hours'=> 0,
  94. 'minutes'=> 0,
  95. 'seconds'=> 0,
  96. ];
  97. $shopInfo['snap_time'] = $snapTime;
  98. $shopInfo['snap_time_lock'] = max(0,$curTime - $startTime);
  99. $shopInfo['end_time_lock'] = max(0,$endTime - $startTime);
  100. $shopInfo['time_lock'] = 0;
  101. $shopInfo['trade_status'] = 2;
  102. // if($timeLock ){
  103. if($timeLock && $timeLock<= $snapTime*60){
  104. $shopInfo['time_lock'] = $timeLock;
  105. $shopInfo['timeData']['hours'] = intval($timeLock/3600);
  106. $shopInfo['timeData']['minutes'] = intval($timeLock%3600/60);
  107. $shopInfo['timeData']['seconds'] = intval($timeLock%3600%60);
  108. $shopInfo['trade_status'] = 1;
  109. }else if($endTime>=$curTime && $curTime>=$startTime){
  110. $shopInfo['trade_status'] = 1;
  111. }
  112. $info['shop_info'] = $shopInfo;
  113. }
  114. $info['parent_info'] = [];
  115. if(isset($info['parent_id']) && $info['parent_id']){
  116. $info['parent_info'] = $this->model->where(['id'=>$info['parent_id'],'mark'=>1])
  117. ->select(['id','nickname','username','code'])
  118. ->first();
  119. }
  120. $type = request()->post('type', 0);
  121. if($type == 3){
  122. // 银行卡信息
  123. $info['bank_info'] = MemberBankService::make()->getBindInfo($info['id']);
  124. }
  125. if($type == 1){
  126. // 交易订单统计
  127. $info['orderCounts'] = TradeService::make()->getNewTradeCountByStatus($info['id'],[1,2,3]);
  128. // 积分订单统计
  129. $info['scoreOrderCount'] = OrderService::make()->getNewTradeCount($info['id'],[1,2,3,4,5]);
  130. }
  131. }
  132. return $info;
  133. }
  134. /**
  135. * 分销中心信息
  136. * @param $where
  137. * @param array $field
  138. * @return array
  139. */
  140. public function getMarketInfo($where, array $field = [])
  141. {
  142. $field = $field ? $field : ['id', 'username', 'realname','mobile', 'nickname','is_trade','login_shop_id','code','parent_id', 'openid','score_rate','merits_count','merits_total','merits_time', 'idcard', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'member_level', 'bonus','bonus_total','score', 'status', 'avatar'];
  143. if (is_array($where)) {
  144. $info = $this->model->where($where)->select($field)->first();
  145. } else {
  146. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  147. }
  148. $info = $info ? $info->toArray() : [];
  149. if ($info) {
  150. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  151. $info['bonus'] = round($info['bonus'],0);
  152. $info['bonus_total'] = round($info['bonus_total'],0);
  153. $info['mobile'] = $info['mobile']? format_mobile($info['mobile']):'';
  154. // 今日是否有拍过商品(业绩有入账)
  155. $info['show_bonus'] = strtotime($info['merits_time'])>=strtotime(date('Y-m-d'))? 1 : 0;
  156. if($info['show_bonus']<=0){
  157. $info['bonus_total']= max(0,$info['bonus_total']-$info['bonus']);
  158. }
  159. $info['shop_info'] = [];
  160. if(isset($info['login_shop_id']) && $info['login_shop_id']) {
  161. $shopInfo = ShopService::make()->getInfo($info['login_shop_id']);
  162. $info['shop_info'] = $shopInfo;
  163. }
  164. if(isset($info['parent_id']) && $info['parent_id']) {
  165. $parentInfo = MemberModel::where(['id'=> $info['parent_id']])->select(['id','nickname','mobile','code'])->first();
  166. $info['parent_info'] = $parentInfo;
  167. $info['parent_mobile'] = isset($parentInfo['mobile'])? format_mobile($parentInfo['mobile']) : '';
  168. }
  169. // 团队人数
  170. $info['team_num'] = MemberService::make()->getInviteNums($info['id']);
  171. // // 本人业绩
  172. // $info['merits_count'] = TradeService::make()->getUserTradeTotal($info['id'],[3,4]);
  173. //
  174. // // 总业绩
  175. // $info['merits_total'] = TradeService::make()->getTeamTradeTotal($info['id'],[3,4]);
  176. }
  177. return $info;
  178. }
  179. /**
  180. * 用户缓存信息
  181. * @param $id
  182. * @param string $field
  183. * @return array|mixed
  184. */
  185. public function getCacheInfo($id, $field='')
  186. {
  187. $cacheKey = "caches:member:info:u_{$id}";
  188. $info = RedisService::get($cacheKey);
  189. if($info){
  190. return $info;
  191. }
  192. $field = $field? $field : ['id','is_trade','status','parent_id','code'];
  193. $info = $this->model->where(['id' => $id,'mark'=>1])->select($field)->first();
  194. $info = $info? $info->toArray() : [];
  195. if($info){
  196. RedisService::set($cacheKey, $info, rand(5, 10));
  197. }
  198. return $info;
  199. }
  200. /**
  201. * 用户登录
  202. * @param $params
  203. * @return array|false
  204. */
  205. public function login($params)
  206. {
  207. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  208. $password = isset($params['password']) ? $params['password'] : '';
  209. $shopCode = isset($params['shop_code']) ? $params['shop_code'] : '';
  210. if (empty($mobile) || empty($password)) {
  211. $this->error = 2009;
  212. return false;
  213. }
  214. if(empty($shopCode)){
  215. $this->error = 2010;
  216. return false;
  217. }
  218. // 验证店铺
  219. $shopInfo = ShopModel::where(['code'=> $shopCode,'status'=>1,'mark'=>1])->first();
  220. $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0;
  221. $shopUserId = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0;
  222. if(!$shopId){
  223. $this->error = 2011;
  224. return false;
  225. }
  226. // 用户验证
  227. $info = $this->model->where(['mobile'=>$mobile,'mark'=>1])
  228. ->select(['id','username','nickname','mobile','password','code','parent_id','login_count','login_shop_id','status'])
  229. ->first();
  230. if (!$info) {
  231. $this->error = 2001;
  232. return false;
  233. }
  234. $userId = isset($info['id'])? $info['id'] : 0;
  235. $loginShopId = isset($info['login_shop_id'])? $info['login_shop_id'] : 0;
  236. if(($shopUserId != $userId) && $loginShopId>0 && ($loginShopId != $shopId)){
  237. $this->error = 2022;
  238. return false;
  239. }
  240. // 密码校验
  241. $password = get_password($password);
  242. if ($password != $info['password']) {
  243. $this->error = 2002;
  244. return false;
  245. }
  246. // 使用状态校验
  247. if ($info['status'] != 1) {
  248. $this->error = 2012;
  249. return false;
  250. }
  251. // 设置日志标题
  252. ActionLogModel::setTitle("会员登录");
  253. ActionLogModel::record($info);
  254. // JWT生成token
  255. $jwt = new Jwt('jwt_app');
  256. $token = $jwt->getToken($info['id']);
  257. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  258. // 登录
  259. $updateData = ['login_time' => time(),'login_shop_id'=> $shopId,'login_count'=>$info['login_count']+1, 'login_ip' => get_client_ip()];
  260. $this->model->where(['id' => $info['id']])->update($updateData);
  261. // 登录数据
  262. return [
  263. 'token' => $token,
  264. 'user_id' => $info['id'],
  265. 'shop_id' => $shopId,
  266. ];
  267. }
  268. /**
  269. * 用户注册
  270. * @param $params
  271. * @return bool
  272. */
  273. public function register($params)
  274. {
  275. // 检测账号是否存在
  276. if ($this->checkExists('mobile', $params['mobile'])) {
  277. $this->error = '2005';
  278. return false;
  279. }
  280. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  281. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  282. $password = isset($params['password']) ? trim($params['password']) : '';
  283. $safePassword = isset($params['safe_password']) ? trim($params['safe_password']) : '';
  284. $inviteCode = isset($params['invite_code']) ? trim($params['invite_code']) : '';
  285. $inviteInfo = $this->model->where(['code'=> $inviteCode,'mark'=>1])->select(['id','code','username','parents'])->first();
  286. if(empty($inviteInfo)){
  287. $this->error = '2013';
  288. return false;
  289. }
  290. $parentId = isset($inviteInfo['id'])? $inviteInfo['id'] : 0;
  291. $parents = isset($inviteInfo['parents'])? $inviteInfo['parents'] : '';
  292. $parents = $parents? rtrim($parents,',').",{$parentId}," : "{$parentId},";
  293. $data = [
  294. 'nickname' => $nickname? $nickname : '用户u'.get_random_code(6),
  295. 'username' => strtoupper('U'.get_random_code(7)),
  296. 'password' => get_password($password),
  297. 'safe_password' => get_password($safePassword),
  298. 'mobile' => $mobile,
  299. 'parents' => $parents,
  300. 'parent_id' => $parentId,
  301. 'status' => 1,
  302. 'mark' => 1,
  303. 'merits_time' => date('Y-m-d H:i:s'),
  304. 'create_time' => time(),
  305. ];
  306. if ($id = $this->model->edit($data)) {
  307. $this->model->where(['id'=> $id])->update(['code'=> strtoupper('Q'.get_random_code(8))]);
  308. $this->error = 2008;
  309. return true;
  310. }
  311. $this->error = 2007;
  312. return false;
  313. }
  314. /**
  315. * 修改保存用户资料
  316. * @param $userId
  317. * @param $params
  318. * @return mixed
  319. */
  320. public function saveInfo($userId, $params)
  321. {
  322. $nickname = isset($params['nickname'])? trim($params['nickname']) : '';
  323. if($nickname){
  324. if($this->model->where(['nickname'=> $nickname,'mark'=> 1])->whereNotIn('id',[$userId])->value('id')){
  325. $this->error = '2028';
  326. return false;
  327. }
  328. }
  329. $data = [
  330. 'nickname' => $nickname,
  331. 'update_time' => time(),
  332. ];
  333. if($this->model->where(['id'=> $userId])->update($data)){
  334. $this->error = '1020';
  335. return true;
  336. }
  337. $this->error = '1021';
  338. return false;
  339. }
  340. /**
  341. * 列表
  342. * @param $params
  343. * @param int $pageSize
  344. * @return array
  345. */
  346. public function getDataList($params, $pageSize = 15)
  347. {
  348. $where = ['a.mark' => 1];
  349. $status = isset($params['status'])? $params['status'] : 0;
  350. $parentId = isset($params['parent_id'])? $params['parent_id'] : 0;
  351. $time = isset($params['time'])&&$params['time']? $params['time'] : 0; // 默认今日
  352. if($parentId>0){
  353. $where['a.parent_id'] = $parentId;
  354. }
  355. $loginShopId = isset($params['login_shop_id'])? $params['login_shop_id'] : 0;
  356. if($loginShopId>0){
  357. $where['a.login_shop_id'] = $loginShopId;
  358. }
  359. if($status>0){
  360. $where['a.status'] = $status;
  361. }
  362. $list = $this->model->from('member as a')
  363. // ->leftJoin('shop as b', 'b.user_id', '=', 'a.id')
  364. ->leftJoin('member as c', 'c.id', '=', 'a.parent_id')
  365. ->where($where)
  366. ->where(function ($query) use($params){
  367. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  368. if($keyword){
  369. $query->where('a.username','like',"%{$keyword}%")->orWhere('a.mobile','like',"%{$keyword}%");
  370. }
  371. $keyword1 = isset($params['keyword1'])? $params['keyword1'] : '';
  372. if($keyword1){
  373. $query->where('a.nickname','like',"%{$keyword1}%")->orWhere('a.mobile','like',"%{$keyword1}%");
  374. }
  375. })
  376. ->select(['a.*','c.code as parent_code','c.nickname as parent_name'])
  377. ->orderBy('a.create_time','desc')
  378. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  379. $list = $list? $list->toArray() :[];
  380. if($list){
  381. foreach($list['data'] as &$item){
  382. $item['selected'] = false;
  383. $item['nickname'] = trim($item['nickname']);
  384. $item['rank_num'] = $item['id']%10+1;
  385. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  386. $item['login_time'] = $item['login_time']? datetime($item['login_time'],'Y-m-d H.i.s') : '';
  387. $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : '';
  388. $item['parent_code'] = isset($item['parent_code']) && $item['parent_code']? $item['parent_code'] : '无';
  389. $showType = isset($params['show_type'])? $params['show_type'] : 1;
  390. if($showType==1){
  391. $item['invite_num'] = MemberService::make()->getInviteNums($item['id']);
  392. }else if($showType == 3){
  393. $item['bonus_total'] = TradeService::make()->getTradeBonusTotal($item['id'], $time);
  394. $item['profit_total'] = TradeService::make()->getTradeProfitTotal($item['id'], $time);
  395. }
  396. }
  397. }
  398. return [
  399. 'pageSize'=> $pageSize,
  400. 'total'=>isset($list['total'])? $list['total'] : 0,
  401. 'list'=> isset($list['data'])? $list['data'] : []
  402. ];
  403. }
  404. /**
  405. * 直推用户数
  406. * @param $userId
  407. * @return array|mixed
  408. */
  409. public function getInviteNums($userId)
  410. {
  411. $cacheKey = "caches:member:inviteNums";
  412. $data = RedisService::get($cacheKey);
  413. if($data){
  414. return $data;
  415. }
  416. $data = $this->model->where(['parent_id'=> $userId, 'mark'=> 1,'status'=>1])->count('id');
  417. if($data){
  418. RedisService::set($cacheKey, $data, rand(3, 5));
  419. }
  420. return $data;
  421. }
  422. /**
  423. * 用户选项
  424. * @return array
  425. */
  426. public function options()
  427. {
  428. // 获取参数
  429. $param = request()->all();
  430. // 用户ID
  431. $keyword = getter($param, "keyword");
  432. $parentId = getter($param, "parent_id");
  433. $userId = getter($param, "user_id");
  434. $datas = $this->model->where(function($query) use($parentId){
  435. if($parentId){
  436. $query->where(['id'=> $parentId,'mark'=>1]);
  437. }else{
  438. $query->where(['status'=> 1,'mark'=>1]);
  439. }
  440. })
  441. ->where(function($query) use($userId){
  442. if($userId){
  443. $query->whereNotIn('id', [$userId]);
  444. }
  445. })
  446. ->where(function($query) use($keyword){
  447. if($keyword){
  448. $query->where('nickname','like',"%{$keyword}%")->orWhere('mobile','like',"%{$keyword}%");
  449. }
  450. })
  451. ->select(['id','username','mobile','code','nickname','status'])
  452. ->get();
  453. return $datas? $datas->toArray() : [];
  454. }
  455. /**
  456. * 上级用户列表
  457. * @return array
  458. */
  459. public function parents()
  460. {
  461. // 获取参数
  462. $param = request()->all();
  463. // 用户ID
  464. $keyword = getter($param, "keyword");
  465. $parentId = getter($param, "parent_id");
  466. $userId = getter($param, "user_id");
  467. $datas = $this->model->where(function($query) use($parentId){
  468. if($parentId){
  469. $query->where(['id'=> $parentId,'mark'=>1]);
  470. }else{
  471. $query->where(['status'=> 1,'mark'=>1]);
  472. }
  473. })
  474. ->where(function($query) use($userId){
  475. if($userId){
  476. $query->whereNotIn('id', [$userId]);
  477. }
  478. })
  479. ->where(function($query) use($keyword){
  480. if($keyword){
  481. $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  482. }
  483. })
  484. ->select(['id','username','mobile','code','nickname','status'])
  485. ->get();
  486. return $datas? $datas->toArray() : [];
  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. $qrFile = '/images/qrcode/';
  497. if (!is_dir('/uploads' . $qrFile)) {
  498. @mkdir('./uploads' . $qrFile, 0755, true);
  499. }
  500. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png';
  501. $cacheKey = "caches:qrcodes:member_" . md5($str);
  502. if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) {
  503. //return $qrFile;
  504. }
  505. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  506. if (!file_exists('./uploads' . $qrFile)) {
  507. return false;
  508. }
  509. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  510. return $qrFile;
  511. }
  512. /**
  513. * 推荐树
  514. * @return array|false|mixed
  515. */
  516. public function getTree()
  517. {
  518. // 请求参数
  519. $keyword = request()->post('keyword','');
  520. $cacheKey = "caches:member:trees:".md5('t'.$keyword);
  521. $datas = RedisService::get($cacheKey);
  522. if($datas){
  523. return $datas;
  524. }
  525. $datas = $this->model->where(['status'=>1,'mark'=>1])
  526. ->select(['id','username','nickname','mobile','parent_id','status'])
  527. ->get()->keyBy('id');
  528. $datas = $datas? $datas->toArray() : [];
  529. $pid = 0;
  530. if($keyword){
  531. $data = $this->model->where(function($query) use($keyword){
  532. $query->where('nickname','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  533. })
  534. ->where(['status'=>1,'mark'=>1])
  535. ->orderBy('parent_id','asc')
  536. ->select(['id','parent_id','nickname','mobile','username'])
  537. ->first();
  538. $nickname = isset($data['nickname'])? $data['nickname'] : '';
  539. $username = isset($data['username'])? $data['username'] : '';
  540. $mobile = isset($data['mobile'])? $data['mobile'] : '';
  541. if($data){
  542. $pid = isset($data['id'])? $data['id'] : 0;
  543. $data['label'] = $nickname.($mobile?"({$mobile})":"");
  544. unset($data['nickname']);
  545. unset($data['username']);
  546. }
  547. }
  548. $datas = get_tree($datas, $pid);
  549. if($datas){
  550. if($pid){
  551. $data['children'] = $datas;
  552. $newDatas[0] = $data;
  553. $datas = $newDatas;
  554. }
  555. RedisService::set($cacheKey, $datas, rand(3,5));
  556. }
  557. return $datas;
  558. }
  559. /**
  560. * 添加会编辑会员
  561. * @return array
  562. * @since 2020/11/11
  563. * @author laravel开发员
  564. */
  565. public function edit()
  566. {
  567. // 请求参数
  568. $data = request()->all();
  569. // 头像处理
  570. $avatar = isset($data['avatar'])? trim($data['avatar']) : '';
  571. if ($avatar && strpos($avatar, "temp")) {
  572. $data['avatar'] = save_image($avatar, 'member');
  573. } else if($avatar){
  574. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  575. }
  576. return parent::edit($data); // TODO: Change the autogenerated stub
  577. }
  578. /**
  579. * 修改头像
  580. * @param $userId
  581. * @param $avatar
  582. * @return mixed
  583. */
  584. public function saveAvatar($userId, $avatar)
  585. {
  586. return $this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()]);
  587. }
  588. /**
  589. * 重置密码
  590. * @return array
  591. * @since 2020/11/14
  592. * @author laravel开发员
  593. */
  594. public function resetPwd()
  595. {
  596. // 获取参数
  597. $param = request()->all();
  598. // 用户ID
  599. $userId = getter($param, "id");
  600. if (!$userId) {
  601. return message("用户ID不能为空", false);
  602. }
  603. $userInfo = $this->model->getInfo($userId);
  604. if (!$userInfo) {
  605. return message("用户信息不存在", false);
  606. }
  607. // 设置新密码
  608. $password = '123456';
  609. $userInfo['password'] = get_password($password);
  610. $result = $this->model->edit($userInfo);
  611. if (!$result) {
  612. return message("重置密码失败", false);
  613. }
  614. return message("重置密码成功");
  615. }
  616. /**
  617. * 修改账号
  618. * @param $userId
  619. * @param $params
  620. * @return bool
  621. */
  622. public function modify($userId, $params)
  623. {
  624. $username = isset($params['username']) ? $params['username'] : '';
  625. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  626. $password = isset($params['password']) ? $params['password'] : '';
  627. if (empty($username) || empty($password)) {
  628. $this->error = 1013;
  629. return false;
  630. }
  631. // 用户验证
  632. $info = $this->model->getOne([['username', '=', $username]]);
  633. if (!$info || $info['id'] != $userId) {
  634. $this->error = 2001;
  635. return false;
  636. }
  637. // 使用状态校验
  638. if ($info['status'] != 1) {
  639. $this->error = 2009;
  640. return false;
  641. }
  642. // 密码校验
  643. $password = get_password($password);
  644. if ($password != $info['password']) {
  645. $this->error = 2002;
  646. return false;
  647. }
  648. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  649. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  650. $this->error = 2005;
  651. return false;
  652. }
  653. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  654. $this->error = 2021;
  655. return false;
  656. }
  657. $this->error = 2020;
  658. return true;
  659. }
  660. /**
  661. * 修改更新登录密码
  662. * @param $userId
  663. * @param $params
  664. * @return bool
  665. */
  666. public function updatePassword($userId, $params)
  667. {
  668. $password = isset($params['password']) ? $params['password'] : '';
  669. $oldPassword = isset($params['old_password']) ? $params['old_password'] : '';
  670. if (empty($oldPassword)) {
  671. $this->error = 2014;
  672. return false;
  673. }
  674. if (empty($password)) {
  675. $this->error = 2015;
  676. return false;
  677. }
  678. // 用户验证
  679. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  680. if (!$info) {
  681. $this->error = 2001;
  682. return false;
  683. }
  684. // 使用状态校验
  685. if ($info['status'] != 1) {
  686. $this->error = 2012;
  687. return false;
  688. }
  689. // 更新登录密码
  690. $passwordStr = get_password($password);
  691. $oldPasswordStr = get_password($oldPassword);
  692. if($info['password'] != $oldPasswordStr){
  693. $this->error = 2002;
  694. return false;
  695. }
  696. if($oldPassword == $password){
  697. $this->error = 2016;
  698. return false;
  699. }
  700. if (!$this->model->where(['id' => $info['id']])->update(['password' => $passwordStr, 'update_time' => time()])) {
  701. $this->error = 2025;
  702. return false;
  703. }
  704. $this->error = 2024;
  705. return true;
  706. }
  707. /**
  708. * 修改更新支付密码
  709. * @param $userId
  710. * @param $params
  711. * @return bool
  712. */
  713. public function updateSafePassword($userId, $params)
  714. {
  715. $password = isset($params['password']) ? $params['password'] : '';
  716. $safePassword = isset($params['safe_password']) ? $params['safe_password'] : '';
  717. if (empty($safePassword)) {
  718. $this->error = 2017;
  719. return false;
  720. }
  721. if (empty($password)) {
  722. $this->error = 2018;
  723. return false;
  724. }
  725. // 用户验证
  726. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  727. if (!$info) {
  728. $this->error = 2001;
  729. return false;
  730. }
  731. // 使用状态校验
  732. if ($info['status'] != 1) {
  733. $this->error = 2012;
  734. return false;
  735. }
  736. // 更新登录密码
  737. $safePassword = get_password($safePassword);
  738. $oldPasswordStr = get_password($password);
  739. if($info['password'] != $oldPasswordStr){
  740. $this->error = 2002;
  741. return false;
  742. }
  743. if (!$this->model->where(['id' => $info['id']])->update(['safe_password' => $safePassword, 'update_time' => time()])) {
  744. $this->error = 2025;
  745. return false;
  746. }
  747. $this->error = 2024;
  748. return true;
  749. }
  750. /**
  751. * 转换佣金
  752. * @param $userId
  753. * @param int $shopId
  754. * @param int $catchUid
  755. * @return bool
  756. */
  757. public function switchBonus($userId, $shopId=0, $catchUid=0)
  758. {
  759. if($userId>0){
  760. $info = $this->model->where(['id'=> $userId,'mark'=>1])
  761. ->select(['id','bonus','login_shop_id','bonus','score','status'])
  762. ->first();
  763. $score = isset($info['score'])? $info['score'] : 0;
  764. $bonus = isset($info['bonus'])? $info['bonus'] : 0;
  765. $status = isset($info['status'])? $info['status'] : 0;
  766. if(empty($info) || $status != 1){
  767. $this->error = 2019;
  768. return false;
  769. }
  770. if($bonus<=0){
  771. $this->error = 2026;
  772. return false;
  773. }
  774. DB::beginTransaction();
  775. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  776. $scoreRate = $scoreRate? $scoreRate : 1;
  777. $switchScore = intval($bonus*$scoreRate);
  778. if(!$this->model->where(['id'=> $userId])->update(['bonus'=> 0,'score'=> intval($score + $switchScore), 'update_time'=> time()])){
  779. DB::rollBack();
  780. return false;
  781. }
  782. $logDatas[0] = [
  783. 'user_id'=> $userId,
  784. 'shop_id'=> $info['login_shop_id'],
  785. 'type'=> 5,
  786. 'coin_type'=> 2,
  787. 'money'=> -$info['bonus'],
  788. 'balance'=> $info['bonus'],
  789. 'create_time'=>time(),
  790. 'update_time'=>time(),
  791. 'remark'=> '佣金转换',
  792. 'status'=>1,
  793. 'mark'=> 1,
  794. ];
  795. $logDatas[1] = [
  796. 'user_id'=> $userId,
  797. 'shop_id'=> $info['login_shop_id'],
  798. 'type'=> 5,
  799. 'coin_type'=> 3,
  800. 'money'=> $switchScore,
  801. 'balance'=> $info['score'],
  802. 'create_time'=>time(),
  803. 'update_time'=>time(),
  804. 'remark'=> '佣金转换【¥'.$bonus.'】',
  805. 'status'=>1,
  806. 'mark'=> 1,
  807. ];
  808. if(!AccountModel::insert($logDatas)){
  809. DB::rollBack();
  810. return false;
  811. }
  812. DB::commit();
  813. $this->error = 1002;
  814. return true;
  815. }
  816. // 店铺一键转换
  817. else if ($shopId>0){
  818. $datas = $this->model->where(['login_shop_id'=> $shopId,'mark'=>1,'status'=>1])
  819. ->where('bonus','>', 0)
  820. ->select(['id','bonus','login_shop_id','bonus','score'])
  821. ->get();
  822. if($datas){
  823. $logDatas = [];
  824. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  825. $scoreRate = $scoreRate? $scoreRate : 1;
  826. foreach($datas as $v){
  827. // 佣金
  828. $logDatas[] = [
  829. 'user_id'=> $v['id'],
  830. 'shop_id'=> $v['login_shop_id'],
  831. 'type'=> 5,
  832. 'coin_type'=> 2,
  833. 'money'=> -$v['bonus'],
  834. 'balance'=> $v['bonus'],
  835. 'create_time'=>time(),
  836. 'update_time'=>time(),
  837. 'remark'=> '佣金转换',
  838. 'status'=>1,
  839. 'mark'=> 1,
  840. ];
  841. // 积分
  842. $logDatas[] = [
  843. 'user_id'=> $v['id'],
  844. 'shop_id'=> $v['login_shop_id'],
  845. 'type'=> 5,
  846. 'coin_type'=> 3,
  847. 'money'=> $v['bonus']*$scoreRate,
  848. 'balance'=> $v['score'],
  849. 'create_time'=>time(),
  850. 'update_time'=>time(),
  851. 'remark'=> '佣金转换',
  852. 'status'=>1,
  853. 'mark'=> 1,
  854. ];
  855. }
  856. $sql = "UPDATE lev_member set `score`=`score`+(`bonus`)*{$scoreRate},`bonus`=0,update_time=".time()." where login_shop_id={$shopId} and status=1 and mark=1 and bonus >0";
  857. DB::beginTransaction();
  858. if(!DB::update(DB::raw($sql))){
  859. DB::rollBack();
  860. return false;
  861. }
  862. if(!AccountModel::insert($logDatas)){
  863. DB::rollBack();
  864. return false;
  865. }
  866. DB::commit();
  867. $this->error = 1002;
  868. return true;
  869. }
  870. }
  871. return true;
  872. }
  873. /**
  874. * 设置抢拍状态
  875. * @param $userId
  876. * @param $shopId
  877. * @param int $status
  878. * @return mixed
  879. */
  880. public function setTrade($userId, $shopId, $status=1)
  881. {
  882. if($userId>0){
  883. return $this->model->where(['id'=> $userId])->update(['is_trade'=>$status,'update_time'=> time()]);
  884. }else if($shopId){
  885. return $this->model->where(['login_shop_id'=> $shopId,'mark'=>1])->update(['is_trade'=>$status,'update_time'=> time()]);
  886. }
  887. }
  888. /**
  889. * 删除用户
  890. * @param $userId
  891. * @return mixed
  892. */
  893. public function setLock()
  894. {
  895. $id = request()->post('id', 0);
  896. if(!$id){
  897. $this->error =1013;
  898. return false;
  899. }
  900. return $this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=> time()]);
  901. }
  902. /**
  903. * 修改登录店铺
  904. * @param $userId
  905. * @return mixed
  906. */
  907. public function modifyShop()
  908. {
  909. $ids = request()->post('ids', 0);
  910. $shopCode = request()->post('shop_code','');
  911. if(empty($ids)){
  912. $this->error =2401;
  913. return false;
  914. }
  915. if(empty($shopCode)){
  916. $this->error = 2402;
  917. return false;
  918. }
  919. $shopInfo = ShopModel::where(['code'=>$shopCode,'mark'=>1,'status'=>1])->first();
  920. $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0;
  921. if(empty($shopInfo) && empty($shopId)){
  922. $this->error = 2403;
  923. return false;
  924. }
  925. return $this->model->whereIn('id', $ids)->update(['login_shop_id'=> $shopId,'update_time'=> time()]);
  926. }
  927. }