MemberService.php 25 KB

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