MemberService.php 36 KB

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