Attire.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 装扮商城接口
  8. */
  9. class Attire extends Api
  10. {
  11. protected $noNeedLogin = ['getAttireList'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->attireModel = new \app\common\model\Attire();
  17. $this->attirebackModel = new \app\common\model\AttireBack();
  18. }
  19. /**
  20. * 获取装扮列表
  21. */
  22. public function getAttireList() {
  23. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  24. $page = $this->request->request('page',1); // 分页
  25. $pageNum = $this->request->request('pageNum',10); // 分页
  26. // 分页搜索构建
  27. $pageStart = ($page-1)*$pageNum;
  28. // 获取基本信息
  29. $where = [];
  30. $where["type"] = $type;
  31. $where["is_show"] = 1;
  32. $attireList = $this->attireModel->where($where)->limit($pageStart,$pageNum)->select();
  33. if($attireList) {
  34. foreach($attireList as $k => $v) {
  35. }
  36. }
  37. $this->success("获取成功!",$attireList);
  38. }
  39. /**
  40. * 购买并加入我的背包
  41. */
  42. public function attireAddToMyBack() {
  43. $attire_id = $this->request->request("attire_id"); //装扮ID
  44. if (!$attire_id) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. if ($this->auth->power->attire == 1) {
  48. $this->error('您已被禁止购买装扮');
  49. }
  50. // 判断用户钻石余额是否充足
  51. $userModel = new \app\common\model\User();
  52. $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
  53. if(!$userInfo) {
  54. $this->error("用户信息获取失败!");
  55. }
  56. // 获取购买装扮需要的价格
  57. $attireInfo = $this->attireModel->where(["id"=>$attire_id])->find();
  58. if(!$attireInfo) {
  59. $this->error("装扮信息获取失败!");
  60. }
  61. if($userInfo["jewel"] < $attireInfo["price"]) {
  62. $this->error("您的余额不足,请先充值!");
  63. }
  64. // 进行购买逻辑
  65. Db::startTrans();
  66. try{
  67. $userjewellogModel = new \app\common\model\UserJewelLog();
  68. // 扣除用户钻石余额
  69. $where = [];
  70. $where["id"] = $this->auth->id;
  71. $res1 = $userModel->where($where)->setDec("jewel",$attireInfo["price"]);
  72. // 添加当前用户钻石流水记录
  73. $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $attireInfo["price"], "-", $userInfo["jewel"], "购买装扮扣除" . $attireInfo["price"] . "钻石!", 6);
  74. // 添加装扮购买记录
  75. $data = [];
  76. $data["user_id"] = $this->auth->id;
  77. $data["attire_id"] = $attire_id;
  78. $data["price"] = $attireInfo["price"];
  79. $data["attire_name"] = $attireInfo["title"];
  80. $data["createtime"] = time();
  81. $attirebylogModel = new \app\common\model\AttireBuyLog();
  82. $res3 = $attirebylogModel->insertGetId($data);
  83. // 加入我的背包
  84. // $where = [];
  85. // $where["user_id"] = $this->auth->id;
  86. // $where["attire_id"] = $attire_id;
  87. // $attirebackInfo = $this->attirebackModel->where($where)->find();
  88. $data = [];
  89. $data["user_id"] = $this->auth->id;
  90. $data["attire_id"] = $attire_id;
  91. $data["attire_name"] = $attireInfo["title"];
  92. $data["price"] = $attireInfo["price"];
  93. $data["file_image"] = $attireInfo["file_image"];
  94. $data["android_image"] = $attireInfo["android_image"];
  95. $data["gif_image"] = $attireInfo["gif_image"];
  96. $data["limit_day"] = $attireInfo["limit_day"];
  97. $data["type"] = $attireInfo["type"];
  98. $data["createtime"] = time();
  99. // if($attirebackInfo) { // 背包中已有商品
  100. // if($attirebackInfo["is_use"] == 1) { // 商品正在使用中
  101. // if($attirebackInfo["duetime"] >= time()) { // 使用中未到期
  102. // $duetime = $attireInfo["limit_day"]*86400;
  103. // $res4 = $this->attirebackModel->setInc("duetime",$duetime);
  104. // } else { // 使用中已到期
  105. // $res4 = $this->attirebackModel->insert($data);
  106. // }
  107. // } else { // 商品未使用
  108. // $res4 = $this->attirebackModel->insert($data);
  109. // }
  110. // } else { // 背包中没有此商品
  111. // $res4 = $this->attirebackModel->insert($data);
  112. // }
  113. $res4 = $this->attirebackModel->insert($data);
  114. if($res1 && $res2 && $res3 && $res4) {
  115. Db::commit();
  116. $this->success("购买成功!");
  117. } else {
  118. $this->error("购买失败,请稍后重试!");
  119. }
  120. }catch (ValidateException $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. } catch (PDOException $e) {
  124. Db::rollback();
  125. $this->error($e->getMessage());
  126. } catch (Exception $e) {
  127. Db::rollback();
  128. $this->error($e->getMessage());
  129. }
  130. }
  131. /**
  132. * 获取我的背包装饰列表
  133. */
  134. public function getBackAttireList() {
  135. try {
  136. $page = $this->request->request('page',1); // 分页
  137. $pageNum = $this->request->request('pageNum',10); // 分页
  138. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  139. // 分页搜索构建
  140. $pageStart = ($page-1)*$pageNum;
  141. global $where;
  142. $where = [];$whereOr=[];
  143. $where["user_id"] = $this->auth->id;
  144. $type && $where["type"] = $type;
  145. $whereOr["is_use"] = "0";
  146. $whereOr["duetime"] = ["gt",time()];
  147. $list = $this->attirebackModel
  148. ->field("id,type,file_image,gif_image,attire_name as title,limit_day,duetime,is_use,is_using")
  149. ->where(function ($query) {
  150. global $where;
  151. $query->where($where);
  152. })
  153. ->where(function ($query) {
  154. $query->whereOr(["is_use"=>0,"duetime"=>["gt",time()]]);
  155. })
  156. ->limit($pageStart,$pageNum)
  157. ->select();
  158. if(!$list) {
  159. $this->success("背包空空如也!");
  160. }
  161. foreach($list as $k => $v) {
  162. // 有效期/剩余天数
  163. if($v["is_use"] == 1 && $v["duetime"]>time()) { // 正在使用,未过期
  164. $dtime = "剩余".ceil(($v["duetime"]-time())/86400)."天";
  165. }
  166. if($v["is_use"] == 0) {
  167. $dtime = "有效期".$v["limit_day"]."天";
  168. }
  169. $list[$k]["dtime"] = $dtime;
  170. }
  171. $this->success("获取成功!",$list);
  172. } catch (Exception $e) {echo '<pre>';var_dump($e->getMessage());exit;
  173. $this->error($e->getMessage());
  174. }
  175. }
  176. /**
  177. * 使用并装上装饰
  178. */
  179. public function toUseAttire() {
  180. $id = $this->request->request("id"); //背包中商品ID
  181. if (!$id) {
  182. $this->error(__('Invalid parameters'));
  183. }
  184. // 获取装饰信息
  185. $where = [];
  186. $where["id"] = $id;
  187. $attirebackInfo = $this->attirebackModel->where($where)->find();
  188. $data = [];
  189. $data["is_use"] = 1;
  190. $data["is_using"] = 1;
  191. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  192. $res = $this->attirebackModel->update($data,$where);
  193. if($res) {
  194. $this->success("设置成功!");
  195. } else {
  196. $this->success("网络错误,请稍后重试!");
  197. }
  198. }
  199. /**
  200. * 装上
  201. */
  202. public function toUsingAttire() {
  203. $id = $this->request->request("id"); //背包中商品ID
  204. $use_type = $this->request->request("use_type",1); //使用方式:1=装上,2=卸下
  205. if (!$id || !in_array($use_type,[1,2])) {
  206. $this->error(__('Invalid parameters'));
  207. }
  208. // 获取装饰信息
  209. $where = [];
  210. $where["id"] = $id;
  211. $attirebackInfo = $this->attirebackModel->where($where)->find();
  212. if($use_type == 2) {
  213. $attirebackInfo->is_using = 0;
  214. $attirebackInfo->save();
  215. $this->success("设置成功!");
  216. }
  217. if($attirebackInfo["is_use"] != 1) {
  218. $data = [];
  219. $data["is_use"] = 1;
  220. $data["is_using"] = 1;
  221. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  222. $res = $this->attirebackModel->update($data,$where);
  223. if(!$res) $this->error("使用失败!");
  224. }
  225. Db::startTrans();
  226. try{
  227. // 先取消掉所有的
  228. $res1 = $this->attirebackModel->update(["is_using"=>0],["user_id"=>$this->auth->id,"type"=>$attirebackInfo["type"]]);
  229. // 再设置当前
  230. $res2 = $this->attirebackModel->update(["is_using"=>1],["id"=>$id]);
  231. if($res1 && $res2) {
  232. Db::commit();
  233. $this->success("设置成功!");
  234. } else {
  235. $this->success("网络错误,请稍后重试!");
  236. }
  237. }catch (ValidateException $e) {
  238. Db::rollback();
  239. $this->error($e->getMessage());
  240. } catch (PDOException $e) {
  241. Db::rollback();
  242. $this->error($e->getMessage());
  243. } catch (Exception $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. }
  247. }
  248. }