MemberService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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\MemberSettingService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use App\Services\UsdtWalletService;
  20. use Illuminate\Support\Facades\DB;
  21. use phpQrcode\QRcode;
  22. /**
  23. * 会员-服务类
  24. * Class MemberService
  25. * @package App\Services\Api
  26. */
  27. class MemberService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @since 2020/11/10
  34. * LoginService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new MemberModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return static|null
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = (new static());
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 获取资料详情
  53. * @param $where
  54. * @param array $field
  55. */
  56. public function getInfo($where, array $field = [])
  57. {
  58. $field = $field ? $field : ['id', 'username', 'realname', 'nickname', 'openid', 'idcard', 'trc_address', 'erc_address', 'source', 'idcard_check', 'idcard_front_img', 'idcard_back_img', 'safe_level', 'user_type', 'member_level', 'usdt_num', 'user_type', 'status', 'credit', 'avatar'];
  59. if (is_array($where)) {
  60. $info = $this->model->where($where)->select($field)->first();
  61. } else {
  62. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  63. }
  64. $info = $info ? $info->toArray() : [];
  65. if ($info) {
  66. $tradePrice = ConfigService::make()->getConfigByCode('usdt_sell_price');
  67. $tradePrice = $tradePrice? $tradePrice : 0;
  68. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  69. $info['idcard_front_img'] = $info['idcard_front_img'] ? get_image_url($info['idcard_front_img']) : '';
  70. $info['idcard_back_img'] = $info['idcard_back_img'] ? get_image_url($info['idcard_back_img']) : '';
  71. $info['usdt_num'] = isset($info['usdt_num']) ? moneyFormat($info['usdt_num'], 4) : '0.0000';
  72. $info['cny_num'] = $info['usdt_num'] && $tradePrice>0? moneyFormat($info['usdt_num']*$tradePrice, 6) : '0.0000';
  73. $info['is_collection'] = MemberPaymentService::make()->checkHasByUser($info['id']);
  74. // 收款二维码
  75. $qrcode = $this->makeQrcode($info['trc_address']);
  76. $info['trc_qrcode'] = $qrcode? get_image_url($qrcode) : '';
  77. $ercQrcode = $this->makeQrcode($info['erc_address']);
  78. $info['erc_qrcode'] = $ercQrcode? get_image_url($ercQrcode) : '';
  79. }
  80. return $info;
  81. }
  82. /**
  83. * 用户注册
  84. * @param $params
  85. * @return bool
  86. */
  87. public function register($params)
  88. {
  89. // 检测账号是否存在
  90. if ($this->checkExists('username', $params['username'])) {
  91. $this->error = '2005';
  92. return false;
  93. }
  94. $username = isset($params['username']) ? trim($params['username']) : '';
  95. $password = isset($params['password']) ? trim($params['password']) : '123456';
  96. $avatar = isset($params['avatar']) ? trim($params['avatar']) : '';
  97. $data = [
  98. 'username' => $username,
  99. 'password' => get_password($password . $username),
  100. 'mobile' => isPhone($username) ? $username : '',
  101. 'avatar' => $avatar ? $avatar : '',
  102. 'status' => 1,
  103. 'mark' => 1,
  104. 'create_time' => time(),
  105. ];
  106. // 生成trc2.0钱包地址
  107. $trcAddress = UsdtWalletService::make()->getTrxAddress();
  108. if ($trcAddress) {
  109. $data['trc_wif'] = isset($trcAddress['wif']) ? $trcAddress['wif'] : '';
  110. $data['trc_hexaddress'] = isset($trcAddress['hexAddress']) ? $trcAddress['hexAddress'] : '';
  111. $data['trc_address'] = isset($trcAddress['address']) ? $trcAddress['address'] : '';
  112. } else {
  113. $this->error = 2201;
  114. return false;
  115. }
  116. // 生erc2.0钱包地址
  117. $ercAddress = UsdtWalletService::make()->getErcAddress();
  118. if ($trcAddress) {
  119. $data['erc_wif'] = isset($ercAddress['wif']) ? $ercAddress['wif'] : '';
  120. $data['erc_hexaddress'] = isset($ercAddress['hexAddress']) ? $ercAddress['hexAddress'] : '';
  121. $data['erc_address'] = isset($ercAddress['address']) ? $ercAddress['address'] : '';
  122. } else {
  123. $this->error = 2202;
  124. return false;
  125. }
  126. if ($id = $this->model->edit($data)) {
  127. MemberSettingService::make()->getInfo($id);
  128. return true;
  129. }
  130. $this->error = 2007;
  131. return false;
  132. }
  133. /**
  134. * 用户登录
  135. * @param $params
  136. * @return array|false
  137. */
  138. public function login($params)
  139. {
  140. $username = isset($params['username']) ? $params['username'] : '';
  141. $password = isset($params['password']) ? $params['password'] : '';
  142. if (empty($username) || empty($password)) {
  143. $this->error = 1013;
  144. return false;
  145. }
  146. // 用户验证
  147. $info = $this->model->getOne([['username', '=', $username]]);
  148. if (!$info) {
  149. $this->error = 2001;
  150. return false;
  151. }
  152. // 密码校验
  153. $password = get_password($password . $username);
  154. if ($password != $info['password']) {
  155. $this->error = 2002;
  156. return false;
  157. }
  158. // 使用状态校验
  159. if ($info['status'] != 1) {
  160. $this->error = 2009;
  161. return false;
  162. }
  163. // 设置日志标题
  164. ActionLogModel::setTitle("会员登录APP");
  165. ActionLogModel::record($info);
  166. // JWT生成token
  167. $jwt = new Jwt('jwt_app');
  168. $token = $jwt->getToken($info['id']);
  169. RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
  170. // 登录数据
  171. return [
  172. 'token' => $token,
  173. 'user_id' => $info['id'],
  174. 'user_type' => $info['user_type'],
  175. ];
  176. }
  177. /**
  178. * 身份认证
  179. * @param $userId
  180. * @param $params
  181. * @return false
  182. */
  183. public function auth($userId, $params)
  184. {
  185. $data = [
  186. 'idcard' => isset($params['idcard']) ? $params['idcard'] : '',
  187. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  188. ];
  189. if (isset($params['idcard_front_img']) && strpos($params['idcard_front_img'], 'http') === false) {
  190. $data['idcard_front_img'] = $params['idcard_front_img'];
  191. }
  192. if (isset($params['idcard_back_img']) && strpos($params['idcard_back_img'], 'http') === false) {
  193. $data['idcard_back_img'] = $params['idcard_back_img'];
  194. }
  195. $info = $this->getInfo($userId);
  196. $idcardCheck = isset($info['idcard_check']) ? $info['idcard_check'] : 0;
  197. if ($idcardCheck == 1) {
  198. $this->error = '2011';
  199. return false;
  200. }
  201. return $this->model->where(['id' => $userId])->update($data);
  202. }
  203. /**
  204. * 获取钱包地址密钥参数
  205. * @param $address
  206. * @param string $type 链类型:trc-trc2.0,erc
  207. * @return mixed
  208. */
  209. public function getWallet($address, $type = 'trc')
  210. {
  211. return $this->model->where([$type . '_address' => $address])->select(['id', $type . '_address', $type . '_hexaddress', $type . '_wif'])->first();
  212. }
  213. /**
  214. * 获取待处理用户数据
  215. * @param int $page
  216. * @param int $pageSize
  217. * @return array|mixed
  218. */
  219. public function getCatchMember($page = 1, $pageSize = 500)
  220. {
  221. $cacheKey = "caches:wallet:members:{$page}_{$pageSize}";
  222. $datas = RedisService::get($cacheKey);
  223. if ($datas) {
  224. return $datas;
  225. }
  226. $datas = $this->model->where(['mark' => 1])
  227. ->select(['id', 'trc_address', 'usdt_num'])
  228. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  229. $datas = $datas ? $datas->toArray() : [];
  230. $datas = isset($datas['data']) ? $datas['data'] : [];
  231. if ($datas) {
  232. RedisService::set($cacheKey, $datas, rand(120, 600));
  233. }
  234. return $datas;
  235. }
  236. /**
  237. * 匹配承兑商
  238. * @param $num 交易数量
  239. * @param int $tradeType 交易类型:1-购买,2-出售
  240. */
  241. public function getTradeMember($num, $tradeType = 1, $userId=0)
  242. {
  243. $data = $this->model->from('member as m')
  244. ->leftJoin('member_setting as ms', 'ms.user_id', '=', 'm.id')
  245. ->where(['m.status' => 1,'m.user_type'=>2, 'm.mark' => 1])
  246. ->where('m.usdt_num', '>=', $num)
  247. ->where(function($query)use($userId){
  248. if($userId){
  249. $query->whereNotIn('m.id', [$userId]);
  250. }
  251. })
  252. ->where(function ($query) use ($tradeType) {
  253. $time = time();
  254. // 买单
  255. if ($tradeType == 1) {
  256. $query->whereRaw("(buy_online = 1 and buy_online_time>{$time}) or buy_online is null");
  257. } // 卖单
  258. else {
  259. $query->whereRaw("((sell_online = 1 and sell_online_time>{$time}) or sell_online is null) and day_sell_quota > day_sell_total");
  260. }
  261. })
  262. ->select(['m.id','m.usdt_num','m.username','m.credit'])
  263. ->orderBy("credit",'desc')
  264. ->orderBy(DB::raw("rand()"))
  265. ->first();
  266. $data = $data? $data->toArray() : [];
  267. return $data;
  268. }
  269. /**
  270. * 生成普通参数二维码
  271. * @param $str 参数
  272. * @param bool $refresh 是否重新生成
  273. * @return bool
  274. */
  275. public function makeQrcode($str, $refresh = false, $size = 4, $margin=2, $level=2)
  276. {
  277. $qrFile = '/images/qrcode/';
  278. if (!is_dir('/uploads'.$qrFile)) {
  279. @mkdir('./uploads' . $qrFile, 0755, true);
  280. }
  281. $qrFile = $qrFile . 'C_' . strtoupper(md5($str . '_' . $size.$margin.$level)) . '.png';
  282. $cacheKey = "caches:qrcodes:member_".md5($str);
  283. if(RedisService::get($cacheKey) && is_file('/uploads'.$qrFile) && !$refresh){
  284. //return $qrFile;
  285. }
  286. QRcode::png($str, './uploads' . $qrFile, $level, $size, $margin);
  287. if(!file_exists('./uploads'.$qrFile)){
  288. return false;
  289. }
  290. RedisService::set($cacheKey, ['str'=> $str, 'qrcode'=> $qrFile,'date'=> date('Y-m-d H:i:s')], 7*24 * 3600);
  291. return $qrFile;
  292. }
  293. /**
  294. * 获取钱包归集用户列表
  295. * @param float $minUsdt
  296. * @param int $page
  297. * @param int $pageSize
  298. * @return array|mixed
  299. */
  300. public function getTriggerAddressList($minUsdt=0.2, $page=1, $pageSize = 200)
  301. {
  302. $cacheKey = "caches:wallet:triggers:{$page}_{$pageSize}";
  303. $datas = RedisService::get($cacheKey);
  304. if ($datas) {
  305. return $datas;
  306. }
  307. $datas = $this->model->where(['mark'=>1])
  308. ->where('trc_usdt','>=', $minUsdt)
  309. ->select(['id','trc_usdt','trc_address','trc_wif','trc_hexaddress','erc_address','erc_wif','erc_hexaddress'])
  310. ->orderBy('trc_usdt')
  311. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  312. $datas = $datas ? $datas->toArray() : [];
  313. $datas = isset($datas['data']) ? $datas['data'] : [];
  314. if ($datas) {
  315. RedisService::set($cacheKey, $datas, rand(10, 30));
  316. }
  317. return $datas;
  318. }
  319. }