Shoping.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\common\service\wechat\wow;
  3. use app\common\model\Wxapp as WxappModel;
  4. use app\common\model\wow\Shoping as ShopingModel;
  5. use app\common\model\wow\Setting as SettingModel;
  6. use app\common\library\wechat\wow\Shoping as WowShoping;
  7. use app\common\library\helper;
  8. /**
  9. * 好物圈-商品收藏 服务类
  10. * Class Shoping
  11. * @package app\common\service\wechat\wow
  12. */
  13. class Shoping
  14. {
  15. /* @var int $wxapp_id 小程序商城id */
  16. private $wxappId;
  17. /* @var WowShoping $ApiDriver 微信api驱动 */
  18. private $ApiDriver;
  19. protected $error;
  20. /**
  21. * 构造方法
  22. * Shoping constructor.
  23. * @param $wxappId
  24. * @throws \app\common\exception\BaseException
  25. * @throws \think\exception\DbException
  26. */
  27. public function __construct($wxappId)
  28. {
  29. $this->wxappId = $wxappId;
  30. $this->initApiDriver();
  31. }
  32. /**
  33. * 添加好物圈商品收藏
  34. * @param \think\Collection $user 用户信息
  35. * @param array $goodsList 商品列表
  36. * @return bool
  37. * @throws \app\common\exception\BaseException
  38. * @throws \think\exception\DbException
  39. * @throws \Exception
  40. */
  41. public function add($user, $goodsList)
  42. {
  43. // 判断是否开启同步设置
  44. $setting = SettingModel::getItem('basic', $this->wxappId);
  45. if ($setting['is_shopping'] == false) {
  46. return false;
  47. }
  48. // 整理商品列表
  49. $productList = $this->getProductListToAdd($goodsList);
  50. // 执行api请求
  51. $status = $this->ApiDriver->addList($user['open_id'], $productList);
  52. if ($status == false) {
  53. $this->error = $this->ApiDriver->getError();
  54. return $status;
  55. }
  56. // 写入商品收藏记录
  57. $goodsIds = helper::getArrayColumn($goodsList, 'goods_id');
  58. $this->model()->add($user['user_id'], $goodsIds);
  59. return $status;
  60. }
  61. /**
  62. * 删除好物圈商品收藏
  63. * @param $id
  64. * @return bool
  65. * @throws \app\common\exception\BaseException
  66. * @throws \think\exception\DbException
  67. */
  68. public function delete($id)
  69. {
  70. // 实例化模型
  71. $model = $this->model($id, ['user']);
  72. // 执行api请求
  73. $status = $this->ApiDriver->delete($model['user']['open_id'], [[
  74. 'item_code' => $model['goods_id'],
  75. 'sku_id' => $model['goods_id'],
  76. ]]);
  77. if ($status == false) {
  78. $this->error = $this->ApiDriver->getError();
  79. }
  80. // 删除商品收藏记录
  81. $model->setDelete();
  82. return true;
  83. }
  84. /**
  85. * 返回错误信息
  86. * @return mixed
  87. */
  88. public function getError()
  89. {
  90. return $this->error;
  91. }
  92. /**
  93. * 实例化微信api驱动
  94. * @throws \app\common\exception\BaseException
  95. * @throws \think\exception\DbException
  96. */
  97. private function initApiDriver()
  98. {
  99. $config = WxappModel::getWxappCache($this->wxappId);
  100. $this->ApiDriver = new WowShoping($config['app_id'], $config['app_secret']);
  101. }
  102. /**
  103. * 获取好物圈订单记录模型
  104. * @param int|null $id
  105. * @param array $with
  106. * @return ShopingModel|null
  107. * @throws \think\exception\DbException
  108. */
  109. private function model($id = null, $with = ['user'])
  110. {
  111. static $model;
  112. if (!$model instanceof ShopingModel) {
  113. $model = $id > 0 ? ShopingModel::detail($id, $with) : (new ShopingModel);
  114. }
  115. return $model;
  116. }
  117. /**
  118. * 整理商品列表 (用于添加收藏接口)
  119. * @param $goodsList
  120. * @return array
  121. */
  122. private function getProductListToAdd(&$goodsList)
  123. {
  124. // 整理api参数
  125. $productList = [];
  126. foreach ($goodsList as $goods) {
  127. $imageList = []; // 商品图片
  128. foreach ($goods['image'] as $image) {
  129. $imageList[] = $image['file_path'];
  130. }
  131. // sku信息
  132. $skuInfo = &$goods['sku'][0];
  133. $productList[] = [
  134. 'item_code' => $goods['goods_id'],
  135. 'title' => $goods['goods_name'],
  136. 'category_list' => [$goods['category']['name']],
  137. 'image_list' => $imageList,
  138. 'src_wxapp_path' => "/pages/goods/index?goods_id={$goods['goods_id']}", // 商品页面路径
  139. 'sku_info' => [ // 商品sku
  140. // 'sku_id' => "{$goods['goods_id']}_{$skuInfo['spec_sku_id']}",
  141. 'sku_id' => $goods['goods_id'],
  142. 'price' => $skuInfo['goods_price'] * 100,
  143. 'original_price' => $skuInfo['line_price'] * 100, // 划线价
  144. 'status' => 1,
  145. ],
  146. ];
  147. }
  148. return $productList;
  149. }
  150. }