链街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.

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