MemberService.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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['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'] = [];
  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. $info['show_bonus'] = strtotime($info['merits_time'])>=strtotime(date('Y-m-d'))? 1 : 0;
  157. if($info['show_bonus']<=0){
  158. $info['bonus_total']= max(0,$info['bonus_total']-$info['bonus']);
  159. }
  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'])) {
  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. $data = [
  295. 'nickname' => $nickname? $nickname : '用户u'.get_random_code(6),
  296. 'username' => strtoupper('U'.get_random_code(7)),
  297. 'password' => get_password($password),
  298. 'safe_password' => get_password($safePassword),
  299. 'mobile' => $mobile,
  300. 'parents' => $parents,
  301. 'parent_id' => $parentId,
  302. 'status' => 1,
  303. 'mark' => 1,
  304. 'merits_time' => date('Y-m-d H:i:s'),
  305. 'create_time' => time(),
  306. ];
  307. if ($id = $this->model->edit($data)) {
  308. $this->model->where(['id'=> $id])->update(['code'=> strtoupper('Q'.get_random_code(8))]);
  309. $this->error = 2008;
  310. return true;
  311. }
  312. $this->error = 2007;
  313. return false;
  314. }
  315. /**
  316. * 修改保存用户资料
  317. * @param $userId
  318. * @param $params
  319. * @return mixed
  320. */
  321. public function saveInfo($userId, $params)
  322. {
  323. $nickname = isset($params['nickname'])? trim($params['nickname']) : '';
  324. if($nickname){
  325. if($this->model->where(['nickname'=> $nickname,'mark'=> 1])->whereNotIn('id',[$userId])->value('id')){
  326. $this->error = '2028';
  327. return false;
  328. }
  329. }
  330. $data = [
  331. 'nickname' => $nickname,
  332. 'update_time' => time(),
  333. ];
  334. if($this->model->where(['id'=> $userId])->update($data)){
  335. $this->error = '1020';
  336. return true;
  337. }
  338. $this->error = '1021';
  339. return false;
  340. }
  341. /**
  342. * 列表
  343. * @param $params
  344. * @param int $pageSize
  345. * @return array
  346. */
  347. public function getDataList($params, $pageSize = 15)
  348. {
  349. $where = ['a.mark' => 1];
  350. $status = isset($params['status'])? $params['status'] : 0;
  351. $parentId = isset($params['parent_id'])? $params['parent_id'] : 0;
  352. $time = isset($params['time'])&&$params['time']? $params['time'] : 0; // 默认今日
  353. if($parentId>0){
  354. $where['a.parent_id'] = $parentId;
  355. }
  356. $loginShopId = isset($params['login_shop_id'])? $params['login_shop_id'] : 0;
  357. if($loginShopId>0){
  358. $where['a.login_shop_id'] = $loginShopId;
  359. }
  360. if($status>0){
  361. $where['a.status'] = $status;
  362. }
  363. $list = $this->model->from('member as a')
  364. // ->leftJoin('shop as b', 'b.user_id', '=', 'a.id')
  365. ->leftJoin('member as c', 'c.id', '=', 'a.parent_id')
  366. ->where($where)
  367. ->where(function ($query) use($params){
  368. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  369. if($keyword){
  370. $query->where('a.username','like',"%{$keyword}%")->orWhere('a.mobile','like',"%{$keyword}%");
  371. }
  372. $keyword1 = isset($params['keyword1'])? $params['keyword1'] : '';
  373. if($keyword1){
  374. $query->where('a.nickname','like',"%{$keyword1}%")->orWhere('a.mobile','like',"%{$keyword1}%");
  375. }
  376. })
  377. ->select(['a.*','c.code as parent_code','c.nickname as parent_name'])
  378. ->orderBy('a.create_time','desc')
  379. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  380. $list = $list? $list->toArray() :[];
  381. if($list){
  382. foreach($list['data'] as &$item){
  383. $item['selected'] = false;
  384. $item['nickname'] = trim($item['nickname']);
  385. $item['rank_num'] = $item['id']%10+1;
  386. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  387. $item['login_time'] = $item['login_time']? datetime($item['login_time'],'Y-m-d H.i.s') : '';
  388. $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : '';
  389. $item['parent_code'] = isset($item['parent_code']) && $item['parent_code']? $item['parent_code'] : '无';
  390. $showType = isset($params['show_type'])? $params['show_type'] : 1;
  391. if($showType==1){
  392. $item['invite_num'] = MemberService::make()->getInviteNums($item['id']);
  393. $item['goods_num'] = GoodsService::make()->getCount($item['id']);
  394. }else if($showType == 3){
  395. $item['bonus_total'] = TradeService::make()->getTradeBonusTotal($item['id'], $time);
  396. $item['profit_total'] = TradeService::make()->getTradeProfitTotal($item['id'], $time);
  397. }
  398. }
  399. }
  400. return [
  401. 'pageSize'=> $pageSize,
  402. 'total'=>isset($list['total'])? $list['total'] : 0,
  403. 'list'=> isset($list['data'])? $list['data'] : []
  404. ];
  405. }
  406. /**
  407. * 直推用户数
  408. * @param $userId
  409. * @return array|mixed
  410. */
  411. public function getInviteNums($userId)
  412. {
  413. $cacheKey = "caches:member:inviteNums";
  414. $data = RedisService::get($cacheKey);
  415. if($data){
  416. return $data;
  417. }
  418. $data = $this->model->where(['parent_id'=> $userId, 'mark'=> 1,'status'=>1])->count('id');
  419. if($data){
  420. RedisService::set($cacheKey, $data, rand(3, 5));
  421. }
  422. return $data;
  423. }
  424. /**
  425. * 用户选项
  426. * @return array
  427. */
  428. public function options()
  429. {
  430. // 获取参数
  431. $param = request()->all();
  432. // 用户ID
  433. $keyword = getter($param, "keyword");
  434. $parentId = getter($param, "parent_id");
  435. $userId = getter($param, "user_id");
  436. $datas = $this->model->where(function($query) use($parentId){
  437. if($parentId){
  438. $query->where(['id'=> $parentId,'mark'=>1]);
  439. }else{
  440. $query->where(['status'=> 1,'mark'=>1]);
  441. }
  442. })
  443. ->where(function($query) use($userId){
  444. if($userId){
  445. $query->whereNotIn('id', [$userId]);
  446. }
  447. })
  448. ->where(function($query) use($keyword){
  449. if($keyword){
  450. $query->where('nickname','like',"%{$keyword}%")->orWhere('mobile','like',"%{$keyword}%");
  451. }
  452. })
  453. ->select(['id','username','mobile','code','nickname','status'])
  454. ->get();
  455. return $datas? $datas->toArray() : [];
  456. }
  457. /**
  458. * 上级用户列表
  459. * @return array
  460. */
  461. public function parents()
  462. {
  463. // 获取参数
  464. $param = request()->all();
  465. // 用户ID
  466. $keyword = getter($param, "keyword");
  467. $parentId = getter($param, "parent_id");
  468. $userId = getter($param, "user_id");
  469. $datas = $this->model->where(function($query) use($parentId){
  470. if($parentId){
  471. $query->where(['id'=> $parentId,'mark'=>1]);
  472. }else{
  473. $query->where(['status'=> 1,'mark'=>1]);
  474. }
  475. })
  476. ->where(function($query) use($userId){
  477. if($userId){
  478. $query->whereNotIn('id', [$userId]);
  479. }
  480. })
  481. ->where(function($query) use($keyword){
  482. if($keyword){
  483. $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  484. }
  485. })
  486. ->select(['id','username','mobile','code','nickname','status'])
  487. ->get();
  488. return $datas? $datas->toArray() : [];
  489. }
  490. /**
  491. * 生成普通参数二维码
  492. * @param $str 参数
  493. * @param bool $refresh 是否重新生成
  494. * @return bool
  495. */
  496. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  497. {
  498. $qrFile = '/images/qrcode/';
  499. if (!is_dir('/uploads' . $qrFile)) {
  500. @mkdir('./uploads' . $qrFile, 0755, true);
  501. }
  502. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png';
  503. $cacheKey = "caches:qrcodes:member_" . md5($str);
  504. if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) {
  505. //return $qrFile;
  506. }
  507. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  508. if (!file_exists('./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. * @return array|false|mixed
  517. */
  518. public function getTree()
  519. {
  520. // 请求参数
  521. $keyword = request()->post('keyword','');
  522. $cacheKey = "caches:member:trees:".md5('t'.$keyword);
  523. $datas = RedisService::get($cacheKey);
  524. if($datas){
  525. return $datas;
  526. }
  527. $datas = $this->model->where(['status'=>1,'mark'=>1])
  528. ->select(['id','username','nickname','mobile','parent_id','status'])
  529. ->get()->keyBy('id');
  530. $datas = $datas? $datas->toArray() : [];
  531. $pid = 0;
  532. if($keyword){
  533. $data = $this->model->where(function($query) use($keyword){
  534. $query->where('nickname','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  535. })
  536. ->where(['status'=>1,'mark'=>1])
  537. ->orderBy('parent_id','asc')
  538. ->select(['id','parent_id','nickname','mobile','username'])
  539. ->first();
  540. $nickname = isset($data['nickname'])? $data['nickname'] : '';
  541. $username = isset($data['username'])? $data['username'] : '';
  542. $mobile = isset($data['mobile'])? $data['mobile'] : '';
  543. if($data){
  544. $pid = isset($data['id'])? $data['id'] : 0;
  545. $data['label'] = $nickname.($mobile?"({$mobile})":"");
  546. unset($data['nickname']);
  547. unset($data['username']);
  548. }
  549. }
  550. $datas = get_tree($datas, $pid);
  551. if($datas){
  552. if($pid){
  553. $data['children'] = $datas;
  554. $newDatas[0] = $data;
  555. $datas = $newDatas;
  556. }
  557. RedisService::set($cacheKey, $datas, rand(3,5));
  558. }
  559. return $datas;
  560. }
  561. /**
  562. * 添加会编辑会员
  563. * @return array
  564. * @since 2020/11/11
  565. * @author laravel开发员
  566. */
  567. public function edit()
  568. {
  569. // 请求参数
  570. $data = request()->all();
  571. // 头像处理
  572. $avatar = isset($data['avatar'])? trim($data['avatar']) : '';
  573. if ($avatar && strpos($avatar, "temp")) {
  574. $data['avatar'] = save_image($avatar, 'member');
  575. } else if($avatar){
  576. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  577. }
  578. if(!isset($data['merits_time']) && empty($data['merits_time'])){
  579. $data['merits_time'] = date('Y-m-d', time() - 2*86400);
  580. }
  581. return parent::edit($data); // TODO: Change the autogenerated stub
  582. }
  583. /**
  584. * 修改头像
  585. * @param $userId
  586. * @param $avatar
  587. * @return mixed
  588. */
  589. public function saveAvatar($userId, $avatar)
  590. {
  591. return $this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()]);
  592. }
  593. /**
  594. * 重置密码
  595. * @return array
  596. * @since 2020/11/14
  597. * @author laravel开发员
  598. */
  599. public function resetPwd()
  600. {
  601. // 获取参数
  602. $param = request()->all();
  603. // 用户ID
  604. $userId = getter($param, "id");
  605. if (!$userId) {
  606. return message("用户ID不能为空", false);
  607. }
  608. $userInfo = $this->model->getInfo($userId);
  609. if (!$userInfo) {
  610. return message("用户信息不存在", false);
  611. }
  612. // 设置新密码
  613. $password = '123456';
  614. $userInfo['password'] = get_password($password);
  615. $result = $this->model->edit($userInfo);
  616. if (!$result) {
  617. return message("重置密码失败", false);
  618. }
  619. return message("重置密码成功");
  620. }
  621. /**
  622. * 修改账号
  623. * @param $userId
  624. * @param $params
  625. * @return bool
  626. */
  627. public function modify($userId, $params)
  628. {
  629. $username = isset($params['username']) ? $params['username'] : '';
  630. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  631. $password = isset($params['password']) ? $params['password'] : '';
  632. if (empty($username) || empty($password)) {
  633. $this->error = 1013;
  634. return false;
  635. }
  636. // 用户验证
  637. $info = $this->model->getOne([['username', '=', $username]]);
  638. if (!$info || $info['id'] != $userId) {
  639. $this->error = 2001;
  640. return false;
  641. }
  642. // 使用状态校验
  643. if ($info['status'] != 1) {
  644. $this->error = 2009;
  645. return false;
  646. }
  647. // 密码校验
  648. $password = get_password($password);
  649. if ($password != $info['password']) {
  650. $this->error = 2002;
  651. return false;
  652. }
  653. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  654. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  655. $this->error = 2005;
  656. return false;
  657. }
  658. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  659. $this->error = 2021;
  660. return false;
  661. }
  662. $this->error = 2020;
  663. return true;
  664. }
  665. /**
  666. * 修改更新登录密码
  667. * @param $userId
  668. * @param $params
  669. * @return bool
  670. */
  671. public function updatePassword($userId, $params)
  672. {
  673. $password = isset($params['password']) ? $params['password'] : '';
  674. $oldPassword = isset($params['old_password']) ? $params['old_password'] : '';
  675. if (empty($oldPassword)) {
  676. $this->error = 2014;
  677. return false;
  678. }
  679. if (empty($password)) {
  680. $this->error = 2015;
  681. return false;
  682. }
  683. // 用户验证
  684. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  685. if (!$info) {
  686. $this->error = 2001;
  687. return false;
  688. }
  689. // 使用状态校验
  690. if ($info['status'] != 1) {
  691. $this->error = 2012;
  692. return false;
  693. }
  694. // 更新登录密码
  695. $passwordStr = get_password($password);
  696. $oldPasswordStr = get_password($oldPassword);
  697. if($info['password'] != $oldPasswordStr){
  698. $this->error = 2002;
  699. return false;
  700. }
  701. if($oldPassword == $password){
  702. $this->error = 2016;
  703. return false;
  704. }
  705. if (!$this->model->where(['id' => $info['id']])->update(['password' => $passwordStr, 'update_time' => time()])) {
  706. $this->error = 2025;
  707. return false;
  708. }
  709. $this->error = 2024;
  710. return true;
  711. }
  712. /**
  713. * 修改更新支付密码
  714. * @param $userId
  715. * @param $params
  716. * @return bool
  717. */
  718. public function updateSafePassword($userId, $params)
  719. {
  720. $password = isset($params['password']) ? $params['password'] : '';
  721. $safePassword = isset($params['safe_password']) ? $params['safe_password'] : '';
  722. if (empty($safePassword)) {
  723. $this->error = 2017;
  724. return false;
  725. }
  726. if (empty($password)) {
  727. $this->error = 2018;
  728. return false;
  729. }
  730. // 用户验证
  731. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  732. if (!$info) {
  733. $this->error = 2001;
  734. return false;
  735. }
  736. // 使用状态校验
  737. if ($info['status'] != 1) {
  738. $this->error = 2012;
  739. return false;
  740. }
  741. // 更新登录密码
  742. $safePassword = get_password($safePassword);
  743. $oldPasswordStr = get_password($password);
  744. if($info['password'] != $oldPasswordStr){
  745. $this->error = 2002;
  746. return false;
  747. }
  748. if (!$this->model->where(['id' => $info['id']])->update(['safe_password' => $safePassword, 'update_time' => time()])) {
  749. $this->error = 2025;
  750. return false;
  751. }
  752. $this->error = 2024;
  753. return true;
  754. }
  755. /**
  756. * 转换佣金
  757. * @param $userId
  758. * @param int $shopId
  759. * @param int $catchUid
  760. * @return bool
  761. */
  762. public function switchBonus($userId, $shopId=0, $catchUid=0)
  763. {
  764. if($userId>0){
  765. $info = $this->model->where(['id'=> $userId,'mark'=>1])
  766. ->select(['id','bonus','login_shop_id','bonus','score','status'])
  767. ->first();
  768. $score = isset($info['score'])? $info['score'] : 0;
  769. $bonus = isset($info['bonus'])? $info['bonus'] : 0;
  770. $status = isset($info['status'])? $info['status'] : 0;
  771. if(empty($info) || $status != 1){
  772. $this->error = 2019;
  773. return false;
  774. }
  775. if($bonus<=0){
  776. $this->error = 2026;
  777. return false;
  778. }
  779. DB::beginTransaction();
  780. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  781. $scoreRate = $scoreRate? $scoreRate : 1;
  782. $switchScore = intval($bonus*$scoreRate);
  783. if(!$this->model->where(['id'=> $userId])->update(['bonus'=> 0,'score'=> intval($score + $switchScore), 'update_time'=> time()])){
  784. DB::rollBack();
  785. return false;
  786. }
  787. $logDatas[0] = [
  788. 'user_id'=> $userId,
  789. 'shop_id'=> $info['login_shop_id'],
  790. 'type'=> 5,
  791. 'coin_type'=> 2,
  792. 'money'=> -$info['bonus'],
  793. 'balance'=> $info['bonus'],
  794. 'create_time'=>time(),
  795. 'update_time'=>time(),
  796. 'remark'=> '佣金转换',
  797. 'status'=>1,
  798. 'mark'=> 1,
  799. ];
  800. $logDatas[1] = [
  801. 'user_id'=> $userId,
  802. 'shop_id'=> $info['login_shop_id'],
  803. 'type'=> 5,
  804. 'coin_type'=> 3,
  805. 'money'=> $switchScore,
  806. 'balance'=> $info['score'],
  807. 'create_time'=>time(),
  808. 'update_time'=>time(),
  809. 'remark'=> '佣金转换【¥'.$bonus.'】',
  810. 'status'=>1,
  811. 'mark'=> 1,
  812. ];
  813. if(!AccountModel::insert($logDatas)){
  814. DB::rollBack();
  815. return false;
  816. }
  817. DB::commit();
  818. $this->error = 1002;
  819. return true;
  820. }
  821. // 店铺一键转换
  822. else if ($shopId>0){
  823. $datas = $this->model->where(['login_shop_id'=> $shopId,'mark'=>1,'status'=>1])
  824. ->where('bonus','>', 0)
  825. ->select(['id','bonus','login_shop_id','bonus','score'])
  826. ->get();
  827. if($datas){
  828. $logDatas = [];
  829. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  830. $scoreRate = $scoreRate? $scoreRate : 1;
  831. foreach($datas as $v){
  832. // 佣金
  833. $logDatas[] = [
  834. 'user_id'=> $v['id'],
  835. 'shop_id'=> $v['login_shop_id'],
  836. 'type'=> 5,
  837. 'coin_type'=> 2,
  838. 'money'=> -$v['bonus'],
  839. 'balance'=> $v['bonus'],
  840. 'create_time'=>time(),
  841. 'update_time'=>time(),
  842. 'remark'=> '佣金转换',
  843. 'status'=>1,
  844. 'mark'=> 1,
  845. ];
  846. // 积分
  847. $logDatas[] = [
  848. 'user_id'=> $v['id'],
  849. 'shop_id'=> $v['login_shop_id'],
  850. 'type'=> 5,
  851. 'coin_type'=> 3,
  852. 'money'=> $v['bonus']*$scoreRate,
  853. 'balance'=> $v['score'],
  854. 'create_time'=>time(),
  855. 'update_time'=>time(),
  856. 'remark'=> '佣金转换',
  857. 'status'=>1,
  858. 'mark'=> 1,
  859. ];
  860. }
  861. $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";
  862. DB::beginTransaction();
  863. if(!DB::update(DB::raw($sql))){
  864. DB::rollBack();
  865. return false;
  866. }
  867. if(!AccountModel::insert($logDatas)){
  868. DB::rollBack();
  869. return false;
  870. }
  871. DB::commit();
  872. $this->error = 1002;
  873. return true;
  874. }
  875. }
  876. return true;
  877. }
  878. /**
  879. * 设置抢拍状态
  880. * @param $userId
  881. * @param $shopId
  882. * @param int $status
  883. * @return mixed
  884. */
  885. public function setTrade($userId, $shopId, $status=1)
  886. {
  887. if($userId>0){
  888. return $this->model->where(['id'=> $userId])->update(['is_trade'=>$status,'update_time'=> time()]);
  889. }else if($shopId){
  890. return $this->model->where(['login_shop_id'=> $shopId,'mark'=>1])->update(['is_trade'=>$status,'update_time'=> time()]);
  891. }
  892. }
  893. /**
  894. * 删除用户
  895. * @param $userId
  896. * @return mixed
  897. */
  898. public function setLock()
  899. {
  900. $id = request()->post('id', 0);
  901. if(!$id){
  902. $this->error =1013;
  903. return false;
  904. }
  905. return $this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=> time()]);
  906. }
  907. /**
  908. * 修改登录店铺
  909. * @param $userId
  910. * @return mixed
  911. */
  912. public function modifyShop()
  913. {
  914. $ids = request()->post('ids', 0);
  915. $shopCode = request()->post('shop_code','');
  916. if(empty($ids)){
  917. $this->error =2401;
  918. return false;
  919. }
  920. if(empty($shopCode)){
  921. $this->error = 2402;
  922. return false;
  923. }
  924. $shopInfo = ShopModel::where(['code'=>$shopCode,'mark'=>1,'status'=>1])->first();
  925. $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0;
  926. if(empty($shopInfo) && empty($shopId)){
  927. $this->error = 2403;
  928. return false;
  929. }
  930. return $this->model->whereIn('id', $ids)->update(['login_shop_id'=> $shopId,'update_time'=> time()]);
  931. }
  932. }