Shop.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\controller\service\shop;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 商家管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Shop extends Backend
  12. {
  13. /**
  14. * Shop模型对象
  15. * @var \app\admin\model\service\shop\Shop
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\service\shop\Shop;
  22. $this->view->assign("toShopList", $this->model->getToShopList());
  23. $this->view->assign("typeList", $this->model->getTypeList());
  24. $this->view->assign("stateList", $this->model->getStateList());
  25. }
  26. public function index()
  27. {
  28. //设置过滤方法
  29. $this->request->filter(['strip_tags', 'trim']);
  30. if (false === $this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. //如果发送的来源是 Selectpage,则转发到 Selectpage
  34. if ($this->request->request('keyField')) {
  35. return $this->selectpage();
  36. }
  37. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  38. $list = $this->model
  39. ->where($where)
  40. ->order($sort, $order)
  41. ->paginate($limit);
  42. foreach ($list as &$row)
  43. {
  44. $row->categoryname = $row->category_ids?model('app\admin\model\service\Category')->getCategoryList($row->category_ids):'';
  45. $row->goodsname = $row->goods_ids?model('app\admin\model\service\Goods')->getGoodsList($row->goods_ids):'';
  46. $row->shopmoney = \app\admin\model\service\User::where('user_id',$row->user_id)->value('shop_user_money');
  47. $row->already_accept_nums = \app\admin\model\service\order\Order::where(['shop_id'=>$row->id,'status'=>['>',1]])->count();
  48. $row->update_time = $row->updatetime?date("Y-m-d",$row->updatetime):'';
  49. $row->create_time = date("Y-m-d",$row->createtime);
  50. }
  51. $result = ['total' => $list->total(), 'rows' => $list->items()];
  52. return json($result);
  53. }
  54. public function edit($ids = null)
  55. {
  56. $row = $this->model->get($ids);
  57. if (!$row) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $adminIds = $this->getDataLimitAdminIds();
  61. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  62. $this->error(__('You have no permission'));
  63. }
  64. if (false === $this->request->isPost()) {
  65. $row->city = $row->province.'/'.$row->city.'/'.$row->district;
  66. $this->view->assign('row', $row);
  67. return $this->view->fetch();
  68. }
  69. $params = $this->request->post('row/a');
  70. if (empty($params)) {
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. $params = $this->preExcludeFields($params);
  74. if($params['city'])
  75. {
  76. $address = explode('/',$params['city']);
  77. $params['province'] = $address[0];
  78. $params['city'] = $address[1];
  79. $params['district'] = isset($address[2])?:'';
  80. }
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  88. $row->validateFailException()->validate($validate);
  89. }
  90. $result = $row->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if (false === $result) {
  97. $this->error(__('No rows were updated'));
  98. }
  99. $this->success();
  100. }
  101. public function deduct($ids = null)
  102. {
  103. $row = $this->model->get($ids);
  104. if($this->request->isPost())
  105. {
  106. $params = $this->request->post('row/a');
  107. Db::startTrans();
  108. try{
  109. $params['shop_ensure']>$row->ensure_price && $this->error('保证金不足,无法扣除');
  110. \app\api\model\service\Shop::money(-$params['skill_ensure'], $row->user_id, $params['skill_note']);
  111. Db::commit();
  112. $this->success('保证金已扣除');
  113. } catch (Exception $e) {
  114. Db::rollback();
  115. $this->error('保证金扣除失败',$e->getMessage());
  116. }
  117. }
  118. $this->assign(['row'=>$row]);
  119. return $this->view->fetch();
  120. }
  121. }