MemberService.php 66 KB

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