WeappShareBase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 WeappShareBase 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['path'])['path'] ?? '';
  27. foreach($this->config as $val){
  28. if(in_array($path, $val['path'])){
  29. $method = $val['method_prefix'].'ShareData';
  30. if(method_exists($this, $method)){
  31. return $this->$method(array_merge($param, ['config' => $val]));
  32. }
  33. }
  34. }
  35. }
  36. /**
  37. * 获取分享配置
  38. * @param $param
  39. * @return array
  40. */
  41. public function getShareConfig($param)
  42. {
  43. $data = [];
  44. foreach($this->config as $val){
  45. $method = $val['method_prefix'].'ShareConfig';
  46. if(method_exists($this, $method)){
  47. $item = $this->$method(array_merge($param, ['config' => $val]));
  48. $item['config'] = $val;
  49. $data[] = $item;
  50. }
  51. }
  52. return [
  53. 'sort' => $this->sort,
  54. 'data' => $data,
  55. ];
  56. }
  57. /**
  58. * 设置分享内容
  59. * @param $site_id
  60. * @param $data
  61. * @return array
  62. */
  63. public function setShareConfig($site_id, $data)
  64. {
  65. $config_model = new ConfigModel();
  66. foreach($data as $key=>$val){
  67. $config_key = $val['config_key'];
  68. $config_data = [];
  69. if(isset($val['title'])) $config_data['title'] = $val['title'];
  70. if(isset($val['imageUrl'])) $config_data['imageUrl'] = $val['imageUrl'];
  71. $config_model->setConfig($config_data, '小程序分享设置', 1, [
  72. ['site_id', '=', $site_id],
  73. ['app_module', '=', 'shop'],
  74. ['config_key', '=', $config_key],
  75. ]);
  76. }
  77. return $this->success();
  78. }
  79. /**
  80. * 获取分享路径
  81. * @param $param
  82. * @return string
  83. */
  84. public function getSharePath($param)
  85. {
  86. $member_id = $param['member_id'];
  87. $path = $param['path'];
  88. //如果链接中有原分享人数据要先去掉
  89. $path = preg_replace("/source_member=\d+/", "", $path);
  90. $path = rtrim($path, '?');
  91. if(!empty($member_id)){
  92. if(strpos($path, '?')){
  93. $path .= '&';
  94. }else{
  95. $path .= '?';
  96. }
  97. $path .= 'source_member='.$member_id;
  98. }
  99. return $path;
  100. }
  101. }