Shoping.php 5.0 KB

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