MemberService.php 34 KB

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