Fans.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\wechat;
  10. use app\admin\service\Service;
  11. use app\common\models\Setting as SettingModel;
  12. use app\common\models\wechat\WechatUsers as WechatUsersModel;
  13. use app\common\models\users\Users as UsersModel;
  14. use app\common\models\users\UsersAddress as UsersAddressModel;
  15. use app\common\models\users\UsersBonus as UsersBonusModel;
  16. use app\common\models\users\UsersComment as UsersCommentModel;
  17. use app\common\models\users\UsersConsult as UsersConsultModel;
  18. use app\common\models\users\UsersFavorite as UsersFavoriteModel;
  19. use app\common\models\users\UsersLog as UsersLogModel;
  20. use app\common\models\users\UsersRechange as UsersRechangeModel;
  21. use app\common\models\users\UsersWithdrawLog as UsersWithdrawLogModel;
  22. use app\common\models\order\Order as OrderModel;
  23. use app\common\models\order\OrderGoods as OrderGoodsModel;
  24. use app\common\models\order\OrderLog as OrderLogModel;
  25. use app\common\models\order\OrderCollection as OrderCollectionModel;
  26. use app\common\models\order\OrderDelivery as OrderDeliveryModel;
  27. use app\common\models\order\OrderRefundment as OrderRefundmentModel;
  28. use app\common\models\order\OrderGroup as OrderGroupModel;
  29. use app\common\models\wechat\WechatUsersTags as WechatUsersTagsModel;
  30. use app\common\library\wechat\Factory;
  31. use mall\utils\Tool;
  32. class Fans extends Service {
  33. /**
  34. * 获取粉丝列表
  35. * @param $data
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public static function getList($data){
  42. $count = WechatUsersModel::withSearch(["nickname"],[ 'nickname'=>$data['key']["title"]??'' ])->count();
  43. $result = array_map(function ($res){
  44. $res['photo'] = Tool::thumb($res['headimgurl']);
  45. $tags = WechatUsersTagsModel::column('name', 'id');
  46. $res['tags'] = [];
  47. foreach (explode(',', $res['tagid_list']) as $tagid) {
  48. if (isset($tags[$tagid])) $res['tags'][] = $tags[$tagid];
  49. }
  50. $res['tags'] = implode(",",$res['tags']);
  51. $res['area'] = implode(",",[$res["country"], $res["province"], $res["city"]]);
  52. return $res;
  53. },WechatUsersModel::withSearch(["nickname"],[ 'nickname'=>$data['key']["title"]??'' ])->order("id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray());
  54. return ["count"=>$count, "data"=>$result];
  55. }
  56. /**
  57. * 批量获取用户基本信息
  58. * @param string $next
  59. * @param int $done
  60. * @return int|mixed
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public static function syncFans($next = '', $done = 0){
  66. $appid = SettingModel::getArrayData("wechat.appid");
  67. while (!is_null($next) && is_array($result = Factory::wechat()->user->getUserList($next)) && !empty($result['data']['openid'])) {
  68. foreach (array_chunk($result['data']['openid'], 100) as $openids) {
  69. if (is_array($list = Factory::wechat()->user->batchUserInfo($openids)) && !empty($list['user_info_list'])) {
  70. foreach ($list['user_info_list'] as $user) {
  71. self::saveFans($user, $appid);
  72. }
  73. }
  74. }
  75. $next = $result['total'] > $done ? $result['next_openid'] : null;
  76. }
  77. return $done;
  78. }
  79. /**
  80. * 保存粉丝资料
  81. * @param $user
  82. * @param $appid
  83. * @return WechatUsersModel|bool|\think\Model
  84. */
  85. public static function saveFans($user,$appid){
  86. if (!empty($user['subscribe_time'])) {
  87. $user['subscribe_create_time'] = $user['subscribe_time'];
  88. }
  89. if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
  90. $user['tagid_list'] = is_array($user['tagid_list']) ? join(',', $user['tagid_list']) : '';
  91. }
  92. unset($user['privilege'], $user['groupid']);
  93. $data = array_merge($user,[ 'subscribe'=>'1', 'appid'=>$appid ]);
  94. $condition = ["openid"=>$user["openid"],"appid"=>$appid];
  95. if(WechatUsersModel::where($condition)->count()){
  96. unset($data["subscribe_create_time"]);
  97. return WechatUsersModel::where($condition)->save($data);
  98. }
  99. return WechatUsersModel::create($data);
  100. }
  101. /**
  102. * 获取公众号的黑名单列表
  103. * @param string $next
  104. * @param int $done
  105. * @return int|mixed
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public static function syncBlack($next = '', $done = 0){
  111. while (!is_null($next) && is_array($result = Factory::wechat()->user->getBlackList($next)) && !empty($result['data']['openid'])) {
  112. $done += $result['count'];
  113. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  114. WechatUsersModel::where(['is_black' => '0'])->whereIn('openid', $chunk)->save(['is_black' => '1']);
  115. }
  116. $next = $result['total'] > $done ? $result['next_openid'] : null;
  117. }
  118. return empty($result['total']) ? 0 : $result['total'];
  119. }
  120. /**
  121. * 获取Tags
  122. * @return int
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. */
  127. public static function syncTags(){
  128. $count = 0;
  129. if (is_array($list = Factory::wechat()->user_tag->get()) && !empty($list['tags'])) {
  130. $count = count($list['tags']);
  131. WechatUsersTagsModel::where("1=1")->delete();
  132. WechatUsersTagsModel::insertAll($list['tags']);
  133. return $count;
  134. }
  135. return $count;
  136. }
  137. /**
  138. * 拉入黑名单
  139. * @param $openid
  140. * @return bool
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public static function addBlack($openid){
  146. foreach (array_chunk(explode(',', $openid), 20) as $openids) {
  147. Factory::wechat()->user->batchBlackList($openids);
  148. WechatUsersModel::whereIn('openid', $openids)->save(['is_black' => '1']);
  149. }
  150. return true;
  151. }
  152. /**
  153. * 移出黑名单
  154. * @param $openid
  155. * @return bool
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public static function removeBlack($openid){
  161. foreach (array_chunk(explode(',', $openid), 20) as $openids) {
  162. Factory::wechat()->user->batchUnBlackList($openids);
  163. WechatUsersModel::whereIn('openid', $openids)->save(['is_black' => '0']);
  164. }
  165. return true;
  166. }
  167. /**
  168. * 删除
  169. * @param $id
  170. * @return bool
  171. * @throws \Exception
  172. */
  173. public static function delete($id){
  174. try{
  175. WechatUsersModel::startTrans();
  176. $row = WechatUsersModel::where('id',$id)->find();
  177. if(empty($row)){
  178. throw new \Exception("您要查找的数据不存在!",0);
  179. }
  180. WechatUsersModel::where("id",$id)->delete();
  181. if($row["user_id"] <= 0){
  182. WechatUsersModel::commit();
  183. return true;
  184. }
  185. UsersModel::where("id",$row["user_id"])->delete();
  186. UsersAddressModel::where("user_id",$row["user_id"])->delete();
  187. UsersBonusModel::where("user_id",$row["user_id"])->delete();
  188. UsersCommentModel::where("user_id",$row["user_id"])->delete();
  189. UsersConsultModel::where("user_id",$row["user_id"])->delete();
  190. UsersFavoriteModel::where("user_id",$row["user_id"])->delete();
  191. UsersLogModel::where("user_id",$row["user_id"])->delete();
  192. UsersRechangeModel::where("user_id",$row["user_id"])->delete();
  193. UsersWithdrawLogModel::where("user_id",$row["user_id"])->delete();
  194. $order = OrderModel::where("user_id",$row["user_id"])->select()->toArray();
  195. $orderId = [];
  196. foreach($order as $value){
  197. $orderId[] = $value["id"];
  198. }
  199. if(OrderModel::where("id","in",$orderId)->delete()){
  200. OrderGoodsModel::where("order_id","in",$orderId)->delete();
  201. OrderLogModel::where("order_id","in",$orderId)->delete();
  202. }
  203. OrderCollectionModel::where("user_id",$row["user_id"])->delete();
  204. OrderDeliveryModel::where("user_id",$row["user_id"])->delete();
  205. OrderRefundmentModel::where("user_id",$row["user_id"])->delete();
  206. OrderGroupModel::where("user_id",$row["user_id"])->delete();
  207. WechatUsersModel::commit();
  208. }catch (\Exception $ex){
  209. WechatUsersModel::rollback();
  210. throw new \Exception($ex->getMessage(),$ex->getCode());
  211. }
  212. }
  213. }