User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\controller\api\marketing;
  3. use app\common\repositories\merchant\order\OrderMerchantRepository;
  4. use app\common\repositories\system\attachment\AttachmentRepository;
  5. use app\controller\api\merchant\sxy\Access;
  6. use app\controller\api\Notify;
  7. use crmeb\basic\BaseController;
  8. use crmeb\services\QrcodeService;
  9. use think\exception\ValidateException;
  10. use think\facade\Db;
  11. use think\facade\Request;
  12. class User extends BaseController
  13. {
  14. //1
  15. //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDY5NTA0LCJuYmYiOjE2ODAwNjk1MDQsImV4cCI6MTY5MDg2OTUwNCwianRpIjpbMSwidXNlciJdfQ.44kZssE7apUstzwcrHUoPrS4cSs51mQ1f-Painaz6uk
  16. //18
  17. //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDU4OTM3LCJuYmYiOjE2ODAwNTg5MzcsImV4cCI6MTY5MDg1ODkzNywianRpIjpbMTgsInVzZXIiXX0.a8tbd_isCkSN16VuCMXllibcOkegxA8WolAEeoyxH2k
  18. //389
  19. //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNzIuMTYuMC4xMzU6ODMyNCIsImF1ZCI6IjE3Mi4xNi4wLjEzNTo4MzI0IiwiaWF0IjoxNjgwMDYyMTA1LCJuYmYiOjE2ODAwNjIxMDUsImV4cCI6MTY5MDg2MjEwNSwianRpIjpbMzg5LCJ1c2VyIl19.QiGOp0XvnlF_skxU1iB60fp0goiuKnka8_gHgYqKpJ4
  20. public function create(){
  21. $uid = $this->request->uid();
  22. $check = Db::name("MarketingUser") -> where("uid",$uid) -> find();
  23. if (!$check){
  24. $data = [
  25. "uid" => $uid
  26. ];
  27. Db::name("MarketingUser") -> insert($data);
  28. }
  29. $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();
  30. $res['divvy'] = sprintf("%01.2f",Db::name("user_bill") -> where("uid",$uid) -> where("status",1) -> where("commission_type",13)-> sum("number"));
  31. return app('json')->success($res);
  32. }
  33. public function spread(){
  34. $uid = $this->request->uid();
  35. //用户邀请码
  36. $user = Db::name("User") -> where("uid",$uid) -> field("user_number,avatar") -> find();
  37. //上级信息
  38. $spread = Db::name("MarketingUser") -> alias("mu")-> join("User u","mu.spread_uid = u.uid")-> where("mu.uid",$uid) -> field("account,user_number") -> find();
  39. //生成二维码
  40. $name = "marketing_user_wap_{$uid}.jpg";
  41. $attachmentRepository = app()->make(AttachmentRepository::class);
  42. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  43. //检测远程文件是否存在
  44. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  45. $imageInfo->delete();
  46. $imageInfo = null;
  47. }
  48. $siteUrl = systemConfig('site_url');
  49. if (!$imageInfo) {
  50. $codeUrl = set_http_type($siteUrl . '/share?spread=' . $uid . '&type=marketing', request()->isSsl() ? 0 : 1);//二维码链接
  51. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name,$user['avatar']);
  52. if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
  53. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  54. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  55. $attachmentRepository->create(systemConfig('upload_type') ?: 1, -1, $uid, [
  56. 'attachment_category_id' => 0,
  57. 'attachment_name' => $imageInfo['name'],
  58. 'attachment_src' => $imageInfo['dir']
  59. ]);
  60. $urlCode = $imageInfo['dir'];
  61. } else {
  62. $urlCode = $imageInfo['attachment_src'];
  63. }
  64. $spread_status = 0;
  65. $checkUser = Db::name("MarketingUser") -> where('spread_uid',$uid)->find();
  66. if (empty($spread['account']) && !$checkUser){
  67. $spread_status = 1;
  68. }
  69. $data = [
  70. "spread_status" => $spread_status,
  71. "spread_account" => $spread['account'],
  72. "urlCode" => $urlCode,
  73. "userNumber" => $user['user_number']
  74. ];
  75. return app('json')->success($data);
  76. }
  77. public function fill_code(){
  78. $uid = $this->request->uid();
  79. $code = $this->request->param('code');
  80. $checkUser = Db::name("MarketingUser") -> where('spread_uid',$uid)->find();
  81. if ($checkUser) return app('json')->fail("无法绑定上级");
  82. if (empty($code)) return app('json')->fail("邀请码不能为空");
  83. $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();
  84. if (!$spread) return app('json')->fail("邀请码不存在");
  85. $check = Db::name("MarketingUser") -> where("uid",$uid) -> find();
  86. if ($check && !empty($check['spread_uid'])) return app('json')->fail("已存在邀请关系,无法填写邀请码");
  87. if ($uid == $spread['uid']) return app('json')->fail("不能填写自己的邀请码");
  88. $spread_list = explode(',',$spread['spread_list']);
  89. if (in_array("[".$uid."]",$spread_list)) return app('json')->fail("无法绑定关系");
  90. if (!empty($spread_list)){
  91. $spread_list[] = "[" . $spread['uid'] . "]";
  92. }else{
  93. array_push($spread_list,"[".$spread['uid']."]");
  94. }
  95. $spread_list = array_filter($spread_list);
  96. $data = [
  97. "uid" => $uid,
  98. "spread_uid" => $spread['uid'],
  99. "spread_list" => implode(",",$spread_list)
  100. ];
  101. if (!$check){
  102. $res = Db::name("MarketingUser") -> insert($data);
  103. }else{
  104. $res = Db::name("MarketingUser") -> where("id",$check['id']) -> update($data);
  105. }
  106. if ($res) return app('json')->success();
  107. return app('json')->fail();
  108. }
  109. /**
  110. *
  111. * @remarks 创建会员订单 99
  112. * @author 小菜菜
  113. * @created 2023/03/29 14:37
  114. */
  115. public function create_vip_order()
  116. {
  117. $uid = $this->request->uid();
  118. $type = $this->request->param('type');
  119. if (empty($type))return app('json')->fail("参数错误");
  120. $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
  121. if ($type == 1) $price = systemConfig('cfb_vip');
  122. elseif ($type == 2) $price = systemConfig('cfb_merchant');
  123. elseif ($type == 3) $price = systemConfig('cfb_partner');
  124. else return app('json')->fail("参数错误");
  125. $insertData=[
  126. 'pay_price'=>$price,
  127. 'status'=>0,
  128. 'order_no'=>$OrderMerchantRepository->getNewOrderId("mt"),
  129. 'uid'=>$uid,
  130. 'type'=>$type
  131. ];
  132. $order_id = Db::name('marketing_order')->insertGetId($insertData);
  133. try {
  134. return app('json')->success(['order_id' => $order_id]);
  135. } catch (\Exception $e) {
  136. return app('json')->status('error', $e->getMessage(), ['order_id' => $order_id]);
  137. }
  138. }
  139. /**
  140. * @remarks 会员支付
  141. * @author Tang
  142. * @created 2021/8/27 9:18
  143. */
  144. public function pay_marketing(){
  145. try {
  146. $oid = Request::param('oid', '');
  147. $code = Request::param('code', '');
  148. $payType = Request::param('type', '');
  149. if ($oid == '') return app('json')->fail("请检查参数是否正确");
  150. $groupOrder = Db::name('marketing_order')->where('id', $oid)->where('status', 0)->find();
  151. if (!$groupOrder) return app('json')->fail('订单不存在或已支付');
  152. if ($groupOrder['type'] == 1){
  153. $title = "普通会员";
  154. }elseif ($groupOrder['type'] == 2){
  155. $title = "创客";
  156. }elseif ($groupOrder['type'] == 3){
  157. $title = "合伙人";
  158. }else{
  159. return app('json')->fail('订单异常');
  160. }
  161. $access = new Access();
  162. $groupOrder['order_type'] = "marketing";
  163. $res = $access->pay_huifu("wx", $code, $groupOrder, $title, env('APP_URL') . "/api/hf_notify/marketing");
  164. Db::name('log')->insert(['data' => '汇付marketing支付---' . json_encode($res)]);
  165. Db::name('marketing_order')->where('id', $oid)->update(['hf_pay_id' => $res['id']]);
  166. return app('json')->success($res);
  167. }catch(\Exception $exception){
  168. return app('json')->fail();
  169. }
  170. }
  171. //积分支付
  172. public function pay_marketing_integral(){
  173. $uid = $this->request->uid();
  174. $oid = Request::param('oid', '');
  175. if ($oid == '') return app('json')->fail("请检查参数是否正确");
  176. $groupOrder = Db::name('marketing_order')->where('id', $oid)->where('status', 0)->find();
  177. if (!$groupOrder) return app('json')->fail('订单不存在或已支付');
  178. $checkIntegral = Db::name("MarketingUser") -> where("uid",$uid) -> field("integral") -> find();
  179. if ($checkIntegral["integral"] < $groupOrder['pay_price']) return app('json')->fail('积分不足');
  180. $checkUser = Db::name("MarketingUser") -> where('uid',$groupOrder['uid'])->find();
  181. $notify = new Notify();
  182. $handle = $notify -> marketing($groupOrder,$checkUser,"integral");
  183. if ($handle){
  184. return app('json')->success("支付成功");
  185. }
  186. }
  187. public function integral_list(){
  188. $uid = $this->request->uid();
  189. [$page, $limit] = $this->getPage();
  190. $query = Db::name("marketing_integral") -> where("uid",$uid) -> order("time", "desc");
  191. $count = $query->count("id");
  192. $list = $query
  193. // ->page($page,$limit)
  194. -> select() -> each(function($item){
  195. if($item['pm'] == 0) {
  196. $item['integral'] = "-" . $item['integral'];
  197. }
  198. else {
  199. $item['integral'] = "+" . $item['integral'];
  200. }
  201. return $item;
  202. });
  203. return app('json')->success(compact('list','count'));
  204. }
  205. public function give_integral(){
  206. $uid = $this->request->uid();
  207. $account = Request::param('phone', '');
  208. $integral = Request::param('integral', '');
  209. $check = Db::name("user") -> where("account",$account) -> find();
  210. if (!$check) return app('json')->fail("用户不存在");
  211. $checkIntegral = Db::name("MarketingUser") -> where("uid",$uid) -> field("integral") -> find();
  212. if ($checkIntegral['integral'] < $integral) return app('json')->fail("积分不足");
  213. $checkMarketingUser = Db::name("MarketingUser") -> where("uid",$check['uid']) -> find();
  214. if (!$checkMarketingUser){
  215. Db::name("MarketingUser") -> insert([
  216. "uid" => $check['uid']
  217. ]);
  218. }
  219. Db::startTrans();
  220. try {
  221. $title='好友赠送积分';
  222. $beCheckIntegral = Db::name("marketing_user") -> where('uid',$check['uid']) -> value("integral");
  223. $beIntegralBillData=[
  224. 'uid'=>$check['uid'],
  225. 'pm'=>1,
  226. 'title'=>$title,
  227. 'integral'=>$integral,
  228. 'residual_integral' => $beCheckIntegral + $integral,
  229. // 'mark'=>$achievementMark,
  230. // 'order_sn'=>$order['order_no'],
  231. ];
  232. Db::name('marketing_integral')->insert($beIntegralBillData);
  233. Db::name('marketing_user')->where('uid',$check['uid'])->inc('integral',$integral)->update();
  234. //本人积分减少
  235. $title='赠送好友积分';
  236. $integralBillData=[
  237. 'uid'=>$uid,
  238. 'pm'=>0,
  239. 'title'=>$title,
  240. 'integral'=>$integral,
  241. 'residual_integral' => $beCheckIntegral - $integral,
  242. ];
  243. Db::name('marketing_integral')->insert($integralBillData);
  244. Db::name('marketing_user')->where('uid',$uid)->dec('integral',$integral)->update();
  245. Db::commit();
  246. return app('json')->success("成功");
  247. }catch (\Exception $e){
  248. Db::rollback();
  249. return app('json')->fail("失败");
  250. }
  251. }
  252. public function divvy(){
  253. set_time_limit(0);
  254. //获取昨天总业绩的20%
  255. $achievement = getModelTime(Db::name("marketing_order")->where('status', 1) -> where("divvy",0), 'yesterday')->sum('pay_price');
  256. $achievement = round(bcmul($achievement,'0.2',5),2);//20%
  257. //获取总份数
  258. $number = Db::name('marketing_user') -> sum("divvy_number");
  259. if ($number <= 0) return app('json')->success("无人分红");
  260. //每份多少钱
  261. $money = round(bcdiv($achievement,$number,5),2);
  262. if ($money <= 0) return app('json')->success("每份可分金额不足0.01元,不进行分红");
  263. //查出所有有分红份数的用户
  264. $list = Db::name('marketing_user') -> where("divvy_number",">",0) -> where("divvy_money",">",0) -> select();
  265. Db::startTrans();
  266. try {
  267. $total = 0;
  268. foreach ($list as $item){
  269. //用户可得分红
  270. $userMoney = round(bcmul($item['divvy_number'],$money,5),2);//份数*每份金额
  271. //用户剩余金额是否足够
  272. if($item['divvy_money'] < $userMoney){
  273. $userMoney = $item['divvy_money'];//用户可得金额 = 可分红金额 - 用户剩余金额
  274. }
  275. $title='分红';
  276. $mark = "创富宝分红";
  277. $data=[
  278. 'uid'=>$item['uid'],
  279. 'link_id'=>0,
  280. 'pm'=>1,
  281. 'title'=>$title,
  282. 'category'=>'now_money',
  283. 'type'=>'commission',
  284. 'number'=>$userMoney,
  285. 'balance'=>0,
  286. 'mark'=>$mark,
  287. 'create_time'=>date("Y-m-d H:i:s",time()),
  288. 'status'=>1,
  289. 'order_sn'=>0,
  290. 'type_shop'=>0,
  291. 'commission_type'=>13,
  292. 'order_type'=>1
  293. ];
  294. $total += $userMoney;
  295. Db::name('user_bill')->insert($data);
  296. Db::name('user')->where('uid',$item['uid'])->inc('brokerage_price',$userMoney)->update();
  297. //减少分红金额
  298. Db::name('marketing_user') -> where('uid',$item['uid']) -> dec('divvy_money',$userMoney)->update();
  299. }
  300. getModelTime(Db::name("marketing_order")->where('status', 1), 'yesterday')->update(['divvy'=>1]);
  301. Db::commit();
  302. return app('json')->success("分红成功,总可分红金额:".$achievement."||每份金额:".$money."||分红金额:".$total);
  303. }catch (\Exception $e) {
  304. Db::rollback();
  305. return app('json')->fail($e->getFile().':'.$e->getLine().'【'.$e->getMessage().'】');
  306. }
  307. }
  308. }