MemberService.php 65 KB

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