Deal.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\admin\controller\functional;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 功能详情
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Deal extends Backend
  13. {
  14. /**
  15. * Deal模型对象
  16. * @var \app\admin\model\functional\Deal
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\functional\Deal;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. public function index()
  31. {
  32. $ra_id=input('ids');
  33. if(empty($ra_id)){
  34. $ra_id=session('fid');
  35. }else{
  36. session('fid',$ra_id);
  37. }
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. //如果发送的来源是 Selectpage,则转发到 Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  48. $dts['fid'] =$ra_id;
  49. $list = $this->model
  50. ->where($where)
  51. ->where($dts)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. public function deletecache()
  58. {
  59. $ra_id=input('ids');
  60. if(empty($ra_id)){
  61. $ra_id=session('xids');
  62. }else{
  63. session('xids',$ra_id);
  64. }
  65. $model = new \app\admin\model\functional\Fal;
  66. $row = $model->get($ra_id);
  67. $data['name']=$row->name;
  68. $website=$row->website;
  69. $url ='';
  70. if($website=='alipearlhair'){
  71. $url ='https://www.alipearlhair.com/app-api/cache/deleteImageCache';
  72. }
  73. if($website=='wigginshair'){
  74. $url ='https://www.wigginshair.com/app-api/cache/deleteImageCache';
  75. }
  76. if(empty($url)){
  77. $this->error(__('网站出错'));
  78. }
  79. self::request($url, $data, 'POST');
  80. $this->success();
  81. }
  82. public static function request($url, $params = [], $method = 'GET', $headers = [], $options = [])
  83. {
  84. $ch = curl_init();
  85. // 设置请求方法
  86. $method = strtoupper($method);
  87. // 设置参数
  88. if ($method == 'GET' && !empty($params)) {
  89. $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params);
  90. }
  91. // 设置 URL
  92. curl_setopt($ch, CURLOPT_URL, $url);
  93. // 设置请求方法
  94. if ($method == 'POST') {
  95. curl_setopt($ch, CURLOPT_POST, true);
  96. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  97. } elseif ($method == 'PUT' || $method == 'DELETE') {
  98. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  99. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  100. }
  101. // 设置请求头
  102. if (!empty($headers)) {
  103. $headerArr = [];
  104. foreach ($headers as $key => $value) {
  105. $headerArr[] = $key . ': ' . $value;
  106. }
  107. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  108. }
  109. // 默认选项
  110. $defaultOptions = [
  111. CURLOPT_RETURNTRANSFER => true, // 返回结果而不输出
  112. CURLOPT_HEADER => false, // 不包含响应头
  113. CURLOPT_FOLLOWLOCATION => true, // 跟随重定向
  114. CURLOPT_MAXREDIRS => 10, // 最大重定向次数
  115. CURLOPT_TIMEOUT => 30, // 超时时间(秒)
  116. CURLOPT_CONNECTTIMEOUT => 10, // 连接超时时间
  117. CURLOPT_SSL_VERIFYPEER => false, // 不验证 SSL 证书
  118. CURLOPT_SSL_VERIFYHOST => false, // 不验证主机名
  119. CURLOPT_USERAGENT => 'FastAdmin/' . config('fastadmin.version'), // User-Agent
  120. ];
  121. // 合并选项
  122. foreach ($defaultOptions as $key => $value) {
  123. if (!isset($options[$key])) {
  124. $options[$key] = $value;
  125. }
  126. }
  127. // 设置 cURL 选项
  128. foreach ($options as $key => $value) {
  129. curl_setopt($ch, $key, $value);
  130. }
  131. // 执行请求
  132. $response = curl_exec($ch);
  133. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  134. $error = curl_error($ch);
  135. $errno = curl_errno($ch);
  136. curl_close($ch);
  137. return [
  138. 'success' => $errno === 0 && $response !== false,
  139. 'http_code' => $httpCode,
  140. 'response' => $response,
  141. 'error' => $error,
  142. 'errno' => $errno
  143. ];
  144. }
  145. /**
  146. * 添加
  147. *
  148. * @return string
  149. * @throws \think\Exception
  150. */
  151. public function add()
  152. {
  153. if (false === $this->request->isPost()) {
  154. return $this->view->fetch();
  155. }
  156. $params = $this->request->post('row/a');
  157. if (empty($params)) {
  158. $this->error(__('Parameter %s can not be empty', ''));
  159. }
  160. $params = $this->preExcludeFields($params);
  161. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  162. $params[$this->dataLimitField] = $this->auth->id;
  163. }
  164. $params['fid']=session('fid');
  165. $result = false;
  166. Db::startTrans();
  167. try {
  168. //是否采用模型验证
  169. if ($this->modelValidate) {
  170. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  171. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  172. $this->model->validateFailException()->validate($validate);
  173. }
  174. $result = $this->model->allowField(true)->save($params);
  175. Db::commit();
  176. } catch (ValidateException|PDOException|Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. if ($result === false) {
  181. $this->error(__('No rows were inserted'));
  182. }
  183. $this->success();
  184. }
  185. }