MemberService.php 34 KB

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