| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AccountLogModel;
- use App\Models\BalanceLogModel;
- use App\Models\MemberModel;
- use App\Models\MerchantCategoryModel;
- use App\Models\MerchantModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use App\Services\SmsService;
- use App\Services\WalletService;
- use Illuminate\Support\Facades\DB;
- /**
- * 商户服务管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class MerchantService
- * @package App\Services\Api
- */
- class MerchantService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * MerchantService constructor.
- */
- public function __construct()
- {
- $this->model = new MerchantModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获取缓存列表
- * @param $position
- * @param int $num
- * @return array|mixed
- */
- public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
- {
- $page = request()->post('page', 1);
- $cacheKey = "caches:merchant:page_{$page}_" . md5(json_encode($params).$pageSize);
- $datas = RedisService::get($cacheKey);
- $data = isset($datas['data'])? $datas['data'] : [];
- if ($datas && $data && !$refresh) {
- return [
- 'list'=> $data,
- 'total'=> isset($datas['total'])? $datas['total'] : 0,
- 'pageSize'=>$pageSize
- ];
- }
- $field = $field ? $field : 'lev_a.id,lev_a.user_id,lev_a.name,lev_a.logo,lev_a.type,lev_a.country,lev_a.city,lev_a.category_id,lev_c.name as category_name,lev_a.description,lev_a.service_time,lev_a.receive_num,lev_a.status';
- $order = 'lev_a.id desc';
- $sortType = isset($params['sort_type'])? $params['sort_type'] : 0;
- if($sortType == 1){
- $order = 'lev_a.receive_num desc, lev_a.create_time desc';
- }
- $datas = $this->model->from('merchant as a')
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->leftJoin('merchant_category as c','c.id','=','a.category_id')
- ->where(['a.mark' => 1,'b.mark'=>1])
- ->where(function ($query) use ($params) {
- $kw = isset($params['kw']) ? trim($params['kw']) : '';
- if ($kw) {
- $query->where('a.name', 'like', "%{$kw}%");
- }
- })
- ->where(function ($query) use ($params) {
- // 商户类型
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type) {
- $query->where('a.type', $type);
- }
- // 状态
- $status = isset($params['status']) && $params['status']>=0 ? intval($params['status']) : 2;
- if ($status>0) {
- $query->where('a.status', $status);
- }else{
- $query->whereIn('a.status',[1,2]);
- }
- // 经营类目
- $category = isset($params['category_id']) ? intval($params['category_id']) : 0;
- if ($category) {
- $query->where('a.category_id', $category);
- }
- // 市
- $city = isset($params['city']) ? trim($params['city']) : '';
- if ($city) {
- $query->where(function($query) use($city){
- $query->where('a.city', $city)->orWhere('a.address','like',"%{$city}%");
- });
- }
- $locale = isset($params['locale']) ? trim($params['locale']) : '';
- if ($locale) {
- $query->where(function($query) use($locale){
- $query->where('a.country_code', $locale);
- });
- }
- })
- ->selectRaw($field)
- ->orderByRaw($order)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $datas = $datas ? $datas->toArray() : [];
- if ($datas) {
- foreach($datas['data'] as &$item){
- $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
- }
- unset($item);
- RedisService::set($cacheKey, $datas, rand(3, 5));
- }
- return [
- 'list'=> isset($datas['data'])? $datas['data'] : [],
- 'total'=> isset($datas['total'])? $datas['total'] : 0,
- 'pageSize'=>$pageSize
- ];
- }
- /**
- * 获取详情
- * @param $id 商家ID
- * @param string $type
- * @return array|mixed
- */
- public function getInfoById($userId, $type='info', $id=0)
- {
- $cacheKey = "caches:merch:{$type}_{$userId}_{$id}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $where = ['a.mark'=>1,'b.mark'=>1];
- if($id){
- $where['a.id'] = $id;
- }else if($userId){
- $where['a.user_id'] = $userId;
- }
- $field = ['a.id','a.name','a.user_id','a.type','a.logo','a.category_id','a.business_img','a.albums','a.country','a.city','a.address','a.usdt','a.service_time','a.withdraw_total','a.status','b.username','b.nickname','b.trc_url'];
- $info = $this->model->from('merchant as a')->with(['category'])
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where($where)
- ->select($field)
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- if(isset($info['logo'])){
- $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
- }
- if(isset($info['business_img']) && $info['business_img']){
- $info['business_img'] = get_image_url($info['business_img']);
- }
- if(isset($info['albums'])){
- $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
- $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
- }
- // 收款二维码
- $data = ['mid'=> $info['id'],'scene'=>'merch','type'=>'payment'];
- $qrcode = MemberService::make()->makeQrcode(json_encode($data));
- $info['qrcode'] = $qrcode? get_image_url($qrcode):'';
- $info['day_usdt'] = moneyFormat(AccountLogService::make()->getCountDataByDate($info['id'],1,2,1),2);
- $info['total_usdt'] = moneyFormat(AccountLogService::make()->getCountDataByDate($info['id'],1,2,0),2);
- RedisService::set($cacheKey, $info, rand(2, 3));
- }
- return $info;
- }
- /**
- * 获取缓存信息
- * @param $where
- * @param array $field
- * @param int $expired
- * @return array|mixed
- */
- public function getCacheInfo($where, $field = [], $expired = 0)
- {
- $cacheKey = "caches:merchant:info:cache_" . md5(json_encode($where, 256) . json_encode($field, 256) . $expired);
- $info = RedisService::get($cacheKey);
- if ($info) {
- return $info;
- }
- $defaultField = ['id','user_id', 'name', 'mobile', 'logo', 'category', 'usdt', 'status'];
- $field = $field ? $field : $defaultField;
- $info = $this->model->where($where)->where('mark', 1)->select($field)->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- if (isset($info['logo'])) {
- $info['logo'] = $info['logo']? $info['logo'] : '/images/member/logo.png';
- $info['logo_preview'] = $info['logo']? get_image_url($info['logo']) : '';
- }
- RedisService::set($cacheKey, $info, $expired ? $expired : rand(3, 5));
- }
- return $info;
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- // 图片处理
- $cover = $data['cover'] ? trim($data['cover']) : '';
- if (strpos($cover, "temp")) {
- $data['cover'] = save_image($cover, 'ad');
- } else {
- $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
- }
- // 开始时间
- if (isset($data['start_time'])) {
- $data['start_time'] = strtotime($data['start_time']);
- }
- // 结束时间
- if (isset($data['end_time'])) {
- $data['end_time'] = strtotime($data['end_time']);
- }
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 修改信息
- * @param $userId
- * @param $params
- * @return array|false|int[]
- */
- public function saveInfo($userId, $params)
- {
- // 验证是否入驻过和入驻状态
- $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
- $status = isset($info['status'])? $info['status'] : 0;
- $mark = isset($info['mark'])? $info['mark'] : 0;
- $merchId = isset($info['id'])? $info['id'] : 0;
- if($merchId && empty($info)){
- $this->error = 2216;
- return false;
- }
- // 是否被冻结
- if($merchId && $status == 3 && $mark){
- $this->error = 2202;
- return false;
- }
- // 验证账户是否正常
- $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 2017;
- return false;
- }
- $type = isset($params['type'])? intval($params['type']) : 0;
- $category = isset($params['category'])? intval($params['category']) : 0;
- $lng = isset($params['lng'])? floatval($params['lng']) : 0;
- $lat = isset($params['lat'])? floatval($params['lat']) : 0;
- $address = isset($params['address'])? trim($params['address']) : '';
- $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
- $file1 = isset($params['file1'])? get_format_images($params['file1']) : '';
- $file2 = isset($params['file2'])? get_format_images($params['file2']) : '';
- $file3 = isset($params['file3'])? get_format_images($params['file3']) : '';
- $alipayQrcodeData = isset($params['alipay_qrcode'])? $params['alipay_qrcode'] : [];
- $alipayQrcode = isset($alipayQrcodeData[0]['url'])? get_image_path($alipayQrcodeData[0]['url']) : '';
- $wxpayQrcodeData = isset($params['wxpay_qrcode'])? $params['wxpay_qrcode'] : [];
- $wxpayQrcode = isset($wxpayQrcodeData[0]['url'])? get_image_path($wxpayQrcodeData[0]['url']) : '';
- $logoData = isset($params['logo'])? $params['logo'] : [];
- $logo = isset($logoData[0]['url'])? get_image_path($logoData[0]['url']) : '';
- $name = isset($params['name'])? $params['name'] : '';
- // 验证分类
- if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
- $this->error = 2203;
- return false;
- }
- // 验证logo
- if(empty($logo)){
- $this->error = 2204;
- return false;
- }
- // 位置信息
- if($type == 1 && (empty($lat) || empty($lng) || empty($address))){
- $this->error = 2210;
- //return false;
- }
- // 证书
- if(empty($file1)){
- $this->error = $type == 1? 2206 : 2211;
- return false;
- }
- if(empty($file2)){
- $this->error = 2207;
- return false;
- }
- if(empty($file3)){
- $this->error = $type==1? 2208 : 2209;
- return false;
- }
- // 收款码
- if(empty($alipayQrcode) || empty($wxpayQrcode)){
- $this->error = 2205;
- return false;
- }
- // 入驻数据
- $data = [
- 'name' => $name,
- 'user_id' => $userId,
- 'category'=> $category,
- 'type'=> $type,
- 'logo'=> $logo,
- 'albums'=> $albums? $albums : '',
- 'qualification_imgs'=> $file1? $file1 : '',
- 'other_certificates'=> $file2? $file2 : '',
- 'idcard_imgs'=> $file3? $file3 : '',
- 'alipay_qrcode'=> $alipayQrcode? $alipayQrcode : '',
- 'wxpay_qrcode'=> $wxpayQrcode? $wxpayQrcode : '',
- 'lng'=> $lng,
- 'lat'=> $lat,
- 'address'=> $address,
- 'province'=> isset($params['province'])? trim($params['province']) : '',
- 'city'=> isset($params['city'])? trim($params['city']) : '',
- 'district'=> isset($params['district'])? trim($params['district']) : '',
- 'intro'=> isset($params['intro'])? trim($params['intro']) : '',
- 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '',
- 'business_scope'=> isset($params['business_scope'])? trim($params['business_scope']) : '',
- 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=> 1,
- ];
- // 写入数据
- if($merchId){
- if($this->model->where(['id'=> $merchId])->update($data)){
- $this->error = 2228;
- RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
- return ['id'=> $merchId];
- }else{
- $this->error = 2229;
- return false;
- }
- }else{
- if($merchId = $this->model->insertGetId($data)){
- $this->error = 2228;
- RedisService::keyDel("caches:merchant:info:temp_{$userId}*");
- return ['id'=> $merchId];
- }else{
- $this->error = 2229;
- return false;
- }
- }
- }
- /**
- * 申请入驻
- * @param $userId
- * @param $params
- * @return array|false|int[]
- */
- public function apply($userId, $params)
- {
- // 验证是否入驻过和入驻状态
- $info = $this->model->where(['user_id'=> $userId])->select('id','name','status','mark')->first();
- $status = isset($info['status'])? $info['status'] : 0;
- $mark = isset($info['mark'])? $info['mark'] : 0;
- $merchId = isset($info['id'])? $info['id'] : 0;
- if($merchId && $status == 2 && $mark){
- $this->error = 2801;
- return false;
- }
- // 是否被冻结
- if($merchId && $status == 4){
- $this->error = 2802;
- return false;
- }
- // 验证账户是否正常
- $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 2017;
- return false;
- }
- $type = isset($params['type'])? intval($params['type']) : 1;
- $category = isset($params['categoty_id'])? intval($params['categoty_id']) : 0;
- $currency = isset($params['currency'])? trim($params['currency']) : '';
- $country = isset($params['country'])? trim($params['country']) : '';
- $city = isset($params['city'])? trim($params['city']) : '';
- $address = isset($params['address'])? trim($params['address']) : '';
- $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
- $businessImg = isset($params['business_img'])? get_image_path($params['business_img']) : '';
- $logo = isset($params['logo'])? get_image_path($params['logo']) : '';
- $name = isset($params['name'])? $params['name'] : '';
- // 验证分类
- if($category && !MerchantCategoryModel::where(['id'=> $category,'status'=>1,'mark'=>1])->value('id')){
- $this->error = 2803;
- return false;
- }
- // 验证logo
- if(empty($logo)){
- $this->error = 2804;
- return false;
- }
- $mobile = isset($params['mobile'])? trim($params['mobile']) : '';
- $telegram = isset($params['telegram'])? trim($params['telegram']) : '';
- if(empty($mobile) && empty($telegram)){
- $this->error = 2805;
- return false;
- }
- // 地址信息
- if(empty($country) || empty($city) || empty($address)){
- $this->error = 2806;
- return false;
- }
- if(empty($currency)){
- $this->error = 2807;
- return false;
- }
- // 营业执照
- if(empty($businessImg)){
- $this->error = 2808;
- return false;
- }
- // 入驻数据
- $data = [
- 'name' => $name,
- 'user_id' => $userId,
- 'category_id'=> $category,
- 'type'=> $type,
- 'logo'=> $logo,
- 'albums'=> $albums? $albums : '',
- 'business_img'=> $businessImg? $businessImg : '',
- 'currency'=> $currency,
- 'country'=> $country,
- 'city'=> $city,
- 'address'=> $address,
- 'description'=> isset($params['description'])? trim($params['description']) : '',
- 'mobile'=> $mobile,
- 'telegram'=> $telegram,
- 'service_time'=> isset($params['service_time'])? trim($params['service_time']) : '',
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=> 1,
- ];
- // 写入数据
- if($merchId){
- if($this->model->where(['id'=> $merchId])->update($data)){
- $this->error = 2809;
- return ['id'=> $merchId];
- }else{
- $this->error = 2810;
- return false;
- }
- }else{
- if($merchId = $this->model->insertGetId($data)){
- $this->error = 2811;
- return ['id'=> $merchId];
- }else{
- $this->error = 2810;
- return false;
- }
- }
- }
- /**
- * 获取商家入驻信息
- * @param $userId
- * @return mixed
- */
- public function getApplyInfo($userId)
- {
- $info = $this->model->with(['category'])->where(['user_id'=> $userId,'mark'=>1])
- ->orderBy('id','desc')
- ->first();
- $info = $info? $info->setHidden(['usdt','update_time','mark'])->toArray() : [];
- if($info){
- $info['logo'] = isset($info['logo']) && $info['logo']? get_image_url($info['logo']) : '';
- $info['business_img'] = isset($info['business_img']) && $info['business_img']? get_image_url($info['business_img']) : '';
- $albums = isset($info['albums']) && $info['albums']? json_decode($info['albums'], true) : [];
- $info['albums'] = $albums? get_images_preview($albums) : [];
- if(isset($info['category']) && $info['category']){
- $info['category_name'] = isset($info['category']['name'])? $info['category']['name'] : '';
- $info['category_id'] = isset($info['category']['id'])? $info['category']['id'] : '';
- }
- }
- return $info;
- }
- /**
- * 修改
- * @param $userId
- * @param $params
- * @return bool
- */
- public function modify($userId, $params)
- {
- // 用户验证
- $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
- ->select(['id','user_id', 'status'])
- ->first();
- if (!$info || $info['status'] != 2) {
- $this->error = 2812;
- return false;
- }
- // 验证账户是否正常
- $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId], ['id','status']);
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 2017;
- return false;
- }
- // 手机号
- $mobile = isset($params['mobile']) ? $params['mobile'] : '';
- if (isset($params['mobile']) && $mobile) {
- $data['mobile'] = $mobile;
- }
- $telegram = isset($params['telegram']) ? $params['telegram'] : '';
- if (isset($params['telegram']) && $telegram) {
- $data['telegram'] = $telegram;
- }
- if(isset($params['name']) && $params['name']){
- $data['name'] = trim($params['name']);
- }
- if(isset($params['service_time']) && $params['service_time']){
- $data['service_time'] = trim($params['service_time']);
- }
- $address = isset($params['address'])? trim($params['address']) : '';
- $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
- $logo = isset($params['logo'])? get_image_path($params['logo']) : '';
- if(isset($params['address']) && $address){
- $data['address'] = $address;
- }
- if(isset($params['logo']) && $logo){
- $data['logo'] = $logo;
- }
- if(isset($params['albums']) && $albums){
- $data['albums'] = $albums;
- }
- // 修改数据
- RedisService::clear("caches:merch:detail_{$info['id']}");
- RedisService::keyDel("caches:merchant:info:temp*");
- $this->model->where(['user_id' => $userId])->update($data);
- $this->error = 1008;
- return true;
- }
- /**
- * 余额提现
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function withdraw($userId, $params)
- {
- $money = isset($params['money'])? floatval($params['money']) : 0;
- $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
- $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 20;
- $paymentId = isset($params['payment_id']) && $params['payment_id']? intval($params['payment_id']) : 0;
- $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
- if($money<=0){
- $this->error = 2401;
- return false;
- }
- if($paymentId<=0 && $payType == 30){
- $this->error = 2409;
- return false;
- }
- if(!in_array($userType,[1,2,3])){
- $this->error = 2404;
- return false;
- }
- $merchant = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
- ->select(['id','user_id','name','usdt','status','trade_status'])
- ->first();
- $userInfo = $merchant['member'];
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
- $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
- if(isset($userInfo['pay_password'])){
- $userPayPassword = $userInfo['pay_password'];
- }else{
- $userPayPassword = DB::table('member')->where('id',$userId)->value('pay_password');
- }
- $merchantUsdt = isset($merchant['usdt'])? $merchant['usdt'] : 0;
- $merchantId = isset($merchant['id'])? $merchant['id'] : 0;
- $merchantTradeStatus = isset($merchant['trade_status'])? $merchant['trade_status'] : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 2024;
- return false;
- }
- if($userType == 2 && (empty($merchant) || !in_array($merchantTradeStatus,[1,2]))){
- $this->error = 2024;
- return false;
- }
- // 提现金额验证
- $accountUsdt = $userType==1? $userUsdt : $merchantUsdt;
- if ($money > $accountUsdt) {
- $this->error = web_lang(2402, ['money' => $accountUsdt]);
- return false;
- }
- // 提现账户
- $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
- if (empty($trcUrl)) {
- $this->error = 2403;
- return false;
- }
- if($userPayPassword != get_password($payPassword)){
- $this->error = 2038;
- return false;
- }
- $cacheKey = "caches:merchant:withdraw:lock_{$userId}";
- if(RedisService::get($cacheKey)){
- $this->error = 1034;
- return false;
- }
- DB::beginTransaction();
- RedisService::set($cacheKey, $userInfo, rand(2,3));
- // 提现记录
- $orderNo = get_order_num('DW');
- $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate',5);
- $feeRate = $feeRate>0 && $feeRate<100? moneyFormat($feeRate/100,2) : 0;
- $fee = round($money*$feeRate, 2);
- $realAmount = moneyFormat($money - $fee, 2);
- if($payType != 20){
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $fee = round($fee * $usdtPrice, 2);
- $realAmount = round($realAmount * $usdtPrice, 2);
- }
- $data = [
- 'order_no'=> $orderNo,
- 'user_id'=> $userType==2? $merchantId : $userId,
- 'type'=> 2,
- 'user_type'=> $userType,
- 'coin_type'=> 1,
- 'money'=> $money,
- 'actual_money'=> $realAmount,
- 'fee'=> $fee,
- 'usdt_price'=> $payType!=20? $usdtPrice : 0,
- 'trc_url'=> $trcUrl,
- 'pay_type'=> $payType,
- 'payment_id'=> $paymentId,
- 'date'=> date('Y-m-d'),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=> 1,
- ];
- if(!$id = BalanceLogModel::insertGetId($data)){
- DB::rollBack();
- $this->error = 2405;
- RedisService::clear($cacheKey);
- return false;
- }
- // 商户扣款
- if($userType == 2){
- $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
- if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
- DB::rollBack();
- $this->error = 2406;
- RedisService::clear($cacheKey);
- return false;
- }
- // 明细
- $log = [
- 'user_id' => $merchantId,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 5,
- 'coin_type' => 1,
- 'user_type'=> 2,
- 'money' => -$money,
- 'actual_money' => -$money,
- 'balance' => $merchantUsdt,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => "商户账户余额提现",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- $this->error = 2407;
- DB::rollBack();
- RedisService::clear($cacheKey);
- return false;
- }
- }
- // 用户扣款
- else{
- $updateData = ['usdt'=>DB::raw("usdt - {$money}"),'update_time'=> time()];
- if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
- DB::rollBack();
- $this->error = 2406;
- RedisService::clear($cacheKey);
- return false;
- }
- // 明细
- $log = [
- 'user_id' => $userId,
- 'source_id' => 0,
- 'source_order_no' => $orderNo,
- 'type' => 5,
- 'coin_type' => 1,
- 'user_type'=> 1,
- 'money' => -$money,
- 'actual_money' => -$money,
- 'balance' => $userUsdt,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => "USDT余额提现",
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- $this->error = 2407;
- DB::rollBack();
- RedisService::clear($cacheKey);
- return false;
- }
- }
- DB::commit();
- // 站内消息
- $dateTime = date('Y-m-d H:i:s');
- $title = $userType == 1 ? 'USDT余额提现申请成功' : '商户余额提现申请成功';
- $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额成功,请耐心等候审核!!!";
- MessageService::make()->pushMessage($userId, $title, $message, 3);
- // 提现自动审核,低于该金额自动审核
- $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
- $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
- if ($money <= $autoCheckUsdt && $payType==20) {
- // 打款处理
- $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realAmount);
- $txID = isset($result['txId']) ? $result['txId'] : '';
- $payAddress = isset($result['address']) ? $result['address'] : '';
- if ($txID && $payAddress) {
- $updateData = ['hash'=> $txID,'wallet_url'=> $payAddress,'audit_remark'=>'自动审核打款','status'=>2,'update_time'=>time()];
- if(BalanceLogModel::where(['order_no'=> $orderNo,'user_type'=> $userType])->update($updateData)){
- $title = $userType == 1 ? 'USDT余额提现审核成功' : '商户余额提现审核成功';
- $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT商户余额审核成功,请耐心等候打款到账!!!";
- MessageService::make()->pushMessage($userId, $title, $message,3);
- AccountLogModel::where(['source_order_no'=> $orderNo])->update(['hash'=> $txID,'update_time'=>time()]);
- // 平台明细
- $log = [
- 'user_id' => 0,
- 'source_id' => $userId,
- 'source_order_no' => $orderNo,
- 'type' => 5,
- 'coin_type' => 1,
- 'user_type'=> 4,
- 'money' => $fee,
- 'actual_money' => $fee,
- 'balance' => 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'hash' => $txID,
- 'remark' => "USDT余额提现",
- 'status' => 1,
- 'mark' => 1,
- ];
- AccountLogModel::insertGetId($log);
- // 平台流水
- FinanceService::make()->saveLog(0, $fee, 1);
- }
- }
- }
- $this->error = $title;
- RedisService::clear($cacheKey);
- return [
- 'id'=> $id,
- 'money'=> $money,
- 'user_type'=> $userType,
- ];
- }
- }
|