MemberService.php 30 KB

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