MemberService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberModel;
  16. use App\Models\OrderModel;
  17. use App\Services\BaseService;
  18. use App\Services\MpService;
  19. use App\Services\RedisService;
  20. use App\Services\SmsService;
  21. use Illuminate\Support\Facades\DB;
  22. use phpQrcode\QRcode;
  23. /**
  24. * 会员管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * Class MemberService
  28. * @package App\Services\Api
  29. */
  30. class MemberService extends BaseService
  31. {
  32. // 静态对象
  33. protected static $instance = null;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/11
  38. * MemberService constructor.
  39. */
  40. public function __construct()
  41. {
  42. $this->model = new MemberModel();
  43. }
  44. /**
  45. * 静态入口
  46. * @return MemberService|static|null
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = new static();
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * 账号登录
  57. * @param $params
  58. * @return array|false
  59. */
  60. public function login($params)
  61. {
  62. // 账号登录
  63. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  64. $password = isset($params['password']) ? $params['password'] : '';
  65. if (empty($params) || empty($mobile) || empty($password)) {
  66. $this->error = 1041;
  67. return false;
  68. }
  69. // 验证是否注册,没有则注册
  70. $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'mobile', 'user_type', 'password', 'nickname', 'code', 'status'])->first();
  71. $data = $data ? $data->toArray() : [];
  72. $userId = isset($data['id']) ? $data['id'] : 0;
  73. $status = isset($data['status']) ? $data['status'] : 0;
  74. $userPassword = isset($data['password']) ? $data['password'] : '';
  75. if (empty($data) || $userId <= 0) {
  76. $this->error = 2014;
  77. return false;
  78. }
  79. if ($status == 3) {
  80. $this->error = 2050;
  81. return false;
  82. }
  83. if ($status != 1) {
  84. $this->error = 2015;
  85. return false;
  86. }
  87. // 验证登录密码
  88. if (empty($userPassword) || $userPassword != get_password($password)) {
  89. $this->error = 2017;
  90. return false;
  91. }
  92. // 更新
  93. if (!RedisService::get("caches:members:login_{$userId}")) {
  94. $system = isset($params['system']) ? $params['system'] : [];
  95. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  96. $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios';
  97. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  98. $version = isset($system['app_version']) ? $system['app_version'] : '';
  99. $updateData = [
  100. 'update_time' => time(),
  101. 'login_ip' => get_client_ip(),
  102. 'login_time' => time(),
  103. 'device_code' => $uuid,
  104. 'login_count' => DB::raw("login_count+1"),
  105. 'app_version' => $version,
  106. 'device' => $appSources == 'ios' ? 1 : 2,
  107. ];
  108. $this->model->where(['id' => $userId])->update($updateData);
  109. RedisService::set("caches:members:login_{$userId}", $updateData, rand(300, 600));
  110. }
  111. // 获取登录授权token
  112. $jwt = new Jwt('jwt_jd_app');
  113. $token = $jwt->getToken($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 = 2011;
  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 = 2012;
  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(180, 300));
  240. }
  241. // 获取登录授权token
  242. $jwt = new Jwt('jwt_jd_app');
  243. $token = $jwt->getToken($userId);
  244. // 结果返回
  245. $result = [
  246. 'access_token' => $token,
  247. 'info' => ['uid' => $userId, 'openid' => $openid, 'has_info' => $avatar && $nickName ? 1 : 0, 'nickname' => $data['nickname']],
  248. ];
  249. // 用户缓存信息
  250. $this->error = 2013;
  251. $data['token'] = $token;
  252. unset($data['mobile']);
  253. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  254. return $result;
  255. }
  256. /**
  257. * 重置密码
  258. * @param $params
  259. * @return array|false
  260. */
  261. public function forget($params)
  262. {
  263. // 账号登录
  264. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  265. $password = isset($params['password']) ? trim($params['password']) : '';
  266. if (empty($params) || empty($mobile) || empty($password)) {
  267. $this->error = 1041;
  268. return false;
  269. }
  270. // 验证码验证
  271. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  272. if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) {
  273. $this->error = SmsService::make()->getError();
  274. return false;
  275. }
  276. // 验证是否注册
  277. if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  278. $this->error = 1038;
  279. return false;
  280. }
  281. if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) {
  282. $this->error = 2030;
  283. return false;
  284. }
  285. // 操作日志
  286. ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']);
  287. ActionLogModel::record();
  288. $this->error = 2031;
  289. return true;
  290. }
  291. /**
  292. * 账号注册
  293. * @param $params
  294. * @return array|false
  295. */
  296. public function register($params)
  297. {
  298. // 账号登录
  299. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  300. $password = isset($params['password']) ? trim($params['password']) : '';
  301. $nickname = isset($params['nockname']) ? trim($params['nockname']) : '';
  302. if (empty($params) || empty($mobile) || empty($password)) {
  303. $this->error = 1041;
  304. return false;
  305. }
  306. // 验证码验证
  307. $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : '';
  308. if (!SmsService::make()->check($mobile, $smsCode, 'register')) {
  309. $this->error = SmsService::make()->getError();
  310. return false;
  311. }
  312. // 验证是否注册
  313. if ($this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) {
  314. $this->error = 2002;
  315. return false;
  316. }
  317. // 驾驶证
  318. $drivingLicense = isset($params['driving_license']) && $params['driving_license'] ? get_image_path($params['driving_license']) : '';
  319. if (empty($drivingLicense)) {
  320. $this->error = 2007;
  321. return false;
  322. }
  323. // 行驶证
  324. $driversLicense = isset($params['drivers_license']) && $params['drivers_license'] ? get_image_path($params['drivers_license']) : '';
  325. if (empty($driversLicense)) {
  326. $this->error = 2008;
  327. return false;
  328. }
  329. $id = $this->model->max('id') + 1;
  330. $system = isset($params['system']) ? $params['system'] : [];
  331. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  332. $appSources = isset($system['app_sources']) ? $system['app_sources'] : '';
  333. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  334. $data = [
  335. 'mobile' => $mobile,
  336. 'user_type' => 2,
  337. 'avatar' => '',
  338. 'nickname' => $nickname ? $nickname : 'DU' . rand(10, 99) . substr($mobile, -6, 6),
  339. 'realname' => isset($params['realname']) ? trim($params['realname']) : '',
  340. 'password' => get_password($password),
  341. 'car_number' => isset($params['car_number']) ? trim($params['car_number']) : '',
  342. 'car_type' => isset($params['car_type']) ? trim($params['car_type']) : '',
  343. 'driving_license' => $drivingLicense,
  344. 'drivers_license' => $driversLicense,
  345. 'code' => strtoupper(get_random_code(9, 'D', "{$id}")),
  346. 'app_uuid' => $uuid,
  347. 'device' => $appSources == 'android' ? 2 : 1,
  348. 'create_time' => time(),
  349. 'status' => 1,
  350. 'picker_status' => 1,
  351. 'confirm_remark' => '',
  352. 'confirm_status' => 2,
  353. 'balance' => 0,
  354. 'deposit' => 0,
  355. 'mark' => 1,
  356. 'login_ip' => get_client_ip(),
  357. ];
  358. if (!$userId = $this->model->insertGetId($data)) {
  359. $this->error = 2003;
  360. return false;
  361. }
  362. // 获取登录授权token
  363. $jwt = new Jwt('jwt_jd_app');
  364. $token = $jwt->getToken($userId);
  365. // 结果返回
  366. $result = [
  367. 'access_token' => $token,
  368. 'info' => ['uid' => $userId, 'nickname' => $data['nickname']],
  369. ];
  370. // 注册成功
  371. $this->error = 2004;
  372. $data['token'] = $token;
  373. unset($data['password']);
  374. unset($data['mobile']);
  375. RedisService::keyDel("caches:members:count*");
  376. RedisService::set("auths:info:{$userId}", $data, 24 * 3600);
  377. return $result;
  378. }
  379. /**
  380. * 设置资料
  381. * @param $userId
  382. * @param $params
  383. * @return bool
  384. */
  385. public function setEntry($userId, $params)
  386. {
  387. $cacheLockKey = "caches:members:profile_{$userId}";
  388. if (RedisService::get($cacheLockKey)) {
  389. $this->error = 1034;
  390. return false;
  391. }
  392. // 用户验证
  393. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  394. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  395. ->select(['id', 'password', 'status'])
  396. ->first();
  397. if (!$info || $info['status'] != 1) {
  398. $this->error = 1043;
  399. RedisService::clear($cacheLockKey);
  400. return false;
  401. }
  402. // 获取头像
  403. $avatar = '';
  404. if (isset($params['avatar']) && $params['avatar']) {
  405. $avatar = save_base64_image($params['avatar'], 'avatar');
  406. }
  407. //
  408. $data = [
  409. 'avatar' => $avatar,
  410. 'nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  411. 'update_time' => time()
  412. ];
  413. if (isset($params['province']) && $params['city']) {
  414. $data['province'] = isset($params['province']) ? trim($params['province']) : '';
  415. $data['city'] = isset($params['city']) ? trim($params['city']) : '';
  416. $data['district'] = isset($params['district']) ? trim($params['district']) : '';
  417. }
  418. if (!$this->model->where(['id' => $userId])->update($data)) {
  419. $this->error = 1020;
  420. RedisService::clear($cacheLockKey);
  421. return false;
  422. }
  423. $this->error = 1019;
  424. RedisService::clear($cacheLockKey);
  425. return true;
  426. }
  427. /**
  428. * 获取资料详情
  429. * @param $where
  430. * @param array $field
  431. */
  432. public function getInfo($where, array $field = [], $refresh = true)
  433. {
  434. if (empty($where)) {
  435. return false;
  436. }
  437. $fieldKey = $field ? '_' . md5(json_encode($field)) : '';
  438. $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey));
  439. $info = RedisService::get($cacheKey);
  440. if ($info && !$refresh) {
  441. return $info;
  442. }
  443. $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'balance', 'code', 'openid', 'status', 'avatar'];
  444. $field = $field ? $field : $defaultField;
  445. if (is_array($where)) {
  446. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where($where)->select($field)->first();
  447. } else {
  448. $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first();
  449. }
  450. $info = $info ? $info->toArray() : [];
  451. if ($info) {
  452. if (isset($info['avatar'])) {
  453. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  454. }
  455. if (isset($info['mobile'])) {
  456. $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : '';
  457. }
  458. if (isset($info['create_time'])) {
  459. $info['create_at'] = datetime(strtotime($info['create_time']));
  460. }
  461. $info['store'] = isset($info['store']) ? $info['store'] : [];
  462. $info['agent'] = isset($info['agent']) ? $info['agent'] : [];
  463. $info['agent_level'] = 0;
  464. $params = request()->all();
  465. $type = isset($params['type'])?$params['type']:'';
  466. if ($type == 'agent' && $info['agent']) {
  467. $info['agent_level'] = $this->getAgentLevel($info['id']);
  468. }
  469. RedisService::set($cacheKey, $info, rand(30, 60));
  470. }
  471. return $info;
  472. }
  473. /**
  474. * 获取代理等级
  475. * @param $uid
  476. * @return array|int|mixed
  477. */
  478. public function getAgentLevel($uid)
  479. {
  480. $cacheKey = "caches:members:agentLevel:{$uid}";
  481. $data = RedisService::get($cacheKey);
  482. if ($data) {
  483. return $data;
  484. }
  485. $data = $this->model->from('member as a')
  486. ->leftJoin('agents as b', function ($join) {
  487. $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]);
  488. })
  489. ->where('b.id', '>', 0)
  490. ->where('a.parents', 'like', "%,{$uid},%")
  491. ->where(['a.status' => 1, 'a.mark' => 1])
  492. ->select(['a.id', 'a.parents'])
  493. ->orderBy('a.parents', 'desc')
  494. ->first();
  495. $data = $data ? $data->toArray() : [];
  496. $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : '';
  497. $parents = $parents ? explode(',', $parents) : [];
  498. $level = $parents ? count($parents) : 0;
  499. if($level){
  500. RedisService::set($cacheKey, $level, rand(5,10));
  501. }
  502. return $level;
  503. }
  504. /**
  505. * 生成普通参数二维码
  506. * @param $str 参数
  507. * @param bool $refresh 是否重新生成
  508. * @return bool
  509. */
  510. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  511. {
  512. $basePath = base_path() . '/public';
  513. $qrFile = '/images/qrcode/';
  514. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  515. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  516. }
  517. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  518. $qrFile = $qrFile . "C_{$key}.png";
  519. $cacheKey = "caches:qrcodes:member_" . $key;
  520. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  521. return $qrFile;
  522. }
  523. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  524. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  525. return false;
  526. }
  527. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  528. return $qrFile;
  529. }
  530. /**
  531. * 修改头像
  532. * @param $userId
  533. * @param $avatar
  534. * @return mixed
  535. */
  536. public function saveAvatar($userId, $avatar)
  537. {
  538. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  539. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  540. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  541. @unlink(ATTACHMENT_PATH . $oldAvatar);
  542. }
  543. return true;
  544. }
  545. return false;
  546. }
  547. /**
  548. * 修改账号信息
  549. * @param $userId
  550. * @param $params
  551. * @return bool
  552. */
  553. public function modify($userId, $params)
  554. {
  555. $cacheLockKey = "caches:members:modify_{$userId}";
  556. if (RedisService::get($cacheLockKey)) {
  557. $this->error = 1034;
  558. return false;
  559. }
  560. // 用户验证
  561. RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3));
  562. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  563. ->select(['id', 'password', 'status'])
  564. ->first();
  565. $userPassword = isset($info['password']) ? $info['password'] : '';
  566. if (!$info || $info['status'] != 1) {
  567. $this->error = 1029;
  568. RedisService::clear($cacheLockKey);
  569. return false;
  570. }
  571. // 密码校验
  572. $data = ['update_time' => time()];
  573. // 修改数据
  574. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  575. if (isset($params['nickname']) && $nickname) {
  576. $data['nickname'] = $nickname;
  577. }
  578. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  579. if (isset($params['mobile']) && $mobile) {
  580. $data['mobile'] = $mobile;
  581. }
  582. $address = isset($params['address']) ? $params['address'] : '';
  583. if (isset($params['address']) && $address) {
  584. $data['address'] = $address;
  585. }
  586. $password = isset($params['password']) ? $params['password'] : '';
  587. $newPassword = isset($params['new_password']) ? $params['new_password'] : '';
  588. if (isset($params['password']) && $password) {
  589. if ($userPassword != get_password($password)) {
  590. $this->error = 1038;
  591. RedisService::clear($cacheLockKey);
  592. return false;
  593. }
  594. if (empty($newPassword)) {
  595. $this->error = 1039;
  596. RedisService::clear($cacheLockKey);
  597. return false;
  598. }
  599. $data['password'] = get_password($newPassword);
  600. }
  601. // 头像
  602. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  603. if (isset($params['avatar']) && $avatar) {
  604. $data['avatar'] = get_image_path($avatar);
  605. }
  606. if (!$this->model->where(['id' => $userId])->update($data)) {
  607. $this->error = 1014;
  608. RedisService::clear($cacheLockKey);
  609. return false;
  610. }
  611. $oldAvatar = isset($info['avatar']) ? $info['avatar'] : '';
  612. if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  613. @unlink(ATTACHMENT_PATH . $oldAvatar);
  614. }
  615. $this->error = 1013;
  616. RedisService::clear($cacheLockKey);
  617. return true;
  618. }
  619. /**
  620. * 账号注销
  621. * @param $userId
  622. * @return bool
  623. */
  624. public function logOff($userId)
  625. {
  626. $info = $this->model->where(['id' => $userId, 'mark' => 1])
  627. ->select(['id', 'password', 'status'])
  628. ->first();
  629. $status = isset($info['status']) ? $info['status'] : 0;
  630. if (empty($info)) {
  631. $this->error = 2044;
  632. return false;
  633. }
  634. if ($status != 1) {
  635. $this->error = 2044;
  636. return false;
  637. }
  638. if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  639. $this->error = 2045;
  640. return false;
  641. }
  642. if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) {
  643. $this->error = 2046;
  644. return false;
  645. }
  646. if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) {
  647. $this->error = 2047;
  648. return false;
  649. }
  650. if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) {
  651. $this->error = 2049;
  652. return false;
  653. }
  654. $this->error = 2048;
  655. RedisService::clear("auths:info:" . $userId);
  656. return true;
  657. }
  658. }