PropertyActivityLogic.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\coupon\CouponRules;
  16. use app\common\model\property\PropertyActivity;
  17. use app\common\logic\BaseLogic;
  18. use app\common\service\wechat\WeChatMnpService;
  19. use think\db\Query;
  20. use think\Exception;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. /**
  24. * PropertyActivity逻辑
  25. * Class PropertyActivityLogic
  26. * @package app\adminapi\logic
  27. */
  28. class PropertyActivityLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 添加
  32. * @param array $params
  33. * @return bool
  34. * @author likeadmin
  35. * @date 2024/11/21 15:04
  36. */
  37. public static function add(array $params): bool
  38. {
  39. Db::startTrans();
  40. try {
  41. PropertyActivity::create([
  42. 'property_head_id' => $params['property_head_id'],
  43. 'activity_name' => $params['activity_name'],
  44. 'activity_start_time' => $params['activity_start_time']?strtotime($params['activity_start_time']):0,
  45. 'activity_end_time' => $params['activity_end_time']?strtotime($params['activity_end_time']):0,
  46. 'block_data' => self::configureReservedField($params['block_data']??[], 'block_data'),
  47. 'coupon_data' => self::configureReservedField($params['coupon_data']??[], 'coupon_data'),
  48. 'url_page' => $params['url_page']??'',
  49. 'page_type' => $params['page_type']??0,
  50. 'images' => $params['images']??[],
  51. ]);
  52. Db::commit();
  53. return true;
  54. } catch (\Exception $e) {
  55. Db::rollback();
  56. self::setError($e->getMessage());
  57. return false;
  58. }
  59. }
  60. /**
  61. * @notes 编辑
  62. * @param array $params
  63. * @return bool
  64. * @author likeadmin
  65. * @date 2024/11/21 15:04
  66. */
  67. public static function edit(array $params): bool
  68. {
  69. Db::startTrans();
  70. try {
  71. PropertyActivity::where('id', $params['id'])->update([
  72. 'property_head_id' => $params['property_head_id'],
  73. 'activity_name' => $params['activity_name'],
  74. 'activity_start_time' => $params['activity_start_time']?strtotime($params['activity_start_time']):0,
  75. 'activity_end_time' => $params['activity_end_time']?strtotime($params['activity_end_time']):0,
  76. 'block_data' => json_encode(self::configureReservedField($params['block_data']??[], 'block_data')),
  77. 'coupon_data' => json_encode(self::configureReservedField($params['coupon_data']??[], 'coupon_data')),
  78. 'url_page' => $params['url_page']??'',
  79. 'page_type' => $params['page_type']??0,
  80. 'images' => json_encode($params['images']??[]),
  81. ]);
  82. Db::commit();
  83. return true;
  84. } catch (\Exception $e) {
  85. Db::rollback();
  86. self::setError($e->getMessage());
  87. return false;
  88. }
  89. }
  90. public static function configureReservedField($data, $reserved_type,$reserved_field = []): array
  91. {
  92. if($reserved_type === 'block_data'){
  93. $reserved_field = ['id','recommend_weight','goods_name','base_service_fee','service_total','service_fee','activity_service_fee'];
  94. foreach ($data as $key => &$item) {
  95. $data[$key]['block_key'] = $key+1;
  96. foreach ($item['goods'] as &$good) {
  97. foreach ($good as $k=>$v) {
  98. if(!in_array($k,$reserved_field)){
  99. unset($good[$k]);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if($reserved_type === 'coupon_data'){
  106. $reserved_field = ['id','voucher_count','voucher_status','server_category_name','event_name','couponWithCategory','expire_time',
  107. 'amount_require','amount','max_deductible_price','discount_ratio','mold_type','coupon_type','code','remaining_count'
  108. ];
  109. foreach ($data as &$item) {
  110. foreach ($item as $k=>$v) {
  111. if(!in_array($k,$reserved_field)){
  112. unset($item[$k]);
  113. }
  114. }
  115. }
  116. }
  117. return $data??[];
  118. }
  119. /**
  120. * @notes 删除
  121. * @param array $params
  122. * @return bool
  123. * @author likeadmin
  124. * @date 2024/11/21 15:04
  125. */
  126. public static function delete(array $params): bool
  127. {
  128. return PropertyActivity::destroy($params['id']);
  129. }
  130. /**
  131. * @notes 获取详情
  132. * @param $params
  133. * @return array
  134. * @author likeadmin
  135. * @date 2024/11/21 15:04
  136. */
  137. public static function detail($params): array
  138. {
  139. return PropertyActivity::findOrEmpty($params['id'])->toArray();
  140. }
  141. public static function getQRCode($params,$url='weixiu.kyjlkj.com')
  142. {
  143. try {
  144. // $mnp_page = (isset($params['mnp_page']) && $params['mnp_page'])?urldecode($params['mnp_page']):env('miniprogram.property_activity_qrcode', '');
  145. $mnp_page = 'pages/web_view/index';
  146. if(empty($params['mnp_page'])){
  147. throw new Exception('路径错误');
  148. }
  149. $scene_page = (isset($params['mnp_page']) && $params['mnp_page'])? $params['mnp_page']:'';
  150. Log::info('getQRCode:'.rawurlencode($scene_page));
  151. $response = (new WeChatMnpService())->getUnlimitedQRCode(
  152. 'page='.$scene_page.'&id='.$params['id'],
  153. $mnp_page,
  154. env('miniprogram.mini_env_version', 'release'),
  155. false
  156. );
  157. Log::info('getQRCode:'.json_encode([$response]));
  158. $qrcode = $response->getContent();
  159. if(!is_dir('./uploads/wx_qrcode/'.date('Ymd'))){
  160. mkdir('./uploads/wx_qrcode/'.date('Ymd'));
  161. }
  162. $file_name = 'uploads/wx_qrcode/'.date('Ymd').'/'.time().rand(1000,9999).'.png';
  163. file_put_contents($file_name, $qrcode);
  164. //$file_name = 'https://fushencdn.kyjlkj.com/uploads/miniqrcode/27f140aae615bbe16c1e888c14c9ab10.png';
  165. return $url.'/'.$file_name;
  166. } catch (\Throwable $e) {
  167. Log::info('getQRCode:'.$e->getMessage());
  168. return '';
  169. }
  170. }
  171. public static function copy($params)
  172. {
  173. try {
  174. $activity = PropertyActivity::findOrEmpty($params['id'])->toArray();
  175. PropertyActivity::create([
  176. 'property_head_id' => $activity['property_head_id'],
  177. 'activity_name' => $activity['activity_name'],
  178. 'activity_start_time' => $activity['activity_start_time']??0,
  179. 'activity_end_time' => $activity['activity_end_time']??0,
  180. 'block_data' => self::configureReservedField($activity['block_data']??[], 'block_data'),
  181. 'coupon_data' => self::configureReservedField($activity['coupon_data']??[], 'coupon_data'),
  182. 'url_page' => $activity['url_page']??'',
  183. 'page_type' => $activity['page_type']??0,
  184. 'images' => $activity['images']??[],
  185. ]);
  186. return true;
  187. } catch (\Throwable $e) {
  188. return $e->getMessage();
  189. }
  190. }
  191. }