Merchant.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\badminapi\controller\merchant;
  3. use app\badminapi\controller\AuthController;
  4. use app\models\merchant\MerchantMiniprogram;
  5. use app\models\merchant\Merchant as MerchantModel;
  6. use app\models\merchant\Industry as IndustryModel;
  7. use app\Request;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\UtilService;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\Exception;
  14. use think\Response;
  15. class Merchant extends AuthController
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. }
  21. /**
  22. * 注册商户
  23. * @param Request $request
  24. * @return mixed
  25. * @throws \Exception
  26. */
  27. public function registerMerchant(Request $request)
  28. {
  29. $data = UtilService::postMore([
  30. ['name', '', '', '', [
  31. 'empty_check',
  32. function ($val) {
  33. return MerchantModel::be(['name' => $val]);
  34. }
  35. ], ['请输入店铺名称', '已存在同名店铺']],
  36. ['logo', '', '', '', 'empty_check', '请上传店铺Logo'],
  37. ['business_license', '', '', '', 'empty_check', '请输入营业执照号'],
  38. ['lx_name', '', '', '', 'empty_check', '请输入联系人'],
  39. ['phone', '', '', '', 'empty_check', '请输入电话'],
  40. ['industry_id', '', '', '', 'empty_check', '请选择所属行业'],
  41. ['address', '', '', '', 'empty_check', '', '请填写完整店铺地址'],
  42. ['detail_address', '', '', '', 'empty_check', '请填写完整店铺地址'],
  43. ['facilities', [], '', '', 'empty_check', '请输入店内设施'],
  44. ['day_time', '', '', '', 'empty_check', '请选择营业时间'],
  45. ['local', '', '', '', 'empty_check', '请填写商户域名'],
  46. ], $request, false);
  47. BaseModel::beginTrans();
  48. try {
  49. $data['industry_id'] = implode(',', $data['industry_id']);
  50. $data['address'] = implode(',', $data['address']);
  51. $data['facilities'] = implode(',', $data['facilities']);
  52. $data['day_time'] = implode(' - ', $data['day_time']);
  53. $data['add_time'] = time();
  54. $data['version'] = 1;
  55. $data['valid_time'] = strtotime("+1 year");
  56. if ($data['local']) $data['local_md5'] = strtolower(md5($data['local']));
  57. $res = MerchantModel::create($data);
  58. if ($res) {
  59. BaseModel::commitTrans();
  60. return app('json')->success('注册商户成功', ['id' => $res->id]);
  61. } else {
  62. BaseModel::rollbackTrans();
  63. return app('json')->fail('注册商户失败');
  64. }
  65. } catch (Exception $e) {
  66. BaseModel::rollbackTrans();
  67. return app('json')->fail($e->getMessage());
  68. } catch (DbException $e) {
  69. BaseModel::rollbackTrans();
  70. return app('json')->fail($e->getMessage());
  71. }
  72. }
  73. /**
  74. * 编辑商户
  75. * @param $mer_id
  76. * @param Request $request
  77. * @return mixed
  78. * @throws \Exception
  79. */
  80. public function editMerchant($mer_id, Request $request)
  81. {
  82. $data = UtilService::postMore([
  83. ['name', ''],
  84. ['logo', ''],
  85. ['business_license', ''],
  86. ['lx_name', ''],
  87. ['phone', ''],
  88. ['industry_id', ''],
  89. ['address', ''],
  90. ['detail_address', ''],
  91. ['facilities', []],
  92. ['day_time', ''],
  93. ['local', ''],
  94. ], $request, false);
  95. if (!($mer_id && MerchantModel::be($mer_id))) {
  96. return app('json')->fail('商户不存在');
  97. }
  98. $update = [];
  99. $data['day_time'] = implode(' - ', $data['day_time']);
  100. foreach ($data as $k => $v) {
  101. if (!empty_check($v)) {
  102. if (is_array($v))
  103. $update[$k] = implode(',', $v);
  104. else
  105. $update[$k] = $v;
  106. }
  107. }
  108. BaseModel::beginTrans();
  109. try {
  110. $res = MerchantModel::edit($update, $mer_id);
  111. if ($res) {
  112. BaseModel::commitTrans();
  113. return app('json')->success('编辑商户成功');
  114. } else {
  115. BaseModel::rollbackTrans();
  116. return app('json')->fail('编辑商户失败');
  117. }
  118. } catch (Exception $e) {
  119. BaseModel::rollbackTrans();
  120. return app('json')->fail($e->getMessage());
  121. } catch (DbException $e) {
  122. BaseModel::rollbackTrans();
  123. return app('json')->fail($e->getMessage());
  124. }
  125. }
  126. /**
  127. * 编辑商户支付信息
  128. * @param $mer_id
  129. * @param Request $request
  130. * @return mixed
  131. * @throws \Exception
  132. */
  133. public function editMerchantPay($mer_id, Request $request)
  134. {
  135. $data = UtilService::postMore([
  136. ['mch_id', ''],
  137. ['mch_key', ''],
  138. ['mch_cert_p12', ''],
  139. ], $request, false);
  140. if (!($mer_id && MerchantMiniprogram::vaildWhere()->where(['mer_id' => $mer_id])->count() > 0)) {
  141. return app('json')->fail('商户小程序尚未关联');
  142. }
  143. $update = [];
  144. foreach ($data as $k => $v) {
  145. if (!empty_check($v)) {
  146. $update[$k] = $v;
  147. }
  148. }
  149. BaseModel::beginTrans();
  150. try {
  151. $update['update'] = time();
  152. $res = MerchantMiniprogram::where('mer_id', $mer_id)->update($update);
  153. if ($res) {
  154. BaseModel::commitTrans();
  155. return app('json')->success('编辑商户支付信息成功');
  156. } else {
  157. BaseModel::rollbackTrans();
  158. return app('json')->fail('编辑商户支付信息失败');
  159. }
  160. } catch (Exception $e) {
  161. BaseModel::rollbackTrans();
  162. return app('json')->fail($e->getMessage());
  163. } catch (DbException $e) {
  164. BaseModel::rollbackTrans();
  165. return app('json')->fail($e->getMessage());
  166. }
  167. }
  168. /**
  169. * 获取商户列表
  170. * @param Request $request
  171. * @return mixed
  172. * @throws \Exception
  173. */
  174. public function getMerchantList(Request $request)
  175. {
  176. $where = UtilService::getMore([
  177. ['page', 1],
  178. ['limit', 10],
  179. ['key_word', ''],
  180. ['industry_id', ''],
  181. ['version', ''],
  182. ['data', ''],
  183. ], $request, false);
  184. $list = MerchantModel::getList($where);
  185. return app('json')->success('ok', $list);
  186. }
  187. /**
  188. * 获取商户详情
  189. * @param $mer_id
  190. * @param Request $request
  191. * @return mixed
  192. * @throws DbException
  193. * @throws DataNotFoundException
  194. * @throws ModelNotFoundException
  195. */
  196. public function getMerchantDetail($mer_id, Request $request)
  197. {
  198. if (!$mer_id) {
  199. return app('json')->fail('商户不存在');
  200. }
  201. $merchant = MerchantModel::get($mer_id);
  202. if (!$merchant) {
  203. return app('json')->fail('商户不存在');
  204. }
  205. $merchant = $merchant->toArray();
  206. $merchant['miniprogram_info'] = ($data = MerchantMiniprogram::vaildWhere()->where('mer_id', $mer_id)->find()) && $data ? $data->toArray() : [];
  207. $merchant['industry_name'] = IndustryModel::where('id', 'IN', $merchant['industry_id'])->column('name');
  208. $merchant['industry_id'] = $merchant['industry_id'] ? explode(',', $merchant['industry_id']) : [];
  209. $merchant['address'] = $merchant['address'] ? explode(',', $merchant['address']) : [];
  210. $merchant['facilities'] = $merchant['facilities'] ? explode(',', $merchant['facilities']) : [];
  211. $merchant['day_time'] = $merchant['day_time'] ? explode(' - ', $merchant['day_time']) : [];
  212. return app('json')->success('ok', $merchant);
  213. }
  214. /**
  215. * 删除指定资源
  216. *
  217. * @param $mer_id
  218. * @return Response
  219. */
  220. public function delete($mer_id)
  221. {
  222. if (!MerchantMiniprogram::delMiniprogram($mer_id))
  223. return $this->fail(MerchantMiniprogram::getErrorInfo('删除失败,请稍候再试!'));
  224. else
  225. return $this->success('删除成功!');
  226. }
  227. }