MemberService.php 34 KB

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