MemberService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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\Api;
  12. use App\Models\ActionLogModel;
  13. use App\Models\BalanceLogModel;
  14. use App\Models\MemberBankModel;
  15. use App\Models\MemberModel;
  16. use App\Models\OrderModel;
  17. use App\Services\BaseService;
  18. use App\Services\JwtService;
  19. use App\Services\MpService;
  20. use App\Services\RedisService;
  21. use App\Services\SmsService;
  22. use Illuminate\Support\Facades\DB;
  23. use phpQrcode\QRcode;
  24. /**
  25. * 会员管理-服务类
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * Class MemberService
  29. * @package App\Services\Api
  30. */
  31. class MemberService extends BaseService
  32. {
  33. // 静态对象
  34. protected static $instance = null;
  35. /**
  36. * 构造函数
  37. * @author laravel开发员
  38. * @since 2020/11/11
  39. * MemberService constructor.
  40. */
  41. public function __construct()
  42. {
  43. $this->model = new MemberModel();
  44. }
  45. /**
  46. * 静态入口
  47. * @return MemberService|static|null
  48. */
  49. public static function make()
  50. {
  51. if (!self::$instance) {
  52. self::$instance = new static();
  53. }
  54. return self::$instance;
  55. }
  56. /**
  57. * 账号登录
  58. * @param $params
  59. * @return array|false
  60. */
  61. public function login($params)
  62. {
  63. // 账号登录
  64. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  65. $password = isset($params['password']) ? $params['password'] : '';
  66. if (empty($params) || empty($mobile) || empty($password)) {
  67. $this->error = 1041;
  68. return false;
  69. }
  70. // 验证是否注册,没有则注册
  71. $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'mobile', 'user_type', 'password', 'nickname', 'code', 'status'])->first();
  72. $data = $data ? $data->toArray() : [];
  73. $userId = isset($data['id']) ? $data['id'] : 0;
  74. $status = isset($data['status']) ? $data['status'] : 0;
  75. $userPassword = isset($data['password']) ? $data['password'] : '';
  76. if (empty($data) || $userId <= 0) {
  77. $this->error = 2014;
  78. return false;
  79. }
  80. if ($status == 3) {
  81. $this->error = 2050;
  82. return false;
  83. }
  84. if ($status != 1) {
  85. $this->error = 2015;
  86. return false;
  87. }
  88. // 验证登录密码
  89. if (empty($userPassword) || $userPassword != get_password($password)) {
  90. $this->error = 2017;
  91. return false;
  92. }
  93. // 更新
  94. if (!RedisService::get("caches:members:login_{$userId}")) {
  95. $system = isset($params['system']) ? $params['system'] : [];
  96. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  97. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  98. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  99. $version = isset($system['app_version']) ? $system['app_version'] : '';
  100. $updateData = [
  101. 'update_time' => time(),
  102. 'login_ip' => get_client_ip(),
  103. 'login_time' => time(),
  104. 'device_code' => $uuid,
  105. 'login_count' => DB::raw("login_count+1"),
  106. 'app_version' => $version,
  107. 'device' => $appSources == 'ios' ? 1 : 2,
  108. ];
  109. $this->model->where(['id' => $userId])->update($updateData);
  110. RedisService::set("caches:members:login_{$userId}", $updateData, rand(300, 600));
  111. }
  112. // 获取登录授权token
  113. $token = JwtService::make()->encode($userId);
  114. // 结果返回
  115. $result = [
  116. 'access_token' => $token,
  117. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  118. ];
  119. // 用户缓存信息
  120. $this->error = 2019;
  121. $data['token'] = $token;
  122. unset($data['password']);
  123. unset($data['mobile']);
  124. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  125. return $result;
  126. }
  127. /**
  128. * 授权登录
  129. * @param $code
  130. * @param array $params
  131. * @return array|false
  132. */
  133. public function mpAuth($code, $params = [])
  134. {
  135. // 账号登录
  136. if (empty($code)) {
  137. $this->error = 1041;
  138. return false;
  139. }
  140. // 获取授权用户信息
  141. $phone = '';
  142. $pcode = isset($params['pcode']) ? $params['pcode'] : '';
  143. if ($pcode) {
  144. $phoneData = MpService::make()->getPhoneNumber($pcode);
  145. $phoneData = isset($phoneData['phone_info']) ? $phoneData['phone_info'] : [];
  146. $phone = isset($phoneData['phoneNumber']) ? $phoneData['phoneNumber'] : '';
  147. }
  148. if (empty($phone)) {
  149. $this->error = MpService::make()->getError();
  150. return false;
  151. }
  152. $userInfo = MpService::make()->getUserInfo($code);
  153. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  154. if (empty($userInfo)) {
  155. $this->error = MpService::make()->getError();
  156. return false;
  157. }
  158. if (empty($openid)) {
  159. $this->error = 1042;
  160. return false;
  161. }
  162. // 验证是否注册,没有则注册
  163. $where = ['mobile' => $phone];
  164. $data = $this->model->where($where)
  165. ->select(['id', 'openid', 'mobile', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark'])
  166. ->first();
  167. $data = $data ? $data->toArray() : [];
  168. $userId = isset($data['id']) ? $data['id'] : 0;
  169. $avatar = isset($data['avatar']) ? $data['avatar'] : '';
  170. $nickName = isset($data['nickname']) ? $data['nickname'] : '';
  171. $status = isset($data['status']) ? $data['status'] : 0;
  172. $mark = isset($data['mark']) ? $data['mark'] : 0;
  173. if ($data && $userId && $status != 1 && $mark == 1) {
  174. $this->error = 2015;
  175. return false;
  176. }
  177. $system = isset($params['system']) ? $params['system'] : [];
  178. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  179. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  180. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  181. $version = isset($system['app_version']) ? $system['app_version'] : '';
  182. if (empty($data)) {
  183. $userId = $this->model->max('id') + 1;
  184. // 推荐人
  185. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  186. $parents = '';
  187. if ($rid) {
  188. $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1])
  189. ->select(['id', 'parent_id', 'parents', 'status'])
  190. ->first();
  191. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : '';
  192. if ($inviteInfo) {
  193. $parents = $parents ? $parents . $rid . ',' : ",{$rid},";
  194. }
  195. }
  196. $data = [
  197. 'nickname' => $phone ? '用户' . substr($phone, -6, 6) : '用户' . $userId,
  198. 'openid' => $openid,
  199. 'mobile' => $phone,
  200. 'avatar' => '',
  201. 'parent_id' => $rid,
  202. 'parents' => $parents,
  203. 'code' => get_random_code(9, 'S', $userId),
  204. 'password' => $phone ? get_password(substr($phone, -6, 6)) : get_password('123456'),
  205. 'login_ip' => get_client_ip(),
  206. 'create_time' => time(),
  207. 'login_time' => time(),
  208. 'login_count' => DB::raw("login_count+1"),
  209. 'app_version' => $version,
  210. 'app_uuid' => $uuid,
  211. 'device' => $appSources == 'ios' ? 1 : 2,
  212. ];
  213. if (!$userId = $this->model->insertGetId($data)) {
  214. $this->error = 2018;
  215. return false;
  216. }
  217. } // 更新登录信息
  218. else if ($mark == 0 || !RedisService::get("caches:members:login_{$userId}")) {
  219. $updateData = [
  220. 'login_ip' => get_client_ip(),
  221. 'create_time' => time(),
  222. 'login_time' => time(),
  223. 'app_uuid' => $uuid,
  224. 'login_count' => DB::raw("login_count+1"),
  225. 'app_version' => $version,
  226. 'device' => $appSources == 'ios' ? 1 : 2,
  227. 'mark' => 1,
  228. ];
  229. if ($openid) {
  230. $updateData['openid'] = $openid;
  231. }
  232. if ($mark == 0) {
  233. $data['mobile'] = $phone;
  234. $data['openid'] = $openid;
  235. $avatar = '';
  236. $nickName = '';
  237. }
  238. $this->model->where(['id' => $userId])->update($updateData);
  239. RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60));
  240. }
  241. // 获取登录授权token
  242. $token = JwtService::make()->encode($userId);
  243. // 结果返回
  244. $result = [
  245. 'access_token' => $token,
  246. 'info' => ['uid' => $userId, 'openid' => $openid, 'has_info' => $avatar && $nickName ? 1 : 0, 'nickname' => $data['nickname']],
  247. ];
  248. // 用户缓存信息
  249. $this->error = 2019;
  250. $data['token'] = $token;
  251. unset($data['mobile']);
  252. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  253. return $result;
  254. }
  255. /**
  256. * 重置密码
  257. * @param $params
  258. * @return array|false
  259. */
  260. public function forget($params)
  261. {
  262. // 账号登录
  263. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  264. $password = isset($params['password']) ? trim($params['password']) : '';
  265. if (empty($params) || empty($mobile) || empty($password)) {
  266. $this->error = 1041;
  267. return false;
  268. }
  269. // 验证码验证
  270. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  271. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  272. $this->error = SmsService::make()->getError();
  273. return false;
  274. }
  275. // 验证是否注册
  276. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  277. $this->error = 1038;
  278. return false;
  279. }
  280. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  281. $this->error = 2030;
  282. return false;
  283. }
  284. // 操作日志
  285. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  286. ActionLogModel::record();
  287. $this->error = 2031;
  288. return true;
  289. }
  290. /**
  291. * 设置资料
  292. * @param $userId
  293. * @param $params
  294. * @return bool
  295. */
  296. public function setEntry($userId, $params)
  297. {
  298. $cacheLockKey = "caches:members:profile_{$userId}";
  299. if (RedisService::get($cacheLockKey)) {
  300. $this->error = 1034;
  301. return false;
  302. }
  303. // 用户验证
  304. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  305. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  306. ->select(['id', 'password', 'status'])
  307. ->first();
  308. if (!$info || $info['status'] != 1) {
  309. $this->error = 1043;
  310. RedisService::clear($cacheLockKey);
  311. return false;
  312. }
  313. // 获取头像
  314. $avatar = '';
  315. if (isset($params['avatar']) && $params['avatar']) {
  316. $avatar = save_base64_image($params['avatar'], 'avatar');
  317. }
  318. //
  319. $data = [
  320. 'avatar' => $avatar,
  321. 'nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  322. 'update_time' => time()
  323. ];
  324. if (isset($params['province']) && $params['city']) {
  325. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  326. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  327. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  328. }
  329. if (!$this->model->where(['id' => $userId])->update($data)) {
  330. $this->error = 1020;
  331. RedisService::clear($cacheLockKey);
  332. return false;
  333. }
  334. $this->error = 1019;
  335. RedisService::clear($cacheLockKey);
  336. return true;
  337. }
  338. /**
  339. * 获取资料详情
  340. * @param $where
  341. * @param array $field
  342. */
  343. public function getInfo($where, array $field = [], $refresh = true)
  344. {
  345. if (empty($where)) {
  346. return false;
  347. }
  348. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  349. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  350. $info = RedisService::get($cacheKey);
  351. if ($info && !$refresh) {
  352. return $info;
  353. }
  354. $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'balance', 'code', 'openid', 'status', 'avatar'];
  355. $field = $field ? $field : $defaultField;
  356. if (is_array($where)) {
  357. $info = $this->model->where(['mark' => 1])->where($where)->select($field)->first();
  358. } else {
  359. $info = $this->model->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  360. }
  361. $info = $info ? $info->toArray() : [];
  362. if ($info) {
  363. if (isset($info['avatar'])) {
  364. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  365. }
  366. if (isset($info['mobile'])) {
  367. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  368. }
  369. if (isset($info['create_time'])) {
  370. $info['create_at'] = datetime(strtotime($info['create_time']));
  371. }
  372. $info['store'] = isset($info['store']) ? $info['store'] : [];
  373. $info['agent'] = isset($info['agent']) ? $info['agent'] : [];
  374. $info['agent_level'] = 0;
  375. $info['team_count'] = 0;
  376. $params = request()->all();
  377. $type = isset($params['type'])?$params['type']:'';
  378. if ($type == 'agent' && $info['agent']) {
  379. $info['agent_level'] = $this->getAgentLevel($info['id']);
  380. $info['agent_level'] = $info['agent_level']>=2?2 : 1;
  381. $info['team_count'] = $this->getTeamCount($info['id']);
  382. }
  383. if($type == 'agent'){
  384. $info['qrcode'] = MpService::make()->getMiniQrcode('pages/login/login',"rid={$info['id']}");
  385. $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):'';
  386. }else if($type == 'center'){
  387. $info['order1'] = OrderService::make()->getCountByStatus($info['id'], 1);
  388. }
  389. RedisService::set($cacheKey, $info, rand(30, 60));
  390. }
  391. return $info;
  392. }
  393. /**
  394. * 收款账户
  395. * @param $userId
  396. * @return array|mixed
  397. */
  398. public function accountInfo($userId,$type=0)
  399. {
  400. $cacheKey = "caches:members:account:{$userId}_{$type}";
  401. $datas = RedisService::get($cacheKey);
  402. if ($datas) {
  403. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  404. }
  405. $datas[0] = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'status'=>1,'mark'=>1])
  406. ->select(['id','user_id','type','realname','account','account_remark','status'])
  407. ->first();
  408. $datas[0] = $datas[0]? $datas[0] : ['id'=>0,'type'=>1];
  409. $datas[1] = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'status'=>1,'mark'=>1])
  410. ->select(['id','user_id','type','realname','account','account_remark','status'])
  411. ->first();
  412. $datas[1] = $datas[1]? $datas[1] : ['id'=>0,'type'=>2];
  413. if($datas){
  414. RedisService::set($cacheKey, $datas, rand(5,10));
  415. }else{
  416. $datas = [['id'=>0,'type'=>1],['id'=>0,'type'=>2]];
  417. }
  418. return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas;
  419. }
  420. /**
  421. * 绑定收款账户
  422. * @param $userId
  423. * @return array|mixed
  424. */
  425. public function bindAccount($userId, $params)
  426. {
  427. if($params['type']==1){
  428. $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1])
  429. ->select(['id','user_id','type','realname','account','account_remark','status'])
  430. ->first();
  431. $alipayId = isset($alipay['id'])?$alipay['id'] : 0;
  432. $data = [
  433. 'type'=> 1,
  434. 'user_id'=> $userId,
  435. 'realname'=>$params['realname'],
  436. 'account'=>$params['account'],
  437. 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝',
  438. 'status'=>1
  439. ];
  440. if($alipayId){
  441. $data['update_time']=time();
  442. MemberBankModel::where(['id'=>$alipayId])->update($data);
  443. }else {
  444. $data['create_time']=time();
  445. MemberBankModel::insertGetId($data);
  446. }
  447. } else if($params['type']==2){
  448. $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1])
  449. ->select(['id','user_id','type','realname','account','account_remark','status'])
  450. ->first();
  451. $bankId = isset($banks['id'])?$banks['id'] : 0;
  452. $data = [
  453. 'type'=>2,
  454. 'user_id'=> $userId,
  455. 'realname'=>$params['realname'],
  456. 'account'=>$params['account'],
  457. 'account_remark'=>$params['account_remark'],
  458. 'status'=>1
  459. ];
  460. if($bankId){
  461. $data['update_time']=time();
  462. MemberBankModel::where(['id'=>$bankId])->update($data);
  463. }else {
  464. $data['create_time']=time();
  465. MemberBankModel::insertGetId($data);
  466. }
  467. }else{
  468. $this->error = '账号类型错误';
  469. return false;
  470. }
  471. RedisService::keyDel("caches:members:account:{$userId}*");
  472. $this->error = '绑定收款账号成功';
  473. return true;
  474. }
  475. /**
  476. * 获取代理等级
  477. * @param $uid
  478. * @return array|int|mixed
  479. */
  480. public function getAgentLevel($uid)
  481. {
  482. $cacheKey = "caches:members:agentLevel:{$uid}";
  483. $data = RedisService::get($cacheKey);
  484. if ($data) {
  485. return $data;
  486. }
  487. $data = $this->model->from('member as a')
  488. ->leftJoin('agents as b', function ($join) {
  489. $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]);
  490. })
  491. ->where('b.id', '>', 0)
  492. ->where('a.parents', 'like', "%,{$uid},%")
  493. ->where(['a.status' => 1, 'a.mark' => 1])
  494. ->select(['a.id', 'a.parents'])
  495. ->orderBy('a.parents', 'desc')
  496. ->first();
  497. $data = $data ? $data->toArray() : [];
  498. $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : '';
  499. $parents = $parents ? explode(',', $parents) : [];
  500. $level = $parents ? count($parents) : 0;
  501. if($level){
  502. RedisService::set($cacheKey, $level, rand(5,10));
  503. }
  504. return $level;
  505. }
  506. /**
  507. * 团队人数
  508. * @param $uid
  509. * @return array|int|mixed
  510. */
  511. public function getTeamCount($uid)
  512. {
  513. $cacheKey = "caches:members:teamCount:{$uid}";
  514. $data = RedisService::get($cacheKey);
  515. if ($data) {
  516. return $data;
  517. }
  518. $data = $this->model->from('member as a')
  519. ->where('a.parents', 'like', "%,{$uid},%")
  520. ->where(['a.status' => 1, 'a.mark' => 1])
  521. ->count('id');
  522. if($data){
  523. RedisService::set($cacheKey, $data, rand(5,10));
  524. }
  525. return $data;
  526. }
  527. /**
  528. * 生成普通参数二维码
  529. * @param $str 参数
  530. * @param bool $refresh 是否重新生成
  531. * @return bool
  532. */
  533. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  534. {
  535. $basePath = base_path() . '/public';
  536. $qrFile = '/images/qrcode/';
  537. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  538. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  539. }
  540. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  541. $qrFile = $qrFile . "C_{$key}.png";
  542. $cacheKey = "caches:qrcodes:member_" . $key;
  543. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  544. return $qrFile;
  545. }
  546. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  547. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  548. return false;
  549. }
  550. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  551. return $qrFile;
  552. }
  553. /**
  554. * 修改头像
  555. * @param $userId
  556. * @param $avatar
  557. * @return mixed
  558. */
  559. public function saveAvatar($userId, $avatar)
  560. {
  561. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  562. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  563. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  564. @unlink(ATTACHMENT_PATH . $oldAvatar);
  565. }
  566. return true;
  567. }
  568. return false;
  569. }
  570. /**
  571. * 修改账号信息
  572. * @param $userId
  573. * @param $params
  574. * @return bool
  575. */
  576. public function modify($userId, $params)
  577. {
  578. $cacheLockKey = "caches:members:modify_{$userId}";
  579. if (RedisService::get($cacheLockKey)) {
  580. $this->error = 1034;
  581. return false;
  582. }
  583. // 用户验证
  584. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  585. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  586. ->select(['id', 'password', 'status'])
  587. ->first();
  588. $userPassword = isset($info['password']) ? $info['password'] : '';
  589. if (!$info || $info['status'] != 1) {
  590. $this->error = 1029;
  591. RedisService::clear($cacheLockKey);
  592. return false;
  593. }
  594. // 密码校验
  595. $data = ['update_time' => time()];
  596. // 修改数据
  597. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  598. if (isset($params['nickname']) && $nickname) {
  599. $data['nickname'] = $nickname;
  600. }
  601. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  602. if (isset($params['mobile']) && $mobile) {
  603. $data['mobile'] = $mobile;
  604. }
  605. $address = isset($params['address']) ? $params['address'] : '';
  606. if (isset($params['address']) && $address) {
  607. $data['address'] = $address;
  608. }
  609. $password = isset($params['password']) ? $params['password'] : '';
  610. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  611. if (isset($params['password']) && $password) {
  612. if ($userPassword != get_password($password)) {
  613. $this->error = 1038;
  614. RedisService::clear($cacheLockKey);
  615. return false;
  616. }
  617. if (empty($newPassword)) {
  618. $this->error = 1039;
  619. RedisService::clear($cacheLockKey);
  620. return false;
  621. }
  622. $data['password'] = get_password($newPassword);
  623. }
  624. // 头像
  625. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  626. if (isset($params['avatar']) && $avatar) {
  627. $data['avatar'] = get_image_path($avatar);
  628. }
  629. if (!$this->model->where(['id' => $userId])->update($data)) {
  630. $this->error = 1014;
  631. RedisService::clear($cacheLockKey);
  632. return false;
  633. }
  634. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  635. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  636. @unlink(ATTACHMENT_PATH . $oldAvatar);
  637. }
  638. $this->error = 1013;
  639. RedisService::clear($cacheLockKey);
  640. return true;
  641. }
  642. /**
  643. * 账号注销
  644. * @param $userId
  645. * @return bool
  646. */
  647. public function logOff($userId)
  648. {
  649. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  650. ->select(['id', 'password', 'status'])
  651. ->first();
  652. $status = isset($info['status']) ? $info['status'] : 0;
  653. if (empty($info)) {
  654. $this->error = 2044;
  655. return false;
  656. }
  657. if ($status != 1) {
  658. $this->error = 2044;
  659. return false;
  660. }
  661. if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  662. $this->error = 2045;
  663. return false;
  664. }
  665. if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  666. $this->error = 2046;
  667. return false;
  668. }
  669. if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) {
  670. $this->error = 2047;
  671. return false;
  672. }
  673. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  674. $this->error = 2049;
  675. return false;
  676. }
  677. $this->error = 2048;
  678. RedisService::clear("auths:info:" . $userId);
  679. return true;
  680. }
  681. }