StoreService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\StoreModel;
  13. use App\Models\UserModel;
  14. use App\Models\UserRoleModel;
  15. use App\Services\BaseService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 商户店铺管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class StoreService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * AdService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new StoreModel();
  34. }
  35. /**
  36. * 列表
  37. * @param $params
  38. * @param int $pageSize
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 15)
  42. {
  43. // 分页查询
  44. $list = $this->model
  45. ->with(['member'])
  46. ->where(['type'=>2,'mark'=>1])
  47. ->where(function ($query) use ($params) {
  48. // 状态筛选
  49. if (isset($params['status']) && $params['status'] > 0) {
  50. $query->where('status', $params['status']);
  51. }
  52. // 名称搜索
  53. if (isset($params['name']) && $params['name']) {
  54. $query->where('name', 'like', "%{$params['name']}%");
  55. }
  56. // 联系电话搜索
  57. if (isset($params['mobile']) && $params['mobile']) {
  58. $query->where('mobile', 'like', "%{$params['mobile']}%");
  59. }
  60. // 姓名搜索
  61. if (isset($params['real_name']) && $params['real_name']) {
  62. $query->where('real_name', 'like', "%{$params['real_name']}%");
  63. }
  64. })
  65. ->orderBy('create_time', 'desc')
  66. ->orderBy('id', 'desc')
  67. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  68. $list = $list->toArray();
  69. // 格式化数据
  70. if (isset($list['data']) && !empty($list['data'])) {
  71. foreach ($list['data'] as &$item) {
  72. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  73. $item['status_text'] = $this->getStatusText($item['status']);
  74. // 添加余额信息
  75. $item['balance'] = isset($item['member']['balance']) ? $item['member']['balance'] : 0;
  76. $item['password'] = '';
  77. }
  78. }
  79. return [
  80. 'msg' => '操作成功',
  81. 'code' => 0,
  82. 'data' => $list['data'],
  83. 'count' => $list['total']
  84. ];
  85. }
  86. /**
  87. * 获取状态文本
  88. * @param int $status
  89. * @return string
  90. */
  91. private function getStatusText($status)
  92. {
  93. $statusMap = [
  94. 1 => '已审核(营业中)',
  95. 2 => '待审核',
  96. 3 => '审核失败'
  97. ];
  98. return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
  99. }
  100. /**
  101. * 查询
  102. * @param $params
  103. * @return \Illuminate\Database\Eloquent\Builder
  104. */
  105. public function getQuery($params)
  106. {
  107. $where = ['mark' => 1];
  108. $status = isset($params['status']) ? $params['status'] : 0;
  109. if ($status > 0) {
  110. $where['status'] = $status;
  111. }
  112. $model = $this->model
  113. ->select('user.*')
  114. ->where($where);
  115. // 搜索条件
  116. $model->where(function ($query) use ($params) {
  117. // 店铺名称搜索
  118. $name = isset($params['name']) ? trim($params['name']) : '';
  119. if ($name) {
  120. $query->where('name', 'like', "%{$name}%");
  121. }
  122. // 联系电话搜索
  123. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  124. if ($phone) {
  125. $query->where('mobile', 'like', "%{$phone}%");
  126. }
  127. // 姓名搜索
  128. $realName = isset($params['real_name']) ? trim($params['real_name']) : '';
  129. if ($realName) {
  130. $query->where('real_name', 'like', "%{$realName}%");
  131. }
  132. });
  133. return $model;
  134. }
  135. /**
  136. * 添加或编辑
  137. * @return array
  138. * @since 2020/11/11
  139. * @author laravel开发员
  140. */
  141. public function edit()
  142. {
  143. $data = request()->all();
  144. // 图片处理
  145. if (isset($data['logo'])) {
  146. $data['logo'] = get_image_path($data['logo']);
  147. }
  148. if (isset($data['business_license'])) {
  149. $data['business_license'] = get_image_path($data['business_license']);
  150. }
  151. if (isset($data['driver_license'])) {
  152. $data['driver_license'] = get_image_path($data['driver_license']);
  153. }
  154. if (empty($data['position'])) {
  155. return message('请填写负责人职位', false);
  156. }
  157. if (empty($data['name'])) {
  158. return message('请填写商户名称', false);
  159. }
  160. if (empty($data['legal_person'])) {
  161. return message('请填写商户法人姓名', false);
  162. }
  163. if (empty($data['legal_idcard'])) {
  164. return message('请填写商户法人身份证', false);
  165. }
  166. if (empty($data['credit_code'])) {
  167. return message('请填写商户社会统一信用代码', false);
  168. }
  169. if (empty($data['settle_bank_name'])) {
  170. return message('请填写商户结算开户行', false);
  171. }
  172. $mobile = isset($data['mobile'])?$data['mobile']:'';
  173. if (empty($mobile)) {
  174. return message('请填写商户账号(联系电话)', false);
  175. }
  176. if (empty($data['real_name'])) {
  177. return message('请填写商户负责人姓名', false);
  178. }
  179. // 手机号唯一性验证
  180. $type = isset($data['type']) && $data['type']? $data['type'] : 1;
  181. $id = isset($data['id']) ? $data['id'] : 0;
  182. if ($mobile) {
  183. $checkId = $this->model->where(['mobile' => $mobile,'type'=>$type, 'mark' => 1])->value('id');
  184. if ($checkId && ($id != $checkId)) {
  185. return message('该商户已存在', false);
  186. }
  187. }
  188. var_dump($data);
  189. DB::beginTransaction();
  190. $result = parent::edit($data);
  191. $success = isset($result['success'])? $result['success']:false;
  192. $id = isset($result['data']['id'])? $result['data']['id']:0;
  193. if($success){
  194. $account = [
  195. 'user_id'=>$data['user_id'],
  196. 'store_id'=> $id,
  197. 'username'=> $mobile,
  198. 'avatar'=> $data['logo'],
  199. 'nickname'=> $data['name'],
  200. ];
  201. // 密码处理
  202. if (isset($data['password']) && $data['password']) {
  203. $account['password'] = get_password(trim($data['password']). $mobile);
  204. } else {
  205. // 编辑时如果密码为空则不更新密码
  206. unset($account['password']);
  207. }
  208. $adminId = UserModel::where(['username' => $mobile, 'mark' => 1])->value('id');
  209. // 删除已存在的用户角色关系数据
  210. $userRoleService = new UserRoleService();
  211. $userRole = $adminId?UserRoleModel::where(['user_id'=>$adminId,'role_id'=>6])->first():[];
  212. if ($adminId) {
  213. $account['update_time']=time();
  214. UserModel::where(['id'=>$adminId])->update($account);
  215. }else{
  216. $account['create_time']=time();
  217. if(!$adminId = UserModel::insertGetId($account)){
  218. DB::rollBack();
  219. return message('创建商户管理账户失败', false);
  220. }
  221. }
  222. $userRoleService->deleteUserRole($adminId);
  223. // 插入用户角色关系数据
  224. $userRoleService->insertUserRole($adminId, [6]);
  225. }
  226. DB::commit();
  227. return message('操作成功',true);
  228. }
  229. /**
  230. * 获取当前登录企业信息
  231. * @return array
  232. */
  233. public function getCurrentStoreInfo($storeId)
  234. {
  235. try {
  236. // 查询企业信息
  237. $store = StoreModel::where('id', $storeId)
  238. ->where('mark', 1)
  239. ->with(['member'])
  240. ->first();
  241. if (!$store) {
  242. return ['code' => 1, 'msg' => '企业信息不存在'];
  243. }
  244. // 检查企业状态
  245. if ($store->status !== 1) {
  246. return ['code' => 1, 'msg' => '企业已被禁用'];
  247. }
  248. $data = [
  249. 'user_id' => $store->user_id,
  250. 'name' => $store->nickname,
  251. 'real_name' => $store->realname,
  252. 'phone' => $store->mobile,
  253. 'status' => $store->status
  254. ];
  255. return ['code' => 0, 'msg' => '获取成功', 'data' => $data];
  256. } catch (\Exception $e) {
  257. return ['code' => 1, 'msg' => '获取失败:' . $e->getMessage()];
  258. }
  259. }
  260. /**
  261. * 获取企业详情(重写父类方法)
  262. * @return array
  263. */
  264. public function info()
  265. {
  266. // 记录ID
  267. $id = request()->input("id", 0);
  268. $info = [];
  269. if ($id) {
  270. // 获取企业基本信息(使用关联查询)
  271. $store = $this->model->where(['id' => $id, 'mark' => 1])
  272. ->first();
  273. if ($store) {
  274. $info = $store->toArray();
  275. // 处理图片
  276. if (isset($info['avatar'])) {
  277. $info['avatar'] = get_image_url($info['avatar']);
  278. }
  279. if (isset($info['business_license'])) {
  280. $info['business_license'] = get_image_url($info['business_license']);
  281. }
  282. // 处理时间
  283. if (isset($info['create_time'])) {
  284. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  285. }
  286. if (isset($info['update_time'])) {
  287. $info['update_time'] = $info['update_time'] ? datetime($info['update_time'], 'Y-m-d H:i:s') : '';
  288. }
  289. // 添加状态文本
  290. $info['status_text'] = $this->getStatusText($info['status'] ?? 0);
  291. }
  292. }
  293. return message(MESSAGE_OK, true, $info);
  294. }
  295. /**
  296. * 获取店铺选项列表(用于下拉选择)
  297. * @return array
  298. */
  299. public function options()
  300. {
  301. $param = request()->all();
  302. // 用户ID
  303. $keyword = getter($param, "keyword");
  304. $list = $this->model
  305. ->where(['status'=>1,'mark'=>1])
  306. ->where(function ($query) use ($keyword) {
  307. if ($keyword) {
  308. $query->where('name', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%");
  309. }
  310. })
  311. ->orderBy('id', 'asc')
  312. ->select(['id', 'name','mobile', 'real_name'])
  313. ->get()
  314. ->toArray();
  315. return $list;
  316. }
  317. /**
  318. * 删除企业(重写父类方法以清除缓存)
  319. * @return array
  320. */
  321. public function delete()
  322. {
  323. $result = parent::delete();
  324. return $result;
  325. }
  326. }