MemberService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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','merits_count','merits_total', '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. //
  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 == 3){
  344. $item['bonus_total'] = TradeService::make()->getTradeBonusTotal($item['id'], $time);
  345. $item['profit_total'] = TradeService::make()->getTradeProfitTotal($item['id'], $time);
  346. }
  347. }
  348. }
  349. return [
  350. 'pageSize'=> $pageSize,
  351. 'total'=>isset($list['total'])? $list['total'] : 0,
  352. 'list'=> isset($list['data'])? $list['data'] : []
  353. ];
  354. }
  355. /**
  356. * 直推用户数
  357. * @param $userId
  358. * @return array|mixed
  359. */
  360. public function getInviteNums($userId)
  361. {
  362. $cacheKey = "caches:member:inviteNums";
  363. $data = RedisService::get($cacheKey);
  364. if($data){
  365. return $data;
  366. }
  367. $data = $this->model->where(['parent_id'=> $userId, 'mark'=> 1,'status'=>1])->count('id');
  368. if($data){
  369. RedisService::set($cacheKey, $data, rand(3, 5));
  370. }
  371. return $data;
  372. }
  373. /**
  374. * 用户选项
  375. * @return array
  376. */
  377. public function options()
  378. {
  379. // 获取参数
  380. $param = request()->all();
  381. // 用户ID
  382. $keyword = getter($param, "keyword");
  383. $parentId = getter($param, "parent_id");
  384. $userId = getter($param, "user_id");
  385. $datas = $this->model->where(function($query) use($parentId){
  386. if($parentId){
  387. $query->where(['id'=> $parentId,'mark'=>1]);
  388. }else{
  389. $query->where(['status'=> 1,'mark'=>1]);
  390. }
  391. })
  392. ->where(function($query) use($userId){
  393. if($userId){
  394. $query->whereNotIn('id', [$userId]);
  395. }
  396. })
  397. ->where(function($query) use($keyword){
  398. if($keyword){
  399. $query->where('nickname','like',"%{$keyword}%")->orWhere('mobile','like',"%{$keyword}%");
  400. }
  401. })
  402. ->select(['id','username','mobile','code','nickname','status'])
  403. ->get();
  404. return $datas? $datas->toArray() : [];
  405. }
  406. /**
  407. * 上级用户列表
  408. * @return array
  409. */
  410. public function parents()
  411. {
  412. // 获取参数
  413. $param = request()->all();
  414. // 用户ID
  415. $keyword = getter($param, "keyword");
  416. $parentId = getter($param, "parent_id");
  417. $userId = getter($param, "user_id");
  418. $datas = $this->model->where(function($query) use($parentId){
  419. if($parentId){
  420. $query->where(['id'=> $parentId,'mark'=>1]);
  421. }else{
  422. $query->where(['status'=> 1,'mark'=>1]);
  423. }
  424. })
  425. ->where(function($query) use($userId){
  426. if($userId){
  427. $query->whereNotIn('id', [$userId]);
  428. }
  429. })
  430. ->where(function($query) use($keyword){
  431. if($keyword){
  432. $query->where('username','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  433. }
  434. })
  435. ->select(['id','username','mobile','code','nickname','status'])
  436. ->get();
  437. return $datas? $datas->toArray() : [];
  438. }
  439. /**
  440. * 生成普通参数二维码
  441. * @param $str 参数
  442. * @param bool $refresh 是否重新生成
  443. * @return bool
  444. */
  445. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  446. {
  447. $qrFile = '/images/qrcode/';
  448. if (!is_dir('/uploads' . $qrFile)) {
  449. @mkdir('./uploads' . $qrFile, 0755, true);
  450. }
  451. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png';
  452. $cacheKey = "caches:qrcodes:member_" . md5($str);
  453. if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) {
  454. //return $qrFile;
  455. }
  456. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  457. if (!file_exists('./uploads' . $qrFile)) {
  458. return false;
  459. }
  460. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  461. return $qrFile;
  462. }
  463. /**
  464. * 推荐树
  465. * @return array|false|mixed
  466. */
  467. public function getTree()
  468. {
  469. // 请求参数
  470. $keyword = request()->post('keyword','');
  471. $cacheKey = "caches:member:trees:".md5('t'.$keyword);
  472. $datas = RedisService::get($cacheKey);
  473. if($datas){
  474. return $datas;
  475. }
  476. $datas = $this->model->where(['status'=>1,'mark'=>1])
  477. ->select(['id','username','nickname','mobile','parent_id','status'])
  478. ->get()->keyBy('id');
  479. $datas = $datas? $datas->toArray() : [];
  480. $pid = 0;
  481. if($keyword){
  482. $data = $this->model->where(function($query) use($keyword){
  483. $query->where('nickname','like',"{$keyword}%")->orWhere('mobile','like',"{$keyword}%");
  484. })
  485. ->where(['status'=>1,'mark'=>1])
  486. ->orderBy('parent_id','asc')
  487. ->select(['id','parent_id','nickname','mobile','username'])
  488. ->first();
  489. $nickname = isset($data['nickname'])? $data['nickname'] : '';
  490. $username = isset($data['username'])? $data['username'] : '';
  491. $mobile = isset($data['mobile'])? $data['mobile'] : '';
  492. if($data){
  493. $pid = isset($data['id'])? $data['id'] : 0;
  494. $data['label'] = $nickname.($mobile?"({$mobile})":"");
  495. unset($data['nickname']);
  496. unset($data['username']);
  497. }
  498. }
  499. $datas = get_tree($datas, $pid);
  500. if($datas){
  501. if($pid){
  502. $data['children'] = $datas;
  503. $newDatas[0] = $data;
  504. $datas = $newDatas;
  505. }
  506. RedisService::set($cacheKey, $datas, rand(3,5));
  507. }
  508. return $datas;
  509. }
  510. /**
  511. * 添加会编辑会员
  512. * @return array
  513. * @since 2020/11/11
  514. * @author laravel开发员
  515. */
  516. public function edit()
  517. {
  518. // 请求参数
  519. $data = request()->all();
  520. // 头像处理
  521. $avatar = isset($data['avatar'])? trim($data['avatar']) : '';
  522. if ($avatar && strpos($avatar, "temp")) {
  523. $data['avatar'] = save_image($avatar, 'member');
  524. } else if($avatar){
  525. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  526. }
  527. return parent::edit($data); // TODO: Change the autogenerated stub
  528. }
  529. /**
  530. * 修改头像
  531. * @param $userId
  532. * @param $avatar
  533. * @return mixed
  534. */
  535. public function saveAvatar($userId, $avatar)
  536. {
  537. return $this->model->where(['id'=> $userId])->update(['avatar'=> $avatar,'update_time'=> time()]);
  538. }
  539. /**
  540. * 重置密码
  541. * @return array
  542. * @since 2020/11/14
  543. * @author laravel开发员
  544. */
  545. public function resetPwd()
  546. {
  547. // 获取参数
  548. $param = request()->all();
  549. // 用户ID
  550. $userId = getter($param, "id");
  551. if (!$userId) {
  552. return message("用户ID不能为空", false);
  553. }
  554. $userInfo = $this->model->getInfo($userId);
  555. if (!$userInfo) {
  556. return message("用户信息不存在", false);
  557. }
  558. // 设置新密码
  559. $password = '123456';
  560. $userInfo['password'] = get_password($password);
  561. $result = $this->model->edit($userInfo);
  562. if (!$result) {
  563. return message("重置密码失败", false);
  564. }
  565. return message("重置密码成功");
  566. }
  567. /**
  568. * 修改账号
  569. * @param $userId
  570. * @param $params
  571. * @return bool
  572. */
  573. public function modify($userId, $params)
  574. {
  575. $username = isset($params['username']) ? $params['username'] : '';
  576. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  577. $password = isset($params['password']) ? $params['password'] : '';
  578. if (empty($username) || empty($password)) {
  579. $this->error = 1013;
  580. return false;
  581. }
  582. // 用户验证
  583. $info = $this->model->getOne([['username', '=', $username]]);
  584. if (!$info || $info['id'] != $userId) {
  585. $this->error = 2001;
  586. return false;
  587. }
  588. // 使用状态校验
  589. if ($info['status'] != 1) {
  590. $this->error = 2009;
  591. return false;
  592. }
  593. // 密码校验
  594. $password = get_password($password);
  595. if ($password != $info['password']) {
  596. $this->error = 2002;
  597. return false;
  598. }
  599. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  600. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  601. $this->error = 2005;
  602. return false;
  603. }
  604. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  605. $this->error = 2021;
  606. return false;
  607. }
  608. $this->error = 2020;
  609. return true;
  610. }
  611. /**
  612. * 修改更新登录密码
  613. * @param $userId
  614. * @param $params
  615. * @return bool
  616. */
  617. public function updatePassword($userId, $params)
  618. {
  619. $password = isset($params['password']) ? $params['password'] : '';
  620. $oldPassword = isset($params['old_password']) ? $params['old_password'] : '';
  621. if (empty($oldPassword)) {
  622. $this->error = 2014;
  623. return false;
  624. }
  625. if (empty($password)) {
  626. $this->error = 2015;
  627. return false;
  628. }
  629. // 用户验证
  630. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  631. if (!$info) {
  632. $this->error = 2001;
  633. return false;
  634. }
  635. // 使用状态校验
  636. if ($info['status'] != 1) {
  637. $this->error = 2012;
  638. return false;
  639. }
  640. // 更新登录密码
  641. $passwordStr = get_password($password);
  642. $oldPasswordStr = get_password($oldPassword);
  643. if($info['password'] != $oldPasswordStr){
  644. $this->error = 2002;
  645. return false;
  646. }
  647. if($oldPassword == $password){
  648. $this->error = 2016;
  649. return false;
  650. }
  651. if (!$this->model->where(['id' => $info['id']])->update(['password' => $passwordStr, 'update_time' => time()])) {
  652. $this->error = 2025;
  653. return false;
  654. }
  655. $this->error = 2024;
  656. return true;
  657. }
  658. /**
  659. * 修改更新支付密码
  660. * @param $userId
  661. * @param $params
  662. * @return bool
  663. */
  664. public function updateSafePassword($userId, $params)
  665. {
  666. $password = isset($params['password']) ? $params['password'] : '';
  667. $safePassword = isset($params['safe_password']) ? $params['safe_password'] : '';
  668. if (empty($safePassword)) {
  669. $this->error = 2017;
  670. return false;
  671. }
  672. if (empty($password)) {
  673. $this->error = 2018;
  674. return false;
  675. }
  676. // 用户验证
  677. $info = $this->model->getOne([['id', '=', $userId,'mark'=>1]]);
  678. if (!$info) {
  679. $this->error = 2001;
  680. return false;
  681. }
  682. // 使用状态校验
  683. if ($info['status'] != 1) {
  684. $this->error = 2012;
  685. return false;
  686. }
  687. // 更新登录密码
  688. $safePassword = get_password($safePassword);
  689. $oldPasswordStr = get_password($password);
  690. if($info['password'] != $oldPasswordStr){
  691. $this->error = 2002;
  692. return false;
  693. }
  694. if (!$this->model->where(['id' => $info['id']])->update(['safe_password' => $safePassword, 'update_time' => time()])) {
  695. $this->error = 2025;
  696. return false;
  697. }
  698. $this->error = 2024;
  699. return true;
  700. }
  701. /**
  702. * 转换佣金
  703. * @param $userId
  704. * @param int $shopId
  705. * @param int $catchUid
  706. * @return bool
  707. */
  708. public function switchBonus($userId, $shopId=0, $catchUid=0)
  709. {
  710. if($userId>0){
  711. $info = $this->model->where(['id'=> $userId,'mark'=>1])
  712. ->select(['id','bonus','login_shop_id','bonus','score','status'])
  713. ->first();
  714. $score = isset($info['score'])? $info['score'] : 0;
  715. $bonus = isset($info['bonus'])? $info['bonus'] : 0;
  716. $status = isset($info['status'])? $info['status'] : 0;
  717. if(empty($info) || $status != 1){
  718. $this->error = 2019;
  719. return false;
  720. }
  721. if($bonus<=0){
  722. $this->error = 2026;
  723. return false;
  724. }
  725. DB::beginTransaction();
  726. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  727. $scoreRate = $scoreRate? $scoreRate : 1;
  728. $switchScore = intval($bonus*$scoreRate);
  729. if(!$this->model->where(['id'=> $userId])->update(['bonus'=> 0,'score'=> intval($score + $switchScore), 'update_time'=> time()])){
  730. DB::rollBack();
  731. return false;
  732. }
  733. $logDatas[0] = [
  734. 'user_id'=> $userId,
  735. 'shop_id'=> $info['login_shop_id'],
  736. 'type'=> 5,
  737. 'coin_type'=> 2,
  738. 'money'=> -$info['bonus'],
  739. 'balance'=> $info['bonus'],
  740. 'create_time'=>time(),
  741. 'update_time'=>time(),
  742. 'remark'=> '佣金转换',
  743. 'status'=>1,
  744. 'mark'=> 1,
  745. ];
  746. $logDatas[1] = [
  747. 'user_id'=> $userId,
  748. 'shop_id'=> $info['login_shop_id'],
  749. 'type'=> 5,
  750. 'coin_type'=> 3,
  751. 'money'=> $switchScore,
  752. 'balance'=> $info['score'],
  753. 'create_time'=>time(),
  754. 'update_time'=>time(),
  755. 'remark'=> '佣金转换【¥'.$bonus.'】',
  756. 'status'=>1,
  757. 'mark'=> 1,
  758. ];
  759. if(!AccountModel::insert($logDatas)){
  760. DB::rollBack();
  761. return false;
  762. }
  763. DB::commit();
  764. $this->error = 1002;
  765. return true;
  766. }
  767. // 店铺一键转换
  768. else if ($shopId>0){
  769. $datas = $this->model->where(['login_shop_id'=> $shopId,'mark'=>1,'status'=>1])
  770. ->where('bonus','>', 0)
  771. ->select(['id','bonus','login_shop_id','bonus','score'])
  772. ->get();
  773. if($datas){
  774. $logDatas = [];
  775. $scoreRate = ConfigService::make()->getConfigByCode('score_rate');
  776. $scoreRate = $scoreRate? $scoreRate : 1;
  777. foreach($datas as $v){
  778. // 佣金
  779. $logDatas[] = [
  780. 'user_id'=> $v['id'],
  781. 'shop_id'=> $v['login_shop_id'],
  782. 'type'=> 5,
  783. 'coin_type'=> 2,
  784. 'money'=> -$v['bonus'],
  785. 'balance'=> $v['bonus'],
  786. 'create_time'=>time(),
  787. 'update_time'=>time(),
  788. 'remark'=> '佣金转换',
  789. 'status'=>1,
  790. 'mark'=> 1,
  791. ];
  792. // 积分
  793. $logDatas[] = [
  794. 'user_id'=> $v['id'],
  795. 'shop_id'=> $v['login_shop_id'],
  796. 'type'=> 5,
  797. 'coin_type'=> 3,
  798. 'money'=> $v['bonus']*$scoreRate,
  799. 'balance'=> $v['score'],
  800. 'create_time'=>time(),
  801. 'update_time'=>time(),
  802. 'remark'=> '佣金转换',
  803. 'status'=>1,
  804. 'mark'=> 1,
  805. ];
  806. }
  807. $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";
  808. DB::beginTransaction();
  809. if(!DB::update(DB::raw($sql))){
  810. DB::rollBack();
  811. return false;
  812. }
  813. if(!AccountModel::insert($logDatas)){
  814. DB::rollBack();
  815. return false;
  816. }
  817. DB::commit();
  818. $this->error = 1002;
  819. return true;
  820. }
  821. }
  822. return true;
  823. }
  824. /**
  825. * 设置抢拍状态
  826. * @param $userId
  827. * @param $shopId
  828. * @param int $status
  829. * @return mixed
  830. */
  831. public function setTrade($userId, $shopId, $status=1)
  832. {
  833. if($userId>0){
  834. return $this->model->where(['id'=> $userId])->update(['is_trade'=>$status,'update_time'=> time()]);
  835. }else if($shopId){
  836. return $this->model->where(['login_shop_id'=> $shopId,'mark'=>1])->update(['is_trade'=>$status,'update_time'=> time()]);
  837. }
  838. }
  839. /**
  840. * 删除用户
  841. * @param $userId
  842. * @return mixed
  843. */
  844. public function setLock()
  845. {
  846. $id = request()->post('id', 0);
  847. if(!$id){
  848. $this->error =1013;
  849. return false;
  850. }
  851. return $this->model->where(['id'=> $id])->update(['status'=> 2,'update_time'=> time()]);
  852. }
  853. /**
  854. * 修改登录店铺
  855. * @param $userId
  856. * @return mixed
  857. */
  858. public function modifyShop()
  859. {
  860. $ids = request()->post('ids', 0);
  861. $shopCode = request()->post('shop_code','');
  862. if(empty($ids)){
  863. $this->error =2401;
  864. return false;
  865. }
  866. if(empty($shopCode)){
  867. $this->error = 2402;
  868. return false;
  869. }
  870. $shopInfo = ShopModel::where(['code'=>$shopCode,'mark'=>1,'status'=>1])->first();
  871. $shopId = isset($shopInfo['id'])? $shopInfo['id'] : 0;
  872. if(empty($shopInfo) && empty($shopId)){
  873. $this->error = 2403;
  874. return false;
  875. }
  876. return $this->model->whereIn('id', $ids)->update(['login_shop_id'=> $shopId,'update_time'=> time()]);
  877. }
  878. }