MemberService.php 33 KB

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