StoreLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace app\admin\logic\shop;
  3. use app\common\basics\Logic;
  4. use app\common\enum\ShopEnum;
  5. use app\common\model\shop\Shop;
  6. use app\common\model\shop\ShopAdmin;
  7. use app\common\server\UrlServer;
  8. use Exception;
  9. use think\facade\Db;
  10. class StoreLogic extends Logic
  11. {
  12. /**
  13. * NOTE: 商家列表
  14. * @author: 张无忌
  15. * @param $get
  16. * @return array|bool
  17. */
  18. public static function lists($get)
  19. {
  20. try {
  21. $where = [
  22. ['del', '=', 0]
  23. ];
  24. if (!empty($get['name']) and $get['name'])
  25. $where[] = ['name', 'like', '%'.$get['name'].'%'];
  26. if (!empty($get['type']) and is_numeric($get['type']))
  27. $where[] = ['type', '=', $get['type']];
  28. if (!empty($get['cid']) and is_numeric($get['cid']))
  29. $where[] = ['cid', '=', $get['cid']];
  30. if (isset($get['is_recommend']) && $get['is_recommend'] != '')
  31. $where[] = ['is_recommend', '=', $get['is_recommend']];
  32. if (isset($get['is_run']) && $get['is_run'] != '')
  33. $where[] = ['is_run', '=', $get['is_run']];
  34. if (isset($get['is_freeze']) and $get['is_freeze'] != '')
  35. $where[] = ['is_freeze', '=', $get['is_freeze']];
  36. if (!empty($get['expire_start_time']) and $get['expire_start_time'])
  37. $where[] = ['expire_time', '>=', strtotime($get['expire_start_time'])];
  38. if (!empty($get['expire_end_time']) and $get['expire_end_time'])
  39. $where[] = ['expire_time', '<=', strtotime($get['expire_end_time'])];
  40. $condition = 'del=0';
  41. // 到期状态
  42. if (isset($get['expire_status']) and $get['expire_status'] != '') {
  43. if ($get['expire_status']) {
  44. // 已到期
  45. $where[] = ['expire_time', '<', time()];
  46. $where[] = ['expire_time', '>', 0];
  47. } else {
  48. // 未到期
  49. $condition = "expire_time=0 OR expire_time >". time();
  50. }
  51. }
  52. $model = new Shop();
  53. $lists = $model->field(true)
  54. ->where($where)
  55. ->whereRaw($condition)
  56. ->order('id', 'desc')
  57. ->order('weight', 'asc')
  58. ->with(['category', 'admin'])
  59. ->append(['expire_desc'])
  60. ->paginate([
  61. 'page' => $get['page'],
  62. 'list_rows' => $get['limit'],
  63. 'var_page' => 'page'
  64. ])
  65. ->toArray();
  66. foreach ($lists['data'] as &$item) {
  67. $item['category'] = $item['category']['name'] ?? '未知';
  68. $item['type'] = ShopEnum::getShopTypeDesc($item['type']);
  69. $item['is_run'] = ShopEnum::getShopIsRunDesc($item['is_run']);
  70. $item['is_freeze'] = ShopEnum::getShopFreezeDesc($item['is_freeze']);
  71. $item['is_recommend'] = ShopEnum::getShopIsRecommendDesc($item['is_recommend']);
  72. $item['account'] = $item['admin']['account'] ?? '';
  73. }
  74. return ['count'=>$lists['total'], 'lists'=>$lists['data']];
  75. } catch (Exception $e) {
  76. return ['error'=>$e->getMessage()];
  77. }
  78. }
  79. /**
  80. * NOTE: 商家详细
  81. * @author: 张无忌
  82. * @param $id
  83. * @return array
  84. */
  85. public static function detail($id)
  86. {
  87. $model = new Shop();
  88. $detail = $model->json(['other_qualifications'],true)->findOrEmpty($id)->toArray();
  89. $detail['expire_time'] = $detail['expire_time'] == '无期限' ? 0 : $detail['expire_time'];
  90. $detail['business_license'] = $detail['business_license'] ? UrlServer::getFileUrl($detail['business_license']) : '';
  91. if (!empty($detail['other_qualifications'])) {
  92. foreach ($detail['other_qualifications'] as &$val) {
  93. $val = UrlServer::getFileUrl($val);
  94. }
  95. }
  96. return $detail;
  97. }
  98. public static function getAccountInfo($id)
  99. {
  100. $detail = ShopAdmin::field('id,account')->where(['shop_id' => $id, 'root' => 1])->findOrEmpty()->toArray();
  101. return $detail;
  102. }
  103. /**
  104. * NOTE: 新增商家
  105. * @author: 张无忌
  106. * @param $post
  107. * @return bool
  108. */
  109. public static function add($post)
  110. {
  111. Db::startTrans();
  112. try {
  113. // 校验配送方式
  114. self::checkDeliveryType($post);
  115. // 创建商家
  116. $shop = Shop::create([
  117. 'cid' => $post['cid'],
  118. 'type' => $post['type'],
  119. 'name' => $post['name'],
  120. 'nickname' => $post['nickname'],
  121. 'mobile' => $post['mobile'],
  122. 'logo' => $post['logo'] ?? '',
  123. 'background' => $post['background'] ?? '',
  124. 'license' => $post['license'] ?? '',
  125. 'keywords' => $post['keywords'] ?? '',
  126. 'intro' => $post['intro'] ?? '',
  127. 'weight' => $post['weight'] ?? 0,
  128. 'trade_service_fee' => $post['trade_service_fee'],
  129. 'is_run' => $post['is_run'],
  130. 'is_freeze' => $post['is_freeze'],
  131. 'is_product_audit' => $post['is_product_audit'],
  132. 'is_recommend' => $post['is_recommend'] ?? 0,
  133. 'expire_time' => !empty($post['expire_time']) ? strtotime($post['expire_time']) : 0,
  134. 'province_id' => $post['province_id'] ?? 0,
  135. 'city_id' => $post['city_id'] ?? 0,
  136. 'district_id' => $post['district_id'] ?? 0,
  137. 'address' => $post['address'] ?? '',
  138. 'longitude' => $post['longitude'] ?? '',
  139. 'latitude' => $post['latitude'] ?? '',
  140. 'delivery_type' => $post['delivery_type'] ?? [1]
  141. ]);
  142. // 创建账号
  143. // 新增商家登录账号
  144. $time = time();
  145. $salt = substr(md5($time . $post['name']), 0, 4);//随机4位密码盐
  146. ShopAdmin::create([
  147. 'root' => 1,
  148. 'shop_id' => $shop->id,
  149. 'name' => '超级管理员',
  150. 'account' => $post['account'],
  151. 'password' => generatePassword($post['password'], $salt),
  152. 'salt' => $salt,
  153. 'role_id' => 0,
  154. 'create_time' => $time,
  155. 'update_time' => $time,
  156. 'disable' => 0,
  157. 'del' => 0
  158. ]);
  159. Db::commit();
  160. return true;
  161. } catch (Exception $e) {
  162. Db::rollback();
  163. static::$error = $e->getMessage();
  164. return false;
  165. }
  166. }
  167. /**
  168. * NOTE: 编辑商家
  169. * @author: 张无忌
  170. * @param $post
  171. * @return bool
  172. */
  173. public static function edit($post)
  174. {
  175. try {
  176. // 校验配送方式
  177. self::checkDeliveryType($post);
  178. Shop::update([
  179. 'cid' => $post['cid'],
  180. 'type' => $post['type'],
  181. 'name' => $post['name'],
  182. 'nickname' => $post['nickname'],
  183. 'mobile' => $post['mobile'],
  184. 'logo' => $post['logo'] ?? '',
  185. 'keywords' => $post['keywords'] ?? '',
  186. 'intro' => $post['intro'] ?? '',
  187. 'trade_service_fee' => $post['trade_service_fee'],
  188. 'is_run' => $post['is_run'],
  189. 'is_freeze' => $post['is_freeze'],
  190. 'is_product_audit' => $post['is_product_audit'],
  191. 'expire_time' => !empty($post['expire_time']) ? strtotime($post['expire_time']) : 0,
  192. 'province_id' => $post['province_id'] ?? 0,
  193. 'city_id' => $post['city_id'] ?? 0,
  194. 'district_id' => $post['district_id'] ?? 0,
  195. 'address' => $post['address'] ?? '',
  196. 'longitude' => $post['longitude'] ?? '',
  197. 'latitude' => $post['latitude'] ?? '',
  198. 'delivery_type' => $post['delivery_type'] ?? [1],
  199. 'business_license' => empty($post['business_license']) ? '' : UrlServer::setFileUrl($post['business_license']),
  200. 'other_qualifications' => isset($post['other_qualifications']) ? json_encode($post['other_qualifications'], JSON_UNESCAPED_UNICODE) : '',
  201. ], ['id'=>$post['id']]);
  202. return true;
  203. } catch (Exception $e) {
  204. static::$error = $e->getMessage();
  205. return false;
  206. }
  207. }
  208. /**
  209. * NOTE: 设置商家
  210. * @author: 张无忌
  211. * @param $post
  212. * @return bool
  213. */
  214. public static function set($post)
  215. {
  216. try {
  217. Shop::update([
  218. 'is_distribution' => $post['is_distribution'] ?? 0,
  219. 'is_recommend' => $post['is_recommend'] ?? 0,
  220. 'is_pay' => $post['is_pay'] ?? 1, //是否开启支付功能,默认开启
  221. 'weight' => $post['weight']
  222. ], ['id'=>$post['id']]);
  223. return true;
  224. } catch (Exception $e) {
  225. static::$error = $e->getMessage();
  226. return false;
  227. }
  228. }
  229. /**
  230. * NOTE: 更新账号密码
  231. * @author: 张无忌
  232. * @param $post
  233. * @return bool
  234. */
  235. public static function account($post)
  236. {
  237. Db::startTrans();
  238. try {
  239. if(!isset($post['account']) || empty($post['account'])) {
  240. throw new \think\Exception('账户不能为空');
  241. }
  242. $shopAdmin = ShopAdmin::where([
  243. ['account', '=', trim($post['account'])],
  244. ['shop_id', '<>', $post['id']]
  245. ])->findOrEmpty();
  246. if(!$shopAdmin->isEmpty()) {
  247. throw new \think\Exception('账户已存在,请更换其他名称重试');
  248. }
  249. $shopAdmin = ShopAdmin::where(['shop_id' => $post['id'], 'root' => 1])->findOrEmpty();
  250. $shopAdminUpdateData = [
  251. 'account' => $post['account'],
  252. 'update_time' => time()
  253. ];
  254. if (!empty($post['password'])) {
  255. $shopAdminUpdateData['password'] = generatePassword($post['password'], $shopAdmin->salt);
  256. }
  257. ShopAdmin::where(['shop_id' => $post['id'], 'root' => 1])->update($shopAdminUpdateData);
  258. Db::commit();
  259. return true;
  260. } catch (Exception $e) {
  261. Db::rollback();
  262. static::$error = $e->getMessage();
  263. return false;
  264. }
  265. }
  266. /**
  267. * @notes 批量更新商家营业状态或冻结状态
  268. * @param $ids
  269. * @param $field
  270. * @param $value
  271. * @return Shop|false
  272. * @author 段誉
  273. * @date 2022/3/17 10:42
  274. */
  275. public static function batchOperation($ids, $field, $value)
  276. {
  277. try {
  278. $result = Shop::whereIn('id', $ids)->update([
  279. $field => $value,
  280. 'update_time' => time()
  281. ]);
  282. return $result;
  283. } catch (\Exception $e) {
  284. self::$error = $e->getMessage();
  285. return false;
  286. }
  287. }
  288. /**
  289. * @notes 校验配送方式
  290. * @param $post
  291. * @return bool
  292. * @throws \Exception
  293. * @author 段誉
  294. * @date 2022/11/1 11:30
  295. */
  296. public static function checkDeliveryType($post)
  297. {
  298. // 校验配送方式
  299. if (empty($post['delivery_type'])) {
  300. throw new \Exception('至少选择一种配送方式');
  301. }
  302. // 线下自提时,商家地址必填
  303. if (in_array(ShopEnum::DELIVERY_SELF, $post['delivery_type'])) {
  304. if (empty($post['province_id']) || empty($post['city_id']) || empty($post['district_id']) || empty($post['address'])) {
  305. throw new \Exception('线下自提需完善商家地址');
  306. }
  307. }
  308. return true;
  309. }
  310. }