123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- // +----------------------------------------------------------------------
- // | A3Mall
- // +----------------------------------------------------------------------
- // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: xzncit <158373108@qq.com>
- // +----------------------------------------------------------------------
- namespace app\admin\service\wechat;
- use app\admin\service\Service;
- use app\common\models\Setting as SettingModel;
- use app\common\models\wechat\WechatUsers as WechatUsersModel;
- use app\common\models\users\Users as UsersModel;
- use app\common\models\users\UsersAddress as UsersAddressModel;
- use app\common\models\users\UsersBonus as UsersBonusModel;
- use app\common\models\users\UsersComment as UsersCommentModel;
- use app\common\models\users\UsersConsult as UsersConsultModel;
- use app\common\models\users\UsersFavorite as UsersFavoriteModel;
- use app\common\models\users\UsersLog as UsersLogModel;
- use app\common\models\users\UsersRechange as UsersRechangeModel;
- use app\common\models\users\UsersWithdrawLog as UsersWithdrawLogModel;
- use app\common\models\order\Order as OrderModel;
- use app\common\models\order\OrderGoods as OrderGoodsModel;
- use app\common\models\order\OrderLog as OrderLogModel;
- use app\common\models\order\OrderCollection as OrderCollectionModel;
- use app\common\models\order\OrderDelivery as OrderDeliveryModel;
- use app\common\models\order\OrderRefundment as OrderRefundmentModel;
- use app\common\models\order\OrderGroup as OrderGroupModel;
- use app\common\models\wechat\WechatUsersTags as WechatUsersTagsModel;
- use app\common\library\wechat\Factory;
- use mall\utils\Tool;
- class Fans extends Service {
- /**
- * 获取粉丝列表
- * @param $data
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getList($data){
- $count = WechatUsersModel::withSearch(["nickname"],[ 'nickname'=>$data['key']["title"]??'' ])->count();
- $result = array_map(function ($res){
- $res['photo'] = Tool::thumb($res['headimgurl']);
- $tags = WechatUsersTagsModel::column('name', 'id');
- $res['tags'] = [];
- foreach (explode(',', $res['tagid_list']) as $tagid) {
- if (isset($tags[$tagid])) $res['tags'][] = $tags[$tagid];
- }
- $res['tags'] = implode(",",$res['tags']);
- $res['area'] = implode(",",[$res["country"], $res["province"], $res["city"]]);
- return $res;
- },WechatUsersModel::withSearch(["nickname"],[ 'nickname'=>$data['key']["title"]??'' ])->order("id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray());
- return ["count"=>$count, "data"=>$result];
- }
- /**
- * 批量获取用户基本信息
- * @param string $next
- * @param int $done
- * @return int|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function syncFans($next = '', $done = 0){
- $appid = SettingModel::getArrayData("wechat.appid");
- while (!is_null($next) && is_array($result = Factory::wechat()->user->getUserList($next)) && !empty($result['data']['openid'])) {
- foreach (array_chunk($result['data']['openid'], 100) as $openids) {
- if (is_array($list = Factory::wechat()->user->batchUserInfo($openids)) && !empty($list['user_info_list'])) {
- foreach ($list['user_info_list'] as $user) {
- self::saveFans($user, $appid);
- }
- }
- }
- $next = $result['total'] > $done ? $result['next_openid'] : null;
- }
- return $done;
- }
- /**
- * 保存粉丝资料
- * @param $user
- * @param $appid
- * @return WechatUsersModel|bool|\think\Model
- */
- public static function saveFans($user,$appid){
- if (!empty($user['subscribe_time'])) {
- $user['subscribe_create_time'] = $user['subscribe_time'];
- }
- if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
- $user['tagid_list'] = is_array($user['tagid_list']) ? join(',', $user['tagid_list']) : '';
- }
- unset($user['privilege'], $user['groupid']);
- $data = array_merge($user,[ 'subscribe'=>'1', 'appid'=>$appid ]);
- $condition = ["openid"=>$user["openid"],"appid"=>$appid];
- if(WechatUsersModel::where($condition)->count()){
- unset($data["subscribe_create_time"]);
- return WechatUsersModel::where($condition)->save($data);
- }
- return WechatUsersModel::create($data);
- }
- /**
- * 获取公众号的黑名单列表
- * @param string $next
- * @param int $done
- * @return int|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function syncBlack($next = '', $done = 0){
- while (!is_null($next) && is_array($result = Factory::wechat()->user->getBlackList($next)) && !empty($result['data']['openid'])) {
- $done += $result['count'];
- foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
- WechatUsersModel::where(['is_black' => '0'])->whereIn('openid', $chunk)->save(['is_black' => '1']);
- }
- $next = $result['total'] > $done ? $result['next_openid'] : null;
- }
- return empty($result['total']) ? 0 : $result['total'];
- }
- /**
- * 获取Tags
- * @return int
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function syncTags(){
- $count = 0;
- if (is_array($list = Factory::wechat()->user_tag->get()) && !empty($list['tags'])) {
- $count = count($list['tags']);
- WechatUsersTagsModel::where("1=1")->delete();
- WechatUsersTagsModel::insertAll($list['tags']);
- return $count;
- }
- return $count;
- }
- /**
- * 拉入黑名单
- * @param $openid
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function addBlack($openid){
- foreach (array_chunk(explode(',', $openid), 20) as $openids) {
- Factory::wechat()->user->batchBlackList($openids);
- WechatUsersModel::whereIn('openid', $openids)->save(['is_black' => '1']);
- }
- return true;
- }
- /**
- * 移出黑名单
- * @param $openid
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function removeBlack($openid){
- foreach (array_chunk(explode(',', $openid), 20) as $openids) {
- Factory::wechat()->user->batchUnBlackList($openids);
- WechatUsersModel::whereIn('openid', $openids)->save(['is_black' => '0']);
- }
- return true;
- }
- /**
- * 删除
- * @param $id
- * @return bool
- * @throws \Exception
- */
- public static function delete($id){
- try{
- WechatUsersModel::startTrans();
- $row = WechatUsersModel::where('id',$id)->find();
- if(empty($row)){
- throw new \Exception("您要查找的数据不存在!",0);
- }
- WechatUsersModel::where("id",$id)->delete();
- if($row["user_id"] <= 0){
- WechatUsersModel::commit();
- return true;
- }
- UsersModel::where("id",$row["user_id"])->delete();
- UsersAddressModel::where("user_id",$row["user_id"])->delete();
- UsersBonusModel::where("user_id",$row["user_id"])->delete();
- UsersCommentModel::where("user_id",$row["user_id"])->delete();
- UsersConsultModel::where("user_id",$row["user_id"])->delete();
- UsersFavoriteModel::where("user_id",$row["user_id"])->delete();
- UsersLogModel::where("user_id",$row["user_id"])->delete();
- UsersRechangeModel::where("user_id",$row["user_id"])->delete();
- UsersWithdrawLogModel::where("user_id",$row["user_id"])->delete();
- $order = OrderModel::where("user_id",$row["user_id"])->select()->toArray();
- $orderId = [];
- foreach($order as $value){
- $orderId[] = $value["id"];
- }
- if(OrderModel::where("id","in",$orderId)->delete()){
- OrderGoodsModel::where("order_id","in",$orderId)->delete();
- OrderLogModel::where("order_id","in",$orderId)->delete();
- }
- OrderCollectionModel::where("user_id",$row["user_id"])->delete();
- OrderDeliveryModel::where("user_id",$row["user_id"])->delete();
- OrderRefundmentModel::where("user_id",$row["user_id"])->delete();
- OrderGroupModel::where("user_id",$row["user_id"])->delete();
- WechatUsersModel::commit();
- }catch (\Exception $ex){
- WechatUsersModel::rollback();
- throw new \Exception($ex->getMessage(),$ex->getCode());
- }
- }
- }
|