241 lines
9.8 KiB
PHP
241 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Server\H5;
|
|
|
|
use App\Http\Response\ResponseJson;
|
|
use App\Jobs\AddUnionUser;
|
|
use App\Jobs\NewMerchantDefaultService;
|
|
use App\Jobs\SendEasySms;
|
|
use App\Models\Live\Anchor;
|
|
use App\Models\MEarningRules;
|
|
use App\Models\Server\MerchantAccount;
|
|
use App\Models\Server\MerchantShareChannel;
|
|
use App\Models\Server\MerchantUser;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Repositories\Eloquent\SmsRepository as Sms;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ShareChannelController extends Controller
|
|
{
|
|
|
|
|
|
|
|
use ResponseJson;
|
|
|
|
protected $sms;
|
|
|
|
public function __construct(Sms $sms)
|
|
{
|
|
$this->sms = $sms;
|
|
}
|
|
|
|
public function token(){
|
|
$a = [
|
|
'id' => 8,
|
|
'mobile' => 110,
|
|
'email' => 'example@email.com'
|
|
];
|
|
$token = $this->api_token(json_decode(json_encode($a)));
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* 渠道详情
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return \Illuminate\Http\JsonResponse|string
|
|
*/
|
|
public function shareChannel(Request $request, $id)
|
|
{
|
|
try {
|
|
$channel = MerchantShareChannel::find($id);
|
|
if (empty($channel)) return $this->failure("渠道信息不存在");
|
|
$channel->bind_anchor = $channel->bindMerchantAnchor()->where('m_id', $channel->bind_m_id)->select('id', 'm_id', 'name', 'pic')->first();
|
|
$channel->bind_user = $channel->bindMerchantUser()->where('id', $channel->bind_m_user_id)->select('id', 'nickname', 'pic')->first();
|
|
if (empty($channel->bind_user)) return $this->failure('渠道码所属商户信息不存在');
|
|
return $this->success('ok', $channel);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('服务器休息中,请稍后再试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 绑定商户分享渠道(手机号注册)
|
|
* @param Request $request
|
|
*/
|
|
public function bindMerchantShareChannel(Request $request, $id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
//渠道
|
|
$channel = MerchantShareChannel::find($id);
|
|
if (empty($channel)) throw new \Exception('未获取到渠道信息');
|
|
if ($channel && $channel->bind_m_id) return $this->failure('该渠道已经绑定商户');
|
|
$owner_merchant = MerchantAccount::with('anchor')->where('id', $channel->m_id)->first();
|
|
if (empty($owner_merchant) || empty($owner_merchant->anchorV2)) {
|
|
return $this->failure('渠道拥有者信息不存在');
|
|
}
|
|
//验证手机号验证码
|
|
$mobile = $request->input('mobile');
|
|
$code = $request->input("code");
|
|
$password = $request->input('password');
|
|
if (empty($mobile) || empty($code)) return $this->failure("验证失败,请输入正确验证码");
|
|
$result = $this->sms->check($mobile, $code);
|
|
if ($result) return $this->failure($result);
|
|
if (empty($password)) return $this->failure("请输入密码");
|
|
|
|
//微信信息
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
$openid = null; //orseM6bZX6g9AT3Wvg_G0ccnNGx8
|
|
$merchant = null;
|
|
if ($wechatUser) {
|
|
$openid = $wechatUser->getId();
|
|
$merchant = MerchantAccount::where(['openid' => $openid])->first();
|
|
}
|
|
if (empty($merchant)) {
|
|
$merchant = MerchantAccount::where('mobile', $mobile)->first();
|
|
} elseif ($merchant && empty($merchant->mobile)) {
|
|
$merchant->mobile = $mobile;
|
|
$merchant->save();
|
|
} elseif ($merchant && $merchant->mobile && $merchant->mobile != $mobile) {
|
|
return $this->failure("商户账号已绑定其他手机号");
|
|
}
|
|
if (empty($merchant)) {
|
|
$merchant = new MerchantAccount();
|
|
$merchant->openid = $openid;
|
|
$merchant->mobile = $mobile;
|
|
$merchant->password = encrypt($password);
|
|
$merchant->save();
|
|
$rand_str = $this->randString(6);
|
|
$anchor = new Anchor();
|
|
$anchor->viewer_id = 0;
|
|
$anchor->m_id = $merchant->id;
|
|
$anchor->pic = User::DefaultAvatar;
|
|
$anchor->name = '用户' . $rand_str;
|
|
$anchor->status = 0;
|
|
$anchor->channel = 6;
|
|
$anchor->service_nature = 'person';
|
|
$anchor->mobile = $request->mobile;
|
|
$anchor->save();
|
|
$merchant->name = $rand_str;
|
|
$merchant->pic = User::DefaultAvatar;
|
|
$type = ['service', 'activity', 'consult', 'course', 'shop'];
|
|
foreach ($type as $key => $value) {
|
|
$rules = new MEarningRules();
|
|
$rules->m_id = $merchant->id;
|
|
$rules->name = $value;
|
|
$rules->ratio = 0;
|
|
$rules->first_sharer = 0;
|
|
$rules->last_sharer = 0;
|
|
$rules->other_sharer = 0;
|
|
$rules->forzen_time = 1;
|
|
$rules->save();
|
|
}
|
|
$data = ['merchant_id' => $merchant->id, 'anchor_id' => $anchor->id];
|
|
NewMerchantDefaultService::dispatch($data)->onQueue('love');
|
|
AddUnionUser::dispatch($merchant, $password, 'SPA')->onQueue('love');
|
|
} else {//有手机号没有微信信息
|
|
$merchant->openid = $openid;
|
|
$merchant->save();
|
|
}
|
|
// $merchant->token = $this->api_token($merchant);
|
|
//绑定渠道
|
|
$channel->bind_m_id = $merchant->id;
|
|
$channel->save();
|
|
|
|
//短信通知 todo
|
|
$url = env('APP_URL') . '/pu/#/channelsShare?share_channel_id=' . $channel->id . '&merchant_id=' . $channel->m_id;
|
|
$short_url = \CommonUtilsService::shortUrl($url);;
|
|
$message = '恭喜你成为商家' . $owner_merchant->anchorV2->name . '的渠道分享人,赶紧点击' . $short_url['url'] . ' 查看你的渠道分享码吧!';
|
|
SendEasySms::dispatch(['mobile' => $merchant->mobile, 'message' => $message])->onQueue('love');
|
|
DB::commit();
|
|
return $this->success('ok', $merchant);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
$this->getError($e);
|
|
return $this->failure("服务器休息,请稍后再试");
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 用户绑定商家渠道
|
|
* @param Request $request
|
|
* @param $id
|
|
*/
|
|
public function bindMerchantShareChannelV2(Request $request, $id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
//渠道
|
|
$channel = MerchantShareChannel::find($id);
|
|
if (empty($channel)) throw new \Exception('未获取到渠道信息');
|
|
if ($channel && $channel->bind_m_user_id) return $this->failure('该渠道已经绑定商户');
|
|
$owner_merchant = MerchantAccount::with('anchor')->where('id', $channel->m_id)->first();
|
|
if (empty($owner_merchant) || empty($owner_merchant->anchorV2)) {
|
|
return $this->failure('渠道拥有者信息不存在');
|
|
}
|
|
//微信信息
|
|
$wechatUser = session('wechat.oauth_user.new');
|
|
$openid = null; //orseM6bZX6g9AT3Wvg_G0ccnNGx8
|
|
$user = null;
|
|
if ($wechatUser) {
|
|
$openid = $wechatUser->getId();
|
|
} else {
|
|
return $this->failure("微信授权失败");
|
|
}
|
|
//根据openid获取用户信息
|
|
$user = MerchantUser::firstOrCreate(['openid' => $openid]);
|
|
if (empty($user->nickname)) {
|
|
$rand_str = $this->randString(8);
|
|
$user->rand_str = $rand_str;
|
|
$user->pic = User::DefaultAvatar;
|
|
$user->nickname = '用户' . $rand_str;
|
|
$user->save();
|
|
}
|
|
|
|
//绑定渠道
|
|
$channel->bind_m_user_id = $user->id;
|
|
$channel->save();
|
|
|
|
//短信通知 todo
|
|
// $url = env('APP_URL') . '/pu/#/channelsShare?share_channel_id=' . $channel->id . '&merchant_id=' . $channel->m_id;
|
|
// $short_url = \CommonUtilsService::shortUrl($url);;
|
|
// $message = '恭喜你成为商家' . $owner_merchant->anchorV2->name . '的渠道分享人,赶紧点击' . $short_url['url'] . ' 查看你的渠道分享码吧!';
|
|
// SendEasySms::dispatch(['mobile' => $merchant->mobile, 'message' => $message])->onQueue('love');
|
|
DB::commit();
|
|
return $this->success('ok', $user);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
$this->getError($e);
|
|
return $this->failure("服务器休息,请稍后再试");
|
|
}
|
|
}
|
|
|
|
//创建token
|
|
public function api_token($account)
|
|
{
|
|
try {
|
|
$token = MerchantAccount::where('id', $account->id)->value('api_token');
|
|
if ($token) {
|
|
$result = decrypt($token);
|
|
$time = explode('-', $result)[2];
|
|
if (time() - $time > 604800) {
|
|
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
|
|
MerchantAccount::where('id', $account->id)->update(['api_token' => $token]);
|
|
}
|
|
} else {
|
|
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
|
|
MerchantAccount::where('id', $account->id)->update(['api_token' => $token]);
|
|
}
|
|
return $token;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('服务器休息中,请稍后再试');
|
|
}
|
|
}
|
|
}
|