Mchid.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商户管理
  7. */
  8. namespace app\allwin\controller;
  9. use think\facade\Request;
  10. use think\Validate;
  11. use app\allwin\model\MchId as MchIdStore;
  12. use app\allwin\model\AllwinStore;
  13. use app\allwin\model\MchIdQueen;
  14. use app\allwin\model\MchIdUser;
  15. use filter\Filter;
  16. class Mchid extends Common{
  17. public $allwinconfig = [];
  18. public function initialize(){
  19. parent::initialize();
  20. $this->assign('pathMaps',[['name'=>'商户管理','url'=>url("mchid/index")]]);
  21. $this->allwinconfig = model('AllwinConfig')->getConfig($this->member_miniapp_id);
  22. }
  23. /**
  24. * 商户列表
  25. * @return void
  26. */
  27. public function index(int $types = 0){
  28. $is_pass = 0;
  29. switch ($types) {
  30. case 1:
  31. $is_pass = 0;
  32. break;
  33. case 2:
  34. $is_pass = 1;
  35. break;
  36. case 3:
  37. $is_pass = 2;
  38. break;
  39. case 4:
  40. $is_pass = -1;
  41. break;
  42. }
  43. $where = [];
  44. if ($types) {
  45. $where['is_pass'] = $is_pass;
  46. }
  47. $view['lists'] = MchIdStore::where($this->mini_program)->where($where)->order('is_default desc,is_pass desc,id desc')->paginate(20);
  48. $view['types'] = $types;
  49. return view()->assign($view);
  50. }
  51. /**
  52. * 商家选择
  53. * @return void
  54. */
  55. public function selectmchid(int $types = 0){
  56. $keyword = Request::param('keyword');
  57. if(!empty($keyword)){
  58. $sql = MchIdStore::where($this->mini_program)->whereLike('merchant_shortname','%'.$keyword.'%');
  59. }else{
  60. $sql = MchIdStore::where($this->mini_program);
  61. }
  62. $where = [];
  63. $where['is_pass'] = 2;
  64. $where['status'] = 0;
  65. $view['lists'] = $sql->where($where)->order('is_default desc,is_pass desc,id desc')->paginate(20);
  66. $view['input'] = Request::param('input');
  67. $view['types'] = $types;
  68. $view['keyword'] = $keyword;
  69. return view()->assign($view);
  70. }
  71. /**
  72. * 商户号预览
  73. * @return void
  74. */
  75. public function review(){
  76. $condition['id'] = Request::param('id');
  77. $condition['is_pass'] = 2;
  78. $info = MchIdStore::where($this->mini_program)->where($condition)->find();
  79. if(empty($info)){
  80. $this->error('未找到你要查找的商家信息或管理员更改了商家权限');
  81. }
  82. $view['info'] = $info;
  83. return view()->assign($view);
  84. }
  85. /**
  86. * 申请开通小微商户
  87. * @return void
  88. */
  89. public function reg(){
  90. if(!$this->allwinconfig->is_psp){
  91. $this->error('非服务商模式,禁止添加小微商户');
  92. }
  93. if(request()->isAjax()){
  94. $data = [
  95. 'member_miniapp_id' => $this->member_miniapp_id,
  96. 'merchant_shortname' => Request::param('merchant_shortname/s'),
  97. 'store_name' => Request::param('store_name/s'),
  98. 'store_street' => Request::param('store_street/s'),
  99. 'contact_phone' => Request::param('contact_phone/s'),
  100. 'product_desc' => Request::param('product_desc/s'),
  101. 'store_entrance_pic' => Request::param('store_entrance_pic/s'),
  102. 'indoor_pic' => Request::param('indoor_pic/s'),
  103. 'id_card_name' => Request::param('id_card_name/s'),
  104. 'id_card_number' => Request::param('id_card_number/s'),
  105. 'id_card_copy' => Request::param('id_card_copy/s'),
  106. 'id_card_national' => Request::param('id_card_national/s'),
  107. 'account_bank' => Request::param('account_bank/s'),
  108. 'account_number' => Request::param('account_number/s'),
  109. 'bank_address_code' => Request::param('bank_address_code/s'),
  110. 'business_code' => strtoupper(md5('MIC'.order_no())),
  111. 'member_id' => $this->user['id'],
  112. ];
  113. $validate = $this->validate($data,'MchId.reg');
  114. if(true !== $validate){
  115. return json(['code'=>0,'msg'=>$validate]);
  116. }
  117. $id_time['start_time'] = Request::param('start_time/s');
  118. $id_time['end_time'] = Request::param('end_time/s');
  119. $validate_time = Validate::make()->rule('start_time', 'require|date')->rule('end_time','date')->check($id_time);
  120. if(true !== $validate_time){
  121. return json(['code'=>0,'msg'=>'身份证有效期填写错误']);
  122. }
  123. if(!empty($id_time['end_time'])){
  124. if(strtotime($id_time['start_time']) > strtotime($id_time['end_time'])){
  125. return json(['code'=>0,'msg'=>'身份证有效期开始日期不能大于结束日期']);
  126. }
  127. }
  128. $data['id_card_start_time'] = $id_time['start_time'];
  129. $data['id_card_end_time'] = $id_time['end_time'] ?:'';
  130. $result = MchIdStore::reg($data);
  131. if($result){
  132. return json(['code'=>200,'url'=>url('mchid/index'),'msg'=>'操作成功']);
  133. }else{
  134. return json(['code'=>0,'msg'=>'操作失败']);
  135. }
  136. }else{
  137. return view();
  138. }
  139. }
  140. /**
  141. * 未通过审核待提交
  142. * @return void
  143. */
  144. public function rebuild(){
  145. if(!$this->allwinconfig->is_psp){
  146. $this->error('非服务商模式,禁止添加小微商户');
  147. }
  148. if(request()->isAjax()){
  149. $data = [
  150. 'id' => Request::param('id/d'),
  151. 'member_miniapp_id' => $this->member_miniapp_id,
  152. 'merchant_shortname' => Request::param('merchant_shortname/s'),
  153. 'store_name' => Request::param('store_name/s'),
  154. 'store_street' => Request::param('store_street/s'),
  155. 'contact_phone' => Request::param('contact_phone/s'),
  156. 'product_desc' => Request::param('product_desc/s'),
  157. 'store_entrance_pic' => Request::param('store_entrance_pic/s'),
  158. 'indoor_pic' => Request::param('indoor_pic/s'),
  159. 'id_card_name' => Request::param('id_card_name/s'),
  160. 'id_card_number' => Request::param('id_card_number/s'),
  161. 'id_card_copy' => Request::param('id_card_copy/s'),
  162. 'id_card_national' => Request::param('id_card_national/s'),
  163. 'account_bank' => Request::param('account_bank/s'),
  164. 'account_number' => Request::param('account_number/s'),
  165. 'bank_address_code' => Request::param('bank_address_code/s'),
  166. 'business_code' => strtoupper(md5('MIC'.order_no())),
  167. 'member_id' => $this->user['id'],
  168. ];
  169. $validate = $this->validate($data,'MchId.reg');
  170. if(true !== $validate){
  171. return json(['code'=>0,'msg'=>$validate]);
  172. }
  173. $id_time['start_time'] = Request::param('start_time/s');
  174. $id_time['end_time'] = Request::param('end_time/s');
  175. $validate_time = Validate::make()->rule('start_time', 'require|date')->rule('end_time','date')->check($id_time);
  176. if(true !== $validate_time){
  177. return json(['code'=>0,'msg'=>'身份证有效期填写错误']);
  178. }
  179. if(!empty($id_time['end_time'])){
  180. if(strtotime($id_time['start_time']) > strtotime($id_time['end_time'])){
  181. return json(['code'=>0,'msg'=>'身份证有效期开始日期不能大于结束日期']);
  182. }
  183. }
  184. $data['id_card_start_time'] = $id_time['start_time'];
  185. $data['id_card_end_time'] = $id_time['end_time'] ?:'';
  186. $result = MchIdStore::reg($data);
  187. if($result){
  188. return json(['code'=>200,'url'=>url('mchid/index'),'msg'=>'操作成功']);
  189. }else{
  190. return json(['code'=>0,'msg'=>'操作失败']);
  191. }
  192. }else{
  193. $id = Request::param('id');
  194. $info = MchIdStore::where(['id' => $id,'is_types' => 1])->where('is_pass','<=','0')->find();
  195. if(empty($info)){
  196. $this->error('未找到商家信息');
  197. }
  198. $info['start_time'] = $info->id_card_start_time ?:'';
  199. $info['end_time'] = $info->id_card_end_time ?:'';
  200. $view['info'] = $info;
  201. return view()->assign($view);
  202. }
  203. }
  204. /**
  205. * 商家关联
  206. * @return void
  207. */
  208. public function add(){
  209. if(request()->isAjax()){
  210. $data = [
  211. 'member_miniapp_id' => $this->member_miniapp_id,
  212. 'mchid' => Request::param('mchid/d',0),
  213. 'merchant_shortname'=> Request::param('merchant_shortname/s'),
  214. 'store_name' => Request::param('store_name/s'),
  215. 'contact_phone' => Request::param('contact_phone/s'),
  216. 'store_street' => Request::param('store_street/s'),
  217. 'member_id' => $this->user->id,
  218. 'business_code' => strtoupper(md5('MIC'.order_no())),
  219. ];
  220. $validate = $this->validate($data,'MchId.edit');
  221. if(true !== $validate){
  222. return json(['code'=>0,'msg'=>$validate]);
  223. }
  224. $mchid = MchIdStore::where(['mchid' => $data['mchid']])->count();
  225. if($mchid){
  226. return json(['code'=>0,'msg'=>'商户号重复']);
  227. }
  228. $result = MchIdStore::edit($data);
  229. if($result){
  230. return json(['code'=>200,'url'=>url('mchid/index'),'msg'=>'操作成功']);
  231. }else{
  232. return json(['code'=>0,'msg'=>'操作失败']);
  233. }
  234. }else{
  235. return view();
  236. }
  237. }
  238. /**
  239. * 商家关联
  240. * @return void
  241. */
  242. public function edit(){
  243. if(request()->isAjax()){
  244. $data = [
  245. 'member_miniapp_id' => $this->member_miniapp_id,
  246. 'id' => Request::param('id/d',0),
  247. 'original_mchid' => Request::param('original_mchid/d',0),
  248. 'mchid' => Request::param('mchid/d',0),
  249. 'store_name' => Request::param('store_name/s'),
  250. 'merchant_shortname'=> Request::param('merchant_shortname/s'),
  251. 'contact_phone' => Request::param('contact_phone/s'),
  252. 'store_street' => Request::param('store_street/s'),
  253. ];
  254. $validate = $this->validate($data,'MchId.edit');
  255. if(true !== $validate){
  256. return json(['code'=>0,'msg'=>$validate]);
  257. }
  258. $mchid = MchIdStore::where(['mchid' => $data['mchid']])->where('id','<>',$data['id'])->count();
  259. if($mchid){
  260. return json(['code'=>0,'msg'=>'商户号重复']);
  261. }
  262. $result = MchIdStore::edit($data);
  263. if($result){
  264. if($data['original_mchid'] != $data['mchid']){
  265. MchIdUser::where($this->mini_program)->where(['store_id' => $data['id']])->delete(); //删除队列
  266. }
  267. return json(['code'=>200,'url'=>url('mchid/index'),'msg'=>'操作成功']);
  268. }else{
  269. return json(['code'=>0,'msg'=>'操作失败']);
  270. }
  271. }else{
  272. $condition['id'] = Request::param('id');
  273. $condition['is_types'] = 0;
  274. $view['info'] = MchIdStore::where($this->mini_program)->where($condition)->find();
  275. if(empty($view['info'])){
  276. $this->error('未找到商家信息');
  277. }
  278. return view()->assign($view);
  279. }
  280. }
  281. //删除
  282. public function delStore(int $id){
  283. //判断是否有商家在使用
  284. $info = AllwinStore::where($this->mini_program)->where(['mch_id' => $id])->count();
  285. if($info){
  286. return json(['code'=>0,'msg'=>'有加盟商家正在使用当前商户号,禁止删除']);
  287. }
  288. //判断分账
  289. $info = MchIdQueen::where($this->mini_program)->where(['mch_id' => $id])->count();
  290. if($info){
  291. return json(['code'=>0,'msg'=>'有分账资源,禁止删除']);
  292. }
  293. //删除
  294. $condition = [];
  295. $result = MchIdStore::where($this->mini_program)->where($condition)->where(['id' => $id,'is_delete' => 0])->delete();
  296. if($result){
  297. return json(['code'=>200,'msg'=>'操作成功']);
  298. }else{
  299. return json(['code'=>0,'msg'=>'删除失败']);
  300. }
  301. }
  302. /**
  303. * 判断商户是否重复
  304. * @return boolean
  305. */
  306. public function is_mchid(){
  307. $mchid = Request::param('param/d');
  308. if(empty($mchid)){
  309. return json(['status'=>'n','info'=>'未输入值']);
  310. }
  311. $result = MchIdStore::where(['mchid' => $mchid])->find();
  312. if(empty($result)){
  313. return json(['status'=>'y','info'=>'商户号可用']);
  314. }else{
  315. return json(['status'=>'n','info'=>'商户号重复']);
  316. }
  317. }
  318. /**
  319. * 设置平台默认帐号
  320. * @param integer $id 用户ID
  321. */
  322. public function isTop(int $id){
  323. $result = MchIdStore::where(['id' => $id])->find();
  324. if(empty($result->is_pass)){
  325. return enjson(0,'未通过审核的商户,禁止设为默认商户');
  326. }
  327. if($result->is_default == 1){
  328. return enjson(0,'已是默认商户号,不用重复设置');
  329. }
  330. $rel = MchIdStore::where($this->mini_program)->where(['is_default' => 1])->find();
  331. if($rel){
  332. $rel->is_default = 0;
  333. $rel->save();
  334. }
  335. $result->is_default = 1;
  336. $result = $result->save();
  337. if(!$result){
  338. MchIdUser::where($this->mini_program)->where(['mch_id' => $rel->id])->delete(); //删除队列
  339. return enjson(0,'操作失败');
  340. }else{
  341. return enjson(200,'操作成功');
  342. }
  343. }
  344. /**
  345. * 查看城市编码
  346. * @param integer $id 用户ID
  347. */
  348. public function bankAddressCode(){
  349. $parms = [
  350. 'province' => Request::param('province/s'),
  351. 'city' => Request::param('city/s'),
  352. 'district' => Request::param('district/s')
  353. ];
  354. $validate = $this->validate($parms,'MchId.address_code');
  355. if (true !== $validate) {
  356. return json(['code'=>403,'msg'=>$validate]);
  357. }
  358. $jsonStr = file_get_contents(PATH_STATIC.'allwin/citycode.json');
  359. $arr = json_decode($jsonStr, true);
  360. if(empty($arr['中国'][$parms['province']])){
  361. return json(['code'=>403,'msg'=>'省份和地区不存在']);
  362. }
  363. if(empty($arr['中国'][$parms['province']][$parms['city']])){
  364. $code = $arr['中国'][$parms['province']][$parms['district']]['code'];
  365. }else{
  366. $code = $arr['中国'][$parms['province']][$parms['city']][$parms['district']]['code'];
  367. }
  368. return json(['code'=>200,'msg'=>'成功','data'=> $code]);
  369. }
  370. /**
  371. * 微信分账队列
  372. * @return void
  373. */
  374. public function queen(int $types = 0,string $order = ''){
  375. $condition = [];
  376. if(!empty($order)){
  377. $condition['out_order_no'] = Filter::filter_escape(trim($order));
  378. }
  379. $store_id = Request::param('store_id',0);
  380. $store_name = Request::param('store_name');
  381. if($store_id){
  382. $condition['store_id'] = $store_id;
  383. }
  384. $view['amount_quree'] = MchIdQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 0])->sum('amount');
  385. $view['amount_success'] = MchIdQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 2])->sum('amount');
  386. $view['amount_fail'] = MchIdQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 1])->sum('amount');
  387. if($types){
  388. switch ($types) {
  389. case 2:
  390. $is_finish = 2;
  391. break;
  392. case 3:
  393. $is_finish = 1;
  394. break;
  395. default:
  396. $is_finish = 0;
  397. break;
  398. }
  399. $condition['is_finish'] = $is_finish;
  400. }
  401. $orderby = $types == 1 ? 'id asc':'id desc';
  402. $view['lists'] = MchIdQueen::where($this->mini_program)->where($condition)->order($orderby)->paginate(10,false,[
  403. 'query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name],
  404. ]);
  405. $view['types'] = $types;
  406. $view['order'] = $order;
  407. $view['store_id'] = $store_id;
  408. $view['store_name'] = $store_name;
  409. $view['pathMaps'] = [['name'=>'结算队列','url'=>url("mchid/queen")]];
  410. return view()->assign($view);
  411. }
  412. /**
  413. * 重置分账队列
  414. * @return void
  415. */
  416. public function resetQueen(string $order){
  417. $condition = [];
  418. $condition['out_order_no'] = Filter::filter_escape(trim($order));
  419. $condition['is_finish'] = 1;
  420. $rueen = MchIdQueen::where($this->mini_program)->where($condition)->update(['is_finish' => 0]);
  421. if($rueen){
  422. return json(['code'=>200,'message'=>'成功']);
  423. }else{
  424. return json(['code'=>0,'message'=>'失败,只有分账失败的才允许重置队列']);
  425. }
  426. }
  427. /**
  428. * 锁定
  429. * @param integer $id 用户ID
  430. */
  431. public function islock(int $id){
  432. $result = MchIdStore::isLock($id,$this->member_miniapp_id);
  433. if($result){
  434. return enjson(200,'操作成功');
  435. }else{
  436. return enjson(0,'操作失败');
  437. }
  438. }
  439. }