SystemStore.php 6.5 KB

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