Outactivity.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\mainapp\controller;
  3. use app\common\model\Config;
  4. use app\common\model\OutRecruitReport;
  5. use app\common\model\UserGetmoneyLog;
  6. use app\mainapp\BaseController;
  7. use app\common\model\OutCode as OutCodeModel;
  8. use app\common\model\Broker as BrokerModel;
  9. use app\common\model\UserAuths as UserAuthsModel;
  10. use app\common\model\OutRecruit as OutRecruitModel;
  11. use app\common\model\UserParam as UserParamModel;
  12. use app\mainapp\validate\WorkerReportValidate;
  13. use payment\wechat\WechatTransfers;
  14. use think\exception\ValidateException;
  15. use think\facade\Log;
  16. class Outactivity extends BaseController
  17. {
  18. public function checkCode()
  19. {
  20. $code = input('code/s','');
  21. if (empty($code)) {
  22. page_result(1, "该二维码已失效");
  23. }
  24. $info = OutCodeModel::where('code',$code)->find();
  25. if (empty($info)) {
  26. page_result(1, "该二维码已失效");
  27. }
  28. if ($info['status'] == 1) {
  29. page_result(1, "该二维码已被使用");
  30. }
  31. page_result(0);
  32. }
  33. public function listrecruit()
  34. {
  35. $ppage = input('ppage/d', 1);
  36. $psize = input('psize/d', 20);
  37. $map = [
  38. ['status', '=', 1]
  39. ];
  40. $orderby = ['priority' => 'desc', 'id' => 'desc'];
  41. $plist = OutRecruitModel::where($map)->order($orderby)->page($ppage)->limit($psize)->select();
  42. page_result(0, "", [
  43. 'plist' => $plist,
  44. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  45. ]);
  46. }
  47. public function detailrecruit()
  48. {
  49. $id = input('id/d', 0);
  50. if (empty($id)) {
  51. page_result(1,'招聘不存在');
  52. }
  53. $info = OutRecruitModel::find($id);
  54. if (empty($info) || $info['status'] != 1) {
  55. page_result(1,'该信息不存在或已下架');
  56. }
  57. page_result(0,'',$info);
  58. }
  59. public function apply()
  60. {
  61. $data = input('param.');
  62. try {
  63. validate(WorkerReportValidate::class)->check($data);
  64. } catch (ValidateException $e) {
  65. page_result(1, $e->getError());
  66. }
  67. //报备记录
  68. $report = OutRecruitReport::with(['broker'])->where('mobile', $data['mobile'])->where('recruit_id', $data['recruit_id'])->find();
  69. $report && page_result(1, '请不要重复提交,等待对方联系');
  70. //添加报备信息
  71. $data['brokerid'] = Config::getConfigValue('default_broker');
  72. $broker = BrokerModel::find($data['brokerid']);
  73. $data['workerid'] = $broker['workerid'];
  74. $data['agentid'] = $broker['agentid'];
  75. $data['createtime'] = time();
  76. OutRecruitReport::create($data);
  77. page_result(0);
  78. }
  79. public function getmoney()
  80. {
  81. $code = input('code/s', '');
  82. $userid = input('userid/d', 0);
  83. $info = OutCodeModel::where('code',$code)->find();
  84. if (empty($info)) {
  85. page_result(1, "该二维码已失效");
  86. }
  87. if ($info['status'] == 1) {
  88. page_result(1, "该二维码已被使用");
  89. }
  90. //获取金额
  91. $param = UserParamModel::where(1)->findOrEmpty();
  92. if (empty($param['out_getmoney'])) {
  93. page_result(1,'活动有误,请联系管理员确认!');
  94. }
  95. $money = $this->probabilityPick($param['out_getmoney']);
  96. //提现
  97. $batch_name = '晋江人力小程序用户提现';//转账的名称
  98. $out_trade_no = 'outa' . getUniId();//单号
  99. $openid = UserAuthsModel::where('userid', $userid)->where('identitytype', 'weixin')->value('identifier');//用户openid
  100. if (empty($openid)) {
  101. page_result(1, "登录信息有误,请重新登录");
  102. }
  103. $detail = [];
  104. $transfers = new WechatTransfers();
  105. $detail[] = $transfers->getDetailList($out_trade_no, $money, $batch_name, $openid);
  106. $res = $transfers->transfers($batch_name, $detail);
  107. Log::record('用户提现:' . json_encode($res));
  108. //记录
  109. if (!empty($res['batch_id'])) {
  110. UserGetmoneyLog::create([
  111. 'userid' => $userid,
  112. 'out_trade_no' => $out_trade_no,
  113. 'money' => $money,
  114. 'remark' => '招聘广告宣传奖励提现',
  115. 'createtime' => time(),
  116. ]);
  117. //更改二维码状态
  118. $info->status = 1;
  119. $info->use_time = time();
  120. $info->userid = $userid;
  121. $info->money = $money;
  122. $info->save();
  123. page_result(0, "恭喜您,获得现金奖励{$money}元。已直接发放至微信零钱,请注意查收!",['money'=>$money]);
  124. } else {
  125. page_result(1,'活动有误,请联系管理员确认!');
  126. }
  127. }
  128. private function probabilityPick($data) {
  129. //计算概率总和
  130. $total = 0;
  131. foreach ($data as $k => $v) {
  132. $data[$k]['odds'] *= 100;
  133. $total += $data[$k]['odds'];
  134. }
  135. $random = mt_rand(1, $total);
  136. $current = 0;
  137. $count = count($data);
  138. for ($i = 0; $i < $count; $i++) {
  139. $current += $data[$i]['odds']; // 累计当前概率
  140. if ($random <= $current) {
  141. return $data[$i]['money']; // 如果随机数在当前概率范围内,返回索引
  142. }
  143. }
  144. }
  145. }