MemberService.php 24 KB

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