SystemStore.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\StoreCate;
  5. use crmeb\services\JsonService;
  6. use crmeb\services\JsonService as Json;
  7. use app\admin\model\system\SystemStore as SystemStoreModel;
  8. use crmeb\services\UtilService;
  9. use crmeb\services\FormBuilder as Form;
  10. use think\facade\Route as Url;
  11. /**
  12. * 门店管理控制器
  13. * Class SystemAttachment
  14. * @package app\admin\controller\system
  15. *
  16. */
  17. class SystemStore extends AuthController
  18. {
  19. /**
  20. * 门店列表
  21. */
  22. public function list()
  23. {
  24. $where = UtilService::getMore([
  25. ['page', 1],
  26. ['limit', 20],
  27. ['name', ''],
  28. ['excel', 0],
  29. ['type', $this->request->param('type')]
  30. ]);
  31. return JsonService::successlayui(SystemStoreModel::getStoreList($where));
  32. }
  33. /**
  34. * 门店设置
  35. * @return string
  36. */
  37. public function index()
  38. {
  39. $type = $this->request->param('type');
  40. $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  41. $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
  42. $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
  43. if ($type == null) $type = 1;
  44. $this->assign(compact('type', 'show', 'hide', 'recycle'));
  45. return $this->fetch();
  46. }
  47. /**
  48. * 门店添加
  49. * @param int $id
  50. * @return string
  51. */
  52. public function add($id = 0)
  53. {
  54. $store = SystemStoreModel::getStoreDispose($id);
  55. $cate = StoreCate::select();
  56. $this->assign(compact('store', 'cate'));
  57. return $this->fetch();
  58. }
  59. /**
  60. * 删除恢复门店
  61. * @param $id
  62. */
  63. public function delete($id)
  64. {
  65. if (!$id) return $this->failed('数据不存在');
  66. if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  67. if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
  68. $data['is_del'] = 0;
  69. if (!SystemStoreModel::edit($data, $id))
  70. return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
  71. else
  72. return Json::successful('恢复门店成功!');
  73. } else {
  74. $data['is_del'] = 1;
  75. if (!SystemStoreModel::edit($data, $id))
  76. return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
  77. else
  78. return Json::successful('删除门店成功!');
  79. }
  80. }
  81. /**
  82. * 设置单个门店是否显示
  83. * @param string $is_show
  84. * @param string $id
  85. * @return json
  86. */
  87. public function set_show($is_show = '', $id = '')
  88. {
  89. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  90. $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  91. if ($res) {
  92. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  93. } else {
  94. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  95. }
  96. }
  97. /**
  98. * 设置单个门店是否显示
  99. * @param string $is_show
  100. * @param string $id
  101. * @return json
  102. */
  103. public function set_period($period = '', $id = '')
  104. {
  105. ($period == '' || $id == '') && JsonService::fail('缺少参数');
  106. $res = SystemStoreModel::where(['id' => $id])->update(['period' => (int)$period]);
  107. if ($res) {
  108. return JsonService::successful($period == 1 ? '设置推荐成功' : '设置取消成功');
  109. } else {
  110. return JsonService::fail($period == 1 ? '设置显示失败' : '设置隐藏失败');
  111. }
  112. }
  113. /**
  114. * 位置选择
  115. * @return string|void
  116. */
  117. public function select_address()
  118. {
  119. $key = sys_config('tengxun_map_key');
  120. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  121. $this->assign(compact('key'));
  122. return $this->fetch();
  123. }
  124. /**
  125. * 保存修改门店信息
  126. * @param int $id
  127. */
  128. public function save($id = 0)
  129. {
  130. $data = UtilService::postMore([
  131. ['name', ''],
  132. ['introduction', ''],
  133. ['image', ''],
  134. ['phone', ''],
  135. ['address', ''],
  136. ['detailed_address', ''],
  137. ['latlng', ''],
  138. ['valid_time', []],
  139. ['day_time', []],
  140. ['cate_id', '']
  141. ]);
  142. SystemStoreModel::beginTrans();
  143. try {
  144. $data['address'] = implode(',', $data['address']);
  145. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  146. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return JsonService::fail('请选择门店位置');
  147. $data['latitude'] = $data['latlng'][0];
  148. $data['longitude'] = $data['latlng'][1];
  149. $data['valid_time'] = implode(' - ', $data['valid_time']);
  150. $data['day_time'] = implode(' - ', $data['day_time']);
  151. unset($data['latlng']);
  152. if ($data['image'] && strstr($data['image'], 'http') === false) {
  153. $site_url = sys_config('site_url');
  154. $data['image'] = $site_url . $data['image'];
  155. }
  156. if ($id) {
  157. if (SystemStoreModel::where('id', $id)->update($data)) {
  158. SystemStoreModel::commitTrans();
  159. return JsonService::success('修改成功');
  160. } else {
  161. SystemStoreModel::rollbackTrans();
  162. return JsonService::fail('修改失败或者您没有修改什么!');
  163. }
  164. } else {
  165. $data['add_time'] = time();
  166. $data['is_show'] = 1;
  167. if ($res = SystemStoreModel::create($data)) {
  168. SystemStoreModel::commitTrans();
  169. return JsonService::success('保存成功', ['id' => $res->id]);
  170. } else {
  171. SystemStoreModel::rollbackTrans();
  172. return JsonService::fail('保存失败!');
  173. }
  174. }
  175. } catch (\Exception $e) {
  176. SystemStoreModel::rollbackTrans();
  177. return JsonService::fail($e->getMessage());
  178. }
  179. }
  180. /**
  181. * 上传店内图片
  182. * @param $id
  183. * @return string
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\DbException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. */
  188. public function up($id)
  189. {
  190. if (!$id) Json::fail('数据不存在');
  191. $data = SystemStoreModel::where('id', $id)->find();
  192. $f = [];
  193. if ($data['slider_image'] and $data['gatehead']){
  194. $f[] = Form::frameImages('gatehead', '门头图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'gatehead')), json_decode($data->getData('gatehead'), 1))->maxLength(5)->icon('images')->width('100%')->height('500px');
  195. $f[] = Form::frameImages('slider_image', '店内图片(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), json_decode($data->getData('slider_image'), 1))->maxLength(5)->icon('images')->width('100%')->height('500px');
  196. }else{
  197. $f[] = Form::frameImages('gatehead', '门头图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'gatehead')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  198. $f[] = Form::frameImages('slider_image', '店内图片(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  199. }
  200. $f[] = Form::hidden('id', $id);
  201. $form = Form::make_post_form('修改', $f, Url::buildUrl('image_save', compact('id')));
  202. $this->assign(compact('form'));
  203. return $this->fetch('public/form-builder');
  204. }
  205. /**
  206. * 添加接口
  207. * @return void
  208. * @throws \think\db\exception\DataNotFoundException
  209. * @throws \think\db\exception\DbException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. */
  212. public function image_save()
  213. {
  214. $data = UtilService::postMore([
  215. ['gatehead', ''],
  216. ['slider_image', ''],
  217. ['id']
  218. ]);
  219. if (empty($data['gatehead']) or empty($data['slider_image'])) return JsonService::fail('门头图或店内图不能为空');
  220. $store = SystemStoreModel::find($data['id']);
  221. $store['gatehead'] = json_encode($data['gatehead']);
  222. $store['slider_image'] = json_encode($data['slider_image']);
  223. $res = $store->save();
  224. if ($res) return JsonService::success('上传成功');
  225. return JsonService::fail('上传失败');
  226. }
  227. }