SystemStore.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use app\models\user\User;
  5. use app\models\user\UserLevel;
  6. use crmeb\services\JsonService;
  7. use crmeb\services\JsonService as Json;
  8. use app\admin\model\system\SystemStore as SystemStoreModel;
  9. use crmeb\services\UtilService;
  10. /**
  11. * 门店管理控制器
  12. * Class SystemAttachment
  13. * @package app\admin\controller\system
  14. *
  15. */
  16. class SystemStore extends AuthController
  17. {
  18. /**
  19. * 门店列表
  20. */
  21. public function list()
  22. {
  23. $where = UtilService::getMore([
  24. ['page', 1],
  25. ['limit', 20],
  26. ['name', ''],
  27. ['excel', 0],
  28. ['type', $this->request->param('type')]
  29. ]);
  30. return JsonService::successlayui(SystemStoreModel::getStoreList($where));
  31. }
  32. /**
  33. * 门店设置
  34. * @return string
  35. */
  36. public function index()
  37. {
  38. $type = $this->request->param('type');
  39. $show = SystemStoreModel::where('is_show', 1)->where('is_del', 0)->count();//显示中的门店
  40. $hide = SystemStoreModel::where('is_show', 0)->count();//隐藏的门店
  41. $recycle = SystemStoreModel::where('is_del', 1)->count();//删除的门店
  42. if ($type == null) $type = 1;
  43. $this->assign(compact('type', 'show', 'hide', 'recycle'));
  44. return $this->fetch();
  45. }
  46. /**
  47. * 门店添加
  48. * @param int $id
  49. * @return string
  50. */
  51. public function add($id = 0)
  52. {
  53. $store = SystemStoreModel::getStoreDispose($id);
  54. $this->assign(compact('store'));
  55. return $this->fetch();
  56. }
  57. /**
  58. * 删除恢复门店
  59. * @param $id
  60. */
  61. public function delete($id)
  62. {
  63. if (!$id) return $this->failed('数据不存在');
  64. if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  65. if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
  66. $data['is_del'] = 0;
  67. if (!SystemStoreModel::edit($data, $id))
  68. return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
  69. else
  70. return Json::successful('恢复门店成功!');
  71. } else {
  72. $data['is_del'] = 1;
  73. if (!SystemStoreModel::edit($data, $id))
  74. return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
  75. else
  76. return Json::successful('删除门店成功!');
  77. }
  78. }
  79. /**
  80. * 设置单个门店是否显示
  81. * @param string $is_show
  82. * @param string $id
  83. * @return json
  84. */
  85. public function set_show($is_show = '', $id = '')
  86. {
  87. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  88. $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  89. if ($res) {
  90. $Info = SystemStoreModel::find($id);
  91. if($is_show==1 && $Info['uid']>0)
  92. {
  93. $user = User::find($Info['uid']);
  94. if($user['level']<$Info['level']) UserLevel::setUserLevel($Info['uid'],$Info['level']);
  95. }
  96. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  97. } else {
  98. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  99. }
  100. }
  101. /**
  102. * 位置选择
  103. * @return string|void
  104. */
  105. public function select_address()
  106. {
  107. $key = sys_config('tengxun_map_key');
  108. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  109. $this->assign(compact('key'));
  110. return $this->fetch();
  111. }
  112. /**
  113. * 保存修改门店信息
  114. * @param int $id
  115. */
  116. public function save($id = 0)
  117. {
  118. $data = UtilService::postMore([
  119. ['name', ''],
  120. ['introduction', ''],
  121. ['image', ''],
  122. ['phone', ''],
  123. ['address', ''],
  124. ['detailed_address', ''],
  125. ['latlng', ''],
  126. ['valid_time', []],
  127. ['day_time', []],
  128. ]);
  129. SystemStoreModel::beginTrans();
  130. try {
  131. $data['address'] = implode(',', $data['address']);
  132. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  133. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return JsonService::fail('请选择门店位置');
  134. $data['latitude'] = $data['latlng'][0];
  135. $data['longitude'] = $data['latlng'][1];
  136. $data['valid_time'] = implode(' - ', $data['valid_time']);
  137. $data['day_time'] = implode(' - ', $data['day_time']);
  138. unset($data['latlng']);
  139. if ($data['image'] && strstr($data['image'], 'http') === false) {
  140. $site_url = sys_config('site_url');
  141. $data['image'] = $site_url . $data['image'];
  142. }
  143. if ($id) {
  144. if (SystemStoreModel::where('id', $id)->update($data)) {
  145. SystemStoreModel::commitTrans();
  146. return JsonService::success('修改成功');
  147. } else {
  148. SystemStoreModel::rollbackTrans();
  149. return JsonService::fail('修改失败或者您没有修改什么!');
  150. }
  151. } else {
  152. $data['add_time'] = time();
  153. $data['is_show'] = 1;
  154. if ($res = SystemStoreModel::create($data)) {
  155. SystemStoreModel::commitTrans();
  156. return JsonService::success('保存成功', ['id' => $res->id]);
  157. } else {
  158. SystemStoreModel::rollbackTrans();
  159. return JsonService::fail('保存失败!');
  160. }
  161. }
  162. } catch (\Exception $e) {
  163. SystemStoreModel::rollbackTrans();
  164. return JsonService::fail($e->getMessage());
  165. }
  166. }
  167. }