MemberService.php 33 KB

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