Deal.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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($website=='asteriawigs'){
  77. $url ='https://www.asteriahair.com/app-api/cache/deleteImageCache';
  78. }
  79. if(empty($url)){
  80. $this->error(__('网站出错'));
  81. }
  82. self::request($url, $data, 'POST');
  83. $this->success();
  84. }
  85. public static function request($url, $params = [], $method = 'GET', $headers = [], $options = [])
  86. {
  87. $ch = curl_init();
  88. // 设置请求方法
  89. $method = strtoupper($method);
  90. // 设置参数
  91. if ($method == 'GET' && !empty($params)) {
  92. $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params);
  93. }
  94. // 设置 URL
  95. curl_setopt($ch, CURLOPT_URL, $url);
  96. // 设置请求方法
  97. if ($method == 'POST') {
  98. curl_setopt($ch, CURLOPT_POST, true);
  99. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  100. } elseif ($method == 'PUT' || $method == 'DELETE') {
  101. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  102. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  103. }
  104. // 设置请求头
  105. if (!empty($headers)) {
  106. $headerArr = [];
  107. foreach ($headers as $key => $value) {
  108. $headerArr[] = $key . ': ' . $value;
  109. }
  110. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  111. }
  112. // 默认选项
  113. $defaultOptions = [
  114. CURLOPT_RETURNTRANSFER => true, // 返回结果而不输出
  115. CURLOPT_HEADER => false, // 不包含响应头
  116. CURLOPT_FOLLOWLOCATION => true, // 跟随重定向
  117. CURLOPT_MAXREDIRS => 10, // 最大重定向次数
  118. CURLOPT_TIMEOUT => 30, // 超时时间(秒)
  119. CURLOPT_CONNECTTIMEOUT => 10, // 连接超时时间
  120. CURLOPT_SSL_VERIFYPEER => false, // 不验证 SSL 证书
  121. CURLOPT_SSL_VERIFYHOST => false, // 不验证主机名
  122. CURLOPT_USERAGENT => 'FastAdmin/' . config('fastadmin.version'), // User-Agent
  123. ];
  124. // 合并选项
  125. foreach ($defaultOptions as $key => $value) {
  126. if (!isset($options[$key])) {
  127. $options[$key] = $value;
  128. }
  129. }
  130. // 设置 cURL 选项
  131. foreach ($options as $key => $value) {
  132. curl_setopt($ch, $key, $value);
  133. }
  134. // 执行请求
  135. $response = curl_exec($ch);
  136. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  137. $error = curl_error($ch);
  138. $errno = curl_errno($ch);
  139. curl_close($ch);
  140. return [
  141. 'success' => $errno === 0 && $response !== false,
  142. 'http_code' => $httpCode,
  143. 'response' => $response,
  144. 'error' => $error,
  145. 'errno' => $errno
  146. ];
  147. }
  148. /**
  149. * 添加
  150. *
  151. * @return string
  152. * @throws \think\Exception
  153. */
  154. public function add()
  155. {
  156. if (false === $this->request->isPost()) {
  157. return $this->view->fetch();
  158. }
  159. $params = $this->request->post('row/a');
  160. if (empty($params)) {
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. $params = $this->preExcludeFields($params);
  164. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  165. $params[$this->dataLimitField] = $this->auth->id;
  166. }
  167. $params['fid']=session('fid');
  168. $result = false;
  169. Db::startTrans();
  170. try {
  171. //是否采用模型验证
  172. if ($this->modelValidate) {
  173. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  174. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  175. $this->model->validateFailException()->validate($validate);
  176. }
  177. $result = $this->model->allowField(true)->save($params);
  178. Db::commit();
  179. } catch (ValidateException|PDOException|Exception $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. }
  183. if ($result === false) {
  184. $this->error(__('No rows were inserted'));
  185. }
  186. $this->success();
  187. }
  188. }