WeappShare.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\system\Site as SiteModel;
  12. use app\model\system\Config as ConfigModel;
  13. /**
  14. * 分享
  15. */
  16. class WeappShare extends WeappShareBase
  17. {
  18. protected $config = [
  19. [
  20. 'title' => '商城首页',
  21. 'config_key' => 'WEAPP_SHARE_CONFIG_INDEX',
  22. 'path' => ['/pages/index/index'],
  23. 'method_prefix' => 'index',
  24. ],
  25. ];
  26. protected $sort = 1;
  27. /**
  28. * 首页分享数据
  29. * @param $param
  30. * @return array
  31. */
  32. protected function indexShareData($param)
  33. {
  34. $site_id = $param['site_id'] ?? 0;
  35. //站点设置
  36. $site_model = new SiteModel();
  37. $site_info = $site_model->getSiteInfo([['site_id', '=', $site_id]])['data'];
  38. //获取和替换配置数据
  39. $config_data = $this->indexShareConfig($param);
  40. $title = str_replace('{site_name}', $site_info['site_name'], $config_data['value']['title']);
  41. $image_url = $config_data['value']['imageUrl'] ? img($config_data['value']['imageUrl']) : '';
  42. $path = $this->getSharePath($param);
  43. $data = [
  44. 'title' => $title,
  45. 'path' => $path,
  46. 'imageUrl' => $image_url,
  47. ];
  48. return [
  49. 'permission' => [
  50. 'onShareAppMessage' => true,
  51. 'onShareTimeline' => true,
  52. ],
  53. 'data' => $data,//分享内容
  54. ];
  55. }
  56. /**
  57. * 首页分享配置
  58. * @param $param
  59. * @return array
  60. */
  61. protected function indexShareConfig($param)
  62. {
  63. $site_id = $param['site_id'];
  64. $config = $param['config'];
  65. $config_model = new ConfigModel();
  66. $data = $config_model->getConfig([
  67. ['site_id', '=', $site_id],
  68. ['app_module', '=', 'shop'],
  69. ['config_key', '=', $config['config_key']],
  70. ])['data'];
  71. if (empty($data['value'])) {
  72. $data['value'] = [
  73. 'title' => '{site_name}',
  74. 'imageUrl' => '',
  75. ];
  76. }
  77. $variable = [
  78. ['title' => '店铺名称', 'name' => '{site_name}'],
  79. ];
  80. return [
  81. 'value' => $data['value'],
  82. 'variable' => $variable,
  83. ];
  84. }
  85. }