MemberService.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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'],[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, 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. return parent::edit($data); // TODO: Change the autogenerated stub
  579. }
  580. /**
  581. * 修改头像
  582. * @param $userId
  583. * @param $avatar
  584. * @return mixed
  585. */
  586. public function saveAvatar($userId, $avatar)
  587. {
  588. return $this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()]);
  589. }
  590. /**
  591. * 重置密码
  592. * @return array
  593. * @since 2020/11/14
  594. * @author laravel开发员
  595. */
  596. public function resetPwd()
  597. {
  598. // 获取参数
  599. $param = request()->all();
  600. // 用户ID
  601. $userId = getter($param, "id");
  602. if (!$userId) {
  603. return message("用户ID不能为空", false);
  604. }
  605. $userInfo = $this->model->getInfo($userId);
  606. if (!$userInfo) {
  607. return message("用户信息不存在", false);
  608. }
  609. // 设置新密码
  610. $password = '123456';
  611. $userInfo['password'] = get_password($password);
  612. $result = $this->model->edit($userInfo);
  613. if (!$result) {
  614. return message("重置密码失败", false);
  615. }
  616. return message("重置密码成功");
  617. }
  618. /**
  619. * 修改账号
  620. * @param $userId
  621. * @param $params
  622. * @return bool
  623. */
  624. public function modify($userId, $params)
  625. {
  626. $username = isset($params['username']) ? $params['username'] : '';
  627. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  628. $password = isset($params['password']) ? $params['password'] : '';
  629. if (empty($username) || empty($password)) {
  630. $this->error = 1013;
  631. return false;
  632. }
  633. // 用户验证
  634. $info = $this->model->getOne([['username', '=', $username]]);
  635. if (!$info || $info['id'] != $userId) {
  636. $this->error = 2001;
  637. return false;
  638. }
  639. // 使用状态校验
  640. if ($info['status'] != 1) {
  641. $this->error = 2009;
  642. return false;
  643. }
  644. // 密码校验
  645. $password = get_password($password);
  646. if ($password != $info['password']) {
  647. $this->error = 2002;
  648. return false;
  649. }
  650. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  651. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  652. $this->error = 2005;
  653. return false;
  654. }
  655. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  656. $this->error = 2021;
  657. return false;
  658. }
  659. $this->error = 2020;
  660. return true;
  661. }
  662. /**
  663. * 修改更新登录密码
  664. * @param $userId
  665. * @param $params
  666. * @return bool
  667. */
  668. public function updatePassword($userId, $params)
  669. {
  670. $password = isset($params['password']) ? $params['password'] : '';
  671. $oldPassword = isset($params['old_password']) ? $params['old_password'] : '';
  672. if (empty($oldPassword)) {
  673. $this->error = 2014;
  674. return false;
  675. }
  676. if (empty($password)) {
  677. $this->error = 2015;
  678. return false;
  679. }
  680. // 用户验证
  681. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  682. if (!$info) {
  683. $this->error = 2001;
  684. return false;
  685. }
  686. // 使用状态校验
  687. if ($info['status'] != 1) {
  688. $this->error = 2012;
  689. return false;
  690. }
  691. // 更新登录密码
  692. $passwordStr = get_password($password);
  693. $oldPasswordStr = get_password($oldPassword);
  694. if($info['password'] != $oldPasswordStr){
  695. $this->error = 2002;
  696. return false;
  697. }
  698. if($oldPassword == $password){
  699. $this->error = 2016;
  700. return false;
  701. }
  702. if (!$this->model->where(['id' => $info['id']])->update(['password' => $passwordStr, 'update_time' => time()])) {
  703. $this->error = 2025;
  704. return false;
  705. }
  706. $this->error = 2024;
  707. return true;
  708. }
  709. /**
  710. * 修改更新支付密码
  711. * @param $userId
  712. * @param $params
  713. * @return bool
  714. */
  715. public function updateSafePassword($userId, $params)
  716. {
  717. $password = isset($params['password']) ? $params['password'] : '';
  718. $safePassword = isset($params['safe_password']) ? $params['safe_password'] : '';
  719. if (empty($safePassword)) {
  720. $this->error = 2017;
  721. return false;
  722. }
  723. if (empty($password)) {
  724. $this->error = 2018;
  725. return false;
  726. }
  727. // 用户验证
  728. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  729. if (!$info) {
  730. $this->error = 2001;
  731. return false;
  732. }
  733. // 使用状态校验
  734. if ($info['status'] != 1) {
  735. $this->error = 2012;
  736. return false;
  737. }
  738. // 更新登录密码
  739. $safePassword = get_password($safePassword);
  740. $oldPasswordStr = get_password($password);
  741. if($info['password'] != $oldPasswordStr){
  742. $this->error = 2002;
  743. return false;
  744. }
  745. if (!$this->model->where(['id' => $info['id']])->update(['safe_password' => $safePassword, 'update_time' => time()])) {
  746. $this->error = 2025;
  747. return false;
  748. }
  749. $this->error = 2024;
  750. return true;
  751. }
  752. /**
  753. * 转换佣金
  754. * @param $userId
  755. * @param int $shopId
  756. * @param int $catchUid
  757. * @return bool
  758. */
  759. public function switchBonus($userId, $shopId=0, $catchUid=0)
  760. {
  761. if($userId>0){
  762. $info = $this->model->where(['id'=> $userId,'mark'=>1])
  763. ->select(['id','bonus','login_shop_id','bonus','score','status'])
  764. ->first();
  765. $score = isset($info['score'])? $info['score'] : 0;
  766. $bonus = isset($info['bonus'])? $info['bonus'] : 0;
  767. $status = isset($info['status'])? $info['status'] : 0;
  768. if(empty($info) || $status != 1){
  769. $this->error = 2019;
  770. return false;
  771. }
  772. if($bonus<=0){
  773. $this->error = 2026;
  774. return false;
  775. }
  776. DB::beginTransaction();
  777. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  778. $scoreRate = $scoreRate? $scoreRate : 1;
  779. $switchScore = intval($bonus*$scoreRate);
  780. if(!$this->model->where(['id'=> $userId])->update(['bonus'=> 0,'score'=> intval($score + $switchScore), 'update_time'=> time()])){
  781. DB::rollBack();
  782. return false;
  783. }
  784. $logDatas[0] = [
  785. 'user_id'=> $userId,
  786. 'shop_id'=> $info['login_shop_id'],
  787. 'type'=> 5,
  788. 'coin_type'=> 2,
  789. 'money'=> -$info['bonus'],
  790. 'balance'=> $info['bonus'],
  791. 'create_time'=>time(),
  792. 'update_time'=>time(),
  793. 'remark'=> '佣金转换',
  794. 'status'=>1,
  795. 'mark'=> 1,
  796. ];
  797. $logDatas[1] = [
  798. 'user_id'=> $userId,
  799. 'shop_id'=> $info['login_shop_id'],
  800. 'type'=> 5,
  801. 'coin_type'=> 3,
  802. 'money'=> $switchScore,
  803. 'balance'=> $info['score'],
  804. 'create_time'=>time(),
  805. 'update_time'=>time(),
  806. 'remark'=> '佣金转换【¥'.$bonus.'】',
  807. 'status'=>1,
  808. 'mark'=> 1,
  809. ];
  810. if(!AccountModel::insert($logDatas)){
  811. DB::rollBack();
  812. return false;
  813. }
  814. DB::commit();
  815. $this->error = 1002;
  816. return true;
  817. }
  818. // 店铺一键转换
  819. else if ($shopId>0){
  820. $datas = $this->model->where(['login_shop_id'=> $shopId,'mark'=>1,'status'=>1])
  821. ->where('bonus','>', 0)
  822. ->select(['id','bonus','login_shop_id','bonus','score'])
  823. ->get();
  824. if($datas){
  825. $logDatas = [];
  826. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  827. $scoreRate = $scoreRate? $scoreRate : 1;
  828. foreach($datas as $v){
  829. // 佣金
  830. $logDatas[] = [
  831. 'user_id'=> $v['id'],
  832. 'shop_id'=> $v['login_shop_id'],
  833. 'type'=> 5,
  834. 'coin_type'=> 2,
  835. 'money'=> -$v['bonus'],
  836. 'balance'=> $v['bonus'],
  837. 'create_time'=>time(),
  838. 'update_time'=>time(),
  839. 'remark'=> '佣金转换',
  840. 'status'=>1,
  841. 'mark'=> 1,
  842. ];
  843. // 积分
  844. $logDatas[] = [
  845. 'user_id'=> $v['id'],
  846. 'shop_id'=> $v['login_shop_id'],
  847. 'type'=> 5,
  848. 'coin_type'=> 3,
  849. 'money'=> $v['bonus']*$scoreRate,
  850. 'balance'=> $v['score'],
  851. 'create_time'=>time(),
  852. 'update_time'=>time(),
  853. 'remark'=> '佣金转换',
  854. 'status'=>1,
  855. 'mark'=> 1,
  856. ];
  857. }
  858. $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";
  859. DB::beginTransaction();
  860. if(!DB::update(DB::raw($sql))){
  861. DB::rollBack();
  862. return false;
  863. }
  864. if(!AccountModel::insert($logDatas)){
  865. DB::rollBack();
  866. return false;
  867. }
  868. DB::commit();
  869. $this->error = 1002;
  870. return true;
  871. }
  872. }
  873. return true;
  874. }
  875. /**
  876. * 设置抢拍状态
  877. * @param $userId
  878. * @param $shopId
  879. * @param int $status
  880. * @return mixed
  881. */
  882. public function setTrade($userId, $shopId, $status=1)
  883. {
  884. if($userId>0){
  885. return $this->model->where(['id'=> $userId])->update(['is_trade'=>$status,'update_time'=> time()]);
  886. }else if($shopId){
  887. return $this->model->where(['login_shop_id'=> $shopId,'mark'=>1])->update(['is_trade'=>$status,'update_time'=> time()]);
  888. }
  889. }
  890. /**
  891. * 删除用户
  892. * @param $userId
  893. * @return mixed
  894. */
  895. public function setLock()
  896. {
  897. $id = request()->post('id', 0);
  898. if(!$id){
  899. $this->error =1013;
  900. return false;
  901. }
  902. return $this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=> time()]);
  903. }
  904. /**
  905. * 修改登录店铺
  906. * @param $userId
  907. * @return mixed
  908. */
  909. public function modifyShop()
  910. {
  911. $ids = request()->post('ids', 0);
  912. $shopCode = request()->post('shop_code','');
  913. if(empty($ids)){
  914. $this->error =2401;
  915. return false;
  916. }
  917. if(empty($shopCode)){
  918. $this->error = 2402;
  919. return false;
  920. }
  921. $shopInfo = ShopModel::where(['code'=>$shopCode,'mark'=>1,'status'=>1])->first();
  922. $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0;
  923. if(empty($shopInfo) && empty($shopId)){
  924. $this->error = 2403;
  925. return false;
  926. }
  927. return $this->model->whereIn('id', $ids)->update(['login_shop_id'=> $shopId,'update_time'=> time()]);
  928. }
  929. }