Record.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use addons\third\model\Third;
  4. use app\admin\exception\RedbagException;
  5. use app\admin\model\lottery\RedbagLog;
  6. use app\services\pay\PayService;
  7. use think\Db;
  8. use think\exception\HttpResponseException;
  9. use app\common\controller\Backend;
  10. use think\Log;
  11. /**
  12. * 抽奖记录管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Record extends Backend
  17. {
  18. /**
  19. * Record模型对象
  20. * @var \app\admin\model\lottery\Record
  21. */
  22. protected $model = null;
  23. protected $relationSearch = true;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\lottery\Record;
  28. set_addon_config('epay', ['version' => 'v3'], false);
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. $this->relationSearch = true;
  41. $this->searchFields = "user.nickname,lottery.name,prize.name,id";
  42. if ($this->request->isAjax())
  43. {
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model
  46. ->with(['lottery','prize','user'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->with(['lottery','prize','user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. $result = array("total" => $total, "rows" => $list, "extend" => ['money' => 1024, 'price' => 888]);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. public function deliver()
  62. {
  63. if (!$this->request->isAjax()) {
  64. return $this->view->fetch();
  65. }
  66. $params = $this->request->param();
  67. $id = $params['ids'] ?? 0;
  68. if (intval($id) <= 0){
  69. $this->error(__('请上传正确的ID'));
  70. }
  71. // 查询第三方登录数据
  72. $withdraw = $this->model->lock(true)->where('id', $id)->find();
  73. if (!$withdraw) {
  74. $this->error(__('No Results were found'));
  75. }
  76. $objThird = Third::where('user_id', $withdraw->user_id)
  77. ->where('platform','wechat')
  78. ->where('apptype','mp')
  79. ->find();
  80. if (empty($objThird)){
  81. $this->error(__('用户三方数据不存在'));
  82. }
  83. Db::startTrans();
  84. try {
  85. $withdraw = $this->handleWithdraw($withdraw,$objThird);
  86. Db::commit();
  87. } catch (HttpResponseException $e) {
  88. $data = $e->getResponse()->getData();
  89. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  90. $this->error($message);
  91. } catch (\Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. $this->success('处理成功');
  96. }
  97. // 处理打款
  98. public function handleWithdraw($withdraw, $objThird)
  99. {
  100. $withDrawStatus = false;
  101. if ($withdraw->is_deliver == 1) {
  102. $this->error('请勿重复操作');
  103. }
  104. $withDrawStatus = $this->handleTransfer($withdraw,$objThird);
  105. if ($withDrawStatus) {
  106. $withdraw->is_deliver = 1;
  107. $withdraw->deliver_time = time();
  108. $withdraw->save();
  109. return $this->handleLog($withdraw, '已打款');
  110. }
  111. return $withdraw;
  112. }
  113. // 企业付款提现
  114. private function handleTransfer($withdraw,$objThird)
  115. {
  116. $type = 'wechat';
  117. $platform = 'WechatOfficialAccount';
  118. $payService = new PayService($type, $platform);
  119. $sn = $this->get_sn($withdraw->user_id, 'W');
  120. $emoji_pattern = "/[\x{1F600}-\x{1F64F}|[\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u"; // Emoji表情Unicode范围
  121. $objThird->openname = preg_replace($emoji_pattern, '', $objThird->openname);
  122. $payload = [
  123. 'out_batch_no' => $sn,
  124. 'batch_name' => '商家转账到零钱',
  125. 'batch_remark' => "用户[" . ($objThird->openname ?? '') . "]中奖红包",
  126. //'total_amount' => $withdraw->amount,
  127. 'total_amount' =>$withdraw->num,
  128. 'total_num' => 1,
  129. 'transfer_detail_list' => [
  130. [
  131. 'out_detail_no' => $sn,
  132. //'transfer_amount' => $withdraw->amount,
  133. 'transfer_amount' => $withdraw->num,
  134. 'transfer_remark' => "用户[" . ($objThird->openname ?? '') . "]中奖红包",
  135. 'openid' => $objThird->openid?? '',
  136. //'user_name' => $objThird->openname ?? '',
  137. ],
  138. ],
  139. ];
  140. try {
  141. list($code, $response) = $payService->transfer($payload);
  142. Log::write('transfer-origin-data:' . json_encode($response));
  143. if ($code === 1) {
  144. $withdraw->deliver_info = json_encode($response, JSON_UNESCAPED_UNICODE);
  145. return true;
  146. }
  147. throw new RedbagException(json_encode($response, JSON_UNESCAPED_UNICODE));
  148. } catch (HttpResponseException $e) {
  149. $data = $e->getResponse()->getData();
  150. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  151. Log::write('http-error-reponData:' .$message);
  152. throw new RedbagException($message);
  153. } catch (\Exception $e) {
  154. \think\Log::error('抽奖发红包失败:' . ' 行号:' . $e->getLine() . '文件:' . $e->getFile() . '错误信息:' . $e->getMessage());
  155. $this->handleLog($withdraw, '抽奖发红包失败:' . $e->getMessage());
  156. throw new RedbagException($e->getMessage());
  157. }
  158. return false;
  159. }
  160. private function handleLog($withdraw, $oper_info)
  161. {
  162. $oper = $this->auth->id;
  163. RedbagLog::insert([
  164. 'lottery_record_id' => $withdraw->id,
  165. 'content' => $oper_info,
  166. 'oper_type' => 'admin',
  167. 'oper_id' => $oper,
  168. 'createtime' => time()
  169. ]);
  170. return $withdraw;
  171. }
  172. /**
  173. * 获取唯一编号
  174. *
  175. * @param mixed $id 唯一标识
  176. * @param string $type 类型
  177. * @return string
  178. */
  179. public function get_sn($id, $type = '')
  180. {
  181. $id = (string)$id;
  182. $rand = $id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
  183. $sn = date('Yhis') . $rand;
  184. $id = str_pad($id, (24 - strlen($sn)), '0', STR_PAD_BOTH);
  185. return $type . $sn . $id;
  186. }
  187. }