MemberService.php 26 KB

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