MemberService.php 24 KB

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