PropertyActivityLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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'];
  94. foreach ($data as &$item) {
  95. foreach ($item['goods'] as &$good) {
  96. foreach ($good as $k=>$v) {
  97. if(!in_array($k,$reserved_field)){
  98. unset($good[$k]);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if($reserved_type === 'coupon_data'){
  105. $reserved_field = ['id','voucher_count','voucher_status','server_category_name','event_name','couponWithCategory','expire_time',
  106. 'amount_require','amount','max_deductible_price','discount_ratio','mold_type','coupon_type','code','remaining_count'
  107. ];
  108. foreach ($data as &$item) {
  109. foreach ($item as $k=>$v) {
  110. if(!in_array($k,$reserved_field)){
  111. unset($item[$k]);
  112. }
  113. }
  114. }
  115. }
  116. return $data??[];
  117. }
  118. /**
  119. * @notes 删除
  120. * @param array $params
  121. * @return bool
  122. * @author likeadmin
  123. * @date 2024/11/21 15:04
  124. */
  125. public static function delete(array $params): bool
  126. {
  127. return PropertyActivity::destroy($params['id']);
  128. }
  129. /**
  130. * @notes 获取详情
  131. * @param $params
  132. * @return array
  133. * @author likeadmin
  134. * @date 2024/11/21 15:04
  135. */
  136. public static function detail($params): array
  137. {
  138. return PropertyActivity::findOrEmpty($params['id'])->toArray();
  139. }
  140. public static function getQRCode($params,$url='weixiu.kyjlkj.com')
  141. {
  142. try {
  143. // $mnp_page = (isset($params['mnp_page']) && $params['mnp_page'])?urldecode($params['mnp_page']):env('miniprogram.property_activity_qrcode', '');
  144. $mnp_page = 'pages/web_view/index';
  145. if(empty($params['mnp_page'])){
  146. throw new Exception('路径错误');
  147. }
  148. $scene_page = (isset($params['mnp_page']) && $params['mnp_page'])? $params['mnp_page']:'';
  149. Log::info('getQRCode:'.rawurlencode($scene_page));
  150. $response = (new WeChatMnpService())->getUnlimitedQRCode(
  151. 'page='.$scene_page.'&id='.$params['id'],
  152. $mnp_page,
  153. env('miniprogram.mini_env_version', 'release'),
  154. false
  155. );
  156. Log::info('getQRCode:'.json_encode([$response]));
  157. $qrcode = $response->getContent();
  158. if(!is_dir('./uploads/wx_qrcode/'.date('Ymd'))){
  159. mkdir('./uploads/wx_qrcode/'.date('Ymd'));
  160. }
  161. $file_name = 'uploads/wx_qrcode/'.date('Ymd').'/'.time().rand(1000,9999).'.png';
  162. file_put_contents($file_name, $qrcode);
  163. //$file_name = 'https://fushencdn.kyjlkj.com/uploads/miniqrcode/27f140aae615bbe16c1e888c14c9ab10.png';
  164. return $url.'/'.$file_name;
  165. } catch (\Throwable $e) {
  166. Log::info('getQRCode:'.$e->getMessage());
  167. return '';
  168. }
  169. }
  170. public static function newDataExecute()
  171. {
  172. try {
  173. $activity_list = PropertyActivity::select()->toArray();
  174. foreach ($activity_list as $item) {
  175. if(!empty($itemp['coupon_data'])){
  176. continue;
  177. }
  178. $coupon_list = CouponRules::with(['couponWithCategory'=>function(Query $query){
  179. $query->field('id,name');
  180. }])->where('property_activity_id',$item['id'])
  181. ->field(['id','code', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count','remaining_count','property_activity_id'])
  182. ->select()
  183. ->toArray()??[];
  184. foreach($coupon_list as $k => $v){
  185. $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id');
  186. $coupon_list[$k] = $v;
  187. }
  188. if($coupon_list){
  189. $data = [
  190. 'coupon_data' => json_encode(self::configureReservedField($coupon_list??[], 'coupon_data')),
  191. ];
  192. PropertyActivity::where('id',$item['id'])->update($data);
  193. }
  194. }
  195. return true;
  196. } catch (\Throwable $e) {
  197. return $e->getMessage();
  198. }
  199. }
  200. }