WchatShareBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\share;
  11. use app\model\BaseModel;
  12. use app\model\system\Config as ConfigModel;
  13. /**
  14. * 分享
  15. */
  16. class WchatShareBase extends BaseModel
  17. {
  18. protected $config = [];
  19. protected $sort = 999;
  20. /**
  21. * @param array $param
  22. * @return mixed
  23. */
  24. public function getShareData($param)
  25. {
  26. $path = parse_url($param['url'])['path'] ?? '';
  27. $path = preg_replace("/^\/h5/", '', $path);
  28. foreach($this->config as $val){
  29. if(in_array($path, $val['path'])){
  30. $method = $val['method_prefix'].'ShareData';
  31. if(method_exists($this, $method)){
  32. return $this->$method(array_merge($param, ['config' => $val]));
  33. }
  34. }
  35. }
  36. }
  37. /**
  38. * 获取分享配置
  39. * @param $param
  40. * @return array
  41. */
  42. public function getShareConfig($param)
  43. {
  44. $data = [];
  45. foreach($this->config as $val){
  46. $method = $val['method_prefix'].'ShareConfig';
  47. if(method_exists($this, $method)){
  48. $item = $this->$method(array_merge($param, ['config' => $val]));
  49. $item['config'] = $val;
  50. $data[] = $item;
  51. }
  52. }
  53. return [
  54. 'sort' => $this->sort,
  55. 'data' => $data,
  56. ];
  57. }
  58. /**
  59. * 设置分享内容
  60. * @param $site_id
  61. * @param $data
  62. * @return array
  63. */
  64. public function setShareConfig($site_id, $data)
  65. {
  66. $config_model = new ConfigModel();
  67. foreach($data as $key=>$val){
  68. $config_key = $val['config_key'];
  69. $config_data = [];
  70. if(isset($val['title'])) $config_data['title'] = $val['title'];
  71. if(isset($val['desc'])) $config_data['desc'] = $val['desc'];
  72. if(isset($val['imgUrl'])) $config_data['imgUrl'] = $val['imgUrl'];
  73. $config_model->setConfig($config_data, '公众号分享设置', 1, [
  74. ['site_id', '=', $site_id],
  75. ['app_module', '=', 'shop'],
  76. ['config_key', '=', $config_key],
  77. ]);
  78. }
  79. return $this->success();
  80. }
  81. /**
  82. * 获取分享链接
  83. * @param $param
  84. * @return string
  85. */
  86. protected function getShareLink($param)
  87. {
  88. $member_id = $param['member_id'];
  89. $link = $param['url'];
  90. //如果链接中有原分享人数据要先去掉
  91. $link = preg_replace("/source_member=\d+/", "", $link);
  92. $link = rtrim($link, '?');
  93. if(!empty($member_id)){
  94. if(strpos($link, '?')){
  95. $link .= '&';
  96. }else{
  97. $link .= '?';
  98. }
  99. $link .= 'source_member='.$member_id;
  100. }
  101. return $link;
  102. }
  103. /**
  104. * 默认分享图标
  105. * @return string
  106. */
  107. protected function getDefaultShareIcon()
  108. {
  109. return img('public/static/img/wx_share_icon.png');
  110. }
  111. }