MemberService.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\LiveModel;
  16. use App\Models\MemberBankModel;
  17. use App\Models\MemberLevelModel;
  18. use App\Models\MemberModel;
  19. use App\Models\MerchantModel;
  20. use App\Models\VideoModel;
  21. use App\Services\BaseService;
  22. use App\Services\CityService;
  23. use App\Services\ConfigService;
  24. use App\Services\EmailService;
  25. use App\Services\RedisService;
  26. use App\Services\SmsService;
  27. use App\Services\ToolService;
  28. use App\Services\UdunpayService;
  29. use App\Services\WalletService;
  30. use Illuminate\Support\Facades\DB;
  31. use phpQrcode\QRcode;
  32. /**
  33. * 会员管理-服务类
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * Class MemberService
  37. * @package App\Services\Api
  38. */
  39. class MemberService extends BaseService
  40. {
  41. // 静态对象
  42. protected static $instance = null;
  43. /**
  44. * 构造函数
  45. * @author laravel开发员
  46. * @since 2020/11/11
  47. * MemberService constructor.
  48. */
  49. public function __construct()
  50. {
  51. $this->model = new MemberModel();
  52. }
  53. /**
  54. * 静态入口
  55. * @return MemberService|static|null
  56. */
  57. public static function make()
  58. {
  59. if (!self::$instance) {
  60. self::$instance = new static();
  61. }
  62. return self::$instance;
  63. }
  64. /**
  65. * 获取资料详情
  66. * @param $where
  67. * @param array $field
  68. */
  69. public function getInfo($where, $type = 'detail', array $field = [], $userId=0)
  70. {
  71. $defaultField = ['id', 'username','email', 'realname', 'mobile','gender','member_level','box_num', 'nickname', 'code','supper_point', 'parent_id','birthday','city_id','signature','power_num','upgrade_profit_total','trade_state','trc_url','recharge_trc_url', 'point_id', 'balance','trx','usdt','wait_score','intro', 'score','pay_password','realname','idcard_check', 'member_level', 'status', 'avatar'];
  72. $withs = ['merchant','acceptor','parent','point','level'];
  73. if ($type == 'team') {
  74. $defaultField = ['id', 'username','email','member_level', 'nickname','usdt', 'code', 'balance', 'score','point_id','power_num','parent_id', 'status', 'avatar'];
  75. }else if($type == 'share'){
  76. $withs = ['parent','level'];
  77. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url', 'score', 'status', 'avatar'];
  78. }else if($type == 'check'){
  79. $withs = ['parent','level'];
  80. $defaultField = ['id', 'username', 'nickname','member_level', 'code', 'balance','usdt','trx','trc_url','wait_score', 'score', 'status', 'avatar'];
  81. }else if ($type == 'info'){
  82. $withs = ['parent','level'];
  83. }
  84. $field = $field ? $field : $defaultField;
  85. if (is_array($where)) {
  86. $info = $this->model->with($withs)->where('mark',1)->where($where)->select($field)->first();
  87. } else {
  88. $info = $this->model->with($withs)->where('mark',1)->where(['id' => (int)$where])->select($field)->first();
  89. }
  90. $info = $info ? $info->toArray() : [];
  91. if ($info && !in_array($type, ['auth', 'check','login'])) {
  92. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  93. $info['balance'] = moneyFormat($info['balance'], 2);
  94. $info['usdt'] = isset($info['usdt'])?moneyFormat($info['usdt'], 2):'0.00';
  95. $info['score'] = moneyFormat($info['score'], 2);
  96. $info['merchant'] = isset($info['merchant']) && $info['merchant']? $info['merchant'] : ['id'=>0];
  97. $info['acceptor'] = isset($info['acceptor']) && $info['acceptor']? $info['acceptor'] : ['id'=>0];
  98. $info['level'] = isset($info['level']) && $info['level']? $info['level'] : ['id'=>0,'name'=>'普通用户'];
  99. $info['custom_phone'] = ConfigService::make()->getConfigByCode('custom_phone');
  100. $info['custom_telegram'] = ConfigService::make()->getConfigByCode('custom_telegram');
  101. $info['custom_email'] = ConfigService::make()->getConfigByCode('custom_email');
  102. if(isset($info['birthday'])){
  103. $info['birthday_text'] = $info['birthday']? date('Y-m-d',$info['birthday']) : '';
  104. }
  105. // 二维码
  106. if (in_array($type, ['detail'])) {
  107. $info['parent_code'] = '';
  108. $info['parent'] = $info['parent']? $info['parent'] : ['id'=>0,'nickname' => '无', 'code' => '无'];
  109. if($info['parent']['id']>0){
  110. $info['parent_code'] = isset($info['parent']['code'])? $info['parent']['code'] :'';
  111. }
  112. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  113. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  114. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  115. if($userId != $info['id']){
  116. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  117. }else{
  118. $info['is_follow'] = 1;
  119. }
  120. $info['app_version'] = ConfigService::make()->getConfigByCode('app_version');
  121. }
  122. else if ($type == 'info') {
  123. $liveOpenLevel = ConfigService::make()->getConfigByCode('live_open_level',0);
  124. $liveOpenLevel = $liveOpenLevel>0? $liveOpenLevel : 0;
  125. $info['live_open'] = $info['member_level']>=$liveOpenLevel? 1 : 0;
  126. }else if($type == 'upgrade'){
  127. $level = isset($info['level'])? $info['level'] : [];
  128. $nextLevel = MemberLevelModel::where('id','>', $info['member_level'])->orderBy('id','asc')->first();
  129. $info['next_level'] = isset($nextLevel['id']) && $nextLevel['id']? $nextLevel['id'] : $info['member_level'];
  130. $info['next_level_profit'] = isset($nextLevel['upgrade_usdt_profit']) && $nextLevel['upgrade_usdt_profit']? $nextLevel['upgrade_usdt_profit'] : (isset($level['upgrade_usdt_profit'])? $level['upgrade_usdt_profit']:0);
  131. $info['next_level_usdt'] = isset($nextLevel['upgrade_usdt']) && $nextLevel['upgrade_usdt']? $nextLevel['upgrade_usdt'] : (isset($level['upgrade_usdt'])? $level['upgrade_usdt']:0);
  132. $info['next_level_name'] = isset($nextLevel['name'])? $nextLevel['name'] : (isset($level['name'])? $level['name']:'');
  133. }
  134. else if ($type == 'recharge') {
  135. $wallet = WalletService::make()->getWallet(1);
  136. $info['recharge_trc_url'] = isset($wallet['address'])? $wallet['address'] : '';
  137. $info['xd_price'] = ConfigService::make()->getConfigByCode('xd_price',100);
  138. if($info['recharge_trc_url']){
  139. $qrcode = $this->makeQrcode($info['recharge_trc_url']);
  140. $info['trc_qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  141. }else{
  142. $info['trc_qrcode'] = '';
  143. }
  144. }
  145. else if ($type == 'team' || $type == 'share') {
  146. $inviteUrl = get_web_url('/#/pages/register/index?code=' . $info['code']);
  147. $qrcode = $this->makeQrcode($inviteUrl);
  148. $info['qrcode'] = $qrcode ? get_image_url($qrcode) : '';
  149. $info['invite_url'] = $inviteUrl;
  150. $info['qrcode_base64'] = $qrcode?getImageContent($qrcode):'';
  151. $info['team_count1'] = $this->getUserCountByType($info['id'], 1, 0); // 直推人数
  152. $info['team_count2'] = $this->getUserCountByType($info['id'], 3, 0); // 滑落人数
  153. $info['team_count'] = $this->getUserCountByType($info['id'], 2, 0); // 团队人数
  154. }else {
  155. // 默认地址
  156. $info['addressData'] = MemberAddressService::make()->getBindInfo($info['id']);
  157. }
  158. }
  159. return $info;
  160. }
  161. /**
  162. * 获取主页信息
  163. * @param $where
  164. * @param array $field
  165. */
  166. public function getHomeInfo($id, $userId=0)
  167. {
  168. $field = ['id', 'username','email', 'realname', 'mobile','gender', 'nickname', 'code','supper_point', 'parent_id','province_id','city_id','signature','power_num','trade_state', 'point_id', 'balance','trx','usdt','wait_score', 'score','realname', 'member_level', 'status', 'avatar'];
  169. $info = $this->model->where('mark',1)->where(['id' => $id])->select($field)->first();
  170. $info = $info ? $info->toArray() : [];
  171. if ($info) {
  172. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : get_image_url('/images/member/logo.png');
  173. $info['fans'] = (int)MemberCollectService::make()->getCount($info['id'], 1,1); // 粉丝数量(被关注)
  174. $info['follow'] = (int)MemberCollectService::make()->getCount($info['id'], 1,2); // 关注数量
  175. $info['like_num'] = (int)VideoModel::where(['user_id'=>$info['id'],'mark'=>1])->sum('like_num'); // 点赞喜欢数量
  176. $info['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $info['id'],1); // 是否关注了
  177. $info['is_fans'] = (int)MemberCollectService::make()->checkCollect($info['id'], $userId,1); // 是否我的粉丝
  178. $info['live_id'] = (int)LiveModel::where(['user_id'=> $info['id'],'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id'); // 是否正在直播
  179. }
  180. return $info;
  181. }
  182. /**
  183. * 获取用户缓存信息
  184. * @param $where
  185. * @param array $field
  186. * @param int $expired
  187. * @return array|mixed
  188. */
  189. public function getCacheInfo($where, $field = [], $expired = 0)
  190. {
  191. $cacheKey = "caches:member:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
  192. $info = RedisService::get($cacheKey);
  193. if ($info) {
  194. return $info;
  195. }
  196. $defaultField = ['id', 'username', 'realname', 'mobile', 'nickname', 'code', 'parent_id', 'balance','usdt', 'score', 'member_level', 'status', 'avatar'];
  197. $field = $field ? $field : $defaultField;
  198. $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
  199. $info = $info ? $info->toArray() : [];
  200. if ($info) {
  201. if (isset($info['avatar'])) {
  202. $info['avatar'] = $info['avatar']? $info['avatar'] : '/images/member/logo.png';
  203. $info['avatar_preview'] = $info['avatar']? get_image_url($info['avatar']) : '';
  204. }
  205. RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
  206. }
  207. return $info;
  208. }
  209. /**
  210. * 生成普通参数二维码
  211. * @param $str 参数
  212. * @param bool $refresh 是否重新生成
  213. * @return bool
  214. */
  215. public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2)
  216. {
  217. $basePath = base_path() . '/public';
  218. $qrFile = '/images/qrcode/';
  219. if (!is_dir($basePath . '/uploads' . $qrFile)) {
  220. @mkdir($basePath . '/uploads' . $qrFile, 0755, true);
  221. }
  222. $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level));
  223. $qrFile = $qrFile . "C_{$key}.png";
  224. $cacheKey = "caches:qrcodes:member_" . $key;
  225. if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) {
  226. return $qrFile;
  227. }
  228. QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin);
  229. if (!file_exists($basePath . '/uploads' . $qrFile)) {
  230. return false;
  231. }
  232. RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600);
  233. return $qrFile;
  234. }
  235. /**
  236. * 获取团队用户数量
  237. * @param $userId 当前用户ID
  238. * @param int $type 层级:1-直推,2-团队
  239. * @param int $isPoint 节点:1-滑落推荐,0-直接推荐
  240. * @return array|mixed
  241. */
  242. public function getUserCountByType($userId, $type = 1, $isPoint=0)
  243. {
  244. $cacheKey = "caches:teams:count_{$userId}_{$type}_{$isPoint}";
  245. $data = RedisService::get($cacheKey);
  246. if ($data) {
  247. return $data;
  248. }
  249. $where = ['status' => 1, 'mark' => 1];
  250. $data = $this->model->where($where)
  251. ->where(function ($query) use ($type, $isPoint, $userId) {
  252. if($isPoint){
  253. if ($type == 1) { // 滑落推荐
  254. $query->where('point_id', $userId);
  255. } else if($type == 2){ // 滑落团队人数
  256. $query->whereRaw('FIND_IN_SET(?,points)', $userId);
  257. }
  258. }else{
  259. if ($type == 1) { // 直推人数
  260. $query->where('parent_id', $userId);
  261. } else if($type == 2){ // 团队人数
  262. $query->whereRaw("FIND_IN_SET({$userId},parents)");
  263. } else if($type == 3){ // 滑落人数
  264. $query->where('parent_id', $userId)->where('point_id','!=', $userId);
  265. }
  266. }
  267. })->count('id');
  268. if ($data) {
  269. RedisService::set($cacheKey, $data, rand(3, 5));
  270. }
  271. return $data;
  272. }
  273. /**
  274. * 分销用户列表
  275. * @param $userId
  276. * @param $params
  277. * @param int $pageSize
  278. * @return array
  279. */
  280. public function getTeamList($userId, $params, $pageSize=15)
  281. {
  282. $page = request()->post('page',1);
  283. $cacheKey = "caches:teams:list_{$userId}_{$page}";
  284. $datas = RedisService::get($cacheKey);
  285. if ($datas) {
  286. return [
  287. 'pageSize' => $pageSize,
  288. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  289. 'list' => isset($datas['data']) ? $datas['data'] : []
  290. ];
  291. }
  292. $where = ['status' => 1, 'mark' => 1];
  293. $field = ['id','avatar', 'nickname', 'username', 'email', 'balance', 'parent_id','power_num','point_id','create_time', 'status'];
  294. $datas = $this->model->with(['level'])->where($where)
  295. ->where(function ($query) use ($userId) {
  296. $query->where('parent_id', $userId);
  297. })
  298. ->where(function($query) use($params){
  299. $kw = isset($params['kw'])? trim($params['kw']) : '';
  300. if($kw){
  301. $query->where('id','=', "%{$kw}%")->orWhere('email','like',"%{$kw}%")->orWhere('username','like',"%{$kw}%");
  302. }
  303. })
  304. ->select($field)
  305. ->orderBy('create_time', 'desc')
  306. ->orderBy('id', 'desc')
  307. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  308. $datas = $datas ? $datas->toArray() : [];
  309. if ($datas) {
  310. foreach ($datas['data'] as &$item) {
  311. $item['avatar'] = $item['avatar']? $item['avatar'] : '/images/member/logo.png';
  312. $item['avatar'] = get_image_url($item['avatar']);
  313. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'y-m-d H:i') : '';
  314. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  315. }
  316. unset($item);
  317. RedisService::set($cacheKey, $datas, rand(5, 10));
  318. }
  319. return [
  320. 'pageSize' => $pageSize,
  321. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  322. 'list' => isset($datas['data']) ? $datas['data'] : []
  323. ];
  324. }
  325. /**
  326. * 手机号是否已注册
  327. * @param $mobile
  328. * @return mixed
  329. */
  330. public function checkPhoneExists($mobile)
  331. {
  332. return parent::checkExists('mobile', $mobile); // TODO: Change the autogenerated stub
  333. }
  334. /**
  335. * 账号注册
  336. * @param $params
  337. * @return array|false
  338. */
  339. public function register($params)
  340. {
  341. // 授权
  342. $email = isset($params['email']) ? trim($params['email']) : '';
  343. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  344. $nickname = isset($params['nickname']) ? trim($params['nickname']) : '';
  345. $code = isset($params['code']) ? trim($params['code']) : '';
  346. $scene = isset($params['scene']) ? $params['scene'] : '';
  347. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  348. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  349. if (empty($params) || empty($email) || $code <= 0) {
  350. $this->error = 2014;
  351. return false;
  352. }
  353. // 验证码
  354. if (!EmailService::make()->check($email, $code, $scene)) {
  355. $this->error = EmailService::make()->getError();
  356. return false;
  357. }
  358. // 验证是否注册
  359. if($this->model->where(['email'=> $email,'mark'=>1])->value('id')){
  360. $this->error = 2009;
  361. return false;
  362. }
  363. // 推荐人
  364. $parentId = 0;
  365. $pointId = 0;
  366. $parents = '';
  367. $points = '';
  368. if ($inviteCode) {
  369. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  370. ->select(['id', 'parents', 'parent_id'])
  371. ->first();
  372. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  373. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  374. // 挂靠节点
  375. if($parentId>0){
  376. $pointId = $this->getPointParentId($parentId);
  377. if($pointId){
  378. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  379. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  380. }
  381. }
  382. }
  383. // 注册
  384. $id = $this->model->max('id') + 1;
  385. $system = isset($params['system'])? $params['system'] : [];
  386. $ip = get_client_ip();
  387. $ipData = ToolService::make()->getIpAddress($ip,'');
  388. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  389. $city = isset($ipData['city'])? $ipData['city'] : '';
  390. $emailName = explode('@', $email)[0];
  391. // 钱包
  392. $data = [
  393. 'parent_id' => $parentId,
  394. 'point_id' => $pointId,
  395. 'points' => $points,
  396. 'openid' => '',
  397. 'avatar' => '',
  398. 'nickname' => $nickname ? $nickname : '用户'.substr($emailName,-8,8),
  399. 'username' => $email,
  400. 'password' => get_password('xl123456'),
  401. 'code' => strtoupper(get_random_code(9, 'X', "{$id}")),
  402. 'email' => $email,
  403. 'mobile' => $mobile,
  404. 'recharge_trc_url' => '',
  405. 'parents' => $parents,
  406. 'province_id' => CityService::make()->getFieldByName($province),
  407. 'city_id' => CityService::make()->getFieldByName($city),
  408. 'app_uuid' => isset($system['uuid']) ? $system['uuid'] : '',
  409. 'device' => $appSources == 'android' ? 2 : 1,
  410. 'status' => 1,
  411. 'balance' => 0,
  412. 'score' => 0,
  413. 'mark' => 1,
  414. 'login_ip' => $ip,
  415. 'create_time' => time(),
  416. ];
  417. $userId = $this->model->insertGetId($data);
  418. $this->error = 2008;
  419. return [
  420. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  421. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'point_id'=> $pointId, 'username' => $data['username']],
  422. ];
  423. }
  424. /**
  425. * 邮箱登录
  426. * @param $params
  427. * @return array|false
  428. */
  429. public function loginByEmail($params)
  430. {
  431. // 授权
  432. $email = isset($params['email']) ? $params['email'] : '';
  433. $code = isset($params['code']) ? $params['code'] : '';
  434. $scene = isset($params['scene']) ? $params['scene'] : '';
  435. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  436. if (empty($params) || empty($email) || empty($scene) || $code <= 0) {
  437. $this->error = 2014;
  438. return false;
  439. }
  440. // 验证码
  441. if (!EmailService::make()->check($email, $code, $scene)) {
  442. $this->error = EmailService::make()->getError();
  443. return false;
  444. }
  445. // 验证是否注册,没有则注册
  446. $data = $this->model->where(['email' => $email, 'mark' => 1])->select(['id', 'username','point_id', 'parent_id', 'code', 'status'])->first();
  447. $data = $data ? $data->toArray() : [];
  448. $userId = isset($data['id']) ? $data['id'] : 0;
  449. $status = isset($data['status']) ? $data['status'] : 0;
  450. if(empty($data) || $userId<=0){
  451. $this->error = 2001;
  452. return false;
  453. }
  454. if ($status != 1) {
  455. $this->error = 2015;
  456. return false;
  457. }
  458. // 获取登录授权token
  459. $jwt = new Jwt('jwt_dys_app');
  460. $token = $jwt->getToken($userId);
  461. // 结果返回
  462. $result = [
  463. 'access_token' => $token,
  464. 'info' => ['uid' => $userId,'parent_id'=> $data['parent_id'], 'username' => $data['username']],
  465. ];
  466. // 用户信息
  467. $ip = get_client_ip();
  468. $updateData = [
  469. 'login_time' => time(),
  470. 'login_ip' => $ip,
  471. ];
  472. if(!RedisService::get("caches:member:updateArea:{$userId}")){
  473. $ipData = ToolService::make()->getIpAddress($ip,'');
  474. $province = isset($ipData['regionName'])? $ipData['regionName'] : '';
  475. $city = isset($ipData['city'])? $ipData['city'] : '';
  476. $updateData['province_id'] = CityService::make()->getFieldByName($province);
  477. $updateData['city_id'] = CityService::make()->getFieldByName($city);
  478. }
  479. $this->model->where(['id'=> $userId])->update($updateData);
  480. $this->error = 2004;
  481. $data['token'] = $token;
  482. RedisService::set("caches:member:updateArea:{$userId}", $updateData, 3600);
  483. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  484. return $result;
  485. }
  486. /**
  487. * 手机号登录
  488. * @param $params
  489. * @return array|false
  490. */
  491. public function loginByMobile($params)
  492. {
  493. // 授权
  494. $mobile = isset($params['mobile']) ? $params['mobile'] : '';
  495. $code = isset($params['sms_code']) ? $params['sms_code'] : '';
  496. $smsType = isset($params['sms_type']) ? $params['sms_type'] : '';
  497. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  498. $openid = isset($params['openid']) ? $params['openid'] : '';
  499. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  500. $regType = isset($params['type']) ? $params['type'] : 'app';
  501. $appSources = isset($params['app_sources']) ? $params['app_sources'] : '';
  502. $inviteCode = isset($params['invite_code']) ? $params['invite_code'] : '';
  503. if (empty($params) || empty($mobile) || $code <= 0) {
  504. $this->error = 2014;
  505. return false;
  506. }
  507. // 验证码
  508. if (!SmsService::make()->check($mobile, $code, $smsType?$smsType:'login')) {
  509. $this->error = SmsService::make()->getError();
  510. return false;
  511. }
  512. // 推荐人
  513. $parentId = 0;
  514. $parents = '';
  515. if ($inviteCode) {
  516. $inviteInfo = $this->model->where(['code' => $inviteCode, 'mark' => 1])
  517. ->select(['id', 'parents', 'parent_id'])
  518. ->first();
  519. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  520. $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] . $parentId . ',' : ($parentId ? $parentId . ',' : '');
  521. }
  522. // 验证是否注册,没有则注册
  523. $data = $this->model->with(['merchant','mechanic'])->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'openid', 'username', 'parent_id', 'code', 'push_cid', 'status'])->first();
  524. $data = $data ? $data->toArray() : [];
  525. $userId = isset($data['id']) ? $data['id'] : 0;
  526. $status = isset($data['status']) ? $data['status'] : 0;
  527. if ($userId && $status != 1) {
  528. $this->error = 2015;
  529. return false;
  530. }
  531. // 是否是商家或技师,有订单提醒
  532. $noticeUser = ['id'=> 0,'uid'=>$userId,'type'=>1];
  533. $merchant = isset($data['merchant'])? $data['merchant'] : [];
  534. $merchantStatus = isset($merchant['status'])? $merchant['status'] : 0;
  535. $mechanic = isset($data['mechanic'])? $data['mechanic'] : [];
  536. $mechanicStatus = isset($mechanic['status'])? $mechanic['status'] : 0;
  537. if($merchantStatus==2){
  538. $uid = isset($merchant['id'])? $merchant['id'] : 0;
  539. if($uid>0){
  540. $noticeUser = ['id'=>$uid,'type'=>2,'uid'=> $userId];
  541. }
  542. }else if($mechanicStatus == 2){
  543. $uid = isset($mechanic['id'])? $mechanic['id'] : 0;
  544. if($uid>0){
  545. $noticeUser = ['id'=>$uid,'type'=>3,'uid'=>$userId];
  546. }
  547. }
  548. $pushCid = isset($params['push_cid']) ? $params['push_cid'] : '';
  549. if (!$userId) {
  550. $id = $this->model->max('id') + 1;
  551. $data = [
  552. 'parent_id' => $parentId,
  553. 'openid' => $openid,
  554. 'avatar' => $avatar,
  555. 'nickname' => $nickname ? $nickname : get_random_code(4, '用户u').substr($mobile,-4,4),
  556. 'username' => get_username($id),
  557. 'password' => get_password('123456'),
  558. 'code' => strtoupper(get_random_code(8, 'D', "{$id}")),
  559. 'mobile' => $mobile,
  560. 'parents' => $parents,
  561. 'push_cid' => isset($params['push_cid']) ? $params['push_cid'] : '',
  562. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  563. 'device' => $appSources == 'android' ? 2 : 1,
  564. 'status' => 1,
  565. 'balance' => 0,
  566. 'score' => 0,
  567. 'mark' => 1,
  568. 'login_time' => time(),
  569. 'login_ip' => get_client_ip(),
  570. 'create_time' => time(),
  571. ];
  572. $userId = $this->model->insertGetId($data);
  573. } elseif ($regType != 'h5') {
  574. // 登录更新
  575. $updateData = [
  576. 'app_uuid' => isset($params['uuid']) ? $params['uuid'] : '',
  577. 'login_count' => DB::raw("login_count+1"),
  578. 'login_time' => time(),
  579. 'login_ip' => get_client_ip(),
  580. 'update_time' => time(),
  581. ];
  582. if ($pushCid) {
  583. $updateData['push_cid'] = $pushCid;
  584. }
  585. $this->model->where(['id' => $userId])->update($updateData);
  586. }
  587. // H5 邀请注册,无需登录
  588. if ($regType == 'h5') {
  589. $this->error = $status ? 2023 : 2022;
  590. return [
  591. 'app_url' => ConfigService::make()->getConfigByCode("app_{$appSources}_url"),
  592. 'info' => ['uid' => $userId, 'invite_id' => $parentId, 'username' => $data['username']],
  593. ];
  594. }
  595. // 获取登录授权token
  596. $jwt = new Jwt('jwt_dys_app');
  597. $token = $jwt->getToken($userId);
  598. // 结果返回
  599. $result = [
  600. 'access_token' => $token,
  601. 'info' => ['uid' => $userId,'notice_user'=>$noticeUser, 'openid' => $data['openid'], 'username' => $data['username']],
  602. ];
  603. // 用户信息
  604. $this->error = 2004;
  605. $data['token'] = $token;
  606. unset($data['password']);
  607. // 实现初始化账单明细表
  608. AccountLogModel::where('id','<=',1)->value('id');
  609. RedisService::clear( "caches:map:{$userId}");
  610. RedisService::set("auths:info:{$userId}", $data, 3 * 24 * 3600);
  611. return $result;
  612. }
  613. /**
  614. * 修改头像/商家logo/技师头像
  615. * @param $userId
  616. * @param $avatar
  617. * @return mixed
  618. */
  619. public function saveAvatar($userId, $avatar)
  620. {
  621. $type = request()->post('type', 1);
  622. if($type == 1){
  623. $oldAvatar = $this->model->where(['id' => $userId])->value('avatar');
  624. if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) {
  625. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  626. @unlink(ATTACHMENT_PATH . $oldAvatar);
  627. }
  628. return true;
  629. }
  630. }else if($type == 2){
  631. $oldAvatar = MerchantModel::where(['user_id' => $userId])->value('logo');
  632. if (MerchantModel::where(['user_id' => $userId])->update(['logo' => $avatar, 'update_time' => time()])) {
  633. if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) {
  634. @unlink(ATTACHMENT_PATH . $oldAvatar);
  635. }
  636. return true;
  637. }
  638. }
  639. return false;
  640. }
  641. /**
  642. * 修改账号
  643. * @param $userId
  644. * @param $params
  645. * @return bool
  646. */
  647. public function modify($userId, $params)
  648. {
  649. $email = isset($params['email']) ? $params['email'] : '';
  650. $password = isset($params['password']) ? $params['password'] : '';
  651. $payPassword = isset($params['pay_password']) ? $params['pay_password'] : '';
  652. $scene = isset($params['scene'])? trim($params['scene']) : '';
  653. // 用户验证
  654. $info = $this->model->with(['parent'])->where(['id' => $userId, 'mark' => 1])->select(['id','pay_password','parent_id','code', 'status'])->first();
  655. if (!$info) {
  656. $this->error = 2001;
  657. return false;
  658. }
  659. // 使用状态校验
  660. if ($info['status'] != 1) {
  661. $this->error = 2009;
  662. return false;
  663. }
  664. // 密码校验
  665. $data = ['update_time' => time()];
  666. // 验证交易密码
  667. if(isset($params['pay_password']) && $scene != 'trade'){
  668. $userPayPassword = isset($info['pay_password'])? $info['pay_password'] : '';
  669. if(empty($userPayPassword)){
  670. $this->error = 1040;
  671. return false;
  672. }
  673. if($userPayPassword != get_password($payPassword)){
  674. $this->error = 2038;
  675. return false;
  676. }
  677. }
  678. // 邀请人绑定
  679. $parent = isset($info['parent'])? $info['parent'] : [];
  680. $userParentCode = isset($parent['code'])? $parent['code'] : '';
  681. $userCode = isset($info['code'])? $info['code'] : '';
  682. $parentCode = isset($params['parent_code'])? trim($params['parent_code']) : '';
  683. if(isset($params['parent_code']) && $parentCode){
  684. //
  685. if($userParentCode){
  686. $this->error = 2451;
  687. return false;
  688. }
  689. if(($userCode == $parentCode)){
  690. $this->error = 2452;
  691. return false;
  692. }
  693. // 推荐人验证
  694. $inviteInfo = $this->model->where(['code' => $parentCode, 'mark' => 1])
  695. ->select(['id', 'parents', 'parent_id'])
  696. ->first();
  697. $parentId = isset($inviteInfo['id']) ? $inviteInfo['id'] : 0;
  698. $parents = isset($inviteInfo['parents']) ? $parentId.','.$inviteInfo['parents'] : ($parentId ? $parentId . ',' : '');
  699. if(!$parentId){
  700. $this->error = 2453;
  701. return false;
  702. }
  703. // 验证是否绑定用户为自己线下用户
  704. if(preg_match("/{$userId},/", $parents)){
  705. $this->error = 2454;
  706. return false;
  707. }
  708. // 推荐人
  709. $data['parent_id'] = $parentId;
  710. $data['parents'] = $parents;
  711. // 挂靠节点
  712. if($parentId>0){
  713. $pointId = $this->getPointParentId($parentId);
  714. if($pointId){
  715. $pointParent = $this->model->where(['id'=> $pointId,'mark'=>1])->select(['id','points'])->first();
  716. $points = isset($pointParent['points']) ? $pointId.','.$pointParent['points'] : ($pointId ? $pointId . ',' : '');
  717. $data['point_id'] = $pointId;
  718. $data['points'] = $points;
  719. }
  720. }
  721. }
  722. // 邮箱验证
  723. if (isset($params['email']) && $email) {
  724. // 修改邮箱
  725. $newEmail = isset($params['new_email'])? trim($params['new_email']) : '';
  726. if(isset($params['new_email']) && $newEmail){
  727. $data['email'] = $newEmail;
  728. $data['username'] = $newEmail;
  729. $checkInfo = $this->model->where(['username' => $newEmail, 'mark' => 1])->select(['id', 'status'])->first();
  730. if ($checkInfo && $checkInfo['id'] != $userId) {
  731. $this->error = 2005;
  732. return false;
  733. }
  734. }
  735. // 邮箱验证码
  736. $code = isset($params['code'])? trim($params['code']) : '';
  737. $checkEmail = $scene == 'modify_email'? $newEmail : $email;
  738. if (!EmailService::make()->check($checkEmail, $code, $scene)) {
  739. $this->error = EmailService::make()->getError();
  740. return false;
  741. }
  742. }
  743. // 修改数据
  744. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  745. if (isset($params['nickname']) && $nickname) {
  746. $data['nickname'] = $nickname;
  747. }
  748. $gender = isset($params['gender']) ? intval($params['gender']) : 3;
  749. if (isset($params['gender']) && $gender) {
  750. $data['gender'] = $gender;
  751. }
  752. $intro = isset($params['intro']) ? $params['intro'] : '';
  753. if (isset($params['intro']) && $intro) {
  754. $data['intro'] = $intro;
  755. }
  756. $birthday = isset($params['birthday_text']) ? $params['birthday_text'] : '';
  757. if (isset($params['birthday_text']) && $birthday) {
  758. $data['birthday'] = strtotime($birthday);
  759. }
  760. if (isset($params['pay_password']) && $payPassword) {
  761. $payPassword = get_password($payPassword);
  762. $data['pay_password'] = $payPassword;
  763. }
  764. if($this->model->where(['id' => $userId])->update($data)){
  765. // 更新绑定上级后,团队用户全部更新
  766. if(isset($data['parent_id']) && $data['parent_id']){
  767. $updateData = ['parents'=>DB::raw("concat(parents,'{$data['parent_id']},')"),'update_time'=>time()];
  768. $this->model->where('parents','like',"%{$userId},")->update($updateData);
  769. }
  770. // 更新团队节点关系
  771. if(isset($data['point_id']) && $data['point_id']){
  772. $updateData = ['points'=>DB::raw("concat(points,'{$data['point_id']},')"),'update_time'=>time()];
  773. $this->model->where('points','like',"%{$userId},")->update($updateData);
  774. }
  775. $this->error = 1008;
  776. return true;
  777. }
  778. $this->error = 1008;
  779. return true;
  780. }
  781. /**
  782. * 身份认证
  783. * @param $userId
  784. * @param $params
  785. * @return bool
  786. */
  787. public function auth($userId, $params)
  788. {
  789. // 用户验证
  790. $info = $this->model->where(['id' => $userId, 'mark' => 1])->select(['id','username', 'status'])->first();
  791. if (!$info) {
  792. $this->error = 2001;
  793. return false;
  794. }
  795. // 使用状态校验
  796. if ($info['status'] != 1) {
  797. $this->error = 2009;
  798. return false;
  799. }
  800. // 密码校验
  801. $data = ['update_time' => time(),'idcard_check'=>1];
  802. if (isset($params['realname']) && $params['realname']) {
  803. $data['realname'] = trim($params['realname']);
  804. }
  805. $idcard = isset($params['idcard']) && $params['idcard'];
  806. if ($idcard) {
  807. $data['idcard'] = trim($params['idcard']);
  808. }
  809. if (isset($params['idcard_front_img'][0]) && $params['idcard_front_img'][0]) {
  810. $data['idcard_front_img'] = get_image_path($params['idcard_front_img'][0]['url']);
  811. }
  812. if (isset($params['idcard_back_img'][0]) && $params['idcard_back_img'][0]) {
  813. $data['idcard_back_img'] = get_image_path($params['idcard_back_img'][0]['url']);
  814. }
  815. $this->model->where(['id' => $userId])->update($data);
  816. // 发送消息
  817. $this->error = 2036;
  818. return true;
  819. }
  820. /**
  821. * 转账
  822. * @param $userId
  823. * @param $params
  824. * @return bool
  825. */
  826. public function transfer($userId, $params)
  827. {
  828. $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
  829. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  830. $money = isset($params['money'])? floatval($params['money']) : 0;
  831. if(empty($toUserId) || $money<=0){
  832. $this->error = 2420;
  833. return false;
  834. }
  835. $cacheKey = "caches:members:transfer:{$userId}";
  836. if(RedisService::get($cacheKey)){
  837. $this->error = 1034;
  838. return false;
  839. }
  840. // 验证账户
  841. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  842. ->select(['id','nickname','username','usdt','pay_password','balance','status'])
  843. ->first();
  844. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  845. $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  846. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  847. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  848. if(empty($userInfo) || $status != 1){
  849. $this->error = 2024;
  850. return false;
  851. }
  852. // 余额支付支付密码验证
  853. if(empty($userPayPassword)){
  854. $this->error = 1040;
  855. return false;
  856. }
  857. $payPassword = get_password($payPassword);
  858. if($payPassword != $userPayPassword){
  859. $this->error = 2038;
  860. return false;
  861. }
  862. // 验证账户
  863. $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
  864. ->select(['id','nickname','usdt','username','balance','status'])
  865. ->first();
  866. $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
  867. $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
  868. $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
  869. if(empty($accountInfo) || $status != 1){
  870. $this->error = 2421;
  871. return false;
  872. }
  873. if($money > $usdt){
  874. $this->error = 2422;
  875. return false;
  876. }
  877. // 用户余额转账到用户
  878. DB::beginTransaction();
  879. RedisService::set($cacheKey, $params, rand(2,5));
  880. $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
  881. if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
  882. DB::rollBack();
  883. $this->error = 2028;
  884. RedisService::clear($cacheKey);
  885. return false;
  886. }
  887. // 明细
  888. $orderNo = get_order_num('TR');
  889. $log = [
  890. 'user_id' => $userId,
  891. 'source_id' => $toUserId,
  892. 'source_order_no' => $orderNo,
  893. 'type' => 6,
  894. 'coin_type' => 1,
  895. 'user_type'=> 1,
  896. 'money' => -$money,
  897. 'actual_money' => -$money,
  898. 'balance' => $usdt,
  899. 'create_time' => time(),
  900. 'update_time' => time(),
  901. 'remark' => "USDT转账",
  902. 'status' => 1,
  903. 'mark' => 1,
  904. ];
  905. if(!AccountLogModel::insertGetId($log)){
  906. DB::rollBack();
  907. $this->error = 2423;
  908. RedisService::clear($cacheKey);
  909. return false;
  910. }
  911. // 到账
  912. $fee = ConfigService::make()->getConfigByCode('transfer_fee_rate',5);
  913. $fee = $fee>0 && $fee<100? moneyFormat($fee/100, 2) : 0;
  914. $feeUsdt = round($money * $fee,2);
  915. $realUsdt = moneyFormat($money - $feeUsdt,2);
  916. $updateData = ['usdt'=> DB::raw("usdt + {$realUsdt}"),'update_time'=> time()];
  917. if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
  918. DB::rollBack();
  919. $this->error = 2424;
  920. RedisService::clear($cacheKey);
  921. return false;
  922. }
  923. // 明细
  924. $log = [
  925. 'user_id' => $toUserId,
  926. 'source_id' => $userId,
  927. 'source_order_no' => $orderNo,
  928. 'type' => 6,
  929. 'coin_type' => 1,
  930. 'user_type'=> 1,
  931. 'money' => $realUsdt,
  932. 'actual_money' => $realUsdt,
  933. 'balance' => $accountUsdt,
  934. 'create_time' => time(),
  935. 'update_time' => time(),
  936. 'remark' => "USDT转账",
  937. 'status' => 1,
  938. 'mark' => 1,
  939. ];
  940. if(!AccountLogModel::insertGetId($log)){
  941. DB::rollBack();
  942. $this->error = 2029;
  943. RedisService::clear($cacheKey);
  944. return false;
  945. }
  946. // 平台明细
  947. $log = [
  948. 'user_id' => 0,
  949. 'source_id' => $userId,
  950. 'source_order_no' => $orderNo,
  951. 'type' => 6,
  952. 'coin_type' => 1,
  953. 'user_type'=> 4,
  954. 'money' => $feeUsdt,
  955. 'actual_money' => $feeUsdt,
  956. 'balance' => 0,
  957. 'create_time' => time(),
  958. 'update_time' => time(),
  959. 'remark' => "USDT转账",
  960. 'status' => 1,
  961. 'mark' => 1,
  962. ];
  963. if(!AccountLogModel::insertGetId($log)){
  964. DB::rollBack();
  965. $this->error = 2029;
  966. RedisService::clear($cacheKey);
  967. return false;
  968. }
  969. // 平台
  970. FinanceService::make()->saveLog(0, $feeUsdt,1);
  971. // 消息
  972. $dateTime = date('Y-m-d H:i:s');
  973. MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
  974. $dateTime = date('Y-m-d H:i:s');
  975. MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}的{$realUsdt}USDT转账",3);
  976. DB::commit();
  977. RedisService::clear($cacheKey);
  978. $this->error = 2425;
  979. return true;
  980. }
  981. /**
  982. * 余额充值
  983. * @param $userId
  984. * @param $params
  985. * @return array|false
  986. */
  987. public function recharge($userId, $params)
  988. {
  989. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  990. $usdt = isset($params['usdt'])? moneyFormat($params['usdt'], 2) : 0;
  991. $coinType = isset($params['coin_type']) && $params['coin_type']? intval($params['coin_type']) : 2;
  992. $payType = 10;
  993. if($usdt<=0){
  994. $this->error = 2031;
  995. return false;
  996. }
  997. $cacheKey = "caches:member:recharge:lock_{$userId}";
  998. if(RedisService::get($cacheKey)){
  999. $this->error = 1034;
  1000. return false;
  1001. }
  1002. $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
  1003. ->select(['id','nickname','usdt','pay_password','balance','status'])
  1004. ->first();
  1005. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1006. $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  1007. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1008. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1009. if(empty($userInfo) || $status != 1){
  1010. $this->error = 2024;
  1011. return false;
  1012. }
  1013. if($userPayPassword != get_password($payPassword)){
  1014. $this->error = 2038;
  1015. return false;
  1016. }
  1017. // 充值订单
  1018. $orderNo = get_order_num('DR');
  1019. $money = $usdt;
  1020. // 星豆
  1021. DB::beginTransaction();
  1022. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1023. if($coinType == 2){
  1024. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1025. $xdPrice = $xdPrice>0 && $xdPrice <=10000? $xdPrice : 100;
  1026. $money = round($xdPrice * $usdt,2);
  1027. if($usdt > $userUsdt){
  1028. $this->error = 2035;
  1029. RedisService::clear($cacheKey);
  1030. return false;
  1031. }
  1032. // 扣除usdt余额
  1033. $updateData = ['usdt'=>DB::raw("usdt - {$usdt}"),'balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
  1034. if(!$this->model->where(['id'=> $userId])->update($updateData)){
  1035. RedisService::clear($cacheKey);
  1036. $this->error = 2036;
  1037. DB::rollBack();
  1038. return false;
  1039. }
  1040. }
  1041. $data = [
  1042. 'order_no'=> $orderNo,
  1043. 'user_id'=> $userId,
  1044. 'type'=> 1,
  1045. 'user_type'=> 1,
  1046. 'coin_type'=> 2,
  1047. 'money'=> $usdt,
  1048. 'actual_money'=> $money,
  1049. 'pay_type'=> $payType,
  1050. 'pay_status'=> 20,
  1051. 'pay_at'=> date('Y-m-d H:i:s'),
  1052. 'create_time'=> time(),
  1053. 'update_time'=> time(),
  1054. 'status'=> 2,
  1055. 'mark'=> 1,
  1056. ];
  1057. if(!$orderId = BalanceLogModel::insertGetId($data)){
  1058. RedisService::clear($cacheKey);
  1059. $this->error = 2033;
  1060. DB::rollBack();
  1061. return false;
  1062. }
  1063. // 扣除USDT
  1064. $log = [
  1065. 'user_id'=> $userId,
  1066. 'source_order_no'=> $data['order_no'],
  1067. 'user_type'=> 1,
  1068. 'type'=> 5,
  1069. 'coin_type'=> 1,
  1070. 'money'=> -$usdt,
  1071. 'actual_money'=> -$usdt,
  1072. 'balance'=> $userUsdt,
  1073. 'date'=> date('Y-m-d'),
  1074. 'create_time'=> time(),
  1075. 'remark'=> '星豆余额充值扣除',
  1076. 'status'=>1,
  1077. 'mark'=>1
  1078. ];
  1079. if(!AccountLogModel::insertGetId($log)){
  1080. RedisService::clear($cacheKey);
  1081. $this->error = 2029;
  1082. DB::rollBack();
  1083. return false;
  1084. }
  1085. // 增加星豆余额
  1086. $log = [
  1087. 'user_id'=> $userId,
  1088. 'source_order_no'=> $data['order_no'],
  1089. 'user_type'=> 1,
  1090. 'type'=> 5,
  1091. 'coin_type'=> 2,
  1092. 'money'=> $usdt,
  1093. 'actual_money'=> $money,
  1094. 'balance'=> $balance,
  1095. 'date'=> date('Y-m-d'),
  1096. 'create_time'=> time(),
  1097. 'remark'=> '星豆余额充值',
  1098. 'status'=>1,
  1099. 'mark'=>1
  1100. ];
  1101. if(!AccountLogModel::insertGetId($log)){
  1102. RedisService::clear($cacheKey);
  1103. $this->error = 2029;
  1104. DB::rollBack();
  1105. return false;
  1106. }
  1107. // 消息
  1108. $dateTime = date('Y-m-d H:i:s');
  1109. MessageService::make()->pushMessage($userId,'星豆余额充值成功',"您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}星豆",3);
  1110. DB::commit();
  1111. // 充值星豆任务
  1112. TaskService::make()->updateTask($userId,9, 0);
  1113. $this->error = 2037;
  1114. RedisService::clear($cacheKey);
  1115. return ['order_id'=> $orderId,'balance'=> moneyFormat($balance + $money,2),'usdt'=> moneyFormat($userUsdt-$usdt, 2)];
  1116. }
  1117. /**
  1118. * 余额提现
  1119. * @param $userId
  1120. * @param $params
  1121. * @return array|false
  1122. */
  1123. public function withdraw($userId, $params)
  1124. {
  1125. $money = isset($params['money'])? floatval($params['money']) : 0;
  1126. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1127. $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
  1128. if($money<=0){
  1129. $this->error = 2401;
  1130. return false;
  1131. }
  1132. if(!in_array($userType,[1,2])){
  1133. $this->error = 2404;
  1134. return false;
  1135. }
  1136. $userInfo = $this->model->with(['merchant'])
  1137. ->where(['id'=> $userId,'mark'=>1])
  1138. ->select(['id','nickname','usdt','trc_url','pay_password','status'])
  1139. ->first();
  1140. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1141. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1142. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1143. $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
  1144. $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
  1145. $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
  1146. $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
  1147. if(empty($userInfo) || $status != 1){
  1148. $this->error = 2024;
  1149. return false;
  1150. }
  1151. if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
  1152. $this->error = 2024;
  1153. return false;
  1154. }
  1155. // 提现金额验证
  1156. $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
  1157. if ($money > $accountUsdt) {
  1158. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  1159. return false;
  1160. }
  1161. // 提现账户
  1162. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  1163. if (empty($trcUrl)) {
  1164. $this->error = 2403;
  1165. return false;
  1166. }
  1167. if($userPayPassword != get_password($payPassword)){
  1168. $this->error = 2038;
  1169. return false;
  1170. }
  1171. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  1172. if(RedisService::get($cacheKey)){
  1173. $this->error = 1034;
  1174. return false;
  1175. }
  1176. DB::beginTransaction();
  1177. RedisService::set($cacheKey, $userInfo, rand(2,3));
  1178. // 提现记录
  1179. $orderNo = get_order_num('DW');
  1180. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
  1181. $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
  1182. $fee = round($money*$feeRate, 2);
  1183. $realUsdt = moneyFormat($money - $fee, 2);
  1184. $data = [
  1185. 'order_no'=> $orderNo,
  1186. 'user_id'=> $userType==1? $userId : $merchantId,
  1187. 'type'=> 2,
  1188. 'user_type'=> $userType,
  1189. 'coin_type'=> 1,
  1190. 'money'=> $money,
  1191. 'actual_money'=> $realUsdt,
  1192. 'fee'=> $fee,
  1193. 'trc_url'=> $trcUrl,
  1194. 'pay_type'=> 20,
  1195. 'date'=> date('Y-m-d'),
  1196. 'create_time'=> time(),
  1197. 'update_time'=> time(),
  1198. 'status'=> 1,
  1199. 'mark'=> 1,
  1200. ];
  1201. if(!$id = BalanceLogModel::insertGetId($data)){
  1202. DB::rollBack();
  1203. $this->error = 2405;
  1204. RedisService::clear($cacheKey);
  1205. return false;
  1206. }
  1207. // 商户扣款
  1208. if($userType == 2){
  1209. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1210. if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
  1211. DB::rollBack();
  1212. $this->error = 2406;
  1213. RedisService::clear($cacheKey);
  1214. return false;
  1215. }
  1216. // 明细
  1217. $log = [
  1218. 'user_id' => $merchantId,
  1219. 'source_id' => $userId,
  1220. 'source_order_no' => $orderNo,
  1221. 'type' => 5,
  1222. 'coin_type' => 1,
  1223. 'user_type'=> 2,
  1224. 'money' => -$money,
  1225. 'actual_money' => -$money,
  1226. 'balance' => $merchantUsdt,
  1227. 'create_time' => time(),
  1228. 'update_time' => time(),
  1229. 'remark' => "商户账户余额提现",
  1230. 'status' => 1,
  1231. 'mark' => 1,
  1232. ];
  1233. if (!AccountLogModel::insertGetId($log)) {
  1234. $this->error = 2407;
  1235. DB::rollBack();
  1236. RedisService::clear($cacheKey);
  1237. return false;
  1238. }
  1239. }
  1240. // 用户扣款
  1241. else{
  1242. $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
  1243. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  1244. DB::rollBack();
  1245. $this->error = 2406;
  1246. RedisService::clear($cacheKey);
  1247. return false;
  1248. }
  1249. // 明细
  1250. $log = [
  1251. 'user_id' => $userId,
  1252. 'source_id' => 0,
  1253. 'source_order_no' => $orderNo,
  1254. 'type' => 5,
  1255. 'coin_type' => 1,
  1256. 'user_type'=> 1,
  1257. 'money' => -$money,
  1258. 'actual_money' => -$money,
  1259. 'balance' => $userUsdt,
  1260. 'create_time' => time(),
  1261. 'update_time' => time(),
  1262. 'remark' => "USDT余额提现",
  1263. 'status' => 1,
  1264. 'mark' => 1,
  1265. ];
  1266. if (!AccountLogModel::insertGetId($log)) {
  1267. $this->error = 2407;
  1268. DB::rollBack();
  1269. RedisService::clear($cacheKey);
  1270. return false;
  1271. }
  1272. }
  1273. DB::commit();
  1274. // 站内消息
  1275. $dateTime = date('Y-m-d H:i:s');
  1276. $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
  1277. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
  1278. MessageService::make()->pushMessage($userId, $title, $message);
  1279. // 提现自动审核,低于该金额自动审核
  1280. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  1281. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  1282. if ($money <= $autoCheckUsdt) {
  1283. // 打款处理
  1284. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  1285. $txID = isset($result['txId']) ? $result['txId'] : '';
  1286. $payAddress = isset($result['address']) ? $result['address'] : '';
  1287. if ($txID && $payAddress) {
  1288. $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
  1289. if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
  1290. $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
  1291. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
  1292. MessageService::make()->pushMessage($userId, $title, $message);
  1293. AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
  1294. // 平台明细
  1295. $log = [
  1296. 'user_id' => 0,
  1297. 'source_id' => $userId,
  1298. 'source_order_no' => $orderNo,
  1299. 'type' => 5,
  1300. 'coin_type' => 1,
  1301. 'user_type'=> 4,
  1302. 'money' => $fee,
  1303. 'actual_money' => $fee,
  1304. 'balance' => 0,
  1305. 'create_time' => time(),
  1306. 'update_time' => time(),
  1307. 'hash' => $txID,
  1308. 'remark' => "USDT余额提现",
  1309. 'status' => 1,
  1310. 'mark' => 1,
  1311. ];
  1312. AccountLogModel::insertGetId($log);
  1313. // 平台流水
  1314. FinanceService::make()->saveLog(0, $fee, 1);
  1315. }
  1316. }
  1317. }
  1318. $this->error = $title;
  1319. RedisService::clear($cacheKey);
  1320. return [
  1321. 'id'=> $id,
  1322. 'money'=> $money,
  1323. 'user_type'=> $userType,
  1324. ];
  1325. }
  1326. /**
  1327. * 获取点对点上级节点
  1328. * @param $userId 推荐人ID
  1329. * @return int|mixed
  1330. */
  1331. public function getPointParentId($userId)
  1332. {
  1333. if($userId<=0){
  1334. return 0;
  1335. }
  1336. $userList = $this->model->where(['mark'=> 1])->whereRaw('FIND_IN_SET(?,points)', $userId)->select(['id','point_id','points'])->orderBy('create_time','asc')->get();
  1337. $userList = $userList? $userList->toArray() : [];
  1338. if(count($userList) < 3){
  1339. return $userId;
  1340. }
  1341. $users = [];
  1342. foreach($userList as $item){
  1343. $points = isset($item['points']) && $item['points']? explode(',', $item['points']) : [];
  1344. $points = array_filter($points);
  1345. $users[$item['id']]['pointLen'] = count($points);
  1346. $users[$item['id']]['children'] = isset($users[$item['id']]['children'])? $users[$item['id']]['children'] : 0;
  1347. $users[$item['point_id']]['children'] = isset($users[$item['point_id']]['children'])? $users[$item['point_id']]['children'] + 1 : 1;
  1348. }
  1349. //var_dump($users);
  1350. $pointId = 0;
  1351. $tempPointLen = 0;
  1352. $tempChildren = 0;
  1353. foreach ($userList as $item) {
  1354. $id = isset($item['id'])? $item['id'] : 0;
  1355. $children = isset($users[$id]['children'])? $users[$id]['children'] : 0;
  1356. $pointLen = isset($users[$id]['pointLen'])? $users[$id]['pointLen'] : 0;
  1357. //var_dump($id.'++'.$pointLen.'--'.$children.'=='.$pointId.'++'.$tempPointLen.'++'.$tempChildren);
  1358. if($pointLen<=$tempPointLen || $pointId == 0){
  1359. //var_dump($id.'++'.$pointLen.'--'.$children."\n\n");
  1360. if($children <$tempChildren || $pointId == 0){
  1361. $pointId = $id;
  1362. $tempPointLen = $pointLen;
  1363. $tempChildren = $children;
  1364. }
  1365. }
  1366. }
  1367. return $pointId;
  1368. }
  1369. /**
  1370. * DAO升级购买
  1371. * @param $userId 用户ID
  1372. * @param $params 参数:level-等级,pay_password-交易密码
  1373. * @return bool
  1374. */
  1375. public function upgrade($userId, $params)
  1376. {
  1377. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  1378. $level = isset($params['level'])? intval($params['level']) : 0;
  1379. if($level<=0 || empty($payPassword)){
  1380. $this->error = 2014;
  1381. return false;
  1382. }
  1383. $cacheKey = "caches:members:upgrade:buy_{$userId}_{$level}";
  1384. if(RedisService::get($cacheKey.'_lock')){
  1385. $this->error = 1034;
  1386. return false;
  1387. }
  1388. $levelInfo = MemberLevelModel::from('member_level as a')
  1389. ->where(['a.id'=> $level,'a.status'=>1,'a.mark'=>1])
  1390. ->select(['a.*'])
  1391. ->first();
  1392. $upgradeUsdt = isset($levelInfo['upgrade_usdt'])? floatval($levelInfo['upgrade_usdt']) : 0;
  1393. $upgradeProfit = isset($levelInfo['upgrade_profit'])? floatval($levelInfo['upgrade_profit']) : 0;
  1394. $boxNum = isset($levelInfo['box_num'])? intval($levelInfo['box_num']) : 0;
  1395. $levelName = isset($levelInfo['name'])? $levelInfo['name'] : '';
  1396. if(empty($levelInfo)){
  1397. $this->error = 2511;
  1398. return false;
  1399. }
  1400. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  1401. ->select(['id','nickname','usdt','wait_score','parents','points','parent_id','member_level','power_num','trc_url','pay_password','status'])
  1402. ->first();
  1403. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  1404. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  1405. $userTotalProfit = isset($userInfo['upgrade_profit_total'])? $userInfo['upgrade_profit_total'] : 0;
  1406. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  1407. $userWaitScore = isset($userInfo['wait_score'])? $userInfo['wait_score'] : 0;
  1408. $userPowerNum = isset($userInfo['power_num'])? $userInfo['power_num'] : 0;
  1409. $userLevel = isset($userInfo['member_level'])? intval($userInfo['member_level']) : 0;
  1410. if(empty($userInfo) || $status != 1){
  1411. $this->error = 2024;
  1412. return false;
  1413. }
  1414. if($userLevel>= $level){
  1415. $this->error = 2512;
  1416. return false;
  1417. }
  1418. if($level >= 30){
  1419. $this->error = 2513;
  1420. return false;
  1421. }
  1422. // 收益是否满足条件
  1423. if($upgradeProfit>0 && $userTotalProfit < $upgradeProfit){
  1424. $this->error = 2514;
  1425. return false;
  1426. }
  1427. if(AccountLogModel::where(['user_id'=> $userId,'source_id'=> $level,'type'=>2,'coin_type'=>1,'status'=>1,'mark'=>1])->value('id')){
  1428. $this->error = 2512;
  1429. return false;
  1430. }
  1431. // USDT余额是否足够
  1432. if($userUsdt < $upgradeUsdt){
  1433. $this->error = 2035;
  1434. return false;
  1435. }
  1436. // 交易密码
  1437. if(empty($userPayPassword)){
  1438. $this->error = 1040;
  1439. return false;
  1440. }
  1441. if($userPayPassword != get_password($payPassword)){
  1442. $this->error = 2038;
  1443. return false;
  1444. }
  1445. // 购买明细
  1446. RedisService::set($cacheKey, $params, rand(2,3));
  1447. $orderNo = get_order_num('UP');
  1448. DB::beginTransaction();
  1449. $log = [
  1450. 'user_id' => $userId,
  1451. 'source_id' => $level,
  1452. 'source_order_no' => $orderNo,
  1453. 'type' => 2,
  1454. 'coin_type' => 1,
  1455. 'user_type'=> 1,
  1456. 'money' => -$upgradeUsdt,
  1457. 'actual_money' => -$upgradeUsdt,
  1458. 'balance' => $userUsdt,
  1459. 'create_time' => time(),
  1460. 'update_time' => time(),
  1461. 'remark' => "购买{$levelName}升级",
  1462. 'status' => 1,
  1463. 'mark' => 1,
  1464. ];
  1465. if(!AccountLogModel::insertGetId($log)){
  1466. DB::rollBack();
  1467. $this->error = 2029;
  1468. RedisService::clear($cacheKey);
  1469. return false;
  1470. }
  1471. // 待返积分
  1472. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
  1473. $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
  1474. $waitScoreRate = ConfigService::make()->getConfigByCode('upgrade_award_score_rate', 200);
  1475. $waitScoreRate = $waitScoreRate>0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  1476. $waitScore = moneyFormat($upgradeUsdt * $xdPrice * $waitScoreRate/100, 2);
  1477. if($waitScore>0){
  1478. $log = [
  1479. 'user_id' => $userId,
  1480. 'source_id' => $level,
  1481. 'source_order_no' => $orderNo,
  1482. 'type' => 9,
  1483. 'coin_type' => 5,
  1484. 'user_type'=> 1,
  1485. 'money' => $waitScore,
  1486. 'actual_money' => $waitScore,
  1487. 'balance' => $userWaitScore,
  1488. 'create_time' => time(),
  1489. 'update_time' => time(),
  1490. 'remark' => "购买{$levelName}升级",
  1491. 'status' => 1,
  1492. 'mark' => 1,
  1493. ];
  1494. if(!AccountLogModel::insertGetId($log)){
  1495. DB::rollBack();
  1496. $this->error = 2029;
  1497. RedisService::clear($cacheKey);
  1498. return false;
  1499. }
  1500. }
  1501. // 算力明细
  1502. $powerNum = $upgradeUsdt;
  1503. $log = [
  1504. 'user_id' => $userId,
  1505. 'source_id' => $level,
  1506. 'source_order_no' => $orderNo,
  1507. 'type' => 9,
  1508. 'coin_type' => 3,
  1509. 'user_type'=> 1,
  1510. 'money' => $powerNum,
  1511. 'actual_money' => $powerNum,
  1512. 'balance' => $userPowerNum,
  1513. 'create_time' => time(),
  1514. 'update_time' => time(),
  1515. 'remark' => "购买{$levelName}升级",
  1516. 'status' => 1,
  1517. 'mark' => 1,
  1518. ];
  1519. if(!AccountLogModel::insertGetId($log)){
  1520. DB::rollBack();
  1521. $this->error = 2029;
  1522. RedisService::clear($cacheKey);
  1523. return false;
  1524. }
  1525. // 账户
  1526. $updateData = [
  1527. 'usdt'=> DB::raw("usdt - {$upgradeUsdt}"), // 扣除USDT
  1528. 'member_level'=> $level, // 升级等级
  1529. 'box_num'=>DB::raw("box_num + {$boxNum}"), // 奖励盲盒数量
  1530. 'wait_score'=>DB::raw("wait_score + {$waitScore}"), // 奖励盲盒数量
  1531. 'power_num'=>DB::raw("power_num + {$powerNum}"), // 奖励盲盒数量
  1532. 'update_time'=>time()
  1533. ];
  1534. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  1535. DB::rollBack();
  1536. $this->error = 2036;
  1537. RedisService::clear($cacheKey);
  1538. return false;
  1539. }
  1540. // 消息
  1541. $dateTime = date('Y-m-d H:i:s');
  1542. MessageService::make()->pushMessage($userId,"购买{$levelName}升级成功","您在{$dateTime}(UTC+8)成功支付{$upgradeUsdt}USDT购买【{$levelName}】升级成功。",3);
  1543. DB::commit();
  1544. // 结算收益奖励
  1545. FinanceService::make()->settleBonus($userId, $userInfo, $upgradeUsdt, $orderNo, "购买{$levelName}");
  1546. $this->error = 2503;
  1547. RedisService::clear($cacheKey);
  1548. return true;
  1549. }
  1550. }