Merchant.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\adminapi\controller\v1\merchant;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\merchant\MerchantMiniprogram;
  5. use app\models\merchant\Merchant as MerchantModel;
  6. use app\Request;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\UtilService;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\Exception;
  13. use think\Response;
  14. class Merchant extends AuthController
  15. {
  16. public function initialize()
  17. {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. }
  20. /**
  21. * 注册商户
  22. * @param Request $request
  23. * @return mixed
  24. * @throws \Exception
  25. */
  26. public function registerMerchant(Request $request)
  27. {
  28. $data = UtilService::postMore([
  29. ['name', '', '', '', [
  30. 'empty_check',
  31. function ($val) {
  32. return MerchantModel::be(['name' => $val]);
  33. }
  34. ], ['请输入小程序名称', '已存在同名小程序']]
  35. ], $request, false);
  36. try {
  37. $data['add_time'] = time();
  38. $res = MerchantModel::create($data);
  39. if ($res)
  40. return app('json')->success('添加小程序成功', ['id' => $res->id]);
  41. else
  42. return app('json')->fail('添加小程序失败');
  43. } catch (Exception $e) {
  44. return app('json')->fail($e->getMessage());
  45. } catch (DbException $e) {
  46. return app('json')->fail($e->getMessage());
  47. }
  48. }
  49. /**
  50. * 编辑商户
  51. * @param $mer_id
  52. * @param Request $request
  53. * @return mixed
  54. * @throws \Exception
  55. */
  56. public function editMerchant($mer_id, Request $request)
  57. {
  58. $data = UtilService::postMore([
  59. ['name', ''],
  60. ['lx_name', ''],
  61. ['phone', '']
  62. ], $request, false);
  63. if (!($mer_id && MerchantModel::be($mer_id))) {
  64. return app('json')->fail('小程序不存在');
  65. }
  66. try {
  67. $res = MerchantModel::edit($data, $mer_id);
  68. if ($res)
  69. return app('json')->success('编辑小程序成功');
  70. else
  71. return app('json')->fail('编辑小程序失败');
  72. } catch (Exception $e) {
  73. return app('json')->fail($e->getMessage());
  74. } catch (DbException $e) {
  75. return app('json')->fail($e->getMessage());
  76. }
  77. }
  78. /**
  79. * 编辑商户支付信息
  80. * @param $mer_id
  81. * @param Request $request
  82. * @return mixed
  83. * @throws \Exception
  84. */
  85. public function editMerchantPay($mer_id, Request $request)
  86. {
  87. $data = UtilService::postMore([
  88. ['mch_id', ''],
  89. ['mch_key', ''],
  90. ['mch_cert_p12', ''],
  91. ], $request, false);
  92. if (!($mer_id && MerchantMiniprogram::vaildWhere()->where(['mer_id' => $mer_id])->count() > 0)) {
  93. return app('json')->fail('商户小程序尚未关联');
  94. }
  95. $update = [];
  96. foreach ($data as $k => $v) {
  97. if (!empty_check($v)) {
  98. $update[$k] = $v;
  99. }
  100. }
  101. BaseModel::beginTrans();
  102. try {
  103. $update['update'] = time();
  104. $res = MerchantMiniprogram::where('mer_id', $mer_id)->update($update);
  105. if ($res) {
  106. BaseModel::commitTrans();
  107. return app('json')->success('编辑商户支付信息成功');
  108. } else {
  109. BaseModel::rollbackTrans();
  110. return app('json')->fail('编辑商户支付信息失败');
  111. }
  112. } catch (Exception $e) {
  113. BaseModel::rollbackTrans();
  114. return app('json')->fail($e->getMessage());
  115. } catch (DbException $e) {
  116. BaseModel::rollbackTrans();
  117. return app('json')->fail($e->getMessage());
  118. }
  119. }
  120. /**
  121. * 获取商户列表
  122. * @param Request $request
  123. * @return mixed
  124. * @throws \Exception
  125. */
  126. public function getMerchantList(Request $request)
  127. {
  128. $where = UtilService::getMore([
  129. ['page', 1],
  130. ['limit', 10],
  131. ['name', '']
  132. ], $request, false);
  133. $list = MerchantModel::getList($where);
  134. return app('json')->success('ok', $list);
  135. }
  136. /**
  137. * 获取商户详情
  138. * @param $mer_id
  139. * @param Request $request
  140. * @return mixed
  141. * @throws DbException
  142. * @throws DataNotFoundException
  143. * @throws ModelNotFoundException
  144. */
  145. public function getMerchantDetail($mer_id, Request $request)
  146. {
  147. if (!$mer_id) {
  148. return app('json')->fail('小程序不存在');
  149. }
  150. $merchant = MerchantModel::get($mer_id);
  151. if (!$merchant) {
  152. return app('json')->fail('小程序不存在');
  153. }
  154. $merchant = $merchant->toArray();
  155. $merchant['miniprogram_info'] = ($data = MerchantMiniprogram::vaildWhere()->where('mer_id', $mer_id)->find()) && $data ? $data->toArray() : [];
  156. return app('json')->success('ok', $merchant);
  157. }
  158. /**
  159. * 删除指定资源
  160. *
  161. * @param $mer_id
  162. * @return Response
  163. */
  164. public function delete($mer_id)
  165. {
  166. if (!MerchantMiniprogram::delMiniprogram($mer_id))
  167. return $this->fail(MerchantMiniprogram::getErrorInfo('删除失败,请稍候再试!'));
  168. else
  169. return $this->success('删除成功!');
  170. }
  171. }