Users.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\users;
  10. use mall\utils\Tool;
  11. use think\facade\Session;
  12. use app\admin\service\Service;
  13. use app\common\models\users\UsersGroup as UsersGroupModel;
  14. use app\common\models\users\UsersTags as UsersTagsModel;
  15. use app\admin\model\Users as UsersModel;
  16. use app\common\models\users\UsersAddress as UsersAddressModel;
  17. use app\common\models\Area as AreaModel;
  18. use app\admin\model\users\UsersFavorite as UsersFavoriteModel;
  19. use app\common\models\users\UsersLog as UsersLogModel;
  20. use app\common\models\users\UsersBonus as UsersBonusModel;
  21. use app\common\models\wechat\WechatUsers as WechatUsersModel;
  22. use app\common\models\users\UsersComment as UsersCommentModel;
  23. use app\common\models\users\UsersConsult as UsersConsultModel;
  24. use app\common\models\users\UsersRechange as UsersRechangeModel;
  25. use app\common\models\users\UsersWithdrawLog as UsersWithdrawLogModel;
  26. use app\common\models\order\Order as OrderModel;
  27. use app\common\models\order\OrderGoods as OrderGoodsModel;
  28. use app\common\models\order\OrderLog as OrderLogModel;
  29. use app\common\models\order\OrderCollection as OrderCollectionModel;
  30. use app\common\models\order\OrderDelivery as OrderDeliveryModel;
  31. use app\common\models\order\OrderRefundment as OrderRefundmentModel;
  32. use app\common\models\order\OrderGroup as OrderGroupModel;
  33. class Users extends Service {
  34. /**
  35. * 获取会员等级分组和会员tag
  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 getSearchData(){
  42. return [
  43. "cat"=>UsersGroupModel::select()->toArray(),
  44. "tags"=>UsersTagsModel::select()->toArray()
  45. ];
  46. }
  47. /**
  48. * 获取列表数据
  49. * @param $data
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public static function getList($data,$condition=[]){
  56. $count = UsersModel::withSearch(["group_id","username"],[
  57. 'group_id'=>$data['key']["cat_id"]??'',
  58. 'username'=>$data['key']["title"]??''
  59. ])->withJoin("group")->where($condition)->count();
  60. $result = UsersModel::withSearch(["group_id","username"],[
  61. 'group_id'=>$data['key']["cat_id"]??'',
  62. 'username'=>$data['key']["title"]??''
  63. ])->withJoin("group")->where($condition)->page($data["page"]??1,$data["limit"]??10)->order("users.id","desc")->select()->toArray();
  64. return [ "count"=>$count, "data"=>array_map(function ($res){
  65. $res["nickname"] = getUserName($res);
  66. $tags = UsersTagsModel::where('id','in',$res['tags'])->select()->toArray();
  67. $arr = [];
  68. foreach($tags as $val){
  69. $arr[] = $val['name'];
  70. }
  71. $res['tags'] = implode(",",$arr);
  72. return $res;
  73. },$result) ];
  74. }
  75. /**
  76. * 详情
  77. * @param $id
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public static function detail($id){
  84. $row = UsersModel::where("id",$id)->find();
  85. return ["cat"=>UsersGroupModel::select()->toArray(), "data"=>$row??[] ];
  86. }
  87. /**
  88. * 保存会员数据
  89. * @param $data
  90. * @return bool
  91. * @throws \Exception
  92. */
  93. public static function save($data){
  94. if(UsersModel::where("id",$data["id"])->count()){
  95. if(!empty($data["password"]) || !empty($data["confirm_password"])){
  96. if($data["password"] != $data["confirm_password"]){
  97. throw new \Exception("您输入的两次密码不致。",0);
  98. }
  99. $data["password"] = md5($data["password"]);
  100. }else{
  101. unset($data['password'],$data['confirm_password']);
  102. }
  103. if(UsersModel::where("username",$data["username"])->where("id","<>",$data["id"])->count()){
  104. throw new \Exception("该用户名已存在,请更换用户名。",0);
  105. }
  106. UsersModel::where("id",$data["id"])->save($data);
  107. }else{
  108. if(empty($data["password"])){
  109. throw new \Exception("请填写密码",0);
  110. }else if(empty($data["confirm_password"])){
  111. throw new \Exception("请填写确认密码",0);
  112. }else if($data["password"] != $data["confirm_password"]){
  113. throw new \Exception("您输入的两次密码不致。",0);
  114. }
  115. if(UsersModel::where("username",$data["username"])->count()){
  116. throw new \Exception("该用户名已存在,请更换用户名。",0);
  117. }
  118. $data["password"] = md5($data["password"]);
  119. UsersModel::create($data);
  120. }
  121. return true;
  122. }
  123. /**
  124. * 删除
  125. * @param array|string $ids
  126. * @return bool
  127. * @throws \Exception
  128. */
  129. public static function delete($ids){
  130. try{
  131. UsersModel::startTrans();
  132. $array = array_map("intval",explode(",",$ids));
  133. foreach($array as $id) {
  134. $row = UsersModel::where('id', $id)->find();
  135. if (empty($row)) {
  136. continue;
  137. }
  138. UsersModel::where("id",$id)->delete();
  139. UsersAddressModel::where("user_id", $id)->delete();
  140. WechatUsersModel::where("user_id", $id)->delete();
  141. UsersBonusModel::where("user_id", $id)->delete();
  142. UsersCommentModel::where("user_id", $id)->delete();
  143. UsersConsultModel::where("user_id", $id)->delete();
  144. UsersFavoriteModel::where("user_id", $id)->delete();
  145. UsersLogModel::where("user_id", $id)->delete();
  146. UsersRechangeModel::where("user_id", $id)->delete();
  147. UsersWithdrawLogModel::where("user_id", $id)->delete();
  148. $order = OrderModel::where("user_id", $id)->select();
  149. $orderId = [];
  150. foreach ($order as $value) {
  151. $orderId[] = $value["id"];
  152. }
  153. if (OrderModel::where("id", "in", $orderId)->delete()) {
  154. OrderGoodsModel::where("order_id", "in", $orderId)->delete();
  155. OrderLogModel::where("order_id", "in", $orderId)->delete();
  156. }
  157. OrderCollectionModel::where("user_id", $id)->delete();
  158. OrderDeliveryModel::where("user_id", $id)->delete();
  159. OrderRefundmentModel::where("user_id", $id)->delete();
  160. OrderGroupModel::where("user_id", $id)->delete();
  161. }
  162. UsersModel::commit();
  163. }catch (\Exception $ex){
  164. UsersModel::rollback();
  165. throw new \Exception($ex->getMessage(),$ex->getCode());
  166. }
  167. return true;
  168. }
  169. /**
  170. * 获取会员资料
  171. * @param $id
  172. * @return array
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. public static function financeDetail($id){
  178. if(!$user = UsersModel::where(["id"=>$id])->find()){
  179. throw new \Exception("您操作的会员不存在!", 0);
  180. }
  181. $user["username"] = getUserName($user);
  182. return [ "id"=>$id,"user"=>$user ];
  183. }
  184. /**
  185. * 财务明细
  186. * @param $data
  187. * @return bool
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public static function financeSave($data){
  193. $user = self::financeDetail($data["id"])["user"];
  194. $action = $data["action"]??"";
  195. $operation = $data["operation"]??"";
  196. $num = $data["num"]??0;
  197. if($operation == 1 && ($action == 0 && $user["amount"] < $num)){
  198. throw new \Exception("提现失败,用户余额不足",0);
  199. }
  200. if($operation == 1 && ($action == 1 && $user["point"] < $num)){
  201. throw new \Exception("提现失败,用户积分不足",0);
  202. }
  203. if($operation == 1 && ($action == 2 && $user["exp"] < $num)){
  204. throw new \Exception("提现失败,用户经验不足",0);
  205. }
  206. if($operation == 1 && ($action == 4 && $user["spread_amount"] < $num)){
  207. throw new \Exception("提现失败,用户佣金不足",0);
  208. }
  209. $field = "";
  210. $description = "管理员对您的";
  211. switch($action){
  212. case 0:
  213. $field = "amount";
  214. $description .= "【金额】";
  215. break;
  216. case 1:
  217. $field = "point";
  218. $description .= "【积分】";
  219. break;
  220. case 2:
  221. $field = "exp";
  222. $description .= "【经验】";
  223. break;
  224. case 4:
  225. $field = "spread_amount";
  226. $description .= "【佣金】";
  227. break;
  228. }
  229. if($operation == 1){
  230. $total = $user[$field] - $num;
  231. $description .= "执行了提现操作,";
  232. }else{
  233. $total = $user[$field] + $num;
  234. $description .= "执行了充值操作,";
  235. }
  236. $description .= "操作数值【".$num."】";
  237. $arr = [];
  238. $arr[$field] = $total;
  239. UsersModel::where(["id"=>$data["id"]])->update($arr);
  240. UsersLogModel::create([ "user_id"=>$data["id"], "admin_id"=>Session::get("system_user_id"), "action"=>$action, "operation"=>$operation, "description"=>$description, $field=>$num, "create_time"=>time()]);
  241. return true;
  242. }
  243. /**
  244. * 更新会员标签
  245. * @param $data
  246. * @return bool
  247. */
  248. public static function updateUsersTags($data){
  249. if(UsersModel::where("id",$data["user_id"])->count()){
  250. UsersModel::where("id",$data["user_id"])->save([ "tags"=>implode(",",$data["id"]) ]);
  251. return true;
  252. }
  253. return false;
  254. }
  255. /**
  256. * 获取会员收藏列表
  257. * @param $id
  258. * @return array
  259. * @throws \think\db\exception\DataNotFoundException
  260. * @throws \think\db\exception\DbException
  261. * @throws \think\db\exception\ModelNotFoundException
  262. */
  263. public static function getFavoriteList($id){
  264. $count = UsersFavoriteModel::withJoin("goods")->where("users_favorite.user_id",$id)->count();
  265. $result = UsersFavoriteModel::withJoin("goods")->where("users_favorite.user_id",$id)->page($data["page"]??1,$data["limit"]??10)->order("users_favorite.id","desc")->select()->toArray();
  266. $array = [];
  267. foreach($result as $key=>$value){
  268. $goods = $value["goods"];
  269. $array[$key] = [
  270. "id"=>$goods["id"],
  271. "title"=>$goods["title"],
  272. "sell_price"=>$goods["sell_price"],
  273. "photo"=>Tool::thumb($goods["photo"]),
  274. "favorite_time"=>$value["create_time"]
  275. ];
  276. }
  277. return [ "data"=>$array, "count"=>$count ];
  278. }
  279. /**
  280. * 获取地址数据
  281. * @param $id
  282. * @return array
  283. * @throws \think\db\exception\DataNotFoundException
  284. * @throws \think\db\exception\DbException
  285. * @throws \think\db\exception\ModelNotFoundException
  286. */
  287. public static function getAddressList($id){
  288. $result = UsersAddressModel::where("user_id",$id)->select()->toArray();
  289. foreach($result as $key=>$value){
  290. //$result[$key]["create_time"] = date("Y-m-d H:i:s",$value["create_time"]);
  291. $result[$key]["province"] = AreaModel::getArea([$value["province"],$value["city"],$value["area"]],",");
  292. }
  293. return $result;
  294. }
  295. }