MemberService.php 34 KB

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