MemberService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\Common\ApiService;
  17. use App\Services\Common\MemberSettingService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use App\Services\UsdtWalletService;
  21. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  22. use Illuminate\Support\Facades\DB;
  23. use phpQrcode\QRcode;
  24. /**
  25. * 会员-服务类
  26. * Class MemberService
  27. * @package App\Services\Api
  28. */
  29. class MemberService extends BaseService
  30. {
  31. // 静态对象
  32. protected static $instance = null;
  33. /**
  34. * 构造函数
  35. * @since 2020/11/10
  36. * MemberService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new MemberModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 获取资料详情
  55. * @param $where
  56. * @param array $field
  57. */
  58. public function getInfo($where, array $field = [])
  59. {
  60. $field = $field ? $field : ['id', 'api_id', 'username', 'realname', 'nickname', 'openid', 'idcard', 'google_secret', 'password', 'trade_password', 'trc_address', 'erc_address', 'erc_hexaddress', 'source', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'idcard_hand_img', 'bank_front_img', 'bank_back_img', 'safe_level', 'user_type', 'member_level', 'usdt_num', 'user_type', 'status', 'credit', 'avatar'];
  61. if (is_array($where)) {
  62. $info = $this->model->where($where)->select($field)->first();
  63. } else {
  64. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  65. }
  66. $info = $info ? $info->toArray() : [];
  67. if ($info) {
  68. $tradePrice = ConfigService::make()->getConfigByCode('usdt_sell_price');
  69. $tradePrice = $tradePrice ? $tradePrice : 0;
  70. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  71. $info['idcard_front_img'] = $info['idcard_front_img'] ? get_image_url($info['idcard_front_img']) : '';
  72. $info['idcard_back_img'] = $info['idcard_back_img'] ? get_image_url($info['idcard_back_img']) : '';
  73. $info['idcard_hand_img'] = $info['idcard_hand_img'] ? get_image_url($info['idcard_hand_img']) : '';
  74. $info['bank_front_img'] = $info['bank_front_img'] ? get_image_url($info['bank_front_img']) : '';
  75. $info['bank_back_img'] = $info['bank_back_img'] ? get_image_url($info['bank_back_img']) : '';
  76. $info['usdt_num'] = isset($info['usdt_num']) ? moneyFormat($info['usdt_num'], 4) : '0.0000';
  77. $info['cny_num'] = $info['usdt_num'] && $tradePrice > 0 ? moneyFormat($info['usdt_num'] * $tradePrice, 6) : '0.0000';
  78. $info['is_collection'] = MemberPaymentService::make()->checkHasByUser($info['id']);
  79. $info['set_trade_password'] = $info['trade_password'] ? 1 : 0;
  80. $info['set_trade_password'] = $info['trade_password'] == $info['password'] ? 2 : $info['set_trade_password'];
  81. unset($info['password']);
  82. unset($info['trade_password']);
  83. // 收款二维码
  84. $qrcode = $this->makeQrcode($info['trc_address']);
  85. $info['trc_qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  86. $ercQrcode = $this->makeQrcode($info['erc_hexaddress']);
  87. $info['erc_qrcode'] = $ercQrcode ? get_image_url($ercQrcode) : '';
  88. }
  89. return $info;
  90. }
  91. /**
  92. * 用户注册
  93. * @param $params
  94. * @return bool
  95. */
  96. public function register($params)
  97. {
  98. // 检测账号是否存在
  99. if ($this->checkExists('username', $params['username'])) {
  100. $this->error = '2005';
  101. return false;
  102. }
  103. $username = isset($params['username']) ? trim($params['username']) : '';
  104. $password = isset($params['password']) ? trim($params['password']) : '';
  105. $userType = isset($params['user_type']) ? intval($params['user_type']) : 1;
  106. if (!checkPassword($password)) {
  107. $this->error = '2007';
  108. return false;
  109. }
  110. $data = [
  111. 'user_type' => $userType ? $userType : 1,
  112. 'username' => $username,
  113. 'password' => get_password($password . md5($password . 'otc')),
  114. 'trade_password' => get_password($password . md5($password . 'otc')),
  115. 'mobile' => isPhone($username) ? $username : '',
  116. 'status' => 1,
  117. 'mark' => 1,
  118. 'create_time' => time(),
  119. ];
  120. // 谷歌验证码
  121. $google = GoogleAuthenticator::CreateSecret();
  122. $data['google_secret'] = isset($google['secret']) ? $google['secret'] : '';
  123. // 生成trc2.0钱包地址
  124. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  125. if ($trcAddress) {
  126. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  127. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  128. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  129. } else {
  130. $this->error = 2201;
  131. return false;
  132. }
  133. // 生erc2.0钱包地址
  134. $ercAddress = UsdtWalletService::make()->getErcAddress();
  135. if ($trcAddress) {
  136. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  137. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  138. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  139. } else {
  140. $this->error = 2202;
  141. return false;
  142. }
  143. if ($id = $this->model->edit($data)) {
  144. MemberSettingService::make()->getInfo($id);
  145. return true;
  146. }
  147. $this->error = 2007;
  148. return false;
  149. }
  150. /**
  151. * 用户登录
  152. * @param $params
  153. * @return array|false
  154. */
  155. public function login($params)
  156. {
  157. $username = isset($params['username']) ? $params['username'] : '';
  158. $password = isset($params['password']) ? $params['password'] : '';
  159. if (empty($username) || empty($password)) {
  160. $this->error = 1013;
  161. return false;
  162. }
  163. // 用户验证
  164. $info = $this->model->getOne([['username', '=', $username]]);
  165. if (!$info) {
  166. $this->error = 2001;
  167. return false;
  168. }
  169. // 密码校验
  170. $password = get_password($password . md5($password . 'otc'));
  171. if ($password != $info['password']) {
  172. $this->error = 2002;
  173. return false;
  174. }
  175. // 使用状态校验
  176. if ($info['status'] != 1) {
  177. $this->error = 2009;
  178. return false;
  179. }
  180. // 承兑商
  181. if ($info['user_type'] == 2) {
  182. $this->error = 1024;
  183. return false;
  184. }
  185. // 设置日志标题
  186. ActionLogModel::setTitle("会员登录APP");
  187. ActionLogModel::record($info);
  188. // JWT生成token
  189. $jwt = new Jwt('jwt_app');
  190. $token = $jwt->getToken($info['id']);
  191. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  192. // 登录
  193. $updateData = ['login_time' => time(),'is_online'=>1,'login_count'=>$info['login_count']+1, 'login_ip' => get_client_ip()];
  194. if (empty($info['google_secret'])) {
  195. $google = GoogleAuthenticator::CreateSecret();
  196. $updateData['google_secret'] = isset($google['secret']) ? $google['secret'] : '';
  197. }
  198. $this->model->where(['id' => $info['id']])->update($updateData);
  199. // 登录数据
  200. return [
  201. 'token' => $token,
  202. 'user_id' => $info['id'],
  203. 'user_type' => $info['user_type'],
  204. ];
  205. }
  206. /**
  207. * API用户注册
  208. * @param $apiId
  209. * @param $params
  210. * @return bool
  211. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  212. * @throws \Tron\Exceptions\TronErrorException
  213. */
  214. public function apiRegister($apiId, $params)
  215. {
  216. // 检测账号是否存在
  217. if ($this->checkExists('username', $params['username'])) {
  218. $this->error = '2005';
  219. return false;
  220. }
  221. $username = isset($params['username']) ? trim($params['username']) : '';
  222. $password = isset($params['password']) ? trim($params['password']) : '';
  223. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  224. $apiInfo = ApiService::make()->getInfo($apiId);
  225. $limits = isset($apiInfo['user_limits']) ? $apiInfo['user_limits'] : [];
  226. if ($limits && !in_array($userType, $limits)) {
  227. $this->error = '6004';
  228. return false;
  229. }
  230. $data = [
  231. 'username' => $username,
  232. 'api_id' => $apiId,
  233. 'user_type' => $userType ? $userType : 3,
  234. 'password' => get_password($password . md5($password . 'otc')),
  235. 'trade_password' => get_password($password . md5($password . 'otc')),
  236. 'mobile' => isPhone($username) ? $username : '',
  237. 'idcard' => isset($params['idcard'])? $params['idcard'] : '',
  238. 'realname' => isset($params['realname'])? $params['realname'] : '',
  239. 'status' => 1,
  240. 'mark' => 1,
  241. 'create_time' => time(),
  242. ];
  243. // 生成trc2.0钱包地址
  244. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  245. if ($trcAddress) {
  246. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  247. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  248. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  249. } else {
  250. $this->error = 2201;
  251. return false;
  252. }
  253. // 生erc2.0钱包地址
  254. $ercAddress = UsdtWalletService::make()->getErcAddress();
  255. if ($trcAddress) {
  256. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  257. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  258. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  259. } else {
  260. $this->error = 2202;
  261. return false;
  262. }
  263. if ($id = $this->model->edit($data)) {
  264. MemberSettingService::make()->getInfo($id);
  265. return true;
  266. }
  267. $this->error = 2007;
  268. return false;
  269. }
  270. /**
  271. * API用户登录
  272. * @param $params
  273. * @return array|false
  274. */
  275. public function apiLogin($apiId, $params)
  276. {
  277. $username = isset($params['username']) ? $params['username'] : '';
  278. $password = isset($params['password']) ? $params['password'] : '';
  279. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  280. $apiKey = isset($params['api_key']) ? $params['api_key'] : '';
  281. if (empty($username) || empty($password) || empty($apiKey) || empty($userType)) {
  282. $this->error = 1013;
  283. return false;
  284. }
  285. // 用户验证
  286. $info = $this->model->getOne([['username', '=', $username]]);
  287. if (!$info) {
  288. // 直接注册
  289. if (!$this->apiRegister($apiId, $params)) {
  290. return false;
  291. }
  292. $info = $this->model->getOne([['username', '=', $username]]);
  293. } else {
  294. // 密码校验
  295. $password = get_password($password . md5($password . 'otc'));
  296. if ($password != $info['password']) {
  297. $this->error = 2002;
  298. return false;
  299. }
  300. // 使用状态校验
  301. if ($info['status'] != 1) {
  302. $this->error = 2009;
  303. return false;
  304. }
  305. if ($info['api_id'] != $apiId) {
  306. $this->error = 6010;
  307. return false;
  308. }
  309. // 用户类型
  310. if (!in_array($info['user_type'], [3, 4, 5])) {
  311. $this->error = 1024;
  312. return false;
  313. }
  314. }
  315. // 设置日志标题
  316. ActionLogModel::setTitle("接口平台会员授权登录");
  317. ActionLogModel::record($info);
  318. // JWT生成token
  319. $token = 'TB'.strtoupper(md5($info['id'] . $username).rand(100,999));
  320. RedisService::set("apis:auths:info:{$info['id']}", $info, rand(5, 10));
  321. RedisService::set("apis:tokens:{$token}", ['id' => $info['id'], 'info' => $info, 'token' => $token], 48 * 3600);
  322. // 登录
  323. $updateData = ['login_time' => time(), 'login_ip' => get_client_ip()];
  324. $this->model->where(['id' => $info['id']])->update($updateData);
  325. // 登录数据
  326. if ($info['idcard_check'] != 1) {
  327. $appUrl = ConfigService::make()->getConfigByCode('app_download_url');
  328. return [
  329. 'app_url' => $appUrl,
  330. ];
  331. } else {
  332. return [
  333. 'token' => $token,
  334. 'user_id' => $info['id'],
  335. ];
  336. }
  337. }
  338. /**
  339. * 身份认证
  340. * @param $userId
  341. * @param $params
  342. * @return false
  343. */
  344. public function auth($userId, $params)
  345. {
  346. $data = [
  347. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  348. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  349. ];
  350. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  351. $data['idcard_front_img'] = $params['idcard_front_img'];
  352. }
  353. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  354. $data['idcard_back_img'] = $params['idcard_back_img'];
  355. }
  356. if (isset($params['idcard_hand_img']) && strpos($params['idcard_hand_img'], 'http') === false) {
  357. $data['idcard_hand_img'] = $params['idcard_hand_img'];
  358. }
  359. if (isset($params['bank_front_img']) && strpos($params['bank_front_img'], 'http') === false) {
  360. $data['bank_front_img'] = $params['bank_front_img'];
  361. }
  362. if (isset($params['bank_back_img']) && strpos($params['bank_back_img'], 'http') === false) {
  363. $data['bank_back_img'] = $params['bank_back_img'];
  364. }
  365. $info = $this->getInfo($userId);
  366. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  367. if ($idcardCheck == 1) {
  368. $this->error = '2011';
  369. return false;
  370. }
  371. return $this->model->where(['id' => $userId])->update($data);
  372. }
  373. /**
  374. * 修改账号
  375. * @param $userId
  376. * @param $params
  377. * @return bool
  378. */
  379. public function modify($userId, $params)
  380. {
  381. $username = isset($params['username']) ? $params['username'] : '';
  382. $newUsername = isset($params['new_username']) ? $params['new_username'] : '';
  383. $password = isset($params['password']) ? $params['password'] : '';
  384. if (empty($username) || empty($password)) {
  385. $this->error = 1013;
  386. return false;
  387. }
  388. // 用户验证
  389. $info = $this->model->getOne([['username', '=', $username]]);
  390. if (!$info || $info['id'] != $userId) {
  391. $this->error = 2001;
  392. return false;
  393. }
  394. // 使用状态校验
  395. if ($info['status'] != 1) {
  396. $this->error = 2009;
  397. return false;
  398. }
  399. // 密码校验
  400. $password = get_password($password . md5($password . 'otc'));
  401. if ($password != $info['password']) {
  402. $this->error = 2002;
  403. return false;
  404. }
  405. $checkInfo = $this->model->getOne([['username', '=', $newUsername]]);
  406. if ($checkInfo && $checkInfo['id'] != $info['id']) {
  407. $this->error = 2005;
  408. return false;
  409. }
  410. if (!$this->model->where(['id' => $info['id']])->update(['username' => $newUsername, 'update_time' => time()])) {
  411. $this->error = 2021;
  412. return false;
  413. }
  414. $this->error = 2020;
  415. return true;
  416. }
  417. /**
  418. * 修改更新登录密码
  419. * @param $userId
  420. * @param $params
  421. * @return bool
  422. */
  423. public function updatePassword($userId, $params)
  424. {
  425. $username = isset($params['username']) ? $params['username'] : '';
  426. $password = isset($params['password']) ? $params['password'] : '';
  427. if (empty($username) || empty($password)) {
  428. $this->error = 1013;
  429. return false;
  430. }
  431. // 用户验证
  432. $info = $this->model->getOne([['username', '=', $username]]);
  433. if (!$info || $info['id'] != $userId) {
  434. $this->error = 2001;
  435. return false;
  436. }
  437. // 使用状态校验
  438. if ($info['status'] != 1) {
  439. $this->error = 2009;
  440. return false;
  441. }
  442. // 更新登录密码
  443. $password = get_password($password . md5($password . 'otc'));
  444. if (!$this->model->where(['id' => $info['id']])->update(['password' => $password, 'update_time' => time()])) {
  445. $this->error = 2025;
  446. return false;
  447. }
  448. $this->error = 2024;
  449. return true;
  450. }
  451. /**
  452. * 修改更新交易密码
  453. * @param $userId
  454. * @param $params
  455. * @return bool
  456. */
  457. public function updateTradePassword($userId, $params)
  458. {
  459. $username = isset($params['username']) ? $params['username'] : '';
  460. $tradePassword = isset($params['trade_password']) ? $params['trade_password'] : '';
  461. if (empty($username) || empty($tradePassword)) {
  462. $this->error = 1013;
  463. return false;
  464. }
  465. // 用户验证
  466. $info = $this->model->getOne([['username', '=', $username]]);
  467. if (!$info || $info['id'] != $userId) {
  468. $this->error = 2001;
  469. return false;
  470. }
  471. // 使用状态校验
  472. if ($info['status'] != 1) {
  473. $this->error = 2009;
  474. return false;
  475. }
  476. // 交易密码
  477. $password = get_password($tradePassword . md5($tradePassword . 'otc'));
  478. if (!$this->model->where(['id' => $info['id']])->update(['safe_level' => 2, 'trade_password' => $password, 'update_time' => time()])) {
  479. $this->error = 2023;
  480. return false;
  481. }
  482. $this->error = 2022;
  483. return true;
  484. }
  485. /**
  486. * 获取钱包地址密钥参数
  487. * @param $address
  488. * @param string $type 链类型:trc-trc2.0,erc
  489. * @return mixed
  490. */
  491. public function getWallet($address, $type = 'trc')
  492. {
  493. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  494. }
  495. /**
  496. * 获取待处理用户数据
  497. * @param int $page
  498. * @param int $pageSize
  499. * @return array|mixed
  500. */
  501. public function getCatchMember($page = 1, $pageSize = 500)
  502. {
  503. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  504. $datas = RedisService::get($cacheKey);
  505. if ($datas) {
  506. return $datas;
  507. }
  508. $datas = $this->model->where(['mark' => 1])
  509. ->select(['id', 'trc_address', 'usdt_num'])
  510. ->paginate($pageSize > 0 ? $pageSize : 9999999, [], 'page', $page);
  511. $datas = $datas ? $datas->toArray() : [];
  512. $datas = isset($datas['data']) ? $datas['data'] : [];
  513. if ($datas) {
  514. RedisService::set($cacheKey, $datas, rand(120, 600));
  515. }
  516. return $datas;
  517. }
  518. /**
  519. * 匹配承兑商
  520. * @param $num 交易数量
  521. * @param int $tradeType 交易类型:1-购买,2-出售
  522. */
  523. public function getTradeMember($num, $tradeType = 1, $userId = 0)
  524. {
  525. $data = $this->model->from('member as m')
  526. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  527. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  528. ->where(['m.status' => 1,'m.idcard_check'=>1, 'm.user_type' => 2, 'u.status' => 1, 'm.mark' => 1])
  529. ->where('m.usdt_num', '>=', $num)
  530. ->where(function ($query) use ($userId) {
  531. if ($userId) {
  532. $query->whereNotIn('m.id', [$userId]);
  533. }
  534. })
  535. ->where(function ($query) use ($tradeType) {
  536. $time = time();
  537. // 买单
  538. if ($tradeType == 1) {
  539. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  540. } // 卖单
  541. else {
  542. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  543. }
  544. })
  545. ->select(['m.id', 'm.usdt_num', 'm.username', 'm.credit'])
  546. ->orderBy("credit", 'desc')
  547. ->orderBy(DB::raw("rand()"))
  548. ->first();
  549. $data = $data ? $data->toArray() : [];
  550. return $data;
  551. }
  552. /**
  553. * 匹配承兑商
  554. * @param $num 交易数量
  555. * @param int $tradeType 交易类型:1-购买,2-出售
  556. */
  557. public function getTradeMemberOptions($num, $tradeType = 1, $userId = 0, $keyword = '', $paseSize = 50)
  558. {
  559. $cacheKey = "caches:tradeMenber:{$tradeType}_" . md5($num . $userId . $keyword . $paseSize);
  560. if ($datas = RedisService::get($cacheKey)) {
  561. return $datas;
  562. }
  563. $datas = $this->model->from('member as m')
  564. ->leftJoin('user as u', 'u.user_id', '=', 'm.id')
  565. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  566. ->where(['m.status' => 1, 'm.user_type' => 2, 'u.status' => 1, 'm.mark' => 1])
  567. ->where('m.usdt_num', '>=', $num)
  568. ->where(function ($query) use ($userId, $keyword) {
  569. if ($userId) {
  570. $query->whereNotIn('m.id', [$userId]);
  571. }
  572. if ($keyword) {
  573. $query->where('m.username', 'like', "%{$keyword}%");
  574. }
  575. })
  576. ->where(function ($query) use ($tradeType) {
  577. $time = time();
  578. // 买单
  579. if ($tradeType == 1) {
  580. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  581. } // 卖单
  582. else {
  583. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  584. }
  585. })
  586. ->select(['m.id', 'm.usdt_num', 'm.username', 'm.credit'])
  587. ->orderBy("credit", 'desc')
  588. ->orderBy("m.safe_level", 'desc')
  589. ->paginate($paseSize);
  590. $datas = $datas ? $datas->toArray() : [];
  591. $datas = isset($datas['data']) ? $datas['data'] : [];
  592. if ($datas) {
  593. RedisService::set($cacheKey, $datas, rand(10, 30));
  594. }
  595. return $datas;
  596. }
  597. /**
  598. * 生成普通参数二维码
  599. * @param $str 参数
  600. * @param bool $refresh 是否重新生成
  601. * @return bool
  602. */
  603. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  604. {
  605. $qrFile = '/images/qrcode/';
  606. if (!is_dir('/uploads' . $qrFile)) {
  607. @mkdir('./uploads' . $qrFile, 0755, true);
  608. }
  609. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size . $margin . $level)) . '.png';
  610. $cacheKey = "caches:qrcodes:member_" . md5($str);
  611. if (RedisService::get($cacheKey) && is_file('/uploads' . $qrFile) && !$refresh) {
  612. //return $qrFile;
  613. }
  614. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  615. if (!file_exists('./uploads' . $qrFile)) {
  616. return false;
  617. }
  618. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  619. return $qrFile;
  620. }
  621. /**
  622. * 获取钱包归集用户列表
  623. * @param float $minUsdt
  624. * @param int $page
  625. * @param int $pageSize
  626. * @return array|mixed
  627. */
  628. public function getTriggerAddressList($minUsdt = 0.2, $page = 1, $pageSize = 200)
  629. {
  630. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  631. $datas = RedisService::get($cacheKey);
  632. if ($datas) {
  633. return $datas;
  634. }
  635. $datas = $this->model->where(['mark' => 1])
  636. ->select(['id', 'trc_usdt', 'trc_address', 'trc_wif', 'trc_hexaddress', 'erc_address', 'erc_wif', 'erc_hexaddress'])
  637. ->orderBy('trc_usdt')
  638. ->paginate($pageSize > 0 ? $pageSize : 9999999, [], 'page', $page);
  639. $datas = $datas ? $datas->toArray() : [];
  640. $datas = isset($datas['data']) ? $datas['data'] : [];
  641. if ($datas) {
  642. RedisService::set($cacheKey, $datas, rand(10, 30));
  643. }
  644. return $datas;
  645. }
  646. }