Attire.php 9.4 KB

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