Appindex.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 || $params['showType']==4 ){
  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]['type']=1;
  76. $new[$k]['oldPrice']=$v['price'];
  77. $new[$k]['nowPrice']=number_format($v['final_price'], 2);
  78. $new[$k]['sale_num']=$v['sold'];
  79. $new[$k]['sort_order']=$k+1;
  80. $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
  81. }
  82. Db::name('appindexdes')->insertAll($new);
  83. }else{
  84. Db::rollback();
  85. $this->error('该分类没产品!');
  86. }
  87. }
  88. Db::commit();
  89. } catch (ValidateException|PDOException|Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. if ($result === false) {
  94. $this->error(__('No rows were inserted'));
  95. }
  96. $this->success();
  97. }
  98. /**
  99. * 编辑
  100. *
  101. * @param $ids
  102. * @return string
  103. * @throws DbException
  104. * @throws \think\Exception
  105. */
  106. public function edit($ids = null)
  107. {
  108. $row = $this->model->get($ids);
  109. if (!$row) {
  110. $this->error(__('No Results were found'));
  111. }
  112. $adminIds = $this->getDataLimitAdminIds();
  113. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  114. $this->error(__('You have no permission'));
  115. }
  116. if (false === $this->request->isPost()) {
  117. $this->view->assign('row', $row);
  118. return $this->view->fetch();
  119. }
  120. $params = $this->request->post('row/a');
  121. if (empty($params)) {
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. $params = $this->preExcludeFields($params);
  125. $result = false;
  126. Db::startTrans();
  127. try {
  128. //是否采用模型验证
  129. if ($this->modelValidate) {
  130. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  131. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  132. $row->validateFailException()->validate($validate);
  133. }
  134. $result = $row->allowField(true)->save($params);
  135. if($row['showType']==1 ||$params['showType']==4){
  136. Db::name('appindexdes')->where('pid',$ids)->delete();
  137. $send =new Coupon();
  138. $re = $send->getProduct($params['link']);
  139. $product=array();
  140. if(isset($re['status'])&&$re['status']==1){
  141. $product =$re['data']['product'];
  142. }
  143. if($product){
  144. $new=array();
  145. foreach ($product as $k=>$v){
  146. $new[$k]['pid']=$ids;
  147. $new[$k]['title']=$v['name'];
  148. $new[$k]['link']=$v['entity_id'];
  149. $new[$k]['type']=1;
  150. $new[$k]['oldPrice']=$v['price'];
  151. $new[$k]['nowPrice']=number_format($v['final_price'], 2);
  152. $new[$k]['sale_num']=$v['sold'];
  153. $new[$k]['sort_order']=$k+1;
  154. $new[$k]['images']='https://www.bilisar.com/media/catalog/product'.$v['image'];
  155. }
  156. Db::name('appindexdes')->insertAll($new);
  157. }else{
  158. Db::rollback();
  159. $this->error('该分类没产品!');
  160. }
  161. }
  162. Db::commit();
  163. } catch (ValidateException|PDOException|Exception $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. }
  167. if (false === $result) {
  168. $this->error(__('No rows were updated'));
  169. }
  170. $this->success();
  171. }
  172. }