Store.php 14 KB

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