Store.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/16 23:03
  5. */
  6. namespace app\api\controller;
  7. use app\api\BaseController;
  8. use app\api\model\AdminModel;
  9. use app\api\model\DiscussModel;
  10. use app\api\model\GroupModel;
  11. use app\api\model\StoreModel;
  12. use app\common\until\Until;
  13. use think\Db;
  14. class Store extends BaseController {
  15. /**
  16. * @OA\Get(path="/api/Store/index",
  17. * tags={"门店管理"},
  18. * summary="门店列表",
  19. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  20. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  21. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  22. * @OA\Parameter(name="status", in="query", description="状态 1正常 2闭店 3歇业", @OA\Schema(type="integer",default="1")),
  23. * @OA\Parameter(name="name", in="query", description="门店名称", @OA\Schema(type="string")),
  24. * @OA\Parameter(name="lat", in="query", description="经度", @OA\Schema(type="string")),
  25. * @OA\Parameter(name="lon", in="query", description="纬度", @OA\Schema(type="string")),
  26. * @OA\Parameter(name="brandId", in="query", description="品牌id", @OA\Schema(type="string")),
  27. * @OA\Parameter(name="companyId", in="query", description="公司id", @OA\Schema(type="string")),
  28. * @OA\Parameter(name="groupId", in="query", description="集团id", @OA\Schema(type="string")),
  29. * @OA\Parameter(name="type", in="query", description="类型 1为当前管理人员的门店", @OA\Schema(type="integer")),
  30. * @OA\RequestBody(
  31. * ),
  32. * @OA\Response(response="200", description="请求成功")
  33. * )
  34. */
  35. public function index() {
  36. $input = Until::getInput();
  37. $model = new StoreModel();
  38. $model->setPage($input['page'] ?? 1);
  39. $model->setPageSize($input['pageSize'] ?? 10);
  40. $where = [];
  41. if (!empty($input['status'])) {
  42. $where[] = ['s.status', '=', $input['status']];
  43. }
  44. if (!$this->isAdmin()) {
  45. $where[] = ['s.status', '=', 1];
  46. }
  47. if (!empty($input['name'])) {
  48. $where[] = ['s.store_name', 'like', "%{$input['name']}%"];
  49. }
  50. if (!empty($input['companyId'])) {
  51. $ids = explode(',', $input['companyId']);
  52. $where[] = ['c.id', 'in', $ids];
  53. }
  54. if (!empty($input['brandId'])) {
  55. $ids = explode(',', $input['brandId']);
  56. $where[] = ['b.id', 'in', $ids];
  57. }
  58. if (!empty($input['groupId'])) {
  59. $ids = explode(',', $input['groupId']);
  60. $where[] = ['s.group_id', 'in', $ids];
  61. }
  62. if (!empty($input['type']) && $input['type'] == 1) {
  63. $model->setAdminId($this->adminId);
  64. $where[] = ['sr.admin_id', '=', $this->adminId];
  65. }
  66. $field = '';
  67. if (!empty($input['lon']) && !empty($input['lat'])) {
  68. $squares = Until::returnSquarePoint($input['lon'], $input['lat']);
  69. $where[] = ['s.latitude', '<>', 0];
  70. $where[] = ['s.latitude', '>', $squares['right-bottom']['lat']];
  71. $where[] = ['s.latitude', '<', $squares['left-top']['lat']];
  72. $where[] = ['s.longitude', '>', $squares['left-top']['lng']];
  73. $where[] = ['s.longitude', '<', $squares['right-bottom']['lng']];
  74. $field = "(6378.138 * 2 * asin(sqrt(pow(sin((s.latitude * pi() / 180 - " . $input['lat'] . " * pi() / 180) / 2),2) +
  75. cos(s.latitude * pi() / 180) * cos(" . $input['lat'] . " * pi() / 180) * pow(sin((s.longitude * pi() / 180 - "
  76. . $input['lon'] . " * pi() / 180) / 2),2))) ) as distance";
  77. }
  78. $model->setWhere($where);
  79. $data = $model->getStoreList($field);
  80. foreach ($data['list'] as &$info) {
  81. if (!empty($info['distance'])) {
  82. $info['distanceA'] = $info['distance'];
  83. $info['distance'] = number_format($info['distance'], 2) . "km";
  84. }
  85. }
  86. unset($info);
  87. Until::output($data);
  88. }
  89. /**
  90. * @OA\Post(path="/api/Store/save",
  91. * tags={"门店管理"},
  92. * summary="保存门店信息",
  93. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  94. * @OA\RequestBody(
  95. * @OA\MediaType(
  96. * mediaType="multipart/form-data",
  97. * @OA\Schema(
  98. * @OA\Property(description="门店名称", property="name", type="string", default="测试门店1"),
  99. * @OA\Property(description="门店code", property="code", type="string", default="A001"),
  100. * @OA\Property(description="营业时间", property="openTime", type="string", default="06:00"),
  101. * @OA\Property(description="闭店时间", property="closeTime", type="string", default="22:00"),
  102. * @OA\Property(description="支付id", property="payId", type="integer", default="1"),
  103. * @OA\Property(description="所属集团id", property="groupId", type="integer", default="1"),
  104. * @OA\Property(description="所属公司id", property="companyId", type="integer", default="1"),
  105. * @OA\Property(description="所属品牌id", property="brandId", type="integer", default="1"),
  106. * @OA\Property(description="logo的url", property="logo", type="string", default="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608146390523&di=02b955a1fa80d1c43c6289f846ddc42c&imgtype=0&src=http%3A%2F%2Fimg.sccnn.com%2Fbimg%2F338%2F38706.jpg"),
  107. * @OA\Property(description="纬度", property="latitude", type="string", default="31.241510099342623"),
  108. * @OA\Property(description="经度", property="longitude", type="string", default="121.32174958203123"),
  109. * @OA\Property(description="地址", property="address", type="string", default="上海市普陀区真北路"),
  110. * @OA\Property(description="联系电话", property="mobile", type="string", default="15656789876"),
  111. * @OA\Property(description="门店介绍", property="storeContent", type="string", default="本店所有商品照片为专业摄影师拍摄,后期起精心修制及色彩调整,尽量与实际商品保持一致。"),
  112. * @OA\Property(description="门店id", property="id", type="string", default=""),
  113. * @OA\Property(description="门店状态 1正常 2闭店 3暂歇", property="status", type="0"),
  114. * required={"name","code","openTime","closeTime","groupId","companyId","brandId","logo",
  115. * "latitude","longitude","address","mobile","storeContent"})
  116. * )
  117. * ),
  118. * @OA\Response(response="200", description="请求成功")
  119. * )
  120. */
  121. public function save() {
  122. $input = Until::getInput();
  123. $rule = [
  124. 'name|门店名称' => 'require',
  125. 'code|门店code' => 'require',
  126. 'openTime|营业时间' => 'require',
  127. 'closeTime|闭店时间' => 'require',
  128. // 'payId|支付标识' => 'require',
  129. 'groupId|所属集团id' => 'require',
  130. 'companyId|所属公司id' => 'require',
  131. 'brandId|所属品牌id' => 'require',
  132. 'logo|logo的url' => 'require',
  133. 'latitude|纬度' => 'require',
  134. 'longitude|经度' => 'require',
  135. 'address|地址' => 'require',
  136. 'mobile|联系电话' => 'require',
  137. 'storeContent|门店介绍' => 'require',
  138. ];
  139. Until::check($rule, $input);
  140. $model = new StoreModel();
  141. if (!empty($input['id'])) {
  142. $id = (int)$input['id'];
  143. $model::where(['id' => $id])->update([
  144. 'store_name' => $input['name'],
  145. 'store_code' => $input['code'],
  146. 'open_time' => $input['openTime'],
  147. 'close_time' => $input['closeTime'],
  148. 'pay_id' => $input['payId'] ?? '',
  149. 'group_id' => $input['groupId'],
  150. 'company_id' => $input['companyId'],
  151. 'brand_id' => $input['brandId'],
  152. 'logo' => $input['logo'],
  153. 'latitude' => $input['latitude'],
  154. 'longitude' => $input['longitude'],
  155. 'address' => $input['address'],
  156. 'mobile' => $input['mobile'],
  157. 'store_content' => $input['storeContent'],
  158. 'status' => $input['status']
  159. ]);
  160. } else {
  161. // if (empty($input['payId'])) {
  162. // $input['payCode'] = (new GroupModel())::where(['id' => (int)$input['groupId']])->value('pay_code');
  163. // }
  164. $id = $model->insertGetId([
  165. 'store_name' => $input['name'],
  166. 'store_code' => $input['code'],
  167. 'open_time' => $input['openTime'],
  168. 'close_time' => $input['closeTime'],
  169. 'pay_id' => $input['payId'] ?? 0,
  170. 'group_id' => $input['groupId'],
  171. 'company_id' => $input['companyId'],
  172. 'brand_id' => $input['brandId'],
  173. 'logo' => $input['logo'],
  174. 'latitude' => $input['latitude'],
  175. 'longitude' => $input['longitude'],
  176. 'address' => $input['address'],
  177. 'mobile' => $input['mobile'],
  178. 'store_content' => $input['storeContent'] ?? '',
  179. ]);
  180. Db::table('store_role')->insertGetId(['store_id' => (int)$id, 'admin_id' => $this->adminId]);
  181. }
  182. $model->setWhere(['s.id' => (int)$id]);
  183. $info = $model->getStoreInfo();
  184. Until::output(['info' => Until::modelToArray($info)]);
  185. }
  186. /**
  187. * @OA\GET(path="/api/Store/read",
  188. * tags={"门店管理"},
  189. * summary="查看门店信息",
  190. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  191. * @OA\Parameter(name="id", in="query", description="门店id", @OA\Schema(type="ineger",default="1")),
  192. * @OA\RequestBody(
  193. * ),
  194. * @OA\Response(response="200", description="请求成功")
  195. * )
  196. */
  197. public function read($id) {
  198. $model = new StoreModel();
  199. $where[] = ['s.id', '=', (int)$id];
  200. $model->setWhere($where);
  201. $info = $model->getStoreInfo();
  202. Until::output(['info' => $info]);
  203. }
  204. /**
  205. * @OA\GET(path="/api/Store/delete",
  206. * tags={"门店管理"},
  207. * summary="删除门店信息",
  208. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  209. * @OA\Parameter(name="id", in="query", description="门店id", @OA\Schema(type="ineger",default="1")),
  210. * @OA\Parameter(name="status", in="query", description="1正常 2闭店 3暂歇", @OA\Schema(type="ineger",default="1")),
  211. * @OA\RequestBody(
  212. * ),
  213. * @OA\Response(response="200", description="请求成功")
  214. * )
  215. */
  216. public function delete($id, $status) {
  217. $model = new StoreModel();
  218. $where[] = ['id', '=', (int)$id];
  219. $data = ['status' => (int)$status];
  220. $isSuccess = $model::where($where)->update($data);
  221. Until::output(['isSuccess' => $isSuccess]);
  222. }
  223. /**
  224. * @OA\Post(path="/api/Store/getStore",
  225. * tags={"门店管理"},
  226. * summary="距离最近的一家门店",
  227. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  228. * @OA\RequestBody(
  229. * @OA\MediaType(
  230. * mediaType="multipart/form-data",
  231. * @OA\Schema(
  232. * @OA\Property(description="纬度", property="lat", type="string", default=""),
  233. * @OA\Property(description="经度", property="lon", type="string", default=""),
  234. * required={"lat","lon"})
  235. * )
  236. * ),
  237. * @OA\Response(response="200", description="请求成功")
  238. * )
  239. */
  240. public function getStore() {
  241. $input = Until::getInput();
  242. $rule = [
  243. 'lat|纬度' => 'require',
  244. 'lon|经度' => 'require',
  245. ];
  246. Until::check($rule, $input);
  247. $model = new StoreModel();
  248. $squares = Until::returnSquarePoint($input['lon'], $input['lat']);
  249. $info = $model::where("status = 1 and latitude<>0 and latitude>{$squares['right-bottom']['lat']} and latitude<{$squares['left-top']['lat']} and
  250. longitude>{$squares['left-top']['lng']} and longitude<{$squares['right-bottom']['lng']}")
  251. ->field('*,' . "(6378.138 * 2 * asin(sqrt(pow(sin((latitude * pi() / 180 - " . $input['lat'] . " * pi() / 180) / 2),2) +
  252. cos(latitude * pi() / 180) * cos(" . $input['lat'] . " * pi() / 180) * pow(sin((longitude * pi() / 180 - " . $input['lon'] . " * pi() / 180) / 2),2))) * 1000) as distance")
  253. ->find();
  254. $info = Until::modelToArray($info);
  255. if (empty($info)) {
  256. Until::output(['info' => []]);
  257. }
  258. $disModel = new DiscussModel();
  259. $storeScore = $disModel::where(['store_id' => $info['id']])->avg('store_score');
  260. $info['score'] = (int)$storeScore;
  261. $distance = (float)(substr(($info['distance'] / 1000), 0, 6));
  262. $info['distance'] = number_format($distance, 2) . "km";
  263. Until::output(['info' => Until::modelToArray($info)]);
  264. }
  265. /**
  266. * @OA\GET(path="/api/Store/appointmentTime",
  267. * tags={"门店管理"},
  268. * summary="可预约时间",
  269. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  270. * @OA\Parameter(name="storeId", in="query", description="门店id", @OA\Schema(type="ineger",default="1")),
  271. * @OA\RequestBody(
  272. * ),
  273. * @OA\Response(response="200", description="请求成功")
  274. * )
  275. */
  276. public function appointmentTime() {
  277. $input = Until::getInput();
  278. $data = [
  279. date('m-d'),
  280. date('m-d', strtotime('+1 day')),
  281. // '今天',
  282. // '明天',
  283. date('m-d', strtotime('+2 days')),
  284. date('m-d', strtotime('+3 days')),
  285. date('m-d', strtotime('+4 days')),
  286. date('m-d', strtotime('+5 days')),
  287. date('m-d', strtotime('+6 days')),
  288. ];
  289. Until::output(['date' => $data]);
  290. }
  291. }