Store.php 14 KB

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