Appindex.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\common\model\Coupon;
  9. /**
  10. *
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Appindex extends Backend
  15. {
  16. /**
  17. * Appindex模型对象
  18. * @var \app\admin\model\Appindex
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Appindex;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 添加
  34. *
  35. * @return string
  36. * @throws \think\Exception
  37. */
  38. public function add()
  39. {
  40. if (false === $this->request->isPost()) {
  41. return $this->view->fetch();
  42. }
  43. $params = $this->request->post('row/a');
  44. if (empty($params)) {
  45. $this->error(__('Parameter %s can not be empty', ''));
  46. }
  47. $params = $this->preExcludeFields($params);
  48. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  49. $params[$this->dataLimitField] = $this->auth->id;
  50. }
  51. $result = false;
  52. Db::startTrans();
  53. try {
  54. //是否采用模型验证
  55. if ($this->modelValidate) {
  56. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  57. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  58. $this->model->validateFailException()->validate($validate);
  59. }
  60. //$result = $this->model->allowField(true)->save($params);
  61. $result = Db::name('appindex')->insertGetId($params);
  62. if($params['showType']==1){
  63. $send =new Coupon();
  64. $re = $send->getProduct($params['link']);
  65. $product=array();
  66. if(isset($re['status'])&&$re['status']==1){
  67. $product =$re['data']['product'];
  68. }
  69. if($product){
  70. $new=array();
  71. foreach ($product as $k=>$v){
  72. $new[$k]['pid']=$result;
  73. $new[$k]['title']=$v['name'];
  74. $new[$k]['link']=$v['entity_id'];
  75. $new[$k]['oldPrice']=$v['price'];
  76. $new[$k]['nowPrice']=number_format($v['final_price'], 2);
  77. $new[$k]['sale_num']=$v['sold'];
  78. $new[$k]['sort_order']=$k+1;
  79. $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
  80. }
  81. Db::name('appindexdes')->insertAll($new);
  82. }else{
  83. Db::rollback();
  84. $this->error('该分类没产品!');
  85. }
  86. }
  87. Db::commit();
  88. } catch (ValidateException|PDOException|Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result === false) {
  93. $this->error(__('No rows were inserted'));
  94. }
  95. $this->success();
  96. }
  97. /**
  98. * 编辑
  99. *
  100. * @param $ids
  101. * @return string
  102. * @throws DbException
  103. * @throws \think\Exception
  104. */
  105. public function edit($ids = null)
  106. {
  107. $row = $this->model->get($ids);
  108. if (!$row) {
  109. $this->error(__('No Results were found'));
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  113. $this->error(__('You have no permission'));
  114. }
  115. if (false === $this->request->isPost()) {
  116. $this->view->assign('row', $row);
  117. return $this->view->fetch();
  118. }
  119. $params = $this->request->post('row/a');
  120. if (empty($params)) {
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException()->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. if($row['showType']==1){
  135. Db::name('appindexdes')->where('pid',$ids)->delete();
  136. $send =new Coupon();
  137. $re = $send->getProduct($params['link']);
  138. $product=array();
  139. if(isset($re['status'])&&$re['status']==1){
  140. $product =$re['data']['product'];
  141. }
  142. if($product){
  143. $new=array();
  144. foreach ($product as $k=>$v){
  145. $new[$k]['pid']=$ids;
  146. $new[$k]['title']=$v['name'];
  147. $new[$k]['link']=$v['entity_id'];
  148. $new[$k]['oldPrice']=$v['price'];
  149. $new[$k]['nowPrice']=number_format($v['final_price'], 2);
  150. $new[$k]['sale_num']=$v['sold'];
  151. $new[$k]['sort_order']=$k+1;
  152. $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
  153. }
  154. Db::name('appindexdes')->insertAll($new);
  155. }else{
  156. Db::rollback();
  157. $this->error('该分类没产品!');
  158. }
  159. }
  160. Db::commit();
  161. } catch (ValidateException|PDOException|Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. if (false === $result) {
  166. $this->error(__('No rows were updated'));
  167. }
  168. $this->success();
  169. }
  170. }