MemberService.php 35 KB

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