Water.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\admin\controller\questionnaire;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use function fast\e;
  6. /**
  7. * 调查问卷流水管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Water extends Backend
  12. {
  13. /**
  14. * Water模型对象
  15. * @var \app\admin\model\questionnaire\Water
  16. */
  17. protected $model = null;
  18. protected $noNeedLogin = ['makeQWater'];
  19. protected $noNeedRight = ['makeQWater'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\questionnaire\Water;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $total = $this->model
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->count();
  47. $list = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->limit($offset, $limit)
  51. ->select();
  52. $list = collection($list)->toArray();
  53. $questionnaire = Db::table('questionnaire')->column('id,name');
  54. foreach ($list as $k=>$v)
  55. {
  56. $list[$k]['questionnaire'] = $questionnaire[$v['questionnaire_id']] ?? '';
  57. }
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. public function makeQWater()
  64. {
  65. $now = new \DateTime(); // 获取当前时间
  66. $now->modify('first day of this month'); // 设置为当前月的第一天
  67. $lastMonth = clone $now; // 克隆当前时间对象
  68. $lastMonth->sub(new \DateInterval('P1M')); // 减去一个月
  69. $first = $lastMonth->format('Y-m-d 00:00:00'); // 输出上个月的第一天
  70. $lastMonth->modify('last day of this month'); // 设置为上个月最后一天
  71. $last = $lastMonth->format('Y-m-d 23:59:59'); // 输出上个月最后一天
  72. $questionnaire = Db::table('questionnaire')->column('id,name');
  73. $water = Db::table('questionnaire_water')
  74. ->alias('w')
  75. ->join(['user_wechat'=>'u'],'u.wx_openid=w.openid')
  76. ->where("w.createdAt between '$first' and '$last'")
  77. ->field('w.questionnaire_id,w.createdAt,w.money,u.wx_nickname')
  78. ->order('createdAt')
  79. ->select();
  80. if(!empty($water))
  81. {
  82. foreach ($water as $k=>$v)
  83. {
  84. $water[$k]['questionnaire_name'] = $questionnaire[$v['questionnaire_id']] ?? '';
  85. }
  86. $url = $this->makeExcel($water);
  87. $this->sendInstitutionNotice($url,$first);
  88. }
  89. return 'success';
  90. }
  91. public function makeExcel($data)
  92. {
  93. import('phpexcel.PHPExcel', EXTEND_PATH);
  94. $obj = new \PHPExcel();
  95. // 设置 Excel 文件的信息描述信息
  96. $obj->getProperties()->setCreator(''); // 设置创建者
  97. $obj->getProperties()->setLastModifiedBy(''); // 设置修改者
  98. $obj->getProperties()->setTitle(''); // 设置标题
  99. $obj->getProperties()->setSubject(''); // 设置主题
  100. $obj->getProperties()->setDescription(''); // 设置描述
  101. $obj->getProperties()->setKeywords(''); // 设置关键词
  102. $obj->getProperties()->setCategory(''); // 设置类型
  103. // 设置当前 sheet
  104. $obj->setActiveSheetIndex(0);
  105. // 设置当前 sheet 的名称
  106. $obj->getActiveSheet()->setTitle('问卷流水');
  107. // 列标
  108. $list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
  109. // 填充第一行数据
  110. $obj->getActiveSheet()
  111. ->setCellValue($list[0] . '1', '问卷')
  112. ->setCellValue($list[1] . '1', '价格(分)')
  113. ->setCellValue($list[2] . '1', '支付时间')
  114. ->setCellValue($list[3] . '1', '微信昵称');
  115. // 填充第 n 行数据
  116. $length = count($data);
  117. for ($i = 0; $i < $length; $i++) {
  118. $obj->getActiveSheet()->setCellValue($list[0] . ($i + 2), $data[$i]['questionnaire_name'], \PHPExcel_Cell_DataType::TYPE_STRING); // 设置为文本格式
  119. $obj->getActiveSheet()->setCellValue($list[1] . ($i + 2), $data[$i]['money']);
  120. $obj->getActiveSheet()->setCellValue($list[2] . ($i + 2), $data[$i]['createdAt']);
  121. $obj->getActiveSheet()->setCellValue($list[3] . ($i + 2), $data[$i]['wx_nickname']);
  122. }
  123. // 设置加粗和左对齐
  124. foreach ($list as $col) {
  125. // 设置第一行加粗
  126. $obj->getActiveSheet()->getStyle($col . '1')->getFont()->setBold(true);
  127. // 设置第 1-n 行,左对齐
  128. for ($i = 1; $i <= $length + 1; $i++) {
  129. $obj->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  130. }
  131. }
  132. // 设置列宽
  133. $obj->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  134. $obj->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  135. $obj->getActiveSheet()->getColumnDimension('C')->setWidth(30);
  136. $obj->getActiveSheet()->getColumnDimension('D')->setWidth(40);
  137. // 检查目标目录
  138. $targetDir = "excel"; // 替换为你的目标目录
  139. if (!is_dir($targetDir)) {
  140. mkdir($targetDir, 0777, true); // 如果目录不存在,则创建目录
  141. }
  142. if (!is_writable($targetDir)) {
  143. chmod($targetDir, 0777); // 确保目录可写
  144. }
  145. // 生成文件名
  146. $name = time() . '.xlsx';
  147. $filePath = $targetDir . '/' . $name;
  148. // 创建 Excel2007 写入器
  149. $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
  150. // 保存到指定路径
  151. $objWriter->save($filePath);
  152. return $filePath;
  153. }
  154. public function sendInstitutionNotice($sendUrl,$date)
  155. {
  156. $base_url = "https://oapi.dingtalk.com/robot/send?access_token=f40711574780111ea157cdcbc6f81e267a7a6869ab257a1ff67beebcebc305a9";
  157. $time = time()* 1000;
  158. $secret = 'SEC39d1d634cda3a428884a98395f40ffe8e7ef43b9546eee23be552010dab78236';
  159. $m = $time."\n".$secret;
  160. $s = hash_hmac('sha256', $m, $secret, true);
  161. $a = base64_encode($s);
  162. $b = urlencode($a);
  163. $url = $base_url . "&timestamp=$time&sign=$b";
  164. $month = date('Ym',strtotime($date));
  165. $data = [
  166. 'msgtype' => 'link',
  167. "link"=>[
  168. "text"=> $month."月的问卷流水",
  169. "title"=> $month."月的问卷流水",
  170. "messageUrl"=> "https://rismanage3.pacsonline.cn/".$sendUrl
  171. ],
  172. // 'at'=>['atMobiles'=>['15210490031']]
  173. ];
  174. $data_string = json_encode($data);
  175. $res = $this->request_by_curl($url, $data_string);
  176. return $res;
  177. }
  178. function request_by_curl($remote_server, $post_string,$header = array ('Content-Type: application/json;charset=utf-8')) {
  179. $ch = curl_init();
  180. curl_setopt($ch, CURLOPT_URL, $remote_server);
  181. curl_setopt($ch, CURLOPT_POST, 1);
  182. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  183. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  184. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  185. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  186. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  187. // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  188. // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  189. $data = curl_exec($ch);
  190. curl_close($ch);
  191. return $data;
  192. }
  193. }