| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace app\controller\api\marketing;
- use app\common\repositories\merchant\order\OrderMerchantRepository;
- use app\common\repositories\system\attachment\AttachmentRepository;
- use app\controller\api\merchant\sxy\Access;
- use app\controller\api\Notify;
- use crmeb\basic\BaseController;
- use crmeb\services\QrcodeService;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Request;
- class User extends BaseController
- {
- //1
- //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDY5NTA0LCJuYmYiOjE2ODAwNjk1MDQsImV4cCI6MTY5MDg2OTUwNCwianRpIjpbMSwidXNlciJdfQ.44kZssE7apUstzwcrHUoPrS4cSs51mQ1f-Painaz6uk
- //18
- //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDU4OTM3LCJuYmYiOjE2ODAwNTg5MzcsImV4cCI6MTY5MDg1ODkzNywianRpIjpbMTgsInVzZXIiXX0.a8tbd_isCkSN16VuCMXllibcOkegxA8WolAEeoyxH2k
- //389
- //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDYyMTA1LCJuYmYiOjE2ODAwNjIxMDUsImV4cCI6MTY5MDg2MjEwNSwianRpIjpbMzg5LCJ1c2VyIl19.QiGOp0XvnlF_skxU1iB60fp0goiuKnka8_gHgYqKpJ4
- public function create(){
- $uid = $this->request->uid();
- $check = Db::name("MarketingUser") -> where("uid",$uid) -> find();
- if (!$check){
- $data = [
- "uid" => $uid
- ];
- Db::name("MarketingUser") -> insert($data);
- }
- $res = Db::name("MarketingUser") -> alias("mu")-> join("User u","mu.uid = u.uid")-> where("mu.uid",$uid) -> field("mu.type,mu.uid,u.nickname,u.avatar,mu.integral") -> find();
- $res['divvy'] = sprintf("%01.2f",Db::name("user_bill") -> where("uid",$uid) -> where("status",1) -> where("commission_type",13)-> sum("number"));
- return app('json')->success($res);
- }
- public function spread(){
- $uid = $this->request->uid();
- //用户邀请码
- $user = Db::name("User") -> where("uid",$uid) -> field("user_number,avatar") -> find();
- //上级信息
- $spread = Db::name("MarketingUser") -> alias("mu")-> join("User u","mu.spread_uid = u.uid")-> where("mu.uid",$uid) -> field("account,user_number") -> find();
- //生成二维码
- $name = "marketing_user_wap_{$uid}.jpg";
- $attachmentRepository = app()->make(AttachmentRepository::class);
- $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
- //检测远程文件是否存在
- if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
- $imageInfo->delete();
- $imageInfo = null;
- }
- $siteUrl = systemConfig('site_url');
- if (!$imageInfo) {
- $codeUrl = set_http_type($siteUrl . '/share?spread=' . $uid . '&type=marketing', request()->isSsl() ? 0 : 1);//二维码链接
- $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name,$user['avatar']);
- if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
- $imageInfo['dir'] = path_to_url($imageInfo['dir']);
- if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
- $attachmentRepository->create(systemConfig('upload_type') ?: 1, -1, $uid, [
- 'attachment_category_id' => 0,
- 'attachment_name' => $imageInfo['name'],
- 'attachment_src' => $imageInfo['dir']
- ]);
- $urlCode = $imageInfo['dir'];
- } else {
- $urlCode = $imageInfo['attachment_src'];
- }
- $spread_status = 0;
- $checkUser = Db::name("MarketingUser") -> where('spread_uid',$uid)->find();
- if (empty($spread['account']) && !$checkUser){
- $spread_status = 1;
- }
- $data = [
- "spread_status" => $spread_status,
- "spread_account" => $spread['account'],
- "urlCode" => $urlCode,
- "userNumber" => $user['user_number']
- ];
- return app('json')->success($data);
- }
- public function fill_code(){
- $uid = $this->request->uid();
- $code = $this->request->param('code');
- $checkUser = Db::name("MarketingUser") -> where('spread_uid',$uid)->find();
- if ($checkUser) return app('json')->fail("无法绑定上级");
- if (empty($code)) return app('json')->fail("邀请码不能为空");
- $spread = Db::name("User") -> alias("u") -> leftjoin("MarketingUser mu","u.uid = mu.uid") -> where("u.user_number",$code) -> field("u.uid,mu.spread_list") -> find();
- if (!$spread) return app('json')->fail("邀请码不存在");
- $check = Db::name("MarketingUser") -> where("uid",$uid) -> find();
- if ($check && !empty($check['spread_uid'])) return app('json')->fail("已存在邀请关系,无法填写邀请码");
- if ($uid == $spread['uid']) return app('json')->fail("不能填写自己的邀请码");
- $spread_list = explode(',',$spread['spread_list']);
- if (in_array("[".$uid."]",$spread_list)) return app('json')->fail("无法绑定关系");
- if (!empty($spread_list)){
- $spread_list[] = "[" . $spread['uid'] . "]";
- }else{
- array_push($spread_list,"[".$spread['uid']."]");
- }
- $spread_list = array_filter($spread_list);
- $data = [
- "uid" => $uid,
- "spread_uid" => $spread['uid'],
- "spread_list" => implode(",",$spread_list)
- ];
- if (!$check){
- $res = Db::name("MarketingUser") -> insert($data);
- }else{
- $res = Db::name("MarketingUser") -> where("id",$check['id']) -> update($data);
- }
- if ($res) return app('json')->success();
- return app('json')->fail();
- }
- /**
- *
- * @remarks 创建会员订单 99
- * @author 小菜菜
- * @created 2023/03/29 14:37
- */
- public function create_vip_order()
- {
- $uid = $this->request->uid();
- $type = $this->request->param('type');
- if (empty($type))return app('json')->fail("参数错误");
- $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
- if ($type == 1) $price = systemConfig('cfb_vip');
- elseif ($type == 2) $price = systemConfig('cfb_merchant');
- elseif ($type == 3) $price = systemConfig('cfb_partner');
- else return app('json')->fail("参数错误");
- $insertData=[
- 'pay_price'=>$price,
- 'status'=>0,
- 'order_no'=>$OrderMerchantRepository->getNewOrderId("mt"),
- 'uid'=>$uid,
- 'type'=>$type
- ];
- $order_id = Db::name('marketing_order')->insertGetId($insertData);
- try {
- return app('json')->success(['order_id' => $order_id]);
- } catch (\Exception $e) {
- return app('json')->status('error', $e->getMessage(), ['order_id' => $order_id]);
- }
- }
- /**
- * @remarks 会员支付
- * @author Tang
- * @created 2021/8/27 9:18
- */
- public function pay_marketing(){
- try {
- $oid = Request::param('oid', '');
- $code = Request::param('code', '');
- $payType = Request::param('type', '');
- if ($oid == '') return app('json')->fail("请检查参数是否正确");
- $groupOrder = Db::name('marketing_order')->where('id', $oid)->where('status', 0)->find();
- if (!$groupOrder) return app('json')->fail('订单不存在或已支付');
- if ($groupOrder['type'] == 1){
- $title = "普通会员";
- }elseif ($groupOrder['type'] == 2){
- $title = "创客";
- }elseif ($groupOrder['type'] == 3){
- $title = "合伙人";
- }else{
- return app('json')->fail('订单异常');
- }
- $access = new Access();
- $groupOrder['order_type'] = "marketing";
- $res = $access->pay_huifu("wx", $code, $groupOrder, $title, env('APP_URL') . "/api/hf_notify/marketing");
- Db::name('log')->insert(['data' => '汇付marketing支付---' . json_encode($res)]);
- Db::name('marketing_order')->where('id', $oid)->update(['hf_pay_id' => $res['id']]);
- return app('json')->success($res);
- }catch(\Exception $exception){
- return app('json')->fail();
- }
- }
- //积分支付
- public function pay_marketing_integral(){
- $uid = $this->request->uid();
- $oid = Request::param('oid', '');
- if ($oid == '') return app('json')->fail("请检查参数是否正确");
- $groupOrder = Db::name('marketing_order')->where('id', $oid)->where('status', 0)->find();
- if (!$groupOrder) return app('json')->fail('订单不存在或已支付');
- $checkIntegral = Db::name("MarketingUser") -> where("uid",$uid) -> field("integral") -> find();
- if ($checkIntegral["integral"] < $groupOrder['pay_price']) return app('json')->fail('积分不足');
- $checkUser = Db::name("MarketingUser") -> where('uid',$groupOrder['uid'])->find();
- $notify = new Notify();
- $handle = $notify -> marketing($groupOrder,$checkUser,"integral");
- if ($handle){
- return app('json')->success("支付成功");
- }
- }
- public function integral_list(){
- $uid = $this->request->uid();
- [$page, $limit] = $this->getPage();
- $query = Db::name("marketing_integral") -> where("uid",$uid) -> order("time", "desc");
- $count = $query->count("id");
- $list = $query
- // ->page($page,$limit)
- -> select() -> each(function($item){
- if($item['pm'] == 0) {
- $item['integral'] = "-" . $item['integral'];
- }
- else {
- $item['integral'] = "+" . $item['integral'];
- }
- return $item;
- });
- return app('json')->success(compact('list','count'));
- }
- public function give_integral(){
- $uid = $this->request->uid();
- $account = Request::param('phone', '');
- $integral = Request::param('integral', '');
- $check = Db::name("user") -> where("account",$account) -> find();
- if (!$check) return app('json')->fail("用户不存在");
- $checkIntegral = Db::name("MarketingUser") -> where("uid",$uid) -> field("integral") -> find();
- if ($checkIntegral['integral'] < $integral) return app('json')->fail("积分不足");
- $checkMarketingUser = Db::name("MarketingUser") -> where("uid",$check['uid']) -> find();
- if (!$checkMarketingUser){
- Db::name("MarketingUser") -> insert([
- "uid" => $check['uid']
- ]);
- }
- Db::startTrans();
- try {
- $title='好友赠送积分';
- $beCheckIntegral = Db::name("marketing_user") -> where('uid',$check['uid']) -> value("integral");
- $beIntegralBillData=[
- 'uid'=>$check['uid'],
- 'pm'=>1,
- 'title'=>$title,
- 'integral'=>$integral,
- 'residual_integral' => $beCheckIntegral + $integral,
- // 'mark'=>$achievementMark,
- // 'order_sn'=>$order['order_no'],
- ];
- Db::name('marketing_integral')->insert($beIntegralBillData);
- Db::name('marketing_user')->where('uid',$check['uid'])->inc('integral',$integral)->update();
- //本人积分减少
- $title='赠送好友积分';
- $integralBillData=[
- 'uid'=>$uid,
- 'pm'=>0,
- 'title'=>$title,
- 'integral'=>$integral,
- 'residual_integral' => $beCheckIntegral - $integral,
- ];
- Db::name('marketing_integral')->insert($integralBillData);
- Db::name('marketing_user')->where('uid',$uid)->dec('integral',$integral)->update();
- Db::commit();
- return app('json')->success("成功");
- }catch (\Exception $e){
- Db::rollback();
- return app('json')->fail("失败");
- }
- }
- public function divvy(){
- set_time_limit(0);
- //获取昨天总业绩的20%
- $achievement = getModelTime(Db::name("marketing_order")->where('status', 1) -> where("divvy",0), 'yesterday')->sum('pay_price');
- $achievement = round(bcmul($achievement,'0.2',5),2);//20%
- //获取总份数
- $number = Db::name('marketing_user') -> sum("divvy_number");
- if ($number <= 0) return app('json')->success("无人分红");
- //每份多少钱
- $money = round(bcdiv($achievement,$number,5),2);
- if ($money <= 0) return app('json')->success("每份可分金额不足0.01元,不进行分红");
- //查出所有有分红份数的用户
- $list = Db::name('marketing_user') -> where("divvy_number",">",0) -> where("divvy_money",">",0) -> select();
- Db::startTrans();
- try {
- $total = 0;
- foreach ($list as $item){
- //用户可得分红
- $userMoney = round(bcmul($item['divvy_number'],$money,5),2);//份数*每份金额
- //用户剩余金额是否足够
- if($item['divvy_money'] < $userMoney){
- $userMoney = $item['divvy_money'];//用户可得金额 = 可分红金额 - 用户剩余金额
- }
- $title='分红';
- $mark = "创富宝分红";
- $data=[
- 'uid'=>$item['uid'],
- 'link_id'=>0,
- 'pm'=>1,
- 'title'=>$title,
- 'category'=>'now_money',
- 'type'=>'commission',
- 'number'=>$userMoney,
- 'balance'=>0,
- 'mark'=>$mark,
- 'create_time'=>date("Y-m-d H:i:s",time()),
- 'status'=>1,
- 'order_sn'=>0,
- 'type_shop'=>0,
- 'commission_type'=>13,
- 'order_type'=>1
- ];
- $total += $userMoney;
- Db::name('user_bill')->insert($data);
- Db::name('user')->where('uid',$item['uid'])->inc('brokerage_price',$userMoney)->update();
- //减少分红金额
- Db::name('marketing_user') -> where('uid',$item['uid']) -> dec('divvy_money',$userMoney)->update();
- }
- getModelTime(Db::name("marketing_order")->where('status', 1), 'yesterday')->update(['divvy'=>1]);
- Db::commit();
- return app('json')->success("分红成功,总可分红金额:".$achievement."||每份金额:".$money."||分红金额:".$total);
- }catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail($e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
- }
- }
- }
|