SystemStore.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * @return string|void
  100. */
  101. public function select_address()
  102. {
  103. $key = sys_config('tengxun_map_key');
  104. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  105. $this->assign(compact('key'));
  106. return $this->fetch();
  107. }
  108. /**
  109. * 保存修改门店信息
  110. * @param int $id
  111. */
  112. public function save($id = 0)
  113. {
  114. $data = UtilService::postMore([
  115. ['name', ''],
  116. ['introduction', ''],
  117. ['image', ''],
  118. ['phone', ''],
  119. ['address', ''],
  120. ['detailed_address', ''],
  121. ['latlng', ''],
  122. ['valid_time', []],
  123. ['day_time', []],
  124. ['cate_id', '']
  125. ]);
  126. SystemStoreModel::beginTrans();
  127. try {
  128. $data['address'] = implode(',', $data['address']);
  129. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  130. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return JsonService::fail('请选择门店位置');
  131. $data['latitude'] = $data['latlng'][0];
  132. $data['longitude'] = $data['latlng'][1];
  133. $data['valid_time'] = implode(' - ', $data['valid_time']);
  134. $data['day_time'] = implode(' - ', $data['day_time']);
  135. unset($data['latlng']);
  136. if ($data['image'] && strstr($data['image'], 'http') === false) {
  137. $site_url = sys_config('site_url');
  138. $data['image'] = $site_url . $data['image'];
  139. }
  140. if ($id) {
  141. if (SystemStoreModel::where('id', $id)->update($data)) {
  142. SystemStoreModel::commitTrans();
  143. return JsonService::success('修改成功');
  144. } else {
  145. SystemStoreModel::rollbackTrans();
  146. return JsonService::fail('修改失败或者您没有修改什么!');
  147. }
  148. } else {
  149. $data['add_time'] = time();
  150. $data['is_show'] = 1;
  151. if ($res = SystemStoreModel::create($data)) {
  152. SystemStoreModel::commitTrans();
  153. return JsonService::success('保存成功', ['id' => $res->id]);
  154. } else {
  155. SystemStoreModel::rollbackTrans();
  156. return JsonService::fail('保存失败!');
  157. }
  158. }
  159. } catch (\Exception $e) {
  160. SystemStoreModel::rollbackTrans();
  161. return JsonService::fail($e->getMessage());
  162. }
  163. }
  164. /**
  165. * 上传店内图片
  166. * @param $id
  167. * @return string
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function up($id)
  173. {
  174. if (!$id) Json::fail('数据不存在');
  175. $data = SystemStoreModel::where('id', $id)->find();
  176. $f = [];
  177. if ($data['slider_image'] and $data['gatehead']){
  178. $f[] = Form::frameImages('gatehead', '门头图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'gatehead')), json_decode($data->getData('slider_image'), 1))->maxLength(5)->icon('images')->width('100%')->height('500px');
  179. $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');
  180. }else{
  181. $f[] = Form::frameImages('gatehead', '门头图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'gatehead')))->maxLength(5)->icon('images')->width('100%')->height('500px');
  182. $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');
  183. }
  184. $f[] = Form::hidden('id', $id);
  185. $form = Form::make_post_form('修改', $f, Url::buildUrl('image_save', compact('id')));
  186. $this->assign(compact('form'));
  187. return $this->fetch('public/form-builder');
  188. }
  189. /**
  190. * 添加接口
  191. * @return void
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function image_save()
  197. {
  198. $data = UtilService::postMore([
  199. ['gatehead', ''],
  200. ['slider_image', ''],
  201. ['id']
  202. ]);
  203. if (empty($data['gatehead']) or empty($data['slider_image'])) return JsonService::fail('门头图或店内图不能为空');
  204. $store = SystemStoreModel::find($data['id']);
  205. $store['gatehead'] = json_encode($data['gatehead']);
  206. $store['slider_image'] = json_encode($data['slider_image']);
  207. $res = $store->save();
  208. if ($res) return JsonService::success('上传成功');
  209. return JsonService::fail('上传失败');
  210. }
  211. }