Ucenter.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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\api\service;
  10. use app\common\library\utils\Image;
  11. use app\common\models\Setting as SettingModel;
  12. use mall\basic\Users;
  13. use mall\utils\Check;
  14. use mall\utils\CString;
  15. use mall\utils\Tool;
  16. use think\facade\Config;
  17. use app\common\exception\BaseException;
  18. use app\common\models\users\Users as UsersModel;
  19. use app\common\models\users\UsersLog as UsersLogModel;
  20. use app\common\models\users\UsersBonus as UsersBonusModel;
  21. use app\common\models\users\UsersFavorite as UsersFavoriteModel;
  22. use app\common\models\StoreUsers as StoreUsersModel;
  23. use app\common\models\order\Order as OrderModel;
  24. use app\common\models\users\UsersRechange as UsersRechangeModel;
  25. use app\common\models\Archives as ArchivesModel;
  26. use app\common\models\users\UsersWithdrawLog as UsersWithdrawLogModel;
  27. use app\admin\service\common\Uploadfiy as UploadfiyService;
  28. class Ucenter extends Service {
  29. /**
  30. * 获取收藏列表数据
  31. * @param $data
  32. * @return array
  33. */
  34. public static function getFavoriteList($data){
  35. $size = Config::get("website.pageSize");
  36. $page = $data["page"]??1;
  37. $count = UsersFavoriteModel::where([ "user_id"=>Users::get("id") ])->count();
  38. $result = UsersFavoriteModel::alias("f")->field("g.*,f.id as f_id")->join("goods g","f.goods_id=g.id","LEFT")->where("f.user_id",Users::get("id"))->order("f.id","DESC")->page($page,$size)->select()->toArray();
  39. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  40. $total = ceil($count / $size);
  41. $array["total"] = $total;
  42. if($total == $page -1){
  43. throw new BaseException("没有数据了哦!",-1,$array);
  44. }
  45. $list = [];
  46. foreach($result as $key=>$value){
  47. $list[$key] = [
  48. "id"=>$value["f_id"],
  49. "goods_id"=>$value["id"],
  50. "title"=>$value["title"],
  51. "price"=>$value["sell_price"],
  52. "origin_price"=>$value["market_price"],
  53. "thumb"=>Tool::thumb($value["photo"],"medium",true),
  54. "desc"=>CString::msubstr($value["briefly"],100,false),
  55. ];
  56. }
  57. $array["list"] = $list;
  58. return $array;
  59. }
  60. /**
  61. * 删除收藏商品
  62. * @param $id
  63. * @return bool
  64. * @throws \Exception
  65. */
  66. public static function favoriteDelete($id){
  67. $id = array_map("intval",explode(",",$id));
  68. if(!UsersFavoriteModel::where("user_id",Users::get("id"))->where("id","in",$id)->count()){
  69. throw new \Exception("删除失败,请稍后在试",0);
  70. }
  71. UsersFavoriteModel::where("user_id",Users::get("id"))->where("id","in",$id)->delete();
  72. return true;
  73. }
  74. /**
  75. * 获取优惠劵
  76. * @param $data
  77. * @return array
  78. */
  79. public static function getCouponList($data){
  80. $size = Config::get("website.pageSize");
  81. $page = $data["page"]??1;
  82. $type = $data["type"]??1;
  83. $condition = '';
  84. $nowTime = time();
  85. switch($type){
  86. case 2:
  87. $condition = 'u.status=1 || ' . $nowTime . ' > b .end_time';
  88. break;
  89. case 1:
  90. default:
  91. $condition = 'u.status=0 and b.end_time > ' . $nowTime;
  92. }
  93. $count = UsersBonusModel::alias("u")->field("b.*")->join("promotion_bonus b","u.bonus_id=b.id","LEFT")->where($condition)->where("u.user_id",Users::get("id"))->count();
  94. $bonus = UsersBonusModel::alias("u")->field("b.*")->join("promotion_bonus b","u.bonus_id=b.id","LEFT")->where($condition)->where("u.user_id",Users::get("id"))->order("u.id","DESC")->page($page,$size)->select()->toArray();
  95. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  96. $total = ceil($count / $size);
  97. $array["total"] = $total;
  98. if($total == $page -1){
  99. throw new BaseException("没有数据了哦!",-1,$array);
  100. }
  101. $list = [];
  102. foreach($bonus as $key=>$value){
  103. $list[$key] = [
  104. "name"=>$value["name"],
  105. "amount"=>number_format($value["amount"]),
  106. "price"=>$value["order_amount"],
  107. "end_time"=>date('Y-m-d',$value["end_time"]),
  108. ];
  109. }
  110. $array["list"] = $list;
  111. return $array;
  112. }
  113. /**
  114. * 获取会员积分数据
  115. * @param $data
  116. * @return array
  117. * @throws BaseException
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public static function getPointList($data){
  123. $size = Config::get("website.pageSize");
  124. $page = $data["page"]??1;
  125. $count = UsersLogModel::where([ "user_id"=>Users::get("id") ])->count();
  126. $result = UsersLogModel::field("operation,point,description,create_time")->where("user_id",Users::get("id"))->where("action",1)->order("id","DESC")->page($page,$size)->select()->toArray();
  127. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  128. $total = ceil($count / $size);
  129. $array["total"] = $total;
  130. if($total == $page -1){
  131. throw new BaseException("没有数据了哦!",-1,$array);
  132. }
  133. $list = [];
  134. foreach($result as $key=>$value){
  135. $list[$key] = [
  136. "point"=>$value["operation"] == 0 ? '+' . $value["point"] : '-' . $value["point"],
  137. "operation"=>$value["operation"] == 0 ? '增加' : '减少',
  138. "description"=>$value["description"],
  139. "time"=>$value["create_time"],
  140. ];
  141. }
  142. $array["point"] = Users::get("point");
  143. $array["list"] = $list;
  144. return $array;
  145. }
  146. /**
  147. * 获取会员资料
  148. * @return array
  149. * @throws \Exception
  150. */
  151. public static function getUsersInfo(){
  152. $info = Users::info(Users::get("id"));
  153. return [
  154. "username"=>$info["username"],
  155. "nickname"=>$info["nickname"],
  156. "mobile"=>$info["mobile"],
  157. "coupon_count"=>$info["coupon_count"],
  158. "amount"=>$info["amount"],
  159. "avatar"=>Image::avatar($info["avatar"]),
  160. "spread"=>$info["is_spread"],
  161. "shop"=>StoreUsersModel::where("user_id",Users::get("id"))->count(),
  162. "order_count"=>[
  163. "a"=>OrderModel::where(["status"=>1,"pay_status"=>0,"user_id"=>Users::get("id")])->count(),
  164. "b"=>OrderModel::where(["status"=>2,"pay_status"=>1,"user_id"=>Users::get("id")])->where('distribution_status','=','0')->count(),
  165. "c"=>OrderModel::where(["status"=>2,"pay_status"=>1,"user_id"=>Users::get("id")])->where('distribution_status','in','1,2')->count(),
  166. "d"=>OrderModel::where(["status"=>5,"pay_status"=>1,"delivery_status"=>1,"user_id"=>Users::get("id")])->where('evaluate_status','in','0,2')->count()
  167. ]
  168. ];
  169. }
  170. /**
  171. * 我的钱包
  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 wallet(){
  178. $info = Users::info(Users::get("id"));
  179. $config = SettingModel::getArrayData("wemini_base");
  180. return [
  181. "switch_1"=>$config["is_rechange"] == 0 ? 1 : 0,
  182. "switch_2"=>$config["is_withdrawal"] == 0 ? 1 : 0,
  183. "amount"=>$info["amount"],
  184. "recharge_amount"=>UsersRechangeModel::where("user_id",Users::get("id"))->where("status",1)->sum("order_amount"),
  185. "consume_amount"=>OrderModel::where("user_id",Users::get("id"))->where("status",5)->sum("order_amount")
  186. ];
  187. }
  188. /**
  189. * 获取会员资料
  190. * @return array
  191. * @throws \Exception
  192. */
  193. public static function getSetting(){
  194. $info = Users::info(Users::get("id"));
  195. return [
  196. "nickname"=>$info["nickname"],
  197. "birthday"=>date("Y-m-d",$info["birthday"]),
  198. "sex"=>$info["sex"],
  199. "avatar"=>Image::avatar($info["avatar"])
  200. ];
  201. }
  202. /**
  203. * 保存会员设置
  204. * @param $post
  205. * @return bool
  206. * @throws \Exception
  207. */
  208. public static function settingSave($post){
  209. if(!Check::chsDash($post["username"])){
  210. throw new \Exception("您填写的昵称不合法",0);
  211. }
  212. if(!in_array($post["sex"],['男', '女', '未知'])){
  213. $post["sex"] = '男';
  214. }
  215. $post["sex"] = $post["sex"] == '男' ? 1 : ($post["sex"] == '女' ? 2 : 0);
  216. if(!preg_match('/\d{4}\-[0-9]{1,2}\-[0-9]{1,2}/is',$post["birthday"])){
  217. throw new \Exception("您填写的日期不合法",0);
  218. }
  219. UsersModel::where("id",Users::get("id"))->update([ "nickname"=>$post["username"], "sex"=>$post["sex"], "birthday"=>strtotime($post["birthday"]) ]);
  220. return true;
  221. }
  222. /**
  223. * 获取帮助内容
  224. * @return array
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\DbException
  227. * @throws \think\db\exception\ModelNotFoundException
  228. */
  229. public static function getHelpList(){
  230. return array_map(function($res){
  231. $res["content"] = Tool::replaceContentImage(Tool::removeContentAttr($res["content"]));
  232. return $res;
  233. },$archives = ArchivesModel::where([ "pid"=>"60" ])->select()->toArray());
  234. }
  235. /**
  236. * 资金明细
  237. * @param $data
  238. * @return array
  239. * @throws BaseException
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public static function getFundList($data){
  245. $size = Config::get("website.pageSize");
  246. $page = $data["page"]??1;
  247. $count = UsersLogModel::where("user_id",Users::get("id"))->where("action","in",[0,3,4])->count();
  248. $result = UsersLogModel::where([
  249. ["user_id","=",Users::get("id")],
  250. ["action","in",[0,3,4]]
  251. ])->page($page,$size)->order('id','DESC')->select()->toArray();
  252. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  253. $total = ceil($count / $size);
  254. $array["total"] = $total;
  255. if($total == $page -1){
  256. throw new BaseException("没有数据了哦!",-1,$array);
  257. }
  258. $list = [];
  259. foreach($result as $key=>$value){
  260. $list[$key]["action"] = $value["action"] == 0 ? "金额" : ( $value["action"] == 3 ? "退款" : "佣金" );
  261. $list[$key]["operation"] = $value["operation"] == 0 ? "+" : "-";
  262. $list[$key]["description"] = $value["description"];
  263. $list[$key]["amount"] = $value["amount"];
  264. $list[$key]['time'] = $value["create_time"];
  265. }
  266. $array["list"] = $list;
  267. return $array;
  268. }
  269. /**
  270. * 提现记录
  271. * @param $data
  272. * @return array
  273. * @throws BaseException
  274. * @throws \think\db\exception\DataNotFoundException
  275. * @throws \think\db\exception\DbException
  276. * @throws \think\db\exception\ModelNotFoundException
  277. */
  278. public static function getCashList($data){
  279. $size = Config::get("website.pageSize");
  280. $page = $data["page"]??1;
  281. $count = UsersWithdrawLogModel::where("user_id",Users::get("id"))->where("withdraw_type","1")->count();
  282. $result = UsersWithdrawLogModel::where("user_id",Users::get("id"))->where("withdraw_type","1")->page($page,$size)->order('id',"DESC")->select()->toArray();
  283. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  284. $total = ceil($count / $size);
  285. $array["total"] = $total;
  286. if($total == $page -1){
  287. throw new BaseException("没有数据了哦!",-1,$array);
  288. }
  289. $list = [];
  290. $status = ["0"=>"审核中","1"=>"已提现","2"=>"未通过"];
  291. foreach($result as $key=>$value){
  292. $list[$key]["description"] = $value["msg"];
  293. $list[$key]["amount"] = $value["price"];
  294. $list[$key]['status'] = $value["status"];
  295. $list[$key]['text'] = $status[$value["status"]];
  296. $list[$key]['time'] = $value["create_time"];
  297. }
  298. $array["list"] = $list;
  299. return $array;
  300. }
  301. public static function rechange(){
  302. }
  303. /**
  304. * 获取充值金额
  305. * @return array
  306. * @throws \think\db\exception\DataNotFoundException
  307. * @throws \think\db\exception\DbException
  308. * @throws \think\db\exception\ModelNotFoundException
  309. */
  310. public static function rechangePrice(){
  311. $data = SettingModel::getArrayData("order_rechange");
  312. $array = [];
  313. if(!empty($data["list"])){
  314. foreach($data["list"] as $key=>$value){
  315. $array[$key] = [
  316. "checked"=> $key==0,
  317. "money"=>$value["num"]
  318. ];
  319. }
  320. $array[] = [
  321. "checked"=>false,
  322. "money"=>"其他"
  323. ];
  324. }
  325. return $array;
  326. }
  327. /**
  328. * 提现配置
  329. * @return array
  330. * @throws \think\db\exception\DataNotFoundException
  331. * @throws \think\db\exception\DbException
  332. * @throws \think\db\exception\ModelNotFoundException
  333. */
  334. public static function settlement(){
  335. $setting = SettingModel::getArrayData("users");
  336. $setting["bank"] = explode("|",$setting["bank"]);
  337. return [ "bank"=>$setting["bank"], "money"=>Users::get("amount") ];
  338. }
  339. /**
  340. * 提交提现申请
  341. * @param $data
  342. * @return bool
  343. * @throws \think\db\exception\DataNotFoundException
  344. * @throws \think\db\exception\DbException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. */
  347. public static function settlementSave($data){
  348. $setting = SettingModel::getArrayData("users");
  349. if(UsersWithdrawLogModel::where(["user_id"=>Users::get("id"),"status"=>0,"withdraw_type"=>1])->count()){
  350. throw new \Exception("您还有提现申请未处理。",0);
  351. }
  352. if(empty($data["name"])){
  353. throw new \Exception("请填写持卡人。",0);
  354. }
  355. if(empty($data["code"])){
  356. throw new \Exception("请填写卡号。",0);
  357. }
  358. if(empty($data["price"])){
  359. throw new \Exception("请填写金额。",0);
  360. }
  361. if($data["price"] < $setting["amount"]){
  362. throw new \Exception("提现金额不能小于" . $setting["amount"],0);
  363. }
  364. if(Users::get("amount") < $data["price"]){
  365. throw new \Exception("提现失败,您的余额不足",0);
  366. }
  367. UsersWithdrawLogModel::create([ "user_id"=>Users::get("id"), "withdraw_type"=>1, "bank_name"=>$data["bank_type"], "bank_real_name"=>$data["name"], "type"=>1, "code"=>$data["code"], "price"=>$data["price"], "status"=>0, "create_time"=>time() ]);
  368. return true;
  369. }
  370. /**
  371. * 上传头像
  372. * @return string
  373. * @throws \Exception
  374. */
  375. public static function upload(){
  376. try{
  377. $fileInfo = UploadfiyService::upload("file",false,"public",["jpg","png","gif","jpeg","bmp"]);
  378. $path = '/'.trim($fileInfo["path"],"/");
  379. UsersModel::where(["id"=>Users::get("id")])->save([ 'avatar'=> $path ]);
  380. return Tool::thumb($path,'',true);
  381. }catch (\Exception $ex){
  382. throw new \Exception($ex->getMessage(),$ex->getCode());
  383. }
  384. }
  385. }