PropertyActivityLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\property;
  15. use app\common\model\property\PropertyActivity;
  16. use app\common\logic\BaseLogic;
  17. use app\common\service\wechat\WeChatMnpService;
  18. use think\Exception;
  19. use think\facade\Db;
  20. use think\facade\Log;
  21. /**
  22. * PropertyActivity逻辑
  23. * Class PropertyActivityLogic
  24. * @package app\adminapi\logic
  25. */
  26. class PropertyActivityLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 添加
  30. * @param array $params
  31. * @return bool
  32. * @author likeadmin
  33. * @date 2024/11/21 15:04
  34. */
  35. public static function add(array $params): bool
  36. {
  37. Db::startTrans();
  38. try {
  39. PropertyActivity::create([
  40. 'property_head_id' => $params['property_head_id'],
  41. 'activity_name' => $params['activity_name'],
  42. 'activity_start_time' => $params['activity_start_time']?strtotime($params['activity_start_time']):0,
  43. 'activity_end_time' => $params['activity_end_time']?strtotime($params['activity_end_time']):0,
  44. 'block_data' => self::configureReservedField($params['block_data']??[], 'block_data'),
  45. 'coupon_data' => self::configureReservedField($params['coupon_data']??[], 'coupon_data'),
  46. 'url_page' => $params['url_page']??'',
  47. 'page_type' => $params['page_type']??0,
  48. 'images' => $params['images']??[],
  49. ]);
  50. Db::commit();
  51. return true;
  52. } catch (\Exception $e) {
  53. Db::rollback();
  54. self::setError($e->getMessage());
  55. return false;
  56. }
  57. }
  58. /**
  59. * @notes 编辑
  60. * @param array $params
  61. * @return bool
  62. * @author likeadmin
  63. * @date 2024/11/21 15:04
  64. */
  65. public static function edit(array $params): bool
  66. {
  67. Db::startTrans();
  68. try {
  69. PropertyActivity::where('id', $params['id'])->update([
  70. 'property_head_id' => $params['property_head_id'],
  71. 'activity_name' => $params['activity_name'],
  72. 'activity_start_time' => $params['activity_start_time']?strtotime($params['activity_start_time']):0,
  73. 'activity_end_time' => $params['activity_end_time']?strtotime($params['activity_end_time']):0,
  74. 'block_data' => json_encode(self::configureReservedField($params['block_data']??[], 'block_data')),
  75. 'coupon_data' => json_encode(self::configureReservedField($params['coupon_data']??[], 'coupon_data')),
  76. 'url_page' => $params['url_page']??'',
  77. 'page_type' => $params['page_type']??0,
  78. 'images' => json_encode($params['images']??[]),
  79. ]);
  80. Db::commit();
  81. return true;
  82. } catch (\Exception $e) {
  83. Db::rollback();
  84. self::setError($e->getMessage());
  85. return false;
  86. }
  87. }
  88. public static function configureReservedField($data, $reserved_type,$reserved_field = []): array
  89. {
  90. if($reserved_type === 'block_data'){
  91. $reserved_field = ['id','recommend_weight','goods_name'];
  92. foreach ($data as &$item) {
  93. foreach ($item['goods'] as &$good) {
  94. foreach ($good as $k=>$v) {
  95. if(!in_array($k,$reserved_field)){
  96. unset($good[$k]);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. if($reserved_type === 'coupon_data'){
  103. $reserved_field = ['id','voucher_count','voucher_status','server_category_name','event_name','couponWithCategory','expire_time',
  104. 'amount_require','amount','max_deductible_price','discount_ratio','mold_type','coupon_type','code','remaining_count'
  105. ];
  106. foreach ($data as &$item) {
  107. foreach ($item as $k=>$v) {
  108. if(!in_array($k,$reserved_field)){
  109. unset($item[$k]);
  110. }
  111. }
  112. }
  113. }
  114. return $data??[];
  115. }
  116. /**
  117. * @notes 删除
  118. * @param array $params
  119. * @return bool
  120. * @author likeadmin
  121. * @date 2024/11/21 15:04
  122. */
  123. public static function delete(array $params): bool
  124. {
  125. return PropertyActivity::destroy($params['id']);
  126. }
  127. /**
  128. * @notes 获取详情
  129. * @param $params
  130. * @return array
  131. * @author likeadmin
  132. * @date 2024/11/21 15:04
  133. */
  134. public static function detail($params): array
  135. {
  136. return PropertyActivity::findOrEmpty($params['id'])->toArray();
  137. }
  138. public static function getQRCode($params,$url='weixiu.kyjlkj.com')
  139. {
  140. try {
  141. // $mnp_page = (isset($params['mnp_page']) && $params['mnp_page'])?urldecode($params['mnp_page']):env('miniprogram.property_activity_qrcode', '');
  142. $mnp_page = 'pages/web_view/index';
  143. if(empty($params['mnp_page'])){
  144. throw new Exception('路径错误');
  145. }
  146. $scene_page = (isset($params['mnp_page']) && $params['mnp_page'])? $params['mnp_page']:'';
  147. Log::info('getQRCode:'.rawurlencode($scene_page));
  148. $response = (new WeChatMnpService())->getUnlimitedQRCode(
  149. 'page='.$scene_page.'&id='.$params['id'],
  150. $mnp_page,
  151. env('miniprogram.mini_env_version', 'release'),
  152. false
  153. );
  154. Log::info('getQRCode:'.json_encode([$response]));
  155. $qrcode = $response->getContent();
  156. if(!is_dir('./uploads/wx_qrcode/'.date('Ymd'))){
  157. mkdir('./uploads/wx_qrcode/'.date('Ymd'));
  158. }
  159. $file_name = 'uploads/wx_qrcode/'.date('Ymd').'/'.time().rand(1000,9999).'.png';
  160. file_put_contents($file_name, $qrcode);
  161. //$file_name = 'https://fushencdn.kyjlkj.com/uploads/miniqrcode/27f140aae615bbe16c1e888c14c9ab10.png';
  162. return $url.'/'.$file_name;
  163. } catch (\Throwable $e) {
  164. Log::info('getQRCode:'.$e->getMessage());
  165. return '';
  166. }
  167. }
  168. }