SystemStore.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAdmin;
  5. use app\models\system\SystemStoreStock;
  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. $admininfo = $this->adminInfo;
  54. $store = SystemStoreModel::getStoreDispose($id);
  55. $type = $admininfo['type'];
  56. $where = UtilService::getMore([
  57. ['level', bcadd($admininfo->level, 1, 0)],
  58. ['type', 1],
  59. ]);
  60. $admin_id = $this->adminId;
  61. $company = SystemAdmin::where('level', bcadd($admininfo->level, 1, 0))->where('type', 1)->field('id,real_name')->select();
  62. $this->assign(compact('store', 'type', 'company', 'admin_id'));
  63. return $this->fetch();
  64. }
  65. /**
  66. * 删除恢复门店
  67. * @param $id
  68. */
  69. public function delete($id)
  70. {
  71. if (!$id) return $this->failed('数据不存在');
  72. if (!SystemStoreModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  73. if (SystemStoreModel::be(['id' => $id, 'is_del' => 1])) {
  74. $data['is_del'] = 0;
  75. if (!SystemStoreModel::edit($data, $id))
  76. return Json::fail(SystemStoreModel::getErrorInfo('恢复失败,请稍候再试!'));
  77. else
  78. return Json::successful('恢复门店成功!');
  79. } else {
  80. $data['is_del'] = 1;
  81. if (!SystemStoreModel::edit($data, $id))
  82. return Json::fail(SystemStoreModel::getErrorInfo('删除失败,请稍候再试!'));
  83. else
  84. return Json::successful('删除门店成功!');
  85. }
  86. }
  87. /**
  88. * 设置单个门店是否显示
  89. * @param string $is_show
  90. * @param string $id
  91. * @return json
  92. */
  93. public function set_show($is_show = '', $id = '')
  94. {
  95. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  96. $res = SystemStoreModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  97. if ($res) {
  98. return JsonService::successful($is_show == 1 ? '设置显示成功' : '设置隐藏成功');
  99. } else {
  100. return JsonService::fail($is_show == 1 ? '设置显示失败' : '设置隐藏失败');
  101. }
  102. }
  103. /**
  104. * 位置选择
  105. * @return string|void
  106. */
  107. public function select_address()
  108. {
  109. $key = sys_config('tengxun_map_key');
  110. if (!$key) return $this->failed('请前往设置->物流设置->物流配置 配置腾讯地图KEY', '#');
  111. $this->assign(compact('key'));
  112. return $this->fetch();
  113. }
  114. /**
  115. * 保存修改门店信息
  116. * @param int $id
  117. */
  118. public function save($id = 0)
  119. {
  120. $data = UtilService::postMore([
  121. ['name', ''],
  122. ['introduction', ''],
  123. ['image', ''],
  124. ['phone', ''],
  125. ['address', ''],
  126. ['detailed_address', ''],
  127. ['latlng', ''],
  128. ['valid_time', []],
  129. ['day_time', []],
  130. ['terminal_number', ''],
  131. ['admin_id', $this->adminId],
  132. ['uid', 0],
  133. ]);
  134. SystemStoreModel::beginTrans();
  135. try {
  136. $data['address'] = implode(',', $data['address']);
  137. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  138. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return JsonService::fail('请选择门店位置');
  139. $data['latitude'] = $data['latlng'][0];
  140. $data['longitude'] = $data['latlng'][1];
  141. $data['valid_time'] = implode(' - ', $data['valid_time']);
  142. $data['day_time'] = implode(' - ', $data['day_time']);
  143. unset($data['latlng']);
  144. if ($data['image'] && strstr($data['image'], 'http') === false) {
  145. $site_url = sys_config('site_url');
  146. $data['image'] = $site_url . $data['image'];
  147. }
  148. if ($id) {
  149. $data['admin_id'] = intval($data['admin_id']);
  150. if (SystemStoreModel::where('id', $id)->update($data)) {
  151. SystemStoreModel::commitTrans();
  152. return JsonService::success('修改成功');
  153. } else {
  154. SystemStoreModel::rollbackTrans();
  155. return JsonService::fail('修改失败或者您没有修改什么!');
  156. }
  157. } else {
  158. $data['add_time'] = time();
  159. $data['is_show'] = 1;
  160. if ($res = SystemStoreModel::create($data)) {
  161. SystemStoreModel::commitTrans();
  162. return JsonService::success('保存成功', ['id' => $res->id]);
  163. } else {
  164. SystemStoreModel::rollbackTrans();
  165. return JsonService::fail('保存失败!');
  166. }
  167. }
  168. } catch (\Exception $e) {
  169. SystemStoreModel::rollbackTrans();
  170. return JsonService::fail($e->getMessage());
  171. }
  172. }
  173. public function init()
  174. {
  175. $store_id = $this->request->get('store_id');
  176. SystemStoreStock::store_init($store_id, 1);
  177. JsonService::success('保存成功');
  178. }
  179. }