链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
19 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Admin\Controllers\v3;
  3. use App\Admin\Actions\Grid\v3\StoreChangeMarket;
  4. use App\Admin\Common\Auth;
  5. use App\Admin\Repositories\v3\Store;
  6. use App\Models\LanzuMmInfo;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Controllers\AdminController;
  12. use App\Models\LanzuMmInfo as MminfoModel;
  13. use App\Models\v3\Market as MarketModel;
  14. use App\Admin\Common\StoreQrCode;
  15. use App\Models\v3\Store as StoreModel;
  16. use App\Models\v3\User as UserModel;
  17. use App\Models\v3\Category as CategoryModel;
  18. use App\Models\LanzuUserBalance as UserBalanceModel;
  19. use App\Models\v3\StoreUsers as StoreUsersModel;
  20. use App\Admin\Actions\Grid\v3\StoreSetTime;
  21. use App\Admin\Renderable\StoreBalance;
  22. class StoreController extends AdminController
  23. {
  24. /**
  25. * Make a grid builder.
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. $user = Admin::user();
  32. $builder = new Store();
  33. if ($user->isRole('lanzu_mm')) {
  34. //如果登陆角色为市场经理,获取市场经理id
  35. $mm = LanzuMmInfo::where(['admin_user_id' => $user->id])->first();
  36. $builder = StoreModel::where(['mm_user_id' => $mm->user_id]);
  37. }elseif ($user->isRole('market_service')){
  38. //如果登陆角色为市场服务站,获取市场id
  39. $marketId = Auth::getMarket();
  40. $builder = StoreModel::where('market_id',$marketId);
  41. }
  42. return Grid::make($builder, function (Grid $grid) {
  43. // 查询市场
  44. $marketList = MarketModel::getMarketArray();
  45. // 查询一级分类
  46. // $categoryList = CategoryModel::getArray([['parent_id','=',0]]);
  47. $grid->id->sortable();
  48. $grid->logo_url->image('',50);
  49. $grid->name->width('12%');
  50. // 可看店铺余额权限
  51. if($this->storeBalanceCan()){
  52. $grid->column('user_id','余额')->display(function($userId){
  53. $userId = $this->user_id;
  54. $balance = 0;
  55. if($userId > 0){
  56. $moneyType = 5;// 商户类型
  57. $balance = UserBalanceModel::getBalance($userId, $moneyType);
  58. }
  59. return $balance;
  60. })->modal(function($modal){
  61. $name = $this->name;
  62. $modal->title($name.'的余额明细');
  63. $table = StoreBalance::make(['user_id'=>$this->user_id]);
  64. return $table;
  65. });
  66. }
  67. $grid->market_id->display(function ($marketId) use($marketList){
  68. return isset($marketList[$marketId]) ? $marketList[$marketId] : '';
  69. });
  70. // $grid->mm_user_id->display(function ($mmUserId){
  71. // $mmUser = MminfoModel::getMmInfo($mmUserId,'name');
  72. // return empty($mmUser) ? '' : $mmUser->name;
  73. // });
  74. $grid->store_applet_img->display(function($appletImg){
  75. $id = $this->id;
  76. $oldPath = 'https://img.lanzu.vip/addons/zh_cjdianc/data/code_store_img/wx_store_'.$id.'.jpg';
  77. $path = empty($appletImg) ? $oldPath : config('filesystems.disks.oss.img_host').'/'.$appletImg;
  78. $img = "<img src='$path' data-action='preview-img' style='max-width:50px;max-height:200px;cursor:pointer' class='img img-thumbnail'/>";
  79. return $img;
  80. });
  81. $grid->cash_code_img->display(function($appletImg){
  82. $id = $this->id;
  83. $oldPath = 'https://img.lanzu.vip/addons/zh_cjdianc/data/code_pay_img/wx_pay_'.$id.'.jpg'.config('filesystems.disks.oss.store_image_resize');
  84. $path = empty($appletImg) ? $oldPath : config('filesystems.disks.oss.img_host').'/'.$appletImg;
  85. $img = "<img src='$path' data-action='preview-img' style='max-width:50px;max-height:200px;cursor:pointer' class='img img-thumbnail'/>";
  86. return $img;
  87. });
  88. $grid->is_operation->switch();
  89. $grid->is_open->switch();
  90. $actions = [
  91. new StoreSetTime()
  92. ];
  93. if($this->storeChangeMarketCan()){
  94. $actions[] = new StoreChangeMarket();
  95. }
  96. $grid->actions($actions);
  97. // 搜索
  98. $grid->filter(function (Grid\Filter $filter) use($marketList){
  99. $filter->equal('id');
  100. $filter->like('name');
  101. if (!(Admin::user()->isRole('market_service'))){
  102. $filter->equal('market_id')->select($marketList);
  103. }
  104. });
  105. $grid->model()->orderBy('id','desc');
  106. // 每页10条
  107. $grid->paginate(10);
  108. // $grid->disableDeleteButton();
  109. if (Admin::user()->isRole('market_service')){
  110. //如果是市场服务站角色,关闭编辑,删除按钮操作
  111. $grid->disableDeleteButton();
  112. $grid->disableEditButton();
  113. $grid->disableCreateButton();
  114. }
  115. });
  116. }
  117. /**
  118. * Make a show builder.
  119. *
  120. * @param mixed $id
  121. *
  122. * @return Show
  123. */
  124. protected function detail($id)
  125. {
  126. return Show::make($id, new Store(), function (Show $show) {
  127. $show->row(function (Show\Row $show) {
  128. $show->width(6)->id;
  129. $show->width(6)->mm_user_id->as(function ($mmUserId){
  130. $item = MminfoModel::getMmInfo($mmUserId,'name');
  131. return empty($item) ? '' : $item->name;
  132. });
  133. $show->width(6)->market_id->as(function ($marketId){
  134. $item = MarketModel::getMarketInfo($marketId,'name');
  135. return empty($item) ? '' : $item->name;
  136. });
  137. $show->width(6)->name;
  138. $show->width(6)->logo->image();
  139. $show->width(6)->user_id->as(function ($userId){
  140. $item = UserModel::getUserInfo($userId,'nick_name');
  141. return empty($item) ? '' : $item->nick_name;
  142. });
  143. $show->width(6)->admin_id->as(function ($userId){
  144. $item = UserModel::getUserInfo($userId,'nick_name');
  145. return empty($item) ? '' : $item->nick_name;
  146. });
  147. $show->width(6)->business_license->image();
  148. $show->width(6)->zm_img->image();
  149. $show->width(6)->fm_img->image();
  150. $show->store_applet_img->width(6)->image();
  151. $show->cash_code_img->width(6)->image();
  152. $show->width(6)->category_id->as(function ($categoryId){
  153. $item = CategoryModel::getInfo($categoryId,'title');
  154. return empty($item) ? '' : $item->title;
  155. });
  156. $show->width(6)->tel;
  157. $show->width(6)->link_name;
  158. $show->width(6)->link_tel;
  159. $show->width(6)->time1;
  160. $show->width(6)->time2;
  161. $show->width(6)->time3;
  162. $show->width(6)->time4;
  163. });
  164. $show->row(function (Show\Row $show) {
  165. $show->width(6)->announcement;
  166. $show->width(6)->address;
  167. $show->width(6)->coordinates;
  168. $show->width(6)->is_rest_text;
  169. $show->width(6)->is_open_text;
  170. $show->width(6)->sort;
  171. // $show->width(6)->environment;
  172. $show->width(6)->expire_time;
  173. $show->width(6)->loudspeaker_imei;
  174. });
  175. $show->panel()->tools(function ($tools) {
  176. $tools->disableDelete();
  177. });
  178. $show->disableEditButton();
  179. });
  180. }
  181. /**
  182. * Make a form builder.
  183. *
  184. * @return Form
  185. */
  186. protected function form()
  187. {
  188. return Form::make(new Store(), function (Form $form) {
  189. // $userId = $form->model()->user_id;
  190. $adminId = $form->model()->admin_id;
  191. // 查询市场经理
  192. // $mmList = MminfoModel::getMmInfoArray();
  193. // 查询市场
  194. $marketList = MarketModel::getMarketArray();
  195. // 查询一级分类
  196. $categoryList = CategoryModel::getArray([['parent_id','=',0]]);
  197. // 用户
  198. // $userList = UserModel::getUserArray();
  199. // 已绑定店铺的用户
  200. // $userHas = StoreModel::pluck('admin_id')->toArray();
  201. // foreach($userList as $ku => $uv){
  202. // if($ku != 0 && in_array($ku,$userHas) && !in_array($adminId,$userHas)){
  203. // unset($userList[$ku]);
  204. // }
  205. // }
  206. $form->column(6, function (Form $form) use($marketList,$categoryList){
  207. $form->hidden('id');
  208. $form->text('mm_user_id')->type('number')->default(0)->attribute('min', 0)->placeholder('市场经理懒ID')->width(4)->help('市场经理的懒ID');
  209. $form->select('market_id')->required()->options($marketList)->help('谨慎选择所属市场,添加成功后不能修改所属市场,因为会影响商品所属市场!');
  210. $form->select('category_id')->options($categoryList);
  211. $form->text('name')->required()->maxLength(50);
  212. if($form->isCreating()){
  213. $form->image('logo')->autoUpload()->retainable()->required();
  214. }else{
  215. $form->image('logo')->autoUpload()->required();
  216. }
  217. $form->mobile('tel');
  218. $form->text('link_name')->required();
  219. $form->mobile('link_tel')->required();
  220. $form->number('sort');
  221. $form->switch('is_operation')
  222. ->customFormat(function ($v) {
  223. return $v == 1 ? 1 : 0;
  224. })
  225. ->saving(function ($v) {
  226. return $v == 1 ? 1 : 0;
  227. })->default(1);
  228. $form->hidden('is_rest');
  229. $form->switch('is_open')
  230. ->customFormat(function ($v) {
  231. return $v == 1 ? 1 : 0;
  232. })
  233. ->saving(function ($v) {
  234. return $v == 1 ? 1 : 0;
  235. })->default(1);
  236. $form->text('address')->required();
  237. });
  238. $form->column(6, function (Form $form) {
  239. if($form->isCreating()){
  240. $form->image('business_license')->autoUpload()->retainable()->required();
  241. $form->image('zm_img')->autoUpload()->retainable()->required();
  242. $form->image('fm_img')->autoUpload()->retainable()->required();
  243. }else{
  244. $form->image('business_license')->autoUpload()->required();
  245. $form->image('zm_img')->autoUpload()->required();
  246. $form->image('fm_img')->autoUpload()->required();
  247. }
  248. $form->text('admin_id')->type('number')->required()->default(0)->attribute('min', 0)->placeholder('管理员懒ID')->width(4);
  249. $form->text('user_id')->type('number')->required()->default(0)->attribute('min', 0)->placeholder('提现用户懒ID')->width(4);
  250. // $form->number('admin_id')->default(0)->required()->min(0)->placeholder('管理员懒ID');
  251. // $form->number('user_id')->default(0)->required()->min(0)->placeholder('提现用户懒ID');
  252. $form->time('time1','时间段一开始')->required()->format('HH:mm');
  253. $form->time('time2','时间段一结束')->required()->format('HH:mm')->rules('after:time1',['after'=>'选择的时间必须比时间段一开始时间晚']);
  254. $form->time('time3','时间段二开始')->format('HH:mm');
  255. $form->time('time4','时间段二结束')->format('HH:mm');
  256. // $form->timeRange('time1','time2','营业时间段一')->required();->rules('after:time2',['after'=>'选择的时间必须比时间段一结束时间晚'])
  257. // $form->timeRange('time3','time4','营业时间段二')->rules('after:time2',['after'=>'选择的时间必须比时间段一结束时间大']);
  258. });
  259. $form->column(12, function (Form $form) {
  260. $form->map('lat','lng','地址');
  261. $form->textarea('introduction')->required();
  262. $form->textarea('announcement')->default('');
  263. $form->multipleImage('environment')->autoUpload()->default('');
  264. });
  265. // $form->text('coordinates')->width(4)
  266. // ->placeholder('输入 经纬度,如: 108.281552,22.83731')
  267. // ->help("通过网址 <a href='https://lbs.amap.com/console/show/picker' target='_blank'>https://lbs.amap.com/console/show/picker</a> 获取经纬度");
  268. $form->saving(function(Form $form){
  269. $id = $form->getKey();
  270. $adminId = $form->input('admin_id');
  271. $marketId = $form->input('market_id');
  272. $isOperation = $form->input('is_operation');
  273. $time1 = $form->input('time1');
  274. $time2 = $form->input('time2');
  275. $time3 = $form->input('time3');
  276. $time4 = $form->input('time4');
  277. if($isOperation !== null){
  278. $form->is_rest = $isOperation == 1 ? 0 : 1;
  279. }
  280. if(!empty($time1) && !empty($time2) && (!empty($time3) || !empty($time4))){
  281. if($time3 && empty($time4)){
  282. return $form->error('请选择时间段二的结束时间!');
  283. }else if($time4 && empty($time3)){
  284. return $form->error('请选择时间段二的开始时间!');
  285. }else if(str_replace(':', '', $time3) <= str_replace(':', '', $time2)){
  286. return $form->error('时间段二的开始时间 必须大于 时间段一的结束时间!');
  287. }else if(str_replace(':', '', $time4) <= str_replace(':', '', $time3)){
  288. return $form->error('时间段二的结束时间 必须大于 时间段二的开始时间!');
  289. }
  290. }
  291. if($form->isEditing() && !empty($marketId) && $marketId != $form->model()->market_id){
  292. return $form->error('不能更换市场,会影响店铺下商品所属市场!');
  293. }
  294. if(!empty($adminId)){
  295. $user = UserModel::getUserInfo($adminId,'id');
  296. if(empty($user)){
  297. return $form->error('管理员不存在');
  298. }
  299. $storeM = StoreModel::select('id')->where('admin_id',$adminId);
  300. if($form->isEditing()){
  301. $storeM->where([['id','<>',$id?$id:0]]);
  302. }
  303. $store = $storeM->first();
  304. if(!empty($store)){
  305. return $form->error('管理员已经绑定了店铺');
  306. }
  307. }
  308. $form->deleteInput('is_operation');
  309. });
  310. $form->saved(function (Form $form){
  311. $id = $form->getKey();
  312. $store = StoreModel::find($id);
  313. // 添加商户钱包
  314. $userBalance = UserBalanceModel::where([
  315. ['user_type','=',5],
  316. ['source_id','=',$id]
  317. ])->first();
  318. if(empty($userBalance)){
  319. $userBalance = new UserBalanceModel();
  320. $userBalance->user_type = 5;
  321. $userBalance->source_id = $id;
  322. $userBalance->save();
  323. }
  324. if($form->isCreating() && !empty($id)){
  325. $qrCode = new StoreQrCode();
  326. // 生成小程序码 店铺
  327. $sRes = $qrCode->SetStoreWeChatCode($id);
  328. // 生成二维码 收银
  329. $pRes = $qrCode->SetPayWeChatCode($id);
  330. // 保存图片
  331. $store->store_applet_img = $sRes['status'] ? $sRes['path'] : '';
  332. $cashImg = $pRes['status'] ? $pRes['path'] : '';
  333. // 保存剪裁图片
  334. $store->cash_code_img = $cashImg.config('filesystems.disks.oss.store_image_resize');
  335. $store->save();
  336. //店长账号
  337. $storeUsersInfo = StoreUsersModel::where('store_id',$id)->where('user_category',1)->first();
  338. if(empty($storeUsersInfo)){
  339. $storeUsersModel = new StoreUsersModel();
  340. $storeUsersModel->store_id = $id;
  341. $storeUsersModel->username = $form->link_tel;
  342. $storeUsersModel->join_ip = $_SERVER['SERVER_NAME'];
  343. $storeUsersModel->user_category = 1;
  344. $storeUsersModel->register_type = 4;
  345. $storeUsersModel->status = 2;
  346. $storeUsersModel->salt = $this->random(8);
  347. $storeUsersModel->password = $this->stringHash(substr($form->link_tel,-6),$storeUsersModel->salt);
  348. $storeUsersModel->remark = '入驻店铺注册';
  349. $storeUsersModel->save();
  350. }
  351. }
  352. });
  353. $form->disableResetButton();
  354. $form->disableViewCheck();
  355. $form->disableEditingCheck();
  356. $form->disableCreatingCheck();
  357. $form->disableDeleteButton();
  358. });
  359. }
  360. public function stringHash($password, $salt) {
  361. $authkey = config('admin.setting.authkey');
  362. $password = "{$password}-{$salt}-{$authkey}";
  363. return sha1($password);
  364. }
  365. /**
  366. * 字符串
  367. */
  368. public function random($length, $numeric = false) {
  369. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  370. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  371. if ($numeric) {
  372. $hash = '';
  373. } else {
  374. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  375. --$length;
  376. }
  377. $max = strlen($seed) - 1;
  378. for ($i = 0; $i < $length; ++$i) {
  379. $hash .= $seed[mt_rand(0, $max)];
  380. }
  381. return $hash;
  382. }
  383. /**
  384. * @return bool
  385. */
  386. protected function storeBalanceCan(): bool
  387. {
  388. $user = Admin::user();
  389. return $user->can('store_balance');
  390. }
  391. /**
  392. * @return bool
  393. */
  394. protected function storeChangeMarketCan(): bool
  395. {
  396. $user = Admin::user();
  397. return $user->can('store_change_market');
  398. }
  399. }