海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\Product;
  4. use App\Common\ProductStatus;
  5. use App\Models\AgentProduct;
  6. use App\Models\AgentProductItem;
  7. use App\Models\AgentSetting;
  8. use App\Models\Category;
  9. use Dcat\Admin\Admin;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Form\NestedForm;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Http\Controllers\AdminController;
  15. use Dcat\Admin\Widgets\Alert;
  16. use Dcat\Admin\Widgets\Card;
  17. use Dcat\Admin\Widgets\Table;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. class ProductController extends AdminController
  21. {
  22. protected $title = '产品';
  23. /**
  24. * Make a grid builder.
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
  31. $type = request()->input('type');
  32. if (isset($type) && isset(admin_trans('product.options.publish_type')[$type])) {
  33. $grid->model()->where(['supplier_id' => Admin::user()->id, 'type' => $type]);
  34. Admin::script('$(function(){
  35. $(".pull-right a").each(function() {
  36. var href = $(this).attr("href");
  37. $(this).attr("href", href + "?type='.$type.'")
  38. });
  39. });');
  40. } else {
  41. $grid->model()->where('supplier_id', Admin::user()->id);
  42. }
  43. $grid->column('id')->sortable();
  44. $grid->column('category.name', '产品分类');
  45. $grid->column('title');
  46. $grid->column('picture')->image('', 60, 60);
  47. $grid->column('price');
  48. $grid->column('original_price');
  49. $grid->column('stock');
  50. $grid->column('sale');
  51. $grid->column('status')->using(ProductStatus::array());
  52. $grid->column('verify_mobile','核销员手机');
  53. $grid->column('created_at');
  54. $grid->column('updated_at');
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->panel();
  57. $filter->equal('id')->width(2);
  58. });
  59. });
  60. }
  61. /**
  62. * Make a show builder.
  63. *
  64. * @param mixed $id
  65. *
  66. * @return Show
  67. */
  68. protected function detail($id)
  69. {
  70. return Show::make($id, new Product(), function (Show $show) {
  71. Admin::script('$(function(){
  72. $(".pull-right a").each(function() {
  73. var href = $(this).attr("href");
  74. $(this).attr("href", href + "?type='.request()->input('type').'")
  75. });
  76. });');
  77. $show->field('id');
  78. $show->field('supplier_id');
  79. $show->field('category_id');
  80. $show->field('title');
  81. $show->field('price');
  82. $show->field('original_price');
  83. $show->field('pictures')->image('', 80, 80);
  84. $show->field('stock');
  85. $show->field('sale');
  86. $show->field('status');
  87. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  88. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  89. $show->field('verify_mobile','核销员手机');
  90. $show->field('extends', '附加信息')
  91. ->unescape()
  92. ->as(function ($v) {
  93. $html = '';
  94. $arr = [
  95. 'project' => '服务项目',
  96. 'date' => '时间',
  97. 'tags' => '包含项目',
  98. 'open_time' => '开放时间',
  99. 'package' => '包含套餐',
  100. ];
  101. foreach ($v as $k => $item) {
  102. $html .= new Card(Table::make([$arr[$k] ?? '项目'], $item));
  103. }
  104. return $html;
  105. });
  106. $show->field('created_at');
  107. $show->field('updated_at');
  108. });
  109. }
  110. /**
  111. * Make a form builder.
  112. *
  113. * @return Form
  114. */
  115. protected function form()
  116. {
  117. Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true);
  118. return Form::make(new Product(), function (Form $form) {
  119. Admin::script('$(function(){
  120. $(".pull-right a").each(function() {
  121. var href = $(this).attr("href");
  122. $(this).attr("href", href + "?type='.request()->input('type').'")
  123. });
  124. });');
  125. //不允许编辑非自己数据
  126. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  127. return $form->response()->error('数据不存在');
  128. }
  129. $type = request()->input('type');
  130. $form->display('id');
  131. $form->hidden('type')->value($type);
  132. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  133. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  134. $form->text('title')->required();
  135. $form->text('price')->required();
  136. $form->text('original_price')->required();
  137. $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
  138. $form->text('stock')->default(99999)->required();
  139. $form->editor('know');
  140. $form->editor('content')->required();
  141. $form->text('verify_mobile','核销员手机');
  142. //扩展字段
  143. if ($type == 0) { //旅游线路
  144. $form->table('extends.project', '包含项目', function (NestedForm $table) {
  145. $table->text('name', '项目名称');
  146. $table->text('num', '数量');
  147. $table->text('price', '费用');
  148. });
  149. $form->dateRange('extends.date.start', 'extends.date.end', '行程时间');
  150. } else if ($type == 1) { //酒店
  151. $default = ['行李寄存', '24小时前台', '前台保险柜', '唤醒服务', '早餐', '送餐服务', '电梯', '空调',
  152. '新风系统', '24小时热水', '吹风机', '加湿器', '自动售货机', '健身房', '桌球室', '洗衣服务'];
  153. $form->table('extends.tags', '酒店设施', function (NestedForm $table) {
  154. $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
  155. });
  156. } else if ($type == 2) { //景区
  157. $form->table('extends.open_time', '开放时间', function (NestedForm $table) {
  158. $table->text('week', '星期')->placeholder('如:周一至周五');
  159. $table->text('section', '时段')->placeholder('如:上午/下午');
  160. $table->text('time', '时间')->placeholder('如:08:00~18:00');
  161. });
  162. $form->table('extends.project', '包含项目', function (NestedForm $table) {
  163. $table->text('name', '项目名称');
  164. $table->text('num', '数量');
  165. $table->text('price', '费用');
  166. });
  167. } else if ($type == 3) { //餐厅
  168. $form->table('extends.open_time', '开放时间', function (NestedForm $table) {
  169. $table->text('week', '星期')->placeholder('如:周一至周五');
  170. $table->text('section', '时段')->placeholder('如:上午/下午');
  171. $table->text('time', '时间')->placeholder('如:08:00~18:00');
  172. });
  173. $form->table('extends.package', '包含套餐', function (NestedForm $table) {
  174. $table->text('name', '名称')->placeholder('如:清蒸鱿鱼');
  175. $table->text('num', '数量')->placeholder('如:1条');
  176. $table->text('price', '价格')->placeholder('如:99元');
  177. });
  178. }
  179. if ($form->isEditing()) {
  180. $form->confirm('提示', '修改标题、价格、产品图片、旅游须知、产品详情需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
  181. }
  182. })->creating(function (Form $form) {
  183. $type = request()->input('type');
  184. if ($form->isCreating()) {
  185. if ($type == null) {
  186. Admin::exit('请选择要发布的产品类型');
  187. }
  188. if (!Admin::user()->publish_type || !in_array($type, Admin::user()->publish_type)) {
  189. Admin::exit(Alert::make('对不起,你没有此类产品的发布权限', '权限不足')->danger());
  190. }
  191. }
  192. })->saving(function (Form $form) {
  193. //不允许编辑非自己数据
  194. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  195. return $form->response()->error('数据不存在');
  196. }
  197. $type = $form->isCreating() ? $form->type : $form->model()->type;
  198. if (!Admin::user()->publish_type || !in_array($type, Admin::user()->publish_type)) {
  199. return $form->response()->error('对不起,你没有此类产品的发布、编辑权限');
  200. }
  201. //不允许编辑的字段,忽略字段不起作用?
  202. $form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
  203. //null字段转为''
  204. foreach ($form->input() as $k => $v) {
  205. if (is_null($v)) {
  206. $form->$k = '';
  207. }
  208. }
  209. //特殊字段处理
  210. if ($form->isCreating()) {
  211. $form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
  212. $form->supplier_id = Admin::user()->id;
  213. $form->status = ProductStatus::UNAUDITED;
  214. }
  215. })->saved(function (Form $form, $result) {
  216. if ($form->isEditing() && $result) {
  217. $ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
  218. DB::beginTransaction();
  219. try {
  220. //如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核
  221. if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) {
  222. $form->model()->update(['status' => ProductStatus::UNAUDITED]);
  223. //下架所有代理商产品,未审核的产品,不能同步信息到代理商产品
  224. AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update(['status' => ProductStatus::SOLD_OUT]);
  225. } else {
  226. //不需要审核的修改信息同步信息到代理商产品,注:组合产品不同步
  227. AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
  228. 'title' => $form->title,
  229. 'pictures' => explode(',', $form->pictures),
  230. 'know' => $form->know,
  231. 'content' => $form->content,
  232. ]);
  233. }
  234. DB::commit();
  235. return $form->response()->success('更新成功!')->script('history.go(-1)');
  236. } catch (\Exception $exception) {
  237. DB::rollBack();
  238. return $form->response()->error($exception->getMessage());
  239. }
  240. }
  241. })->deleting(function (Form $form) {
  242. //不允许删除非自己的数据
  243. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  244. return $form->response()->error('数据不存在');
  245. }
  246. });
  247. }
  248. }