MemberService.php 30 KB

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