AdminUserMerchantController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\AdminUserFilter;
  4. use App\Handlers\Str;
  5. use App\Http\Requests\AdminMerchantRequest;
  6. use App\Http\Requests\AdminUserRequest;
  7. use App\Http\Resources\AdminMerchantsResource;
  8. use App\Http\Resources\AdminUserResource;
  9. use App\Http\Resources\AnnouncementResource;
  10. use App\Models\AdminMerchant;
  11. use App\Models\AdminMerchants;
  12. use App\Models\AdminRole;
  13. use App\Models\AdminUser;
  14. use App\Utils\Admin;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Storage;
  18. class AdminUserMerchantController extends AdminBaseController
  19. {
  20. public function editUser()
  21. {
  22. $user = Admin::user();
  23. $user->load(['roles', 'permissions', 'areas']);
  24. return $this->ok(AdminUserResource::make($user));
  25. }
  26. public function updateUser(AdminMerchantRequest $request)
  27. {
  28. $inputs = $request->validated();
  29. Admin::user()->updateUser($inputs);
  30. return $this->callAction('user', [])->setStatusCode(201);
  31. }
  32. /**
  33. * 商户列表
  34. * @param AdminUserFilter $filter
  35. * @return \Illuminate\Http\JsonResponse
  36. */
  37. public function index(AdminUserFilter $filter)
  38. {
  39. $userQuery = AdminMerchant::query()
  40. ->filter($filter);
  41. $userQuery->with(['admin_user' => function ($query) {
  42. $query->where('type', AdminUser::TYPE_ADMIN);
  43. }]);
  44. if (!Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  45. // 技术
  46. $userQuery->where('id', -1);
  47. }
  48. $users = $userQuery->orderByDesc('id')->paginate();
  49. // return $users;
  50. return $this->ok(AdminMerchantsResource::collection($users));
  51. }
  52. public function store(AdminMerchantRequest $request, AdminMerchant $adminMerchants)
  53. {
  54. $user_add = [
  55. 'account' => $request->account,
  56. 'job_number' => $request->phone,
  57. 'name' => $request->name,
  58. 'phone' => $request->phone,
  59. 'password' => '1234@5678'// 密码默认手机号 123456..
  60. ];
  61. if (!Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  62. // 技术管理员
  63. return $this->error('权限不足');
  64. }
  65. $res = AdminUser::query()->where('account', $request->account)->first();
  66. if ($res) {
  67. return $this->error('账号已经存在');
  68. }
  69. // $add_res = AdminUser::create($user_add);
  70. $add_res = AdminUser::createUser($user_add);
  71. $roles_id = AdminRole::query()->where('slug', AdminRole::MERCHANTSADMIN)->first();
  72. if ($roles_id) {
  73. $roles = [$roles_id->id];
  74. $add_res->roles()->attach($roles);
  75. }
  76. $order_key_data = AdminMerchant::query()->whereNotNull('order_key')->get(['order_key'])->toArray();
  77. $add_mch = [
  78. 'name' => $request->name,
  79. 'phone' => $request->phone,
  80. 'proportion' => $request->proportion ?: 0,
  81. 'email' => $request->email,
  82. 'admin_id' => $add_res->id,
  83. 'mck_key' => Str::uniqueString(),// 商户唯一key
  84. 'order_key' => Str::getChr(array_column($order_key_data, 'order_key')),// 商户唯一order_key
  85. ];
  86. $adminMerchants->create($add_mch);
  87. return $this->ok(AdminMerchantsResource::make($adminMerchants));
  88. }
  89. public function show(AdminUser $adminUser)
  90. {
  91. $adminUser->load(['roles', 'permissions', 'areas']);
  92. return $this->ok(AdminUserResource::make($adminUser));
  93. }
  94. /**
  95. * @param AdminMerchantRequest $request
  96. * @param AdminMerchant $adminMerchants
  97. * @return \Illuminate\Http\JsonResponse
  98. */
  99. public function update(AdminMerchantRequest $request, AdminMerchant $adminMerchants)
  100. {
  101. $inputs = $request->validated();
  102. unset($inputs['account']);
  103. $adminMerchants->update($inputs);
  104. return $this->created(AdminMerchantsResource::make($adminMerchants));
  105. }
  106. public function destroy(AdminMerchant $adminMerchants)
  107. {
  108. $adminMerchants->delete();
  109. return $this->noContent();
  110. }
  111. /**
  112. * @param AdminMerchant $adminMerchants
  113. * @return \Illuminate\Http\JsonResponse
  114. */
  115. public function edit(AdminMerchant $adminMerchants)
  116. {
  117. return $this->ok(AdminMerchantsResource::make($adminMerchants));
  118. }
  119. // 商户配置信息
  120. public function editMerchantConfig(Request $request)
  121. {
  122. //
  123. //
  124. $user = Admin::user();
  125. $params = $request->all();
  126. $admin_id = 0;
  127. if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  128. // 技术
  129. // return $this->error('您不是商户不能修改!');
  130. $admin_id = $params['admin_id'];
  131. }
  132. if (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
  133. $admin_id = $user->id;
  134. }
  135. if (empty($admin_id)) {
  136. return $this->error('您无权修改!');
  137. }
  138. if ($params['sub_type'] == 1) {
  139. // 基础设置
  140. $data = [
  141. 'name' => $params['name'],
  142. 'proportion' => $params['proportion'],
  143. 'cash' => $params['cash'],
  144. 'is_refund' => $params['is_refund'],
  145. 'wxapp_qr_code' => $params['wxapp_qr_code'],
  146. 'username' => $params['username'],
  147. 'area_name' => $params['area_name'],
  148. 'refund_callback_url' => $params['refund_callback_url'],
  149. 'car_qr_code_url' => $params['car_qr_code_url'],
  150. 'ground_qr_code' => $params['ground_qr_code'],
  151. 'factory_qr_code' => $params['factory_qr_code'],
  152. ];
  153. }
  154. if ($params['sub_type'] == 2) {
  155. $data = [
  156. 'wxapp_app_id' => $params['wxapp_app_id'],
  157. 'wxapp_app_secret' => $params['wxapp_app_secret'],
  158. 'wxapp_qr_code' => $params['wxapp_qr_code'],
  159. 'wxapp_name' => $params['wxapp_name'],
  160. 'brand' => $params['brand'],
  161. 'wxapp_desc' => $params['wxapp_desc'],
  162. ];
  163. }
  164. if ($params['sub_type'] == 3) {
  165. $data = [
  166. 'pay_mch_id' => $params['pay_mch_id'],
  167. 'pay_key' => $params['pay_key'],
  168. 'pay_cert_path' => $params['pay_cert_path'],
  169. 'pay_key_path' => $params['pay_key_path'],
  170. ];
  171. }
  172. if ($params['sub_type'] == 4) {
  173. $data = [
  174. 'alipaymini_appId' => $params['alipaymini_appId'],
  175. 'alipaymini_merchantPrivateKey' => $params['alipaymini_merchantPrivateKey'],
  176. 'alipaymini_alipayCertPath' => $params['alipaymini_alipayCertPath'],
  177. 'alipaymini_alipayRootCertPath' => $params['alipaymini_alipayRootCertPath'],
  178. 'alipaymini_merchantCertPath' => $params['alipaymini_merchantCertPath'],
  179. 'alipaymini_aesKey' => $params['alipaymini_aesKey'],
  180. 'alipaymini_pid' => $params['alipaymini_pid']
  181. ];
  182. }
  183. if ($params['sub_type'] == 5) {
  184. $eol = PHP_EOL;//\r\n 传过来 \n
  185. $alipayarrJson = $this->getArrencode($params['alipaymini_message_json']);
  186. $wxapparrJson = $this->getArrencode($params['wxapp_message_json']);
  187. $data = [
  188. 'alipaymini_message_json' => $alipayarrJson,
  189. 'wxapp_message_json' => $wxapparrJson
  190. ];
  191. }
  192. if (count($data)) {
  193. $this->error('缺少参数');
  194. }
  195. $update_res = AdminMerchant::query()->where('admin_id', $admin_id)->update($data);
  196. if ($update_res) {
  197. return $this->ok(['status' => 1]);
  198. }
  199. return $this->error('更新失败');
  200. }
  201. public function getMerchantConfig(Request $request)
  202. {
  203. $user = Admin::user();
  204. $admin_id = $user->id;
  205. $data = [];
  206. if (Admin::user()->isRole(AdminRole::ADMINISTROTOR)) {
  207. // 技术
  208. $model = AdminMerchant::query();
  209. if ($request->id) {
  210. $model->where('id', $request->id);
  211. }
  212. $data = $model->first();
  213. $data['mch_list'] = AdminMerchant::query()->where(AdminMerchant::getMerchantWhere())->get(['id', 'name']);
  214. } elseif (Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
  215. $data = AdminMerchant::query()->where('admin_id', $admin_id)->first();
  216. $data['mch_list'] = AdminMerchant::query()->where('id', AdminMerchant::putMerchantId())->get(['id', 'name']);
  217. } else {
  218. return $this->error('权限不足!');
  219. }
  220. if($data->alipaymini_message_json!=''){
  221. $data->alipaymini_message_json = $this->getArrdecode($data->alipaymini_message_json);
  222. $data->wxapp_message_json = $this->getArrdecode($data->wxapp_message_json);
  223. }
  224. return $data;
  225. }
  226. public function getArrdecode($json){
  227. $arr = json_decode($json);
  228. if($arr!= ''){
  229. foreach($arr as $key => $val){
  230. $arr[$key] = implode('=',$val);
  231. }
  232. }
  233. return implode("\n",$arr );
  234. }
  235. public function getArrencode($param){
  236. $huanhang = str_replace(array("\r","\n","\r\n"),"*******",$param);
  237. $arr = explode("*******",$huanhang);
  238. foreach ($arr as $key => $val){
  239. $arr[$key]= explode('=',$val);
  240. }
  241. $alipayarrJson = json_encode($arr);
  242. return $alipayarrJson;
  243. }
  244. // 上传图片
  245. public function adminMerchantUplode(request $request)
  246. {
  247. $dat = $request->all();//接收所有的
  248. $file = $request->wxapp_qr_code;//接前台图片
  249. if (empty($file)) {
  250. $file = $request->factory_qr_code;//接前台图片
  251. }
  252. if (empty($file)) {
  253. $file = $request->ground_qr_code;//接前台图片
  254. }
  255. // $file = $request->avatar;
  256. $folder_name = "uploads/images/avatars/" . date("Ym/d", time());
  257. $upload_path = public_path() . '/' . $folder_name;
  258. $extension = strtolower($file->getClientOriginalExtension()) ?: 'png';
  259. $filename = time() . '_' . Str::random(10) . '.' . $extension;
  260. $file->move($upload_path, $filename);
  261. $url = config('app.url') . "/$folder_name/$filename";
  262. return $this->ok(['path' => $url]);
  263. }
  264. public function adminMerchantPay(request $request)
  265. {
  266. $type = $request->get('type');
  267. $id = $request->get('id');
  268. $file = $request->file('file');//接前台图片
  269. $pay_type = $request->get('pay_type',1); //证书类型 支付宝 2/微信 1
  270. if($pay_type==1){
  271. $folder = "{$id}";
  272. }else{
  273. $folder = "{$id}/zhifubao";
  274. }
  275. $filename = "{$type}." . strtolower($file->getClientOriginalExtension());
  276. // 将图片移动到我们的目标存储路径中
  277. $path = Storage::disk('merchant')->putFileAs($folder, $file, $filename);
  278. return $this->ok(['path' => $path]);
  279. }
  280. }