You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/application/wap/controller/Spread.php

529 lines
22 KiB

10 months ago
<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------
namespace app\wap\controller;
use app\wap\model\user\SignPoster;
use app\wap\model\user\SmsCode;
use app\wap\model\special\SpecialSubject;
use app\wap\model\store\StoreOrder;
use app\wap\model\store\StorePink;
use app\wap\model\user\User;
use app\wap\model\user\UserBill;
use app\wap\model\user\UserExtract;
use app\wap\model\user\WechatUser;
use service\CanvasService;
use service\GroupDataService;
use service\JsonService;
use service\SystemConfigService;
use service\UtilService;
use service\WechatService;
use think\Db;
use think\response\Json;
use think\Session;
use app\wap\model\special\Special;
use app\wap\model\merchant\Merchant;
use app\wap\model\institution\Institution;
use think\Url;
/**推广控制器
* Class Spread
* @package app\wap\controller
*/
class Spread extends AuthController
{
/**推广佣金
* @return mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function spread()
{
$data['income'] = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
->where('type', 'in', ['brokerage'])->sum('number');
$return = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
->where('type', 'in', ['brokerage_return'])->sum('number');
$data['income'] = bcsub($data['income'], $return, 2);
$uids = User::where('spread_uid', $this->uid)->column('uid');
$orderIds = StoreOrder::whereTime('add_time', 'today')->where('uid', 'in', $uids)->where(['refund_status' => 0, 'paid' => 1])->where('pay_price', '>', 0)->field('order_id,pink_id')->select();
$orderids = count($orderIds) ? $orderIds->toArray() : [];
$order_count = 0;
foreach ($orderids as $item) {
if ($item['pink_id']) {
$res = StorePink::where(['id' => $item['pink_id'], 'is_refund' => 0, 'status' => 3, 'order_id' => $item['order_id']])->count();
if ($res) $order_count++;
} else {
$order_count++;
}
}
$data['order_count'] = $order_count;
$data['spread_count'] = User::whereTime('spread_time', 'today')->where('spread_uid', $this->uid)->count();
$this->assign('data', $data);
return $this->fetch();
}
public function spread_api()
{
$data['income'] = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
->where('type', 'in', ['brokerage'])->sum('number');
$return = UserBill::whereTime('add_time', 'today')->where('uid', $this->uid)->where('category', 'now_money')
->where('type', 'in', ['brokerage_return'])->sum('number');
$data['income'] = bcsub($data['income'], $return, 2);
$uids = User::where('spread_uid', $this->uid)->column('uid');
$orderIds = StoreOrder::whereTime('add_time', 'today')->where('uid', 'in', $uids)->where(['refund_status' => 0, 'paid' => 1])->where('pay_price', '>', 0)->field('order_id,pink_id')->select();
$orderids = count($orderIds) ? $orderIds->toArray() : [];
$order_count = 0;
foreach ($orderids as $item) {
if ($item['pink_id']) {
$res = StorePink::where(['id' => $item['pink_id'], 'is_refund' => 0, 'status' => 3, 'order_id' => $item['order_id']])->count();
if ($res) $order_count++;
} else {
$order_count++;
}
}
$data['order_count'] = $order_count;
$data['spread_count'] = User::whereTime('spread_time', 'today')->where('spread_uid', $this->uid)->count();
return JsonService::successful([
'data' => $data,
'userInfo' => $this->userInfo
]);
}
/**
* 提现页面
* @return mixed
*/
public function withdraw($mer_id = 0)
{
if (Session::has('form__token__')) Session::delete('form__token__');
$token = md5(time() . $this->uid . $this->request->ip());
Session::set('form__token__', $token);
if ($mer_id) {
$now_money = Merchant::where(['uid' => $this->uid])->value('now_money');
$this->assign('brokerage_price', (float)$now_money);
$this->assign('extract_min_money', (float)SystemConfigService::get('extract_price'));
} else {
$this->assign('brokerage_price', $this->userInfo['brokerage_price']);
$this->assign('extract_min_money', (float)SystemConfigService::get('extract_min_money'));
}
$this->assign('token', $token);
$this->assign('extract_bank', json_encode(GroupDataService::getData('extract_bank') ?: []));
return $this->fetch();
}
9 months ago
public function withdraw_api($mer_id = 0, $type = 0)
10 months ago
{
if (Session::has('form__token__')) Session::delete('form__token__');
$token = md5(time() . $this->uid . $this->request->ip());
Session::set('form__token__', $token);
$brokerage_price = 0;
$extract_min_money = 0;
if ($mer_id) {
9 months ago
if ($type == 1) {
10 months ago
$now_money = Merchant::where(['uid' => $this->uid])->value('now_money');
9 months ago
10 months ago
$brokerage_price = (float)$now_money;
$extract_min_money = (float)SystemConfigService::get('extract_price');
9 months ago
} else {
10 months ago
$now_money = Institution::where(['uid' => $this->uid])->value('now_money');
9 months ago
10 months ago
$brokerage_price = (float)$now_money;
$extract_min_money = (float)SystemConfigService::get('institution_price');
}
} else {
$brokerage_price = $this->userInfo['brokerage_price'];
$extract_min_money = (float)SystemConfigService::get('extract_min_money');
}
$this->assign('token', $token);
$this->assign('extract_bank', json_encode(GroupDataService::getData('extract_bank') ?: []));
return JsonService::successful([
'token' => $token,
9 months ago
'extract_bank' => GroupDataService::getData('extract_bank') ?: [],
10 months ago
'brokerage_price' => $brokerage_price,
'extract_min_money' => $extract_min_money
]);
}
/**
* 保存提现信息发起企业付款到个人
* @praem $number int 提现金额
* @return json
* */
public function save_withdraw($token = '')
{
if (!$token) return JsonService::fail('token不能为空');
/* if (!Session::has('form__token__')) return JsonService::fail('请刷新页面后重试!');
if (Session::get('form__token__') != $token) return JsonService::fail('token验证失败,非法操作');
Session::delete('form__token__'); */
$extractInfo = UtilService::postMore([
['alipay_code', ''],
['extract_type', ''],
['business', 0],
['money', 0],
['name', ''],
['bankname', ''],
['cardnum', ''],
['weixin', ''],
9 months ago
['type', 0]
10 months ago
]);
if ($extractInfo['business']) {
$res = UserExtract::userMerExtract($this->userInfo, $extractInfo);
} else {
$res = UserExtract::userExtract($this->userInfo, $extractInfo);
}
if ($res) {
return JsonService::successful('申请成功');
} else {
return JsonService::fail(UserExtract::getErrorInfo());
}
}
/**
* 专题推广
*
* */
public function special()
{
return $this->fetch();
}
/**
* 获取年级列表
*
* */
public function get_grade_list()
{
return JsonService::successful(SpecialSubject::wapSpecialCategoryAll(1));
}
/**
* 获取每个年级下的专题并分页
* */
public function getSpecialSpread()
{
$where = UtilService::getMore([
['limit', 10],
['page', 1],
['grade_id', 0],
]);
$is_member = isset($this->userInfo['level']) ? $this->userInfo['level'] : 0;
return JsonService::successful(Special::getSpecialSpread($where, $is_member));
}
/**
* 专题推广二维码
* */
public function poster_special($special_id = 0)
{
if (!$special_id) $this->failed('缺少专题id无法查看海报', Url::build('spread/special'));
$special = Special::getSpecialInfo($special_id);
if ($special === false) $this->failed(Special::getErrorInfo(), Url::build('spread/special'));
if (!$special['poster_image']) $this->failed('您查看的海报不存在', Url::build('spread/special'));
if ($special['is_light']) {
$url = SystemConfigService::get('site_url') . Url::build('special/single_details') . '?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
} else {
$url = SystemConfigService::get('site_url') . Url::build('special/details') . '?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
}
$this->assign([
'url' => $url,
'poster_image' => $special['poster_image']
]);
return $this->fetch();
}
public function poster_special_api($special_id = 0)
{
if (!$special_id) return JsonService::fail('缺少专题id无法查看海报');
$special = Special::getSpecialInfo($special_id);
if ($special === false) return JsonService::fail("专题不存在!");
if (!$special['poster_image']) return JsonService::fail('您查看的海报不存在');
if ($special['is_light']) {
$url = SystemConfigService::get('site_url') . '/h5#/pages/special/single_details?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
} else {
$url = SystemConfigService::get('site_url') . '/h5#/pages/special/details?id=' . $special['id'] . '&link_pay_uid=' . $this->uid . '&link_pay=1&spread_uid=' . $this->uid . '#link_pay';
}
return JsonService::successful([
'url' => $url,
'poster_image' => $special['poster_image'],
'site_name' => SystemConfigService::get('site_name')
]);
}
/**
* 我的推广人
*
* */
public function my_promoter()
{
$uids = User::where('spread_uid', $this->uid)->group('uid')->column('uid');
$data['one_spread_count'] = count($uids);
if ($data['one_spread_count']) {
$data['order_count'] = UserBill::where(['u.paid' => 1, 'u.is_del' => 0])->where('a.category', 'now_money')->where('a.type', 'in', ['brokerage'])->group('u.id')->where('a.uid', 'in', $uids)->join("__STORE_ORDER__ u", 'u.id=a.link_id')->alias('a')->count();
} else {
$data['order_count'] = 0;
}
$isPromoter = 0;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;
if ($storeBrokerageStatu == 1) {
if ($this->userInfo['is_promoter']) {
$isPromoter = 1;
}
} else {
$isPromoter = 1;
}
$this->assign('isPromoter', $isPromoter);
$this->assign('data', $data);
return $this->fetch();
}
public function my_promoter_api()
{
$uids = User::where('spread_uid', $this->uid)->group('uid')->column('uid');
$data['one_spread_count'] = count($uids);
if ($data['one_spread_count']) {
$data['order_count'] = UserBill::where(['u.paid' => 1, 'u.is_del' => 0])->where('a.category', 'now_money')->where('a.type', 'in', ['brokerage'])->group('u.id')->where('a.uid', 'in', $uids)->join("__STORE_ORDER__ u", 'u.id=a.link_id')->alias('a')->count();
} else {
$data['order_count'] = 0;
}
$isPromoter = 0;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;
if ($storeBrokerageStatu == 1) {
if ($this->userInfo['is_promoter']) {
$isPromoter = 1;
}
} else {
$isPromoter = 1;
}
return JsonService::successful([
'isPromoter' => $isPromoter,
'data' => $data
]);
}
/**
* 佣金明细
* @return mixed
*/
public function commission()
{
$uids = User::where('spread_uid', $this->uid)->column('uid');
$data['spread_two'] = 0;
$data['spread_one'] = 0;
if ($uids) {
$data['spread_one'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
->join('store_order o', 'o.id = a.link_id')
->whereIn('o.uid', $uids)->where('o.refund_status', 'eq', 0)
->where('a.link_id', 'neq', 0)->sum('a.number');
$uids1 = User::where('spread_uid', 'in', $uids)->group('uid')->column('uid');
if ($uids1) {
$data['spread_two'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
->join('store_order o', 'o.id = a.link_id')
->whereIn('o.uid', $uids1)->where('o.refund_status', 'eq', 0)
->where('a.link_id', 'neq', 0)->sum('a.number');
}
}
$data['sum_spread'] = bcadd($data['spread_one'], $data['spread_two'], 2);
$this->assign('data', $data);
return $this->fetch();
}
public function commission_api()
{
$uids = User::where('spread_uid', $this->uid)->column('uid');
$data['spread_two'] = 0;
$data['spread_one'] = 0;
if ($uids) {
$data['spread_one'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
->join('store_order o', 'o.id = a.link_id')
->whereIn('o.uid', $uids)->where('o.refund_status', 'eq', 0)
->where('a.link_id', 'neq', 0)->sum('a.number');
$uids1 = User::where('spread_uid', 'in', $uids)->group('uid')->column('uid');
if ($uids1) {
$data['spread_two'] = UserBill::where(['a.uid' => $this->uid, 'a.type' => 'brokerage', 'a.category' => 'now_money'])->alias('a')
->join('store_order o', 'o.id = a.link_id')
->whereIn('o.uid', $uids1)->where('o.refund_status', 'eq', 0)
->where('a.link_id', 'neq', 0)->sum('a.number');
}
}
$data['sum_spread'] = bcadd($data['spread_one'], $data['spread_two'], 2);
return JsonService::successful([
'data' => $data
]);
}
/**
* 推广明细
* @param $type int 明细类型
* @return html
* */
public function spread_detail($type = 0)
{
$this->assign('type', $type);
return $this->fetch();
}
public function get_spread_list()
{
$where = UtilService::getMore([
['page', 1],
['limit', 10],
['type', 0]
]);
return JsonService::successful(UserBill::getSpreadList($where, $this->uid));
}
public function get_withdrawal_list()
{
$where = UtilService::getMore([
['page', 1],
['limit', 10]
]);
return JsonService::successful(UserBill::get_user_withdrawal_list($where, $this->uid));
}
/**
* 推广海报
*
* */
public function poster_spread()
{
$spread_poster_url = SystemConfigService::get('spread_poster_url');
if (!$spread_poster_url) $this->failed('海报不存在', Url::build('spread/special'));
$url = SystemConfigService::get('site_url') . Url::build('Spread/become_promoter', ['spread_uid' => $this->uid]);
$this->assign([
'url' => $url,
'spread_poster_url' => $spread_poster_url
]);
return $this->fetch();
}
public function poster_spread_api()
{
$spread_poster_url = SystemConfigService::get('spread_poster_url');
9 months ago
if (!$spread_poster_url) JsonService::fail('海报不存在');
$url = SystemConfigService::get('site_url') . '/h5#/pages/spread/become_promoter?spread_uid=' . $this->uid;
10 months ago
return JsonService::successful([
9 months ago
'uid' => $this->uid,
10 months ago
'url' => $url,
'spread_poster_url' => $spread_poster_url,
'site_name' => SystemConfigService::get('site_name')
]);
}
/**
* 绑定推广人
* @param int $pread_uid
* @return json
* */
public function save_promoter($spread_uid = 0)
{
if (!$spread_uid) return JsonService::fail('缺少推广人UID');
list($phone, $code) = UtilService::postMore([
['phone', ''],
['code', ''],
], $this->request, true);
if (!$phone || !$code) return JsonService::fail('请输入登录账号');
if (!$code) return JsonService::fail('请输入验证码');
$code = md5('is_phone_code' . $code);
if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
SmsCode::setCodeInvalid($phone, $code);
$spreadUserInfo = User::getUserData($spread_uid);
if (!$spreadUserInfo) return JsonService::fail('您想要绑定的上级不存在!');
if ($this->userInfo['is_promoter']) return JsonService::fail('您已经成为推广人,无法绑定!');
if ($spreadUserInfo['spread_uid'] == $this->uid) return JsonService::fail('您绑定的上级的推广人不能是您自己!');
if ($this->uid == $spread_uid) return JsonService::fail('您不能绑定自己!');
$data = ['phone' => $phone];
$data = User::manageSpread($spread_uid, $data, $spreadUserInfo['is_promoter']);
if ($data === false) return JsonService::fail(User::getErrorInfo());
if (User::edit($data, $this->uid, 'uid'))
return JsonService::successful('恭喜您,加入成功!');
else
return JsonService::fail('很抱歉加入失败!');
}
9 months ago
10 months ago
public function save_promoter_api()
{
9 months ago
list($phone, $code, $spread_uid) = UtilService::postMore([
10 months ago
['phone', ''],
['code', ''],
['spread_uid', 0],
], $this->request, true);
if (!$spread_uid) return JsonService::fail('缺少推广人UID');
if (!$phone || !$code) return JsonService::fail('请输入登录账号');
if (!$code) return JsonService::fail('请输入验证码');
$code = md5('is_phone_code' . $code);
if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
SmsCode::setCodeInvalid($phone, $code);
$spreadUserInfo = User::getUserData($spread_uid);
if (!$spreadUserInfo) return JsonService::fail('您想要绑定的上级不存在!');
if ($this->userInfo['is_promoter']) return JsonService::fail('您已经成为推广人,无法绑定!');
if ($spreadUserInfo['spread_uid'] == $this->uid) return JsonService::fail('您绑定的上级的推广人不能是您自己!');
if ($this->uid == $spread_uid) return JsonService::fail('您不能绑定自己!');
$data = ['phone' => $phone];
$data = User::manageSpread($spread_uid, $data, $spreadUserInfo['is_promoter']);
if ($data === false) return JsonService::fail(User::getErrorInfo());
9 months ago
$uid = User::where('phone', $phone)->value('uid');
if (!$uid) return JsonService::fail('被绑定人uid获取失败!');
10 months ago
if (User::edit($data, $uid, 'uid'))
return JsonService::successful('恭喜您,加入成功!');
else
return JsonService::fail('很抱歉加入失败!');
}
9 months ago
10 months ago
/**
* 新增推广人注册
*
* */
public function become_promoter($spread_uid = 0)
{
if (!$spread_uid) $this->failed('缺少推广人uid');
$this->assign('spread_uid', $spread_uid);
$this->assign('promoter_content', SystemConfigService::get('promoter_content'));
$this->assign('home_logo', SystemConfigService::get('home_logo'));
return $this->fetch();
}
/**
* 推广人列表获取
*
* */
public function spread_list()
{
$where = UtilService::getMore([
['page', 1],
['limit', 10],
['search', '']
]);
return JsonService::successful(User::getSpreadList($where, $this->uid));
}
/**
* 移除当前用下的推广人
* @param int $uid 需要移除的用户id
* */
public function remove_spread($uid = 0)
{
if (!$uid) return JsonService::fail('缺少用户id');
$res = User::where('uid', $uid)->update(['spread_uid' => 0, 'valid_time' => 0]);
if ($res)
return JsonService::successful('移除成功');
else
return JsonService::fail('移除失败');
}
}